potiuk commented on a change in pull request #4353: [AIRFLOW-3480] Add Database
Deploy/Update/Delete operators
URL: https://github.com/apache/incubator-airflow/pull/4353#discussion_r245062825
##########
File path: airflow/contrib/operators/gcp_spanner_operator.py
##########
@@ -197,3 +200,201 @@ def execute(self, context):
def sanitize_queries(queries):
if len(queries) and queries[-1] == '':
del queries[-1]
+
+
+class CloudSpannerInstanceDatabaseDeployOperator(BaseOperator):
+ """
+ Creates a new Cloud Spanner database, or if database exists,
+ the operator does nothing.
+
+
+ :param project_id: The ID of the project that owns the Cloud Spanner
Database.
+ :type project_id: str
+ :param instance_id: The Cloud Spanner instance ID.
+ :type instance_id: str
+ :param database_id: The Cloud Spanner database ID.
+ :type database_id: str
+ :param ddl_statements: The string list containing DDL for the new database.
+ :type ddl_statements: [str]
+ :param gcp_conn_id: The connection ID used to connect to Google Cloud
Platform.
+ :type gcp_conn_id: str
+ """
+ # [START gcp_spanner_database_deploy_template_fields]
+ template_fields = ('project_id', 'instance_id', 'database_id',
'ddl_statements',
+ 'gcp_conn_id')
+ template_ext = ('.sql', )
+ # [END gcp_spanner_database_deploy_template_fields]
+
+ @apply_defaults
+ def __init__(self,
+ project_id,
+ instance_id,
+ database_id,
+ ddl_statements,
+ gcp_conn_id='google_cloud_default',
+ *args, **kwargs):
+ # type: (str, str, str, [str], str, object, object) -> None
+ self.instance_id = instance_id
+ self.project_id = project_id
+ self.database_id = database_id
+ self.ddl_statements = ddl_statements
+ self.gcp_conn_id = gcp_conn_id
+ self._validate_inputs()
+ self._hook = CloudSpannerHook(gcp_conn_id=gcp_conn_id)
+ super(CloudSpannerInstanceDatabaseDeployOperator,
self).__init__(*args, **kwargs)
+
+ def _validate_inputs(self):
+ if not self.project_id:
+ raise AirflowException("The required parameter 'project_id' is
empty")
+ if not self.instance_id:
+ raise AirflowException("The required parameter 'instance_id' is
empty")
+ if not self.database_id:
+ raise AirflowException("The required parameter 'database_id' is
empty")
+ if not self.ddl_statements:
+ raise AirflowException("The required parameter 'ddl_statements' is
empty")
+
+ def execute(self, context):
+ if not self._hook.get_database(self.project_id,
+ self.instance_id,
+ self.database_id):
+ self.log.info("Creating Cloud Spanner database "
+ "'{}' in project '{}' and instance '{}'".
+ format(self.database_id, self.project_id,
self.instance_id))
+ return self._hook.create_database(project_id=self.project_id,
+ instance_id=self.instance_id,
+ database_id=self.database_id,
+
ddl_statements=self.ddl_statements)
+ else:
+ self.log.info("The database '{}' in project '{}' and instance '{}'"
Review comment:
Make sense.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services