Skip to content

Commit

Permalink
Safely access maps in storage transfer (#499)
Browse files Browse the repository at this point in the history
<!-- This change is generated by MagicModules. -->
/cc @rileykarson
  • Loading branch information
modular-magician authored and rileykarson committed Mar 6, 2019
1 parent f38e731 commit cd7d819
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 32 deletions.
47 changes: 26 additions & 21 deletions google-beta/resource_cloud_scheduler_job_generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,27 @@ func TestAccCloudSchedulerJob_schedulerJobPubsubExample(t *testing.T) {

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Providers: testAccProvidersOiCS,
CheckDestroy: testAccCheckCloudSchedulerJobDestroy,
Steps: []resource.TestStep{
{
Config: testAccCloudSchedulerJob_schedulerJobPubsubExample(context),
},
{
ResourceName: "google_cloud_scheduler_job.job",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"region"},
},
},
})
}

func testAccCloudSchedulerJob_schedulerJobPubsubExample(context map[string]interface{}) string {
return Nprintf(`
resource "google_pubsub_topic" "topic" {
provider = "google-beta"
name = "job-topic-%{random_suffix}"
}
resource "google_cloud_scheduler_job" "job" {
provider = "google-beta"
name = "test-job-%{random_suffix}"
description = "test job"
schedule = "*/2 * * * *"
Expand All @@ -65,6 +63,11 @@ resource "google_cloud_scheduler_job" "job" {
data = "${base64encode("test")}"
}
}
provider "google-beta"{
region = "us-central1"
zone = "us-central1-a"
}
`, context)
}

Expand All @@ -77,25 +80,21 @@ func TestAccCloudSchedulerJob_schedulerJobHttpExample(t *testing.T) {

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Providers: testAccProvidersOiCS,
CheckDestroy: testAccCheckCloudSchedulerJobDestroy,
Steps: []resource.TestStep{
{
Config: testAccCloudSchedulerJob_schedulerJobHttpExample(context),
},
{
ResourceName: "google_cloud_scheduler_job.job",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"region"},
},
},
})
}

func testAccCloudSchedulerJob_schedulerJobHttpExample(context map[string]interface{}) string {
return Nprintf(`
resource "google_cloud_scheduler_job" "job" {
provider = "google-beta"
name = "test-job-%{random_suffix}"
description = "test http job"
schedule = "*/8 * * * *"
Expand All @@ -106,6 +105,11 @@ resource "google_cloud_scheduler_job" "job" {
uri = "https://example.com/ping"
}
}
provider "google-beta"{
region = "us-central1"
zone = "us-central1-a"
}
`, context)
}

Expand All @@ -118,25 +122,21 @@ func TestAccCloudSchedulerJob_schedulerJobAppEngineExample(t *testing.T) {

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Providers: testAccProvidersOiCS,
CheckDestroy: testAccCheckCloudSchedulerJobDestroy,
Steps: []resource.TestStep{
{
Config: testAccCloudSchedulerJob_schedulerJobAppEngineExample(context),
},
{
ResourceName: "google_cloud_scheduler_job.job",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"region"},
},
},
})
}

func testAccCloudSchedulerJob_schedulerJobAppEngineExample(context map[string]interface{}) string {
return Nprintf(`
resource "google_cloud_scheduler_job" "job" {
provider = "google-beta"
name = "test-job-%{random_suffix}"
schedule = "*/4 * * * *"
description = "test app engine job"
Expand All @@ -154,6 +154,11 @@ resource "google_cloud_scheduler_job" "job" {
relative_uri = "/ping"
}
}
provider "google-beta"{
region = "us-central1"
zone = "us-central1-a"
}
`, context)
}

Expand Down
47 changes: 36 additions & 11 deletions google-beta/resource_storage_transfer_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,12 +491,23 @@ func expandDates(dates []interface{}) *storagetransfer.Date {
return nil
}

