コード例 #1
0
ファイル: cloud_sql.py プロジェクト: zw39125432/airflow
 def execute(self, context):
     self._validate_body_fields()
     hook = CloudSqlHook(
         gcp_conn_id=self.gcp_conn_id,
         api_version=self.api_version
     )
     return hook.import_instance(
         project_id=self.project_id,
         instance=self.instance,
         body=self.body)
コード例 #2
0
ファイル: cloud_sql.py プロジェクト: zw39125432/airflow
 def execute(self, context):
     hook = CloudSqlHook(
         gcp_conn_id=self.gcp_conn_id,
         api_version=self.api_version
     )
     if not self._check_if_instance_exists(self.instance, hook):
         print("Cloud SQL instance with ID {} does not exist. Aborting delete."
               .format(self.instance))
         return True
     else:
         return hook.delete_instance(
             project_id=self.project_id,
             instance=self.instance)
コード例 #3
0
ファイル: cloud_sql.py プロジェクト: kpathak13/apache-airflow
 def __init__(self,
              instance: str,
              project_id: Optional[str] = None,
              gcp_conn_id: str = 'google_cloud_default',
              api_version: str = 'v1beta4',
              *args, **kwargs) -> None:
     self.project_id = project_id
     self.instance = instance
     self.gcp_conn_id = gcp_conn_id
     self.api_version = api_version
     self._validate_inputs()
     self._hook = CloudSqlHook(gcp_conn_id=self.gcp_conn_id,
                               api_version=self.api_version)  # type: CloudSqlHook
     super().__init__(*args, **kwargs)
コード例 #4
0
ファイル: cloud_sql.py プロジェクト: zw39125432/airflow
 def execute(self, context):
     hook = CloudSqlHook(
         gcp_conn_id=self.gcp_conn_id,
         api_version=self.api_version
     )
     if not self._check_if_instance_exists(self.instance, hook):
         raise AirflowException('Cloud SQL instance with ID {} does not exist. '
                                'Please specify another instance to patch.'
                                .format(self.instance))
     else:
         return hook.patch_instance(
             project_id=self.project_id,
             body=self.body,
             instance=self.instance)
コード例 #5
0
ファイル: cloud_sql.py プロジェクト: zw39125432/airflow
 def execute(self, context):
     hook = CloudSqlHook(
         gcp_conn_id=self.gcp_conn_id,
         api_version=self.api_version
     )
     if not self._check_if_db_exists(self.database, hook):
         print("Cloud SQL instance with ID {} does not contain database '{}'. "
               "Aborting database delete."
               .format(self.instance, self.database))
         return True
     else:
         return hook.delete_database(
             project_id=self.project_id,
             instance=self.instance,
             database=self.database)
コード例 #6
0
 def __init__(self,
              instance,
              project_id=None,
              gcp_conn_id='google_cloud_default',
              api_version='v1beta4',
              *args,
              **kwargs):
     self.project_id = project_id
     self.instance = instance
     self.gcp_conn_id = gcp_conn_id
     self.api_version = api_version
     self._validate_inputs()
     self._hook = CloudSqlHook(gcp_conn_id=self.gcp_conn_id,
                               api_version=self.api_version)
     super().__init__(*args, **kwargs)
コード例 #7
0
ファイル: cloud_sql.py プロジェクト: zw39125432/airflow
 def execute(self, context):
     self._validate_body_fields()
     hook = CloudSqlHook(
         gcp_conn_id=self.gcp_conn_id,
         api_version=self.api_version
     )
     if not self._check_if_db_exists(self.database, hook):
         raise AirflowException("Cloud SQL instance with ID {instance} does not contain "
                                "database '{database}'. "
                                "Please specify another database to patch.".
                                format(instance=self.instance, database=self.database))
     else:
         return hook.patch_database(
             project_id=self.project_id,
             instance=self.instance,
             database=self.database,
             body=self.body)
