Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added overwrite_when attribute in storage_transfer_job #12573

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/6544.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
storage: added support for `overwriteWhen` field to `transfer_options` in `google_storage_transfer_job` resource
```
10 changes: 10 additions & 0 deletions google/resource_storage_transfer_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var (
"transfer_spec.0.transfer_options.0.overwrite_objects_already_existing_in_sink",
"transfer_spec.0.transfer_options.0.delete_objects_unique_in_sink",
"transfer_spec.0.transfer_options.0.delete_objects_from_source_after_transfer",
"transfer_spec.0.transfer_options.0.overwrite_when",
}

transferSpecDataSourceKeys = []string{
Expand Down Expand Up @@ -284,6 +285,13 @@ func transferOptionsSchema() *schema.Schema {
ConflictsWith: []string{"transfer_spec.transfer_options.delete_objects_unique_in_sink"},
Description: `Whether objects should be deleted from the source after they are transferred to the sink. Note that this option and delete_objects_unique_in_sink are mutually exclusive.`,
},
"overwrite_when": {
Type: schema.TypeString,
Optional: true,
AtLeastOneOf: transferOptionsKeys,
ValidateFunc: validation.StringInSlice([]string{"DIFFERENT", "NEVER", "ALWAYS"}, false),
Description: `When to overwrite objects that already exist in the sink. If not set, overwrite behavior is determined by overwriteObjectsAlreadyExistingInSink.`,
},
},
},
Description: `Characteristics of how to treat files from datasource and sink during job. If the option delete_objects_unique_in_sink is true, object conditions based on objects' last_modification_time are ignored and do not exclude objects in a data source or a data sink.`,
Expand Down Expand Up @@ -995,6 +1003,7 @@ func expandTransferOptions(options []interface{}) *storagetransfer.TransferOptio
DeleteObjectsFromSourceAfterTransfer: option["delete_objects_from_source_after_transfer"].(bool),
DeleteObjectsUniqueInSink: option["delete_objects_unique_in_sink"].(bool),
OverwriteObjectsAlreadyExistingInSink: option["overwrite_objects_already_existing_in_sink"].(bool),
OverwriteWhen: option["overwrite_when"].(string),
}
}

Expand All @@ -1003,6 +1012,7 @@ func flattenTransferOption(option *storagetransfer.TransferOptions) []map[string
"delete_objects_from_source_after_transfer": option.DeleteObjectsFromSourceAfterTransfer,
"delete_objects_unique_in_sink": option.DeleteObjectsUniqueInSink,
"overwrite_objects_already_existing_in_sink": option.OverwriteObjectsAlreadyExistingInSink,
"overwrite_when": option.OverwriteWhen,
}

return []map[string]interface{}{data}
Expand Down
12 changes: 7 additions & 5 deletions google/resource_storage_transfer_job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ func TestAccStorageTransferJob_transferOptions(t *testing.T) {
testDataSourceBucketName := randString(t, 10)
testDataSinkName := randString(t, 10)
testTransferJobDescription := randString(t, 10)
testOverwriteWhen := []string{"ALWAYS", "NEVER", "DIFFERENT"}

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -158,23 +159,23 @@ func TestAccStorageTransferJob_transferOptions(t *testing.T) {
ImportStateVerify: true,
},
{
Config: testAccStorageTransferJob_transferOptions(getTestProjectFromEnv(), testDataSourceBucketName, testDataSinkName, testTransferJobDescription, false, false, false),
Config: testAccStorageTransferJob_transferOptions(getTestProjectFromEnv(), testDataSourceBucketName, testDataSinkName, testTransferJobDescription, false, false, false, testOverwriteWhen[0]),
},
{
ResourceName: "google_storage_transfer_job.transfer_job",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccStorageTransferJob_transferOptions(getTestProjectFromEnv(), testDataSourceBucketName, testDataSinkName, testTransferJobDescription, true, true, false),
Config: testAccStorageTransferJob_transferOptions(getTestProjectFromEnv(), testDataSourceBucketName, testDataSinkName, testTransferJobDescription, true, true, false, testOverwriteWhen[1]),
},
{
ResourceName: "google_storage_transfer_job.transfer_job",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccStorageTransferJob_transferOptions(getTestProjectFromEnv(), testDataSourceBucketName, testDataSinkName, testTransferJobDescription, true, false, true),
Config: testAccStorageTransferJob_transferOptions(getTestProjectFromEnv(), testDataSourceBucketName, testDataSinkName, testTransferJobDescription, true, false, true, testOverwriteWhen[2]),
},
{
ResourceName: "google_storage_transfer_job.transfer_job",
Expand Down Expand Up @@ -546,7 +547,7 @@ resource "google_storage_transfer_job" "transfer_job" {
`, project, dataSourceBucketName, project, transferJobDescription, project)
}

func testAccStorageTransferJob_transferOptions(project string, dataSourceBucketName string, dataSinkBucketName string, transferJobDescription string, overwriteObjectsAlreadyExistingInSink bool, deleteObjectsUniqueInSink bool, deleteObjectsFromSourceAfterTransfer bool) string {
func testAccStorageTransferJob_transferOptions(project string, dataSourceBucketName string, dataSinkBucketName string, transferJobDescription string, overwriteObjectsAlreadyExistingInSink bool, deleteObjectsUniqueInSink bool, deleteObjectsFromSourceAfterTransfer bool, overwriteWhenVal string) string {
return fmt.Sprintf(`
data "google_storage_transfer_project_service_account" "default" {
project = "%s"
Expand Down Expand Up @@ -595,6 +596,7 @@ resource "google_storage_transfer_job" "transfer_job" {
overwrite_objects_already_existing_in_sink = %t
delete_objects_unique_in_sink = %t
delete_objects_from_source_after_transfer = %t
overwrite_when = "%s"
}
}

Expand Down Expand Up @@ -623,5 +625,5 @@ resource "google_storage_transfer_job" "transfer_job" {
google_storage_bucket_iam_member.data_sink,
]
}
`, project, dataSourceBucketName, project, dataSinkBucketName, project, transferJobDescription, project, overwriteObjectsAlreadyExistingInSink, deleteObjectsUniqueInSink, deleteObjectsFromSourceAfterTransfer)
`, project, dataSourceBucketName, project, dataSinkBucketName, project, transferJobDescription, project, overwriteObjectsAlreadyExistingInSink, deleteObjectsUniqueInSink, deleteObjectsFromSourceAfterTransfer, overwriteWhenVal)
}
2 changes: 2 additions & 0 deletions website/docs/r/storage_transfer_job.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ A duration in seconds with up to nine fractional digits, terminated by 's'. Exam

* `delete_objects_from_source_after_transfer` - (Optional) Whether objects should be deleted from the source after they are transferred to the sink. Note that this option and `delete_objects_unique_in_sink` are mutually exclusive.

* `overwrite_when` - (Optional) When to overwrite objects that already exist in the sink. If not set, overwrite behavior is determined by `overwrite_objects_already_existing_in_sink`. Possible values: ALWAYS, DIFFERENT, NEVER.

<a name="nested_gcs_data_sink"></a>The `gcs_data_sink` block supports:

* `bucket_name` - (Required) Google Cloud Storage bucket name.
Expand Down