def execute(self, context): if self.hook is None: self.hook = GoogleCampaignManagerHook(gcp_conn_id=self.gcp_conn_id, delegate_to=self.delegate_to) if self.report_id is not None: self.hook.delete_report(self.profile_id, self.report_id, self.ignore_if_missing) if self.report_name is not None: self.hook.delete_report_by_name(self.profile_id, self.report_name, self.ignore_if_missing)
def execute(self, context): if self.gcs_hook is None: self.gcs_hook = GoogleCloudStorageHook( google_cloud_storage_conn_id=self.gcp_conn_id, delegate_to=self.delegate_to) if self.cm_hook is None: self.cm_hook = GoogleCampaignManagerHook( gcp_conn_id=self.gcp_conn_id, delegate_to=self.delegate_to) temp_file = tempfile.NamedTemporaryFile(delete=False) try: report_file_name = self._download_report(self.report_id, self.file_id, temp_file, self.chunk_size) destination_object_name = self._get_destination_uri( self.destination_object, report_file_name) self.gcs_hook.upload(bucket=self.destination_bucket, object=destination_object_name, filename=temp_file.name, gzip=True, multipart=True) context['task_instance'].xcom_push('destination_bucket', self.destination_bucket) context['task_instance'].xcom_push('destination_object', destination_object_name) finally: temp_file.close() os.unlink(temp_file.name)
class GoogleCampaignManagerReportSensor(BaseSensorOperator): """Sensor for detecting the completion of DCM reports. Waits for a Campaign Manger report to complete. Attributes: report_id: The ID of the report to poll. (templated) file_id: The ID of the file associated with the report. (templated) profile_id: DCM profile ID used when making API requests. (templated) gcp_conn_id: The connection ID to use when fetching connection info. delegate_to: The account to impersonate, if any. poke_interval: Time, in seconds, that the job should wait in between tries. timeout: Time, in seconds, before the task times out and fails. mode: Whether the sensor should poke or reschedule the task when the criteria is not met. """ template_fields = ['report_id', 'file_id', 'profile_id'] def __init__(self, report_id, file_id, profile_id, gcp_conn_id='google_cloud_default', delegate_to=None, poke_interval=60 * 5, timeout=60 * 60 * 24, mode='reschedule', *args, **kwargs): super(GoogleCampaignManagerReportSensor, self).__init__(poke_interval=poke_interval, timeout=timeout, mode=mode, *args, **kwargs) self.gcp_conn_id = gcp_conn_id self.profile_id = profile_id self.file_id = file_id self.report_id = report_id self.delegate_to = delegate_to self.hook = None def poke(self, context): if self.hook is None: self.hook = GoogleCampaignManagerHook(gcp_conn_id=self.gcp_conn_id, delegate_to=self.delegate_to) logger.info(self.gcp_conn_id) logger.info(self.report_id) logger.info(self.file_id) request = self.hook.get_service().reports().files().get( profileId=self.profile_id, reportId=self.report_id, fileId=self.file_id) response = request.execute() logger.info(response) if response: if response['status'] != 'PROCESSING': return True return False
class GoogleCampaignManagerDeleteReportOperator( GoogleMarketingPlatformBaseOperator): """Deletes Campaign Manager reports. Attributes: report_id: The DCM report ID to delete. (templated) report_name: The DCM report name to delete. (templated) profile_id: DCM profile ID to validate against when interacting with the API. (templated) ignore_if_missing: If True, return success even if the report is missing. gcp_conn_id: The connection ID to use when fetching connection info. delegate_to: The account to impersonate, if any. """ template_fields = ['profile_id', 'report_id', 'report_name'] def __init__(self, profile_id, report_id=None, report_name=None, ignore_if_missing=False, gcp_conn_id='google_cloud_default', delegate_to=None, *args, **kwargs): super(GoogleCampaignManagerDeleteReportOperator, self).__init__(*args, **kwargs) self.report_id = report_id self.report_name = report_name self.profile_id = profile_id self.ignore_if_missing = ignore_if_missing self.gcp_conn_id = gcp_conn_id self.delegate_to = delegate_to self.hook = None def execute(self, context): if self.hook is None: self.hook = GoogleCampaignManagerHook(gcp_conn_id=self.gcp_conn_id, delegate_to=self.delegate_to) if self.report_id is not None: self.hook.delete_report(self.profile_id, self.report_id, self.ignore_if_missing) if self.report_name is not None: self.hook.delete_report_by_name(self.profile_id, self.report_name, self.ignore_if_missing)
def poke(self, context): if self.hook is None: self.hook = GoogleCampaignManagerHook(gcp_conn_id=self.gcp_conn_id, delegate_to=self.delegate_to) logger.info(self.gcp_conn_id) logger.info(self.report_id) logger.info(self.file_id) request = self.hook.get_service().reports().files().get( profileId=self.profile_id, reportId=self.report_id, fileId=self.file_id) response = request.execute() logger.info(response) if response: if response['status'] != 'PROCESSING': return True return False
def execute(self, context): if self.hook is None: self.hook = GoogleCampaignManagerHook(gcp_conn_id=self.gcp_conn_id, delegate_to=self.delegate_to) report_id = self._create_report(self.profile_id, self.report) file_id = self._run_report(self.profile_id, report_id) context['task_instance'].xcom_push('report_id', report_id) context['task_instance'].xcom_push('file_id', file_id) msg = 'Report Created report_id = %s and file_id = %s' % (report_id, file_id) logger.info(msg)