From 283cd1c1294ebc45694967af963f423558f76092 Mon Sep 17 00:00:00 2001 From: emily Date: Thu, 28 Feb 2019 00:15:17 +0000 Subject: [PATCH] Autogen for region read, cloud scheduler examples Signed-off-by: Modular Magician --- google-beta/resource_cloud_scheduler_job.go | 37 ++-- ...urce_cloud_scheduler_job_generated_test.go | 183 ++++++++++++++++++ .../resource_cloudscheduler_job_test.go | 179 +---------------- google-beta/resource_redis_instance.go | 8 + .../docs/r/cloud_scheduler_job.html.markdown | 82 +++++++- 5 files changed, 284 insertions(+), 205 deletions(-) create mode 100644 google-beta/resource_cloud_scheduler_job_generated_test.go diff --git a/google-beta/resource_cloud_scheduler_job.go b/google-beta/resource_cloud_scheduler_job.go index d888928b1d4..45747b6caed 100644 --- a/google-beta/resource_cloud_scheduler_job.go +++ b/google-beta/resource_cloud_scheduler_job.go @@ -47,6 +47,12 @@ func resourceCloudSchedulerJob() *schema.Resource { Required: true, ForceNew: true, }, + "region": { + Type: schema.TypeString, + Computed: true, + Optional: true, + ForceNew: true, + }, "app_engine_http_target": { Type: schema.TypeList, Optional: true, @@ -170,12 +176,6 @@ func resourceCloudSchedulerJob() *schema.Resource { }, ConflictsWith: []string{"app_engine_http_target", "http_target"}, }, - "region": { - Type: schema.TypeString, - Computed: true, - Optional: true, - ForceNew: true, - }, "retry_config": { Type: schema.TypeList, Optional: true, @@ -260,12 +260,6 @@ func resourceCloudSchedulerJobCreate(d *schema.ResourceData, meta interface{}) e } else if v, ok := d.GetOkExists("time_zone"); !isEmptyValue(reflect.ValueOf(timeZoneProp)) && (ok || !reflect.DeepEqual(v, timeZoneProp)) { obj["timeZone"] = timeZoneProp } - regionProp, err := expandCloudSchedulerJobRegion(d.Get("region"), d, config) - if err != nil { - return err - } else if v, ok := d.GetOkExists("region"); !isEmptyValue(reflect.ValueOf(regionProp)) && (ok || !reflect.DeepEqual(v, regionProp)) { - obj["region"] = regionProp - } retryConfigProp, err := expandCloudSchedulerJobRetryConfig(d.Get("retry_config"), d, config) if err != nil { return err @@ -335,6 +329,14 @@ func resourceCloudSchedulerJobRead(d *schema.ResourceData, meta interface{}) err return fmt.Errorf("Error reading Job: %s", err) } + region, err := getRegion(d, config) + if err != nil { + return err + } + if err := d.Set("region", region); err != nil { + return fmt.Errorf("Error reading Job: %s", err) + } + if err := d.Set("name", flattenCloudSchedulerJobName(res["name"], d)); err != nil { return fmt.Errorf("Error reading Job: %s", err) } @@ -347,9 +349,6 @@ func resourceCloudSchedulerJobRead(d *schema.ResourceData, meta interface{}) err if err := d.Set("time_zone", flattenCloudSchedulerJobTimeZone(res["timeZone"], d)); err != nil { return fmt.Errorf("Error reading Job: %s", err) } - if err := d.Set("region", flattenCloudSchedulerJobRegion(res["region"], d)); err != nil { - return fmt.Errorf("Error reading Job: %s", err) - } if err := d.Set("retry_config", flattenCloudSchedulerJobRetryConfig(res["retryConfig"], d)); err != nil { return fmt.Errorf("Error reading Job: %s", err) } @@ -420,10 +419,6 @@ func flattenCloudSchedulerJobTimeZone(v interface{}, d *schema.ResourceData) int return v } -func flattenCloudSchedulerJobRegion(v interface{}, d *schema.ResourceData) interface{} { - return v -} - func flattenCloudSchedulerJobRetryConfig(v interface{}, d *schema.ResourceData) interface{} { if v == nil { return nil @@ -683,10 +678,6 @@ func expandCloudSchedulerJobTimeZone(v interface{}, d *schema.ResourceData, conf return v, nil } -func expandCloudSchedulerJobRegion(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) { - return v, nil -} - func expandCloudSchedulerJobRetryConfig(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) { l := v.([]interface{}) if len(l) == 0 || l[0] == nil { diff --git a/google-beta/resource_cloud_scheduler_job_generated_test.go b/google-beta/resource_cloud_scheduler_job_generated_test.go new file mode 100644 index 00000000000..a50f9b0436f --- /dev/null +++ b/google-beta/resource_cloud_scheduler_job_generated_test.go @@ -0,0 +1,183 @@ +// ---------------------------------------------------------------------------- +// +// *** AUTO GENERATED CODE *** AUTO GENERATED CODE *** +// +// ---------------------------------------------------------------------------- +// +// This file is automatically generated by Magic Modules and manual +// changes will be clobbered when the file is regenerated. +// +// Please read more about how to change this file in +// .github/CONTRIBUTING.md. +// +// ---------------------------------------------------------------------------- + +package google + +import ( + "fmt" + "strings" + "testing" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestAccCloudSchedulerJob_schedulerJobPubsubExample(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "random_suffix": acctest.RandString(10), + } + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + 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" { + name = "job-topic-%{random_suffix}" +} + +resource "google_cloud_scheduler_job" "job" { + name = "test-job-%{random_suffix}" + description = "test job" + schedule = "*/2 * * * *" + + pubsub_target { + topic_name = "${google_pubsub_topic.topic.id}" + data = "${base64encode("test")}" + } +} +`, context) +} + +func TestAccCloudSchedulerJob_schedulerJobHttpExample(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "random_suffix": acctest.RandString(10), + } + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + 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" { + name = "test-job-%{random_suffix}" + description = "test http job" + schedule = "*/8 * * * *" + time_zone = "America/New_York" + + http_target { + http_method = "POST" + uri = "https://example.com/ping" + } +} +`, context) +} + +func TestAccCloudSchedulerJob_schedulerJobAppEngineExample(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "random_suffix": acctest.RandString(10), + } + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + 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" { + name = "test-job-%{random_suffix}" + schedule = "*/4 * * * *" + description = "test app engine job" + time_zone = "Europe/London" + + app_engine_http_target { + http_method = "POST" + + app_engine_routing { + service = "web" + version = "prod" + instance = "my-instance-001" + } + + relative_uri = "/ping" + } +} +`, context) +} + +func testAccCheckCloudSchedulerJobDestroy(s *terraform.State) error { + for name, rs := range s.RootModule().Resources { + if rs.Type != "google_cloud_scheduler_job" { + continue + } + if strings.HasPrefix(name, "data.") { + continue + } + + config := testAccProvider.Meta().(*Config) + + url, err := replaceVarsForTest(rs, "https://cloudscheduler.googleapis.com/v1beta1/projects/{{project}}/locations/{{region}}/jobs/{{name}}") + if err != nil { + return err + } + + _, err = sendRequest(config, "GET", url, nil) + if err == nil { + return fmt.Errorf("CloudSchedulerJob still exists at %s", url) + } + } + + return nil +} diff --git a/google-beta/resource_cloudscheduler_job_test.go b/google-beta/resource_cloudscheduler_job_test.go index d1fb4851f50..2e83620f898 100644 --- a/google-beta/resource_cloudscheduler_job_test.go +++ b/google-beta/resource_cloudscheduler_job_test.go @@ -1,190 +1,13 @@ package google import ( - "fmt" "reflect" "testing" - "github.com/hashicorp/terraform/helper/acctest" - "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/schema" - "github.com/hashicorp/terraform/terraform" ) -func TestAccCloudSchedulerJob_pubsub(t *testing.T) { - t.Parallel() - - jobResourceName := "google_cloud_scheduler_job.job" - pubSubJobName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) - project := getTestProjectFromEnv() - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckCloudSchedulerJobDestroy, - Steps: []resource.TestStep{ - { - Config: testAccCloudSchedulerJob_pubSubConfig(pubSubJobName, project), - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr(jobResourceName, "name", pubSubJobName), - resource.TestCheckResourceAttr(jobResourceName, "description", "test job"), - resource.TestCheckResourceAttr(jobResourceName, "schedule", "*/2 * * * *"), - resource.TestCheckResourceAttr(jobResourceName, "time_zone", "Etc/UTC"), - ), - }, - { - ResourceName: jobResourceName, - ImportState: true, - ImportStateVerify: true, - }, - }, - }) -} - -func TestAccCloudSchedulerJob_http(t *testing.T) { - t.Parallel() - - jobResourceName := "google_cloud_scheduler_job.job" - httpJobName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckCloudSchedulerJobDestroy, - Steps: []resource.TestStep{ - { - Config: testAccCloudSchedulerJob_httpConfig(httpJobName), - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr(jobResourceName, "name", httpJobName), - resource.TestCheckResourceAttr(jobResourceName, "description", "test http job"), - resource.TestCheckResourceAttr(jobResourceName, "schedule", "*/8 * * * *"), - resource.TestCheckResourceAttr(jobResourceName, "time_zone", "America/New_York"), - ), - }, - { - ResourceName: jobResourceName, - ImportState: true, - ImportStateVerify: true, - }, - }, - }) -} - -func TestAccCloudSchedulerJob_appEngine(t *testing.T) { - t.Parallel() - - jobResourceName := "google_cloud_scheduler_job.job" - appEngineJobName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckCloudSchedulerJobDestroy, - Steps: []resource.TestStep{ - { - Config: testAccCloudSchedulerJob_appEngineConfig(appEngineJobName), - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr(jobResourceName, "name", appEngineJobName), - resource.TestCheckResourceAttr(jobResourceName, "description", "test app engine job"), - resource.TestCheckResourceAttr(jobResourceName, "schedule", "*/4 * * * *"), - resource.TestCheckResourceAttr(jobResourceName, "time_zone", "Europe/London"), - ), - }, - { - ResourceName: jobResourceName, - ImportState: true, - ImportStateVerify: true, - }, - }, - }) -} - -func testAccCheckCloudSchedulerJobDestroy(s *terraform.State) error { - for _, rs := range s.RootModule().Resources { - if rs.Type != "google_cloud_scheduler_job" { - continue - } - - config := testAccProvider.Meta().(*Config) - - url, err := replaceVarsForTest(rs, "https://cloudscheduler.googleapis.com/v1beta1/projects/{{project}}/locations/{{region}}/jobs/{{name}}") - if err != nil { - return err - } - - _, err = sendRequest(config, "GET", url, nil) - if err == nil { - return fmt.Errorf("Function still exists at %s", url) - } - } - - return nil -} - -func testAccCloudSchedulerJob_pubSubConfig(name string, project string) string { - return fmt.Sprintf(` - -resource "google_pubsub_topic" "topic" { - name = "build-triggers" -} - -resource "google_cloud_scheduler_job" "job" { - name = "%s" - description = "test job" - schedule = "*/2 * * * *" - - pubsub_target { - topic_name = "projects/%s/topics/build-triggers" - data = "${base64encode("test")}" - } -} - - `, name, project) -} - -func testAccCloudSchedulerJob_appEngineConfig(name string) string { - return fmt.Sprintf(` - -resource "google_cloud_scheduler_job" "job" { - name = "%s" - schedule = "*/4 * * * *" - description = "test app engine job" - time_zone = "Europe/London" - - app_engine_http_target { - http_method = "POST" - app_engine_routing { - service = "web" - version = "prod" - instance = "my-instance-001" - } - - relative_uri = "/ping" - } -} - - `, name) -} - -func testAccCloudSchedulerJob_httpConfig(name string) string { - return fmt.Sprintf(` - -resource "google_cloud_scheduler_job" "job" { - name = "%s" - description = "test http job" - schedule = "*/8 * * * *" - time_zone = "America/New_York" - - http_target { - http_method = "POST" - uri = "https://example.com/ping" - } -} - - `, name) -} - -func TestFlattenHttpHeaders(t *testing.T) { +func TestCloudScheduler_FlattenHttpHeaders(t *testing.T) { cases := []struct { Input map[string]interface{} diff --git a/google-beta/resource_redis_instance.go b/google-beta/resource_redis_instance.go index 97a91ea3de3..60b5d488f4b 100644 --- a/google-beta/resource_redis_instance.go +++ b/google-beta/resource_redis_instance.go @@ -266,6 +266,14 @@ func resourceRedisInstanceRead(d *schema.ResourceData, meta interface{}) error { return fmt.Errorf("Error reading Instance: %s", err) } + region, err := getRegion(d, config) + if err != nil { + return err + } + if err := d.Set("region", region); err != nil { + return fmt.Errorf("Error reading Instance: %s", err) + } + if err := d.Set("alternative_location_id", flattenRedisInstanceAlternativeLocationId(res["alternativeLocationId"], d)); err != nil { return fmt.Errorf("Error reading Instance: %s", err) } diff --git a/website/docs/r/cloud_scheduler_job.html.markdown b/website/docs/r/cloud_scheduler_job.html.markdown index 3955a2f628c..d87034018cd 100644 --- a/website/docs/r/cloud_scheduler_job.html.markdown +++ b/website/docs/r/cloud_scheduler_job.html.markdown @@ -34,6 +34,80 @@ To get more information about Job, see: * How-to Guides * [Official Documentation](https://cloud.google.com/scheduler/) + +## Example Usage - Scheduler Job Pubsub + + +```hcl +resource "google_pubsub_topic" "topic" { + name = "job-topic" +} + +resource "google_cloud_scheduler_job" "job" { + name = "test-job" + description = "test job" + schedule = "*/2 * * * *" + + pubsub_target { + topic_name = "${google_pubsub_topic.topic.id}" + data = "${base64encode("test")}" + } +} +``` + +## Example Usage - Scheduler Job Http + + +```hcl +resource "google_cloud_scheduler_job" "job" { + name = "test-job" + description = "test http job" + schedule = "*/8 * * * *" + time_zone = "America/New_York" + + http_target { + http_method = "POST" + uri = "https://example.com/ping" + } +} +``` + +## Example Usage - Scheduler Job App Engine + + +```hcl +resource "google_cloud_scheduler_job" "job" { + name = "test-job" + schedule = "*/4 * * * *" + description = "test app engine job" + time_zone = "Europe/London" + + app_engine_http_target { + http_method = "POST" + + app_engine_routing { + service = "web" + version = "prod" + instance = "my-instance-001" + } + + relative_uri = "/ping" + } +} +``` + ## Argument Reference The following arguments are supported: @@ -43,6 +117,10 @@ The following arguments are supported: (Required) The name of the job. +* `region` - + (Required) + Region where the scheduler job resides + - - - @@ -61,10 +139,6 @@ The following arguments are supported: Specifies the time zone to be used in interpreting schedule. The value of this field must be a time zone name from the tz database. -* `region` - - (Optional) - Region where the schedule job resides. - * `retry_config` - (Optional) By default, if a job does not complete successfully,