コード例 #8
0
ファイル: cloud_sql.py プロジェクト: zw39125432/airflow
 def _check_if_db_exists(self, db_name, hook: CloudSqlHook):
     try:
         return hook.get_database(
             project_id=self.project_id,
             instance=self.instance,
             database=db_name)
     except HttpError as e:
         status = e.resp.status
         if status == 404:
             return False
         raise e
コード例 #9
0
ファイル: cloud_sql.py プロジェクト: zw39125432/airflow
 def _check_if_instance_exists(self, instance, hook: CloudSqlHook):
     try:
         return hook.get_instance(
             project_id=self.project_id,
             instance=instance
         )
     except HttpError as e:
         status = e.resp.status
         if status == 404:
             return False
         raise e
コード例 #10
0
ファイル: cloud_sql.py プロジェクト: zw39125432/airflow
 def execute(self, context):
     self._validate_body_fields()
     database = self.body.get("name")
     if not database:
         self.log.error("Body doesn't contain 'name'. Cannot check if the"
                        " database already exists in the instance %s.", self.instance)
         return False
     hook = CloudSqlHook(
         gcp_conn_id=self.gcp_conn_id,
         api_version=self.api_version
     )
     if self._check_if_db_exists(database, hook):
         self.log.info("Cloud SQL instance with ID %s already contains database"
                       " '%s'. Aborting database insert.", self.instance, database)
         return True
     else:
         return hook.create_database(
             project_id=self.project_id,
             instance=self.instance,
             body=self.body
         )
コード例 #11
0
ファイル: cloud_sql.py プロジェクト: zw39125432/airflow
    def execute(self, context):
        hook = CloudSqlHook(
            gcp_conn_id=self.gcp_conn_id,
            api_version=self.api_version
        )
        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)

        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)
コード例 #12
0
class CloudSqlBaseOperator(BaseOperator):
    """
    Abstract base operator for Google Cloud SQL operators to inherit from.

    :param instance: Cloud SQL instance ID. This does not include the project ID.
    :type instance: str
    :param project_id: Optional, Google Cloud Platform Project ID.  f set to None or missing,
            the default project_id from the GCP connection is used.
    :type project_id: str
    :param gcp_conn_id: The connection ID used to connect to Google Cloud Platform.
    :type gcp_conn_id: str
    :param api_version: API version used (e.g. v1beta4).
    :type api_version: str
    """
    @apply_defaults
    def __init__(self,
                 instance: str,
                 project_id: Optional[str] = None,
                 gcp_conn_id: str = 'google_cloud_default',
                 api_version: str = 'v1beta4',
                 *args,
                 **kwargs) -> None:
        self.project_id = project_id
        self.instance = instance
        self.gcp_conn_id = gcp_conn_id
        self.api_version = api_version
        self._validate_inputs()
        self._hook = CloudSqlHook(
            gcp_conn_id=self.gcp_conn_id,
            api_version=self.api_version)  # type: CloudSqlHook
        super().__init__(*args, **kwargs)

    def _validate_inputs(self):
        if self.project_id == '':
            raise AirflowException(
                "The required parameter 'project_id' is empty")
        if not self.instance:
            raise AirflowException(
                "The required parameter 'instance' is empty or None")

    def _check_if_instance_exists(self, instance):
        try:
            return self._hook.get_instance(project_id=self.project_id,
                                           instance=instance)
        except HttpError as e:
            status = e.resp.status
            if status == 404:
                return False
            raise e

    def _check_if_db_exists(self, db_name):
        try:
            return self._hook.get_database(project_id=self.project_id,
                                           instance=self.instance,
                                           database=db_name)
        except HttpError as e:
            status = e.resp.status
            if status == 404:
                return False
            raise e

    def execute(self, context):
        pass

    @staticmethod
    def _get_settings_version(instance):
        return instance.get(SETTINGS).get(SETTINGS_VERSION)