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()
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)
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")
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 self.assertRaises(AirflowException) as cm: op = CloudSQLCreateInstanceOperator(project_id=PROJECT_ID, body=wrong_list_type_body, instance=INSTANCE_NAME, task_id="id") op.execute(None) err = cm.exception self.assertIn( "The field 'settings.ipConfiguration.authorizedNetworks' " "should be of list type according to the specification", str(err)) mock_hook.assert_called_once_with(api_version="v1beta4", gcp_conn_id="google_cloud_default")
default_args = {'start_date': days_ago(1)} with models.DAG( 'example_gcp_sql', default_args=default_args, schedule_interval=None # Override to match your needs ) 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=INSTANCE_NAME2,