Example #1
0
    def execute(self, context: 'Context') -> None:
        hook = CloudSQLHook(
            gcp_conn_id=self.gcp_conn_id,
            api_version=self.api_version,
            impersonation_chain=self.impersonation_chain,
        )
        self._validate_body_fields()
        if not self._check_if_instance_exists(self.instance, hook):
            hook.create_instance(project_id=self.project_id, body=self.body)
        else:
            self.log.info(
                "Cloud SQL instance with ID %s already exists. Aborting create.",
                self.instance)

        CloudSQLInstanceLink.persist(
            context=context,
            task_instance=self,
            cloud_sql_instance=self.instance,
            project_id=self.project_id or hook.project_id,
        )

        instance_resource = hook.get_instance(project_id=self.project_id,
                                              instance=self.instance)
        service_account_email = instance_resource["serviceAccountEmailAddress"]
        task_instance = context['task_instance']
        task_instance.xcom_push(key="service_account_email",
                                value=service_account_email)
Example #2
0
 def execute(self, context: 'Context') -> None:
     self._validate_body_fields()
     hook = CloudSQLHook(
         gcp_conn_id=self.gcp_conn_id,
         api_version=self.api_version,
         impersonation_chain=self.impersonation_chain,
     )
     CloudSQLInstanceLink.persist(
         context=context,
         task_instance=self,
         cloud_sql_instance=self.instance,
         project_id=self.project_id or hook.project_id,
     )
     FileDetailsLink.persist(
         context=context,
         task_instance=self,
         uri=self.body["importContext"]["uri"][5:],
         project_id=self.project_id or hook.project_id,
     )
     return hook.import_instance(project_id=self.project_id, instance=self.instance, body=self.body)
Example #3
0
    def execute(self, context: 'Context'):
        hook = CloudSQLHook(
            gcp_conn_id=self.gcp_conn_id,
            api_version=self.api_version,
            impersonation_chain=self.impersonation_chain,
        )
        if not self._check_if_instance_exists(self.instance, hook):
            raise AirflowException(
                f'Cloud SQL instance with ID {self.instance} does not exist. '
                'Please specify another instance to patch.'
            )
        else:
            CloudSQLInstanceLink.persist(
                context=context,
                task_instance=self,
                cloud_sql_instance=self.instance,
                project_id=self.project_id or hook.project_id,
            )

            return hook.patch_instance(project_id=self.project_id, body=self.body, instance=self.instance)