Exemple #1
0
 def test_bucket_create_acl(self, mock_hook):
     operator = GCSBucketCreateAclEntryOperator(
         bucket="test-bucket",
         entity="test-entity",
         role="test-role",
         user_project="test-user-project",
         task_id="id",
     )
     operator.execute(None)
     mock_hook.return_value.insert_bucket_acl.assert_called_once_with(
         bucket_name="test-bucket",
         entity="test-entity",
         role="test-role",
         user_project="test-user-project",
     )
        database=DB_NAME,
        task_id='sql_db_patch_task2')
    # [END howto_operator_cloudsql_db_patch]

    # ############################################## #
    # ### EXPORTING SQL FROM INSTANCE 1 ############ #
    # ############################################## #
    export_url_split = urlsplit(EXPORT_URI)

    # For export to work we need to add the Cloud SQL instance's Service Account
    # write access to the destination GCS bucket.
    # [START howto_operator_cloudsql_export_gcs_permissions]
    sql_gcp_add_bucket_permission_task = GCSBucketCreateAclEntryOperator(
        entity="user-{{ task_instance.xcom_pull("
        "'sql_instance_create_task', key='service_account_email') "
        "}}",
        role="WRITER",
        bucket=export_url_split[1],  # netloc (bucket)
        task_id='sql_gcp_add_bucket_permission_task')
    # [END howto_operator_cloudsql_export_gcs_permissions]

    # [START howto_operator_cloudsql_export]
    sql_export_task = CloudSQLExportInstanceOperator(project_id=GCP_PROJECT_ID,
                                                     body=export_body,
                                                     instance=INSTANCE_NAME,
                                                     task_id='sql_export_task')
    sql_export_task2 = CloudSQLExportInstanceOperator(
        body=export_body, instance=INSTANCE_NAME, task_id='sql_export_task2')
    # [END howto_operator_cloudsql_export]

    # ############################################## #
        task_id="upload_file",
        src=PATH_TO_UPLOAD_FILE,
        dst=BUCKET_FILE_LOCATION,
        bucket=BUCKET_1,
    )

    transform_file = GCSFileTransformOperator(
        task_id="transform_file",
        source_bucket=BUCKET_1,
        source_object=BUCKET_FILE_LOCATION,
        transform_script=["python", PATH_TO_TRANSFORM_SCRIPT]
    )
    # [START howto_operator_gcs_bucket_create_acl_entry_task]
    gcs_bucket_create_acl_entry_task = GCSBucketCreateAclEntryOperator(
        bucket=BUCKET_1,
        entity=GCS_ACL_ENTITY,
        role=GCS_ACL_BUCKET_ROLE,
        task_id="gcs_bucket_create_acl_entry_task",
    )
    # [END howto_operator_gcs_bucket_create_acl_entry_task]

    # [START howto_operator_gcs_object_create_acl_entry_task]
    gcs_object_create_acl_entry_task = GCSObjectCreateAclEntryOperator(
        bucket=BUCKET_1,
        object_name=BUCKET_FILE_LOCATION,
        entity=GCS_ACL_ENTITY,
        role=GCS_ACL_OBJECT_ROLE,
        task_id="gcs_object_create_acl_entry_task",
    )
    # [END howto_operator_gcs_object_create_acl_entry_task]

    download_file = GCSToLocalOperator(
    # [START howto_operator_update_instance]
    update_instance = CloudMemorystoreUpdateInstanceOperator(
        task_id="update-instance",
        location="europe-north1",
        instance_id=INSTANCE_NAME,
        project_id=GCP_PROJECT_ID,
        update_mask={"paths": ["memory_size_gb"]},
        instance={"memory_size_gb": 2},
    )
    # [END howto_operator_update_instance]

    # [START howto_operator_set_acl_permission]
    set_acl_permission = GCSBucketCreateAclEntryOperator(
        task_id="gcs-set-acl-permission",
        bucket=BUCKET_NAME,
        entity=
        "user-{{ task_instance.xcom_pull('get-instance')['persistenceIamIdentity']"
        ".split(':', 2)[1] }}",
        role="OWNER",
    )
    # [END howto_operator_set_acl_permission]

    # [START howto_operator_export_instance]
    export_instance = CloudMemorystoreExportInstanceOperator(
        task_id="export-instance",
        location="europe-north1",
        instance=INSTANCE_NAME,
        output_config={"gcs_destination": {
            "uri": EXPORT_GCS_URL
        }},
        project_id=GCP_PROJECT_ID,
    )
        database=DB_NAME,
        task_id='sql_db_patch_task2')
    # [END howto_operator_cloudsql_db_patch]

    # ############################################## #
    # ### EXPORTING SQL FROM INSTANCE 1 ############ #
    # ############################################## #
    export_url_split = urlsplit(EXPORT_URI)

    # For export to work we need to add the Cloud SQL instance's Service Account
    # write access to the destination GCS bucket.
    # [START howto_operator_cloudsql_export_gcs_permissions]
    sql_gcp_add_bucket_permission_task = GCSBucketCreateAclEntryOperator(
        entity=
        f"user-{sql_instance_create_task.output['service_account_email']}",
        role="WRITER",
        bucket=export_url_split[1],  # netloc (bucket)
        task_id='sql_gcp_add_bucket_permission_task',
    )
    # [END howto_operator_cloudsql_export_gcs_permissions]

    # [START howto_operator_cloudsql_export]
    sql_export_task = CloudSQLExportInstanceOperator(project_id=GCP_PROJECT_ID,
                                                     body=export_body,
                                                     instance=INSTANCE_NAME,
                                                     task_id='sql_export_task')
    sql_export_task2 = CloudSQLExportInstanceOperator(
        body=export_body, instance=INSTANCE_NAME, task_id='sql_export_task2')
    # [END howto_operator_cloudsql_export]

    # ############################################## #