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

Fix unnecessary nesting in google_storage_transfer_job #4005

Merged
merged 1 commit into from
Jul 15, 2019
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
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)
}