date := dates[0].([]interface{})
return &storagetransfer.Date{
Day: int64(extractFirstMapConfig(date)["day"].(int)),
Month: int64(extractFirstMapConfig(date)["month"].(int)),
Year: int64(extractFirstMapConfig(date)["year"].(int)),
dateElem := dates[0].([]interface{})
date := &storagetransfer.Date{}

dateMap := extractFirstMapConfig(dateElem)
if v, ok := dateMap["day"]; ok {
date.Day = int64(v.(int))
}

if v, ok := dateMap["month"]; ok {
date.Month = int64(v.(int))
}

if v, ok := dateMap["year"]; ok {
date.Year = int64(v.(int))
}

return date
}

func flattenDate(date *storagetransfer.Date) []map[string]interface{} {
Expand All @@ -514,13 +525,27 @@ func expandTimeOfDays(times []interface{}) *storagetransfer.TimeOfDay {
return nil
}

time := times[0].([]interface{})
return &storagetransfer.TimeOfDay{
Hours: int64(extractFirstMapConfig(time)["hours"].(int)),
Minutes: int64(extractFirstMapConfig(time)["minutes"].(int)),
Seconds: int64(extractFirstMapConfig(time)["seconds"].(int)),
Nanos: int64(extractFirstMapConfig(time)["nanos"].(int)),
timeElem := times[0].([]interface{})
time := &storagetransfer.TimeOfDay{}

timeMap := extractFirstMapConfig(timeElem)
if v, ok := timeMap["hours"]; ok {
time.Hours = int64(v.(int))
}

if v, ok := timeMap["minutes"]; ok {
time.Minutes = int64(v.(int))
}

if v, ok := timeMap["seconds"]; ok {
time.Seconds = int64(v.(int))
}

if v, ok := timeMap["nanos"]; ok {
time.Nanos = int64(v.(int))
}

return time
}

func flattenTimeOfDay(timeOfDay *storagetransfer.TimeOfDay) []map[string]interface{} {
Expand Down
23 changes: 23 additions & 0 deletions website/docs/r/cloud_scheduler_job.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,14 @@ To get more information about Job, see:

```hcl
resource "google_pubsub_topic" "topic" {
provider = "google-beta"
name = "job-topic"
}
resource "google_cloud_scheduler_job" "job" {
provider = "google-beta"
name = "test-job"
description = "test job"
schedule = "*/2 * * * *"
Expand All @@ -55,6 +59,11 @@ resource "google_cloud_scheduler_job" "job" {
data = "${base64encode("test")}"
}
}
provider "google-beta"{
region = "us-central1"
zone = "us-central1-a"
}
```
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgithub.jparrowsec.cn%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_working_dir=scheduler_job_http&cloudshell_image=gcr.io%2Fgraphite-cloud-shell-images%2Fterraform%3Alatest&open_in_editor=main.tf&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md" target="_blank">
Expand All @@ -66,6 +75,8 @@ resource "google_cloud_scheduler_job" "job" {

```hcl
resource "google_cloud_scheduler_job" "job" {
provider = "google-beta"
name = "test-job"
description = "test http job"
schedule = "*/8 * * * *"
Expand All @@ -76,6 +87,11 @@ resource "google_cloud_scheduler_job" "job" {
uri = "https://example.com/ping"
}
}
provider "google-beta"{
region = "us-central1"
zone = "us-central1-a"
}
```
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgithub.jparrowsec.cn%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_working_dir=scheduler_job_app_engine&cloudshell_image=gcr.io%2Fgraphite-cloud-shell-images%2Fterraform%3Alatest&open_in_editor=main.tf&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md" target="_blank">
Expand All @@ -87,6 +103,8 @@ resource "google_cloud_scheduler_job" "job" {

```hcl
resource "google_cloud_scheduler_job" "job" {
provider = "google-beta"
name = "test-job"
schedule = "*/4 * * * *"
description = "test app engine job"
Expand All @@ -104,6 +122,11 @@ resource "google_cloud_scheduler_job" "job" {
relative_uri = "/ping"
}
}
provider "google-beta"{
region = "us-central1"
zone = "us-central1-a"
}
```

## Argument Reference
Expand Down

0 comments on commit cd7d819

Please sign in to comment.