Skip to content

Commit

Permalink
Fix storagetransfer transferoptions perm diff (#6063) (#11812)
Browse files Browse the repository at this point in the history
* Add transferOptions tests

* Fix transferOptions permanently diff when set all false explicitly

* Update mmv1/third_party/terraform/tests/resource_storage_transfer_job_test.go

Co-authored-by: Sam Levenick <slevenick@google.com>

Co-authored-by: Sam Levenick <slevenick@google.com>
Signed-off-by: Modular Magician <magic-modules@google.com>

Co-authored-by: Sam Levenick <slevenick@google.com>
  • Loading branch information
modular-magician and slevenick committed Jun 2, 2022
1 parent f293f5c commit d91635d
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .changelog/6063.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
storagetransfer: fixed perm diff on transfer_options for `google_storage_transfer_job`
```
11 changes: 7 additions & 4 deletions google/resource_storage_transfer_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -999,9 +999,6 @@ func expandTransferOptions(options []interface{}) *storagetransfer.TransferOptio
}

func flattenTransferOption(option *storagetransfer.TransferOptions) []map[string]interface{} {
if option.DeleteObjectsFromSourceAfterTransfer == false && option.DeleteObjectsUniqueInSink == false && option.OverwriteObjectsAlreadyExistingInSink == false {
return nil
}
data := map[string]interface{}{
"delete_objects_from_source_after_transfer": option.DeleteObjectsFromSourceAfterTransfer,
"delete_objects_unique_in_sink": option.DeleteObjectsUniqueInSink,
Expand Down Expand Up @@ -1042,7 +1039,9 @@ func flattenTransferSpec(transferSpec *storagetransfer.TransferSpec, d *schema.R
if transferSpec.ObjectConditions != nil {
data["object_conditions"] = flattenObjectCondition(transferSpec.ObjectConditions)
}
if transferSpec.TransferOptions != nil {
if transferSpec.TransferOptions != nil &&
(usingPosix(transferSpec) == false ||
(usingPosix(transferSpec) == true && reflect.DeepEqual(transferSpec.TransferOptions, &storagetransfer.TransferOptions{}) == false)) {
data["transfer_options"] = flattenTransferOption(transferSpec.TransferOptions)
}
if transferSpec.GcsDataSource != nil {
Expand All @@ -1059,3 +1058,7 @@ func flattenTransferSpec(transferSpec *storagetransfer.TransferSpec, d *schema.R

return []map[string][]map[string]interface{}{data}
}

func usingPosix(transferSpec *storagetransfer.TransferSpec) bool {
return transferSpec.PosixDataSource != nil || transferSpec.PosixDataSink != nil
}
128 changes: 128 additions & 0 deletions google/resource_storage_transfer_job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,54 @@ func TestAccStorageTransferJob_posixSink(t *testing.T) {
})
}

func TestAccStorageTransferJob_transferOptions(t *testing.T) {
t.Parallel()

testDataSourceBucketName := randString(t, 10)
testDataSinkName := randString(t, 10)
testTransferJobDescription := randString(t, 10)

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccStorageTransferJobDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccStorageTransferJob_basic(getTestProjectFromEnv(), testDataSourceBucketName, testDataSinkName, testTransferJobDescription),
},
{
ResourceName: "google_storage_transfer_job.transfer_job",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccStorageTransferJob_transferOptions(getTestProjectFromEnv(), testDataSourceBucketName, testDataSinkName, testTransferJobDescription, false, false, false),
},
{
ResourceName: "google_storage_transfer_job.transfer_job",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccStorageTransferJob_transferOptions(getTestProjectFromEnv(), testDataSourceBucketName, testDataSinkName, testTransferJobDescription, true, true, false),
},
{
ResourceName: "google_storage_transfer_job.transfer_job",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccStorageTransferJob_transferOptions(getTestProjectFromEnv(), testDataSourceBucketName, testDataSinkName, testTransferJobDescription, true, false, true),
},
{
ResourceName: "google_storage_transfer_job.transfer_job",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccStorageTransferJobDestroyProducer(t *testing.T) func(s *terraform.State) error {
return func(s *terraform.State) error {
config := googleProviderConfig(t)
Expand Down Expand Up @@ -497,3 +545,83 @@ 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 {
return fmt.Sprintf(`
data "google_storage_transfer_project_service_account" "default" {
project = "%s"
}
resource "google_storage_bucket" "data_source" {
name = "%s"
project = "%s"
location = "US"
force_destroy = true
}
resource "google_storage_bucket_iam_member" "data_source" {
bucket = google_storage_bucket.data_source.name
role = "roles/storage.admin"
member = "serviceAccount:${data.google_storage_transfer_project_service_account.default.email}"
}
resource "google_storage_bucket" "data_sink" {
name = "%s"
project = "%s"
location = "US"
force_destroy = true
}
resource "google_storage_bucket_iam_member" "data_sink" {
bucket = google_storage_bucket.data_sink.name
role = "roles/storage.admin"
member = "serviceAccount:${data.google_storage_transfer_project_service_account.default.email}"
}
resource "google_storage_transfer_job" "transfer_job" {
description = "%s"
project = "%s"
transfer_spec {
gcs_data_source {
bucket_name = google_storage_bucket.data_source.name
path = "foo/bar/"
}
gcs_data_sink {
bucket_name = google_storage_bucket.data_sink.name
path = "foo/bar/"
}
transfer_options {
overwrite_objects_already_existing_in_sink = %t
delete_objects_unique_in_sink = %t
delete_objects_from_source_after_transfer = %t
}
}
schedule {
schedule_start_date {
year = 2018
month = 10
day = 1
}
schedule_end_date {
year = 2019
month = 10
day = 1
}
start_time_of_day {
hours = 0
minutes = 30
seconds = 0
nanos = 0
}
repeat_interval = "604800s"
}
depends_on = [
google_storage_bucket_iam_member.data_source,
google_storage_bucket_iam_member.data_sink,
]
}
`, project, dataSourceBucketName, project, dataSinkBucketName, project, transferJobDescription, project, overwriteObjectsAlreadyExistingInSink, deleteObjectsUniqueInSink, deleteObjectsFromSourceAfterTransfer)
}

0 comments on commit d91635d

Please sign in to comment.