Example #1
0
 def execute(self, context: "Context"):
     hook = DataformHook(
         gcp_conn_id=self.gcp_conn_id,
         delegate_to=self.delegate_to,
         impersonation_chain=self.impersonation_chain,
     )
     result = hook.create_workflow_invocation(
         project_id=self.project_id,
         region=self.region,
         repository_id=self.repository_id,
         workflow_invocation=self.workflow_invocation,
         retry=self.retry,
         timeout=self.timeout,
         metadata=self.metadata,
     )
     workflow_invocation_id = result.name.split("/")[-1]
     DataformWorkflowInvocationLink.persist(
         operator_instance=self,
         context=context,
         project_id=self.project_id,
         region=self.region,
         repository_id=self.repository_id,
         workflow_invocation_id=workflow_invocation_id,
     )
     if not self.asynchronous:
         hook.wait_for_workflow_invocation(
             workflow_invocation_id=workflow_invocation_id,
             repository_id=self.repository_id,
             project_id=self.project_id,
             region=self.region,
             timeout=self.timeout,
             wait_time=self.wait_time,
         )
     return WorkflowInvocation.to_dict(result)
Example #2
0
 def execute(self, context: "Context"):
     hook = DataformHook(
         gcp_conn_id=self.gcp_conn_id,
         delegate_to=self.delegate_to,
         impersonation_chain=self.impersonation_chain,
     )
     hook.cancel_workflow_invocation(
         project_id=self.project_id,
         region=self.region,
         repository_id=self.repository_id,
         workflow_invocation_id=self.workflow_invocation_id,
         retry=self.retry,
         timeout=self.timeout,
         metadata=self.metadata,
     )
Example #3
0
 def execute(self, context: "Context"):
     hook = DataformHook(
         gcp_conn_id=self.gcp_conn_id,
         delegate_to=self.delegate_to,
         impersonation_chain=self.impersonation_chain,
     )
     result = hook.create_compilation_result(
         project_id=self.project_id,
         region=self.region,
         repository_id=self.repository_id,
         compilation_result=self.compilation_result,
         retry=self.retry,
         timeout=self.timeout,
         metadata=self.metadata,
     )
     return CompilationResult.to_dict(result)
Example #4
0
    def poke(self, context: 'Context') -> bool:
        self.hook = DataformHook(
            gcp_conn_id=self.gcp_conn_id,
            delegate_to=self.delegate_to,
            impersonation_chain=self.impersonation_chain,
        )

        workflow_invocation = self.hook.get_workflow_invocation(
            project_id=self.project_id,
            region=self.region,
            repository_id=self.repository_id,
            workflow_invocation_id=self.workflow_invocation_id,
        )
        workflow_status = workflow_invocation.state
        if workflow_status is not None:
            if self.failure_statuses and workflow_status in self.failure_statuses:
                raise AirflowException(
                    f"Workflow Invocation with id '{self.workflow_invocation_id}' "
                    f"state is: {workflow_status}. Terminating sensor...")

        return workflow_status in self.expected_statuses
Example #5
0
class DataformWorkflowInvocationStateSensor(BaseSensorOperator):
    """
    Checks for the status of a Workflow Invocation in Google Cloud Dataform.

    :param project_id: Required, the Google Cloud project ID in which to start a job.
        If set to None or missing, the default project_id from the Google Cloud connection is used.
    :param region: Required, The location of the Dataform workflow invocation (for example europe-west1).
    :param repository_id: Required. The ID of the Dataform repository that the task belongs to.
    :param workflow_invocation_id: Required, ID of the workflow invocation to be checked.
    :param expected_statuses: The expected state of the operation.
        See:
        https://cloud.google.com/python/docs/reference/dataform/latest/google.cloud.dataform_v1beta1.types.WorkflowInvocation.State
    :param failure_statuses: State that will terminate the sensor with an exception
    :param gcp_conn_id: The connection ID to use connecting to Google Cloud.
    :param delegate_to: The account to impersonate using domain-wide delegation of authority,
        if any. For this to work, the service account making the request must have
        domain-wide delegation enabled. See:
        https://developers.google.com/identity/protocols/oauth2/service-account#delegatingauthority
    :param impersonation_chain: Optional service account to impersonate using short-term
        credentials, or chained list of accounts required to get the access_token
        of the last account in the list, which will be impersonated in the request.
        If set as a string, the account must grant the originating account
        the Service Account Token Creator IAM role.
        If set as a sequence, the identities from the list must grant
        Service Account Token Creator IAM role to the directly preceding identity, with first
        account from the list granting this role to the originating account (templated).
    """

    template_fields: Sequence[str] = ('workflow_invocation_id', )

    def __init__(
        self,
        *,
        project_id: str,
        region: str,
        repository_id: str,
        workflow_invocation_id: str,
        expected_statuses: Union[Set[int], int],
        failure_statuses: Optional[Iterable[int]] = None,
        gcp_conn_id: str = 'google_cloud_default',
        delegate_to: Optional[str] = None,
        impersonation_chain: Optional[Union[str, Sequence[str]]] = None,
        **kwargs,
    ) -> None:
        super().__init__(**kwargs)
        self.repository_id = repository_id
        self.workflow_invocation_id = workflow_invocation_id
        self.expected_statuses = ({expected_statuses} if isinstance(
            expected_statuses, int) else expected_statuses)
        self.failure_statuses = failure_statuses
        self.project_id = project_id
        self.region = region
        self.gcp_conn_id = gcp_conn_id
        self.delegate_to = delegate_to
        self.impersonation_chain = impersonation_chain
        self.hook: Optional[DataformHook] = None

    def poke(self, context: 'Context') -> bool:
        self.hook = DataformHook(
            gcp_conn_id=self.gcp_conn_id,
            delegate_to=self.delegate_to,
            impersonation_chain=self.impersonation_chain,
        )

        workflow_invocation = self.hook.get_workflow_invocation(
            project_id=self.project_id,
            region=self.region,
            repository_id=self.repository_id,
            workflow_invocation_id=self.workflow_invocation_id,
        )
        workflow_status = workflow_invocation.state
        if workflow_status is not None:
            if self.failure_statuses and workflow_status in self.failure_statuses:
                raise AirflowException(
                    f"Workflow Invocation with id '{self.workflow_invocation_id}' "
                    f"state is: {workflow_status}. Terminating sensor...")

        return workflow_status in self.expected_statuses