Skip to content

Commit

Permalink
Added attempt deadline to scheduler resource
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
guillaumeblaquiere authored and modular-magician committed Jan 16, 2020
1 parent 99af4c9 commit 878c4c2
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 95 deletions.
31 changes: 31 additions & 0 deletions google/resource_cloud_scheduler_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,20 @@ Headers can be set when the job is created.`,
},
ExactlyOneOf: []string{"pubsub_target", "http_target", "app_engine_http_target"},
},
"attempt_deadline": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
DiffSuppressFunc: emptyOrDefaultStringSuppress("180s"),
Description: `The deadline for job attempts. If the request handler does not respond by this deadline then the request is
cancelled and the attempt is marked as a DEADLINE_EXCEEDED failure. The failed attempt can be viewed in
execution logs. Cloud Scheduler will retry the job according to the RetryConfig.
The allowed duration for this deadline is:
* For HTTP targets, between 15 seconds and 30 minutes.
* For App Engine HTTP targets, between 15 seconds and 24 hours.
A duration in seconds with up to nine fractional digits, terminated by 's'. Example: "3.5s"`,
Default: "180s",
},
"description": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -440,6 +454,12 @@ 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
}
attemptDeadlineProp, err := expandCloudSchedulerJobAttemptDeadline(d.Get("attempt_deadline"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("attempt_deadline"); !isEmptyValue(reflect.ValueOf(attemptDeadlineProp)) && (ok || !reflect.DeepEqual(v, attemptDeadlineProp)) {
obj["attemptDeadline"] = attemptDeadlineProp
}
retryConfigProp, err := expandCloudSchedulerJobRetryConfig(d.Get("retry_config"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -533,6 +553,9 @@ 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("attempt_deadline", flattenCloudSchedulerJobAttemptDeadline(res["attemptDeadline"], 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 @@ -614,6 +637,10 @@ func flattenCloudSchedulerJobTimeZone(v interface{}, d *schema.ResourceData) int
return v
}

func flattenCloudSchedulerJobAttemptDeadline(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 @@ -895,6 +922,10 @@ func expandCloudSchedulerJobTimeZone(v interface{}, d TerraformResourceData, con
return v, nil
}

func expandCloudSchedulerJobAttemptDeadline(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandCloudSchedulerJobRetryConfig(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
Expand Down
36 changes: 20 additions & 16 deletions google/resource_cloud_scheduler_job_generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,11 @@ func TestAccCloudSchedulerJob_schedulerJobHttpExample(t *testing.T) {
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"
name = "test-job%{random_suffix}"
description = "test http job"
schedule = "*/8 * * * *"
time_zone = "America/New_York"
attempt_deadline = "320s"
http_target {
http_method = "POST"
Expand Down Expand Up @@ -138,10 +139,11 @@ func TestAccCloudSchedulerJob_schedulerJobAppEngineExample(t *testing.T) {
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"
name = "test-job%{random_suffix}"
schedule = "*/4 * * * *"
description = "test app engine job"
time_zone = "Europe/London"
attempt_deadline = "320s"
app_engine_http_target {
http_method = "POST"
Expand Down Expand Up @@ -191,10 +193,11 @@ data "google_compute_default_service_account" "default" {
}
resource "google_cloud_scheduler_job" "job" {
name = "test-job%{random_suffix}"
description = "test http job"
schedule = "*/8 * * * *"
time_zone = "America/New_York"
name = "test-job%{random_suffix}"
description = "test http job"
schedule = "*/8 * * * *"
time_zone = "America/New_York"
attempt_deadline = "320s"
http_target {
http_method = "GET"
Expand Down Expand Up @@ -239,10 +242,11 @@ data "google_compute_default_service_account" "default" {
}
resource "google_cloud_scheduler_job" "job" {
name = "test-job%{random_suffix}"
description = "test http job"
schedule = "*/8 * * * *"
time_zone = "America/New_York"
name = "test-job%{random_suffix}"
description = "test http job"
schedule = "*/8 * * * *"
time_zone = "America/New_York"
attempt_deadline = "320s"
http_target {
http_method = "GET"
Expand Down
31 changes: 7 additions & 24 deletions google/resource_compute_network_peering.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
computeBeta "google.golang.org/api/compute/v0.beta"
"google.golang.org/api/compute/v1"
"google.golang.org/api/googleapi"
)
Expand Down Expand Up @@ -46,20 +47,6 @@ func resourceComputeNetworkPeering() *schema.Resource {
DiffSuppressFunc: compareSelfLinkRelativePaths,
},

"export_custom_routes": {
Type: schema.TypeBool,
ForceNew: true,
Optional: true,
Default: false,
},

"import_custom_routes": {
Type: schema.TypeBool,
ForceNew: true,
Optional: true,
Default: false,
},

"state": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -91,7 +78,7 @@ func resourceComputeNetworkPeeringCreate(d *schema.ResourceData, meta interface{
return err
}

request := &compute.NetworksAddPeeringRequest{}
request := &computeBeta.NetworksAddPeeringRequest{}
request.NetworkPeering = expandNetworkPeering(d)

// Only one peering operation at a time can be performed for a given network.
Expand All @@ -102,7 +89,7 @@ func resourceComputeNetworkPeeringCreate(d *schema.ResourceData, meta interface{
defer mutexKV.Unlock(kn)
}

addOp, err := config.clientCompute.Networks.AddPeering(networkFieldValue.Project, networkFieldValue.Name, request).Do()
addOp, err := config.clientComputeBeta.Networks.AddPeering(networkFieldValue.Project, networkFieldValue.Name, request).Do()
if err != nil {
return fmt.Errorf("Error adding network peering: %s", err)
}
Expand All @@ -126,7 +113,7 @@ func resourceComputeNetworkPeeringRead(d *schema.ResourceData, meta interface{})
return err
}

network, err := config.clientCompute.Networks.Get(networkFieldValue.Project, networkFieldValue.Name).Do()
network, err := config.clientComputeBeta.Networks.Get(networkFieldValue.Project, networkFieldValue.Name).Do()
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("Network %q", networkFieldValue.Name))
}
Expand All @@ -140,8 +127,6 @@ func resourceComputeNetworkPeeringRead(d *schema.ResourceData, meta interface{})

d.Set("peer_network", peering.Network)
d.Set("name", peering.Name)
d.Set("import_custom_routes", peering.ImportCustomRoutes)
d.Set("export_custom_routes", peering.ExportCustomRoutes)
d.Set("state", peering.State)
d.Set("state_details", peering.StateDetails)

Expand Down Expand Up @@ -191,21 +176,19 @@ func resourceComputeNetworkPeeringDelete(d *schema.ResourceData, meta interface{
return nil
}

func findPeeringFromNetwork(network *compute.Network, peeringName string) *compute.NetworkPeering {
func findPeeringFromNetwork(network *computeBeta.Network, peeringName string) *computeBeta.NetworkPeering {
for _, p := range network.Peerings {
if p.Name == peeringName {
return p
}
}
return nil
}
func expandNetworkPeering(d *schema.ResourceData) *compute.NetworkPeering {
return &compute.NetworkPeering{
func expandNetworkPeering(d *schema.ResourceData) *computeBeta.NetworkPeering {
return &computeBeta.NetworkPeering{
ExchangeSubnetRoutes: true,
Name: d.Get("name").(string),
Network: d.Get("peer_network").(string),
ExportCustomRoutes: d.Get("export_custom_routes").(bool),
ImportCustomRoutes: d.Get("import_custom_routes").(bool),
}
}

Expand Down
43 changes: 6 additions & 37 deletions google/resource_compute_network_peering_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"google.golang.org/api/compute/v1"
computeBeta "google.golang.org/api/compute/v0.beta"
)

func TestAccComputeNetworkPeering_basic(t *testing.T) {
t.Parallel()
var peering_beta compute.NetworkPeering
var peering_beta computeBeta.NetworkPeering

primaryNetworkName := acctest.RandomWithPrefix("network-test-1")
peeringName := acctest.RandomWithPrefix("peering-test-1")
Expand All @@ -27,17 +27,10 @@ func TestAccComputeNetworkPeering_basic(t *testing.T) {
{
Config: testAccComputeNetworkPeering_basic(primaryNetworkName, peeringName),
Check: resource.ComposeTestCheckFunc(
// network foo
testAccCheckComputeNetworkPeeringExist("google_compute_network_peering.foo", &peering_beta),
testAccCheckComputeNetworkPeeringAutoCreateRoutes(true, &peering_beta),
testAccCheckComputeNetworkPeeringImportCustomRoutes(false, &peering_beta),
testAccCheckComputeNetworkPeeringExportCustomRoutes(false, &peering_beta),

// network bar
testAccCheckComputeNetworkPeeringExist("google_compute_network_peering.bar", &peering_beta),
testAccCheckComputeNetworkPeeringAutoCreateRoutes(true, &peering_beta),
testAccCheckComputeNetworkPeeringImportCustomRoutes(true, &peering_beta),
testAccCheckComputeNetworkPeeringExportCustomRoutes(true, &peering_beta),
),
},
{
Expand All @@ -59,7 +52,7 @@ func testAccComputeNetworkPeeringDestroy(s *terraform.State) error {
continue
}

_, err := config.clientCompute.Networks.Get(
_, err := config.clientComputeBeta.Networks.Get(
config.Project, rs.Primary.ID).Do()
if err == nil {
return fmt.Errorf("Network peering still exists")
Expand All @@ -69,7 +62,7 @@ func testAccComputeNetworkPeeringDestroy(s *terraform.State) error {
return nil
}

func testAccCheckComputeNetworkPeeringExist(n string, peering *compute.NetworkPeering) resource.TestCheckFunc {
func testAccCheckComputeNetworkPeeringExist(n string, peering *computeBeta.NetworkPeering) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
Expand All @@ -89,7 +82,7 @@ func testAccCheckComputeNetworkPeeringExist(n string, peering *compute.NetworkPe

networkName, peeringName := parts[0], parts[1]

network, err := config.clientCompute.Networks.Get(config.Project, networkName).Do()
network, err := config.clientComputeBeta.Networks.Get(config.Project, networkName).Do()
if err != nil {
return err
}
Expand All @@ -104,7 +97,7 @@ func testAccCheckComputeNetworkPeeringExist(n string, peering *compute.NetworkPe
}
}

func testAccCheckComputeNetworkPeeringAutoCreateRoutes(v bool, peering *compute.NetworkPeering) resource.TestCheckFunc {
func testAccCheckComputeNetworkPeeringAutoCreateRoutes(v bool, peering *computeBeta.NetworkPeering) resource.TestCheckFunc {
return func(s *terraform.State) error {

if peering.ExchangeSubnetRoutes != v {
Expand All @@ -114,26 +107,6 @@ func testAccCheckComputeNetworkPeeringAutoCreateRoutes(v bool, peering *compute.
}
}

func testAccCheckComputeNetworkPeeringImportCustomRoutes(v bool, peering *compute.NetworkPeering) resource.TestCheckFunc {
return func(s *terraform.State) error {
if peering.ImportCustomRoutes != v {
return fmt.Errorf("should ImportCustomRoutes set to %t", v)
}

return nil
}
}

func testAccCheckComputeNetworkPeeringExportCustomRoutes(v bool, peering *compute.NetworkPeering) resource.TestCheckFunc {
return func(s *terraform.State) error {
if peering.ExportCustomRoutes != v {
return fmt.Errorf("should ExportCustomRoutes set to %t", v)
}

return nil
}
}

func testAccComputeNetworkPeering_basic(primaryNetworkName, peeringName string) string {
s := `
resource "google_compute_network" "network1" {
Expand All @@ -158,10 +131,6 @@ resource "google_compute_network_peering" "bar" {
name = "peering-test-2-%s"
`

s = s +
`import_custom_routes = true
export_custom_routes = true
`
s = s + `}`
return fmt.Sprintf(s, primaryNetworkName, peeringName, acctest.RandString(10), acctest.RandString(10))
}
Loading

0 comments on commit 878c4c2

Please sign in to comment.