Ejemplo n.º 1
0
 def test_create_should_validate_list_type(self, mock_hook):
     wrong_list_type_body = {
         "name": INSTANCE_NAME,
         "settings": {
             "tier": "db-n1-standard-1",
             "ipConfiguration": {
                 "authorizedNetworks": {}  # Should be a list, not a dict.
                 # Testing if the validation catches this.
             },
         },
     }
     with pytest.raises(AirflowException) as ctx:
         op = CloudSQLCreateInstanceOperator(project_id=PROJECT_ID,
                                             body=wrong_list_type_body,
                                             instance=INSTANCE_NAME,
                                             task_id="id")
         op.execute(None)
     err = ctx.value
     assert ("The field 'settings.ipConfiguration.authorizedNetworks' "
             "should be of list type according to the specification"
             in str(err))
     mock_hook.assert_called_once_with(
         api_version="v1beta4",
         gcp_conn_id="google_cloud_default",
         impersonation_chain=None,
     )
Ejemplo n.º 2
0
 def test_create_should_throw_ex_when_empty_instance(self, mock_hook):
     with self.assertRaises(AirflowException) as cm:
         op = CloudSQLCreateInstanceOperator(
             project_id=PROJECT_ID, body=CREATE_BODY, instance="", task_id="id"
         )
         op.execute(None)
     err = cm.exception
     self.assertIn("The required parameter 'instance' is empty", str(err))
     mock_hook.assert_not_called()
Ejemplo n.º 3
0
 def test_create_should_throw_ex_when_empty_body(self, mock_hook):
     with pytest.raises(AirflowException) as ctx:
         op = CloudSQLCreateInstanceOperator(project_id=PROJECT_ID,
                                             body={},
                                             instance=INSTANCE_NAME,
                                             task_id="id")
         op.execute(None)
     err = ctx.value
     assert "The required parameter 'body' is empty" in str(err)
     mock_hook.assert_not_called()
Ejemplo n.º 4
0
 def test_instance_create_missing_project_id(self, mock_hook, _check_if_instance_exists):
     _check_if_instance_exists.return_value = False
     mock_hook.return_value.create_instance.return_value = True
     op = CloudSQLCreateInstanceOperator(instance=INSTANCE_NAME, body=CREATE_BODY, task_id="id")
     result = op.execute(
         context={'task_instance': mock.Mock()}  # pylint: disable=assignment-from-no-return
     )
     mock_hook.assert_called_once_with(
         api_version="v1beta4",
         gcp_conn_id="google_cloud_default",
         impersonation_chain=None,
     )
     mock_hook.return_value.create_instance.assert_called_once_with(project_id=None, body=CREATE_BODY)
     self.assertIsNone(result)
Ejemplo n.º 5
0
 def test_instance_create_idempotent(self, mock_hook,
                                     _check_if_instance_exists):
     _check_if_instance_exists.return_value = True
     mock_hook.return_value.create_instance.return_value = True
     op = CloudSQLCreateInstanceOperator(project_id=PROJECT_ID,
                                         instance=INSTANCE_NAME,
                                         body=CREATE_BODY,
                                         task_id="id")
     op.execute(context={'task_instance': mock.Mock()})
     mock_hook.assert_called_once_with(
         api_version="v1beta4",
         gcp_conn_id="google_cloud_default",
         impersonation_chain=None,
     )
     mock_hook.return_value.create_instance.assert_not_called()
Ejemplo n.º 6
0
 def test_instance_create_idempotent(self, mock_hook,
                                     _check_if_instance_exists):
     _check_if_instance_exists.return_value = True
     mock_hook.return_value.create_instance.return_value = True
     op = CloudSQLCreateInstanceOperator(project_id=PROJECT_ID,
                                         instance=INSTANCE_NAME,
                                         body=CREATE_BODY,
                                         task_id="id")
     result = op.execute(
         context={  # pylint: disable=assignment-from-no-return
             'task_instance': mock.Mock()
         })
     mock_hook.assert_called_once_with(api_version="v1beta4",
                                       gcp_conn_id="google_cloud_default")
     mock_hook.return_value.create_instance.assert_not_called()
     self.assertIsNone(result)
Ejemplo n.º 7
0
 def test_create_should_validate_non_empty_fields(self, mock_hook):
     empty_tier_body = {
         "name": INSTANCE_NAME,
         "settings": {
             "tier": "",  # Field can't be empty (defined in CLOUD_SQL_VALIDATION).
             # Testing if the validation catches this.
         },
     }
     with self.assertRaises(AirflowException) as cm:
         op = CloudSQLCreateInstanceOperator(
             project_id=PROJECT_ID, body=empty_tier_body, instance=INSTANCE_NAME, task_id="id"
         )
         op.execute(None)
     err = cm.exception
     self.assertIn("The body field 'settings.tier' can't be empty. " "Please provide a value.", str(err))
     mock_hook.assert_called_once_with(
         api_version="v1beta4",
         gcp_conn_id="google_cloud_default",
         impersonation_chain=None,
     )
Ejemplo n.º 8
0
default_args = {'start_date': days_ago(1)}

with models.DAG(
        'example_gcp_sql',
        schedule_interval=None,  # Override to match your needs
        start_date=days_ago(1),
        tags=['example'],
) as dag:
    # ############################################## #
    # ### INSTANCES SET UP ######################### #
    # ############################################## #

    # [START howto_operator_cloudsql_create]
    sql_instance_create_task = CloudSQLCreateInstanceOperator(
        project_id=GCP_PROJECT_ID,
        body=body,
        instance=INSTANCE_NAME,
        task_id='sql_instance_create_task')
    # [END howto_operator_cloudsql_create]

    sql_instance_create_2_task = CloudSQLCreateInstanceOperator(
        project_id=GCP_PROJECT_ID,
        body=body2,
        instance=INSTANCE_NAME2,
        task_id='sql_instance_create_task2')
    # [END howto_operator_cloudsql_create]

    sql_instance_read_replica_create = CloudSQLCreateInstanceOperator(
        project_id=GCP_PROJECT_ID,
        body=read_replica_body,
        instance=READ_REPLICA_NAME,