示例#1
0
 def test_object_create_acl(self, mock_hook):
     operator = GCSObjectCreateAclEntryOperator(
         bucket="test-bucket",
         object_name="test-object",
         entity="test-entity",
         generation=42,
         role="test-role",
         user_project="test-user-project",
         task_id="id",
     )
     operator.execute(None)
     mock_hook.return_value.insert_object_acl.assert_called_once_with(
         bucket_name="test-bucket",
         object_name="test-object",
         entity="test-entity",
         generation=42,
         role="test-role",
         user_project="test-user-project",
     )
示例#2
0
        body=export_body, instance=INSTANCE_NAME, task_id='sql_export_task2')
    # [END howto_operator_cloudsql_export]

    # ############################################## #
    # ### IMPORTING SQL TO INSTANCE 2 ############## #
    # ############################################## #
    import_url_split = urlsplit(IMPORT_URI)

    # For import to work we need to add the Cloud SQL instance's Service Account
    # read access to the target GCS object.
    # [START howto_operator_cloudsql_import_gcs_permissions]
    sql_gcp_add_object_permission_task = GCSObjectCreateAclEntryOperator(
        entity="user-{{ task_instance.xcom_pull("
        "'sql_instance_create_task2', key='service_account_email')"
        " }}",
        role="READER",
        bucket=import_url_split[1],  # netloc (bucket)
        object_name=import_url_split[2][1:],  # path (strip first '/')
        task_id='sql_gcp_add_object_permission_task',
    )

    # For import to work we also need to add the Cloud SQL instance's Service Account
    # write access to the whole bucket!.
    sql_gcp_add_bucket_permission_2_task = GCSBucketCreateAclEntryOperator(
        entity="user-{{ task_instance.xcom_pull("
        "'sql_instance_create_task2', key='service_account_email') "
        "}}",
        role="WRITER",
        bucket=import_url_split[1],  # netloc
        task_id='sql_gcp_add_bucket_permission_2_task',
    )
        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(
        task_id="download_file",
        object_name=BUCKET_FILE_LOCATION,
        bucket=BUCKET_1,
        filename=PATH_TO_SAVED_FILE,
    )

    copy_file = GCSToGCSOperator(
        task_id="copy_file",
        source_bucket=BUCKET_1,
    sql_export_task2 = CloudSQLExportInstanceOperator(
        body=export_body, instance=INSTANCE_NAME, task_id='sql_export_task2')
    # [END howto_operator_cloudsql_export]

    # ############################################## #
    # ### IMPORTING SQL TO INSTANCE 2 ############## #
    # ############################################## #
    import_url_split = urlsplit(IMPORT_URI)

    # For import to work we need to add the Cloud SQL instance's Service Account
    # read access to the target GCS object.
    # [START howto_operator_cloudsql_import_gcs_permissions]
    sql_gcp_add_object_permission_task = GCSObjectCreateAclEntryOperator(
        entity=
        f"user-{sql_instance_create_2_task.output['service_account_email']}",
        role="READER",
        bucket=import_url_split[1],  # netloc (bucket)
        object_name=import_url_split[2][1:],  # path (strip first '/')
        task_id='sql_gcp_add_object_permission_task',
    )

    # For import to work we also need to add the Cloud SQL instance's Service Account
    # write access to the whole bucket!.
    sql_gcp_add_bucket_permission_2_task = GCSBucketCreateAclEntryOperator(
        entity=
        f"user-{sql_instance_create_2_task.output['service_account_email']}",
        role="WRITER",
        bucket=import_url_split[1],  # netloc
        task_id='sql_gcp_add_bucket_permission_2_task',
    )
    # [END howto_operator_cloudsql_import_gcs_permissions]