From 2c1271216393b8d2f30f3304a658b4146c4ffbba Mon Sep 17 00:00:00 2001 From: The Magician Date: Mon, 15 Jul 2019 14:58:59 -0700 Subject: [PATCH] Fix unnecessary nesting in google_storage_transfer_job (#4005) Signed-off-by: Modular Magician --- google/resource_storage_transfer_job.go | 15 +-- google/resource_storage_transfer_job_test.go | 129 ++++++++++++++++--- 2 files changed, 115 insertions(+), 29 deletions(-) diff --git a/google/resource_storage_transfer_job.go b/google/resource_storage_transfer_job.go index e9a5b15372b..e320d421aaf 100644 --- a/google/resource_storage_transfer_job.go +++ b/google/resource_storage_transfer_job.go @@ -493,10 +493,8 @@ func expandDates(dates []interface{}) *storagetransfer.Date { return nil } - dateElem := dates[0].([]interface{}) + dateMap := dates[0].(map[string]interface{}) date := &storagetransfer.Date{} - - dateMap := extractFirstMapConfig(dateElem) if v, ok := dateMap["day"]; ok { date.Day = int64(v.(int)) } @@ -509,6 +507,7 @@ func expandDates(dates []interface{}) *storagetransfer.Date { date.Year = int64(v.(int)) } + log.Printf("[DEBUG] not nil date: %#v", dates) return date } @@ -527,10 +526,8 @@ func expandTimeOfDays(times []interface{}) *storagetransfer.TimeOfDay { return nil } - timeElem := times[0].([]interface{}) + timeMap := times[0].(map[string]interface{}) time := &storagetransfer.TimeOfDay{} - - timeMap := extractFirstMapConfig(timeElem) if v, ok := timeMap["hours"]; ok { time.Hours = int64(v.(int)) } @@ -568,9 +565,9 @@ func expandTransferSchedules(transferSchedules []interface{}) *storagetransfer.S schedule := transferSchedules[0].(map[string]interface{}) return &storagetransfer.Schedule{ - ScheduleStartDate: expandDates([]interface{}{schedule["schedule_start_date"]}), - ScheduleEndDate: expandDates([]interface{}{schedule["schedule_end_date"]}), - StartTimeOfDay: expandTimeOfDays([]interface{}{schedule["start_time_of_day"]}), + ScheduleStartDate: expandDates(schedule["schedule_start_date"].([]interface{})), + ScheduleEndDate: expandDates(schedule["schedule_end_date"].([]interface{})), + StartTimeOfDay: expandTimeOfDays(schedule["start_time_of_day"].([]interface{})), } } diff --git a/google/resource_storage_transfer_job_test.go b/google/resource_storage_transfer_job_test.go index ac8fa64b5cc..85ac77123ba 100644 --- a/google/resource_storage_transfer_job_test.go +++ b/google/resource_storage_transfer_job_test.go @@ -60,6 +60,61 @@ func TestAccStorageTransferJob_basic(t *testing.T) { }) } +func TestAccStorageTransferJob_omitScheduleEndDate(t *testing.T) { + t.Parallel() + + testDataSourceBucketName := acctest.RandString(10) + testDataSinkName := acctest.RandString(10) + testTransferJobDescription := acctest.RandString(10) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccStorageTransferJobDestroy, + Steps: []resource.TestStep{ + { + Config: testAccStorageTransferJob_omitScheduleEndDate(getTestProjectFromEnv(), testDataSourceBucketName, testDataSinkName, testTransferJobDescription), + }, + { + ResourceName: "google_storage_transfer_job.transfer_job", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func testAccStorageTransferJobDestroy(s *terraform.State) error { + config := testAccProvider.Meta().(*Config) + + for _, rs := range s.RootModule().Resources { + if rs.Type != "google_storage_transfer_job" { + continue + } + + rs_attr := rs.Primary.Attributes + name, ok := rs_attr["name"] + if !ok { + return fmt.Errorf("No name set") + } + + project, err := getTestProject(rs.Primary, config) + if err != nil { + return err + } + + res, err := config.clientStorageTransfer.TransferJobs.Get(name).ProjectId(project).Do() + if res.Status != "DELETED" { + return fmt.Errorf("Transfer Job not set to DELETED") + } + if err != nil { + return fmt.Errorf("Transfer Job does not exist, should exist and be DELETED") + } + } + + return nil +} + func testAccStorageTransferJob_basic(project string, dataSourceBucketName string, dataSinkBucketName string, transferJobDescription string) string { return fmt.Sprintf(` data "google_storage_transfer_project_service_account" "default" { @@ -130,33 +185,67 @@ resource "google_storage_transfer_job" "transfer_job" { `, project, dataSourceBucketName, project, dataSinkBucketName, project, transferJobDescription, project) } -func testAccStorageTransferJobDestroy(s *terraform.State) error { - config := testAccProvider.Meta().(*Config) +func testAccStorageTransferJob_omitScheduleEndDate(project string, dataSourceBucketName string, dataSinkBucketName string, transferJobDescription string) string { + return fmt.Sprintf(` +data "google_storage_transfer_project_service_account" "default" { + project = "%s" +} - for _, rs := range s.RootModule().Resources { - if rs.Type != "google_storage_transfer_job" { - continue - } +resource "google_storage_bucket" "data_source" { + name = "%s" + project = "%s" + force_destroy = true +} - rs_attr := rs.Primary.Attributes - name, ok := rs_attr["name"] - if !ok { - return fmt.Errorf("No name set") - } +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}" +} - project, err := getTestProject(rs.Primary, config) - if err != nil { - return err +resource "google_storage_bucket" "data_sink" { + name = "%s" + project = "%s" + 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}" + } + gcs_data_sink { + bucket_name = "${google_storage_bucket.data_sink.name}" } + } - res, err := config.clientStorageTransfer.TransferJobs.Get(name).ProjectId(project).Do() - if res.Status != "DELETED" { - return fmt.Errorf("Transfer Job not set to DELETED") + schedule { + schedule_start_date { + year = 2018 + month = 10 + day = 1 } - if err != nil { - return fmt.Errorf("Transfer Job does not exist, should exist and be DELETED") + start_time_of_day { + hours = 0 + minutes = 30 + seconds = 0 + nanos = 0 } } - return nil + depends_on = [ + "google_storage_bucket_iam_member.data_source", + "google_storage_bucket_iam_member.data_sink", + ] +} +`, project, dataSourceBucketName, project, dataSinkBucketName, project, transferJobDescription, project) }