Skip to content

Commit

Permalink
Autogen for region read, cloud scheduler examples
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
emilymye authored and modular-magician committed Feb 28, 2019
1 parent 1769a49 commit 283cd1c
Show file tree
Hide file tree
Showing 5 changed files with 284 additions and 205 deletions.
37 changes: 14 additions & 23 deletions google-beta/resource_cloud_scheduler_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
183 changes: 183 additions & 0 deletions google-beta/resource_cloud_scheduler_job_generated_test.go
Original file line number Diff line number Diff line change
@@ -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
}
Loading

0 comments on commit 283cd1c

Please sign in to comment.