Esempio n. 1
0
    def test_succeedifexists(self, mock_hook):
        operator = PubSubTopicCreateOperator(task_id=TASK_ID,
                                             project_id=TEST_PROJECT,
                                             topic=TEST_TOPIC,
                                             fail_if_exists=False)

        operator.execute(None)
        mock_hook.return_value.create_topic.assert_called_once_with(
            project_id=TEST_PROJECT,
            topic=TEST_TOPIC,
            fail_if_exists=False,
            labels=None,
            message_storage_policy=None,
            kms_key_name=None,
            retry=None,
            timeout=None,
            metadata=None)
Esempio n. 2
0
# [START howto_operator_gcp_pubsub_pull_messages_result_cmd]
echo_cmd = """
{% for m in task_instance.xcom_pull('pull_messages') %}
    echo "AckID: {{ m.get('ackId') }}, Base64-Encoded: {{ m.get('message') }}"
{% endfor %}
"""
# [END howto_operator_gcp_pubsub_pull_messages_result_cmd]

with models.DAG(
        "example_gcp_pubsub",
        default_args=default_args,
        schedule_interval=None,  # Override to match your needs
) as example_dag:
    # [START howto_operator_gcp_pubsub_create_topic]
    create_topic = PubSubTopicCreateOperator(task_id="create_topic",
                                             topic=TOPIC,
                                             project_id=GCP_PROJECT_ID)
    # [END howto_operator_gcp_pubsub_create_topic]

    # [START howto_operator_gcp_pubsub_create_subscription]
    subscribe_task = PubSubSubscriptionCreateOperator(
        task_id="subscribe_task", project_id=GCP_PROJECT_ID, topic=TOPIC)
    # [END howto_operator_gcp_pubsub_create_subscription]

    # [START howto_operator_gcp_pubsub_pull_message]
    subscription = "{{ task_instance.xcom_pull('subscribe_task') }}"

    pull_messages = PubSubPullSensor(
        task_id="pull_messages",
        ack_messages=True,
        project_id=GCP_PROJECT_ID,