Skip to content

Commit

Permalink
Fix unnecessary nesting in google_storage_transfer_job (#4005)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored and rileykarson committed Jul 15, 2019
1 parent a4cf3a3 commit 2c12712
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 29 deletions.
15 changes: 6 additions & 9 deletions google/resource_storage_transfer_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand All @@ -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
}

Expand All @@ -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))
}
Expand Down Expand Up @@ -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{})),
}
}

Expand Down
129 changes: 109 additions & 20 deletions google/resource_storage_transfer_job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down Expand Up @@ -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)
}

0 comments on commit 2c12712

Please sign in to comment.