From bffe09815b762662f596dbbc67f276e6999ac3e3 Mon Sep 17 00:00:00 2001 From: The Magician Date: Thu, 25 Apr 2019 12:46:31 -0700 Subject: [PATCH] Autogen Global Forwarding Rule, fix Load Balancer related-resources (#593) Signed-off-by: Modular Magician --- google-beta/provider.go | 1 - google-beta/provider_compute_gen.go | 1 + ...resource_compute_global_forwarding_rule.go | 493 ++++++++++++---- ...e_global_forwarding_rule_generated_test.go | 127 +++++ ...rce_compute_global_forwarding_rule_test.go | 536 +++++------------- 5 files changed, 648 insertions(+), 510 deletions(-) create mode 100644 google-beta/resource_compute_global_forwarding_rule_generated_test.go diff --git a/google-beta/provider.go b/google-beta/provider.go index 646a067904b..a6fd64dcbe8 100644 --- a/google-beta/provider.go +++ b/google-beta/provider.go @@ -173,7 +173,6 @@ func ResourceMapWithErrors() (map[string]*schema.Resource, error) { "google_cloudiot_registry": resourceCloudIoTRegistry(), "google_composer_environment": resourceComposerEnvironment(), "google_compute_attached_disk": resourceComputeAttachedDisk(), - "google_compute_global_forwarding_rule": resourceComputeGlobalForwardingRule(), "google_compute_instance": resourceComputeInstance(), "google_compute_instance_from_template": resourceComputeInstanceFromTemplate(), "google_compute_instance_group": resourceComputeInstanceGroup(), diff --git a/google-beta/provider_compute_gen.go b/google-beta/provider_compute_gen.go index 08fe04f65d7..bbe50a22e8b 100644 --- a/google-beta/provider_compute_gen.go +++ b/google-beta/provider_compute_gen.go @@ -27,6 +27,7 @@ var GeneratedComputeResourcesMap = map[string]*schema.Resource{ "google_compute_firewall": resourceComputeFirewall(), "google_compute_forwarding_rule": resourceComputeForwardingRule(), "google_compute_global_address": resourceComputeGlobalAddress(), + "google_compute_global_forwarding_rule": resourceComputeGlobalForwardingRule(), "google_compute_http_health_check": resourceComputeHttpHealthCheck(), "google_compute_https_health_check": resourceComputeHttpsHealthCheck(), "google_compute_health_check": resourceComputeHealthCheck(), diff --git a/google-beta/resource_compute_global_forwarding_rule.go b/google-beta/resource_compute_global_forwarding_rule.go index 06ab2af18f9..0a2b9158759 100644 --- a/google-beta/resource_compute_global_forwarding_rule.go +++ b/google-beta/resource_compute_global_forwarding_rule.go @@ -1,14 +1,28 @@ +// ---------------------------------------------------------------------------- +// +// *** 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" "log" + "reflect" + "time" "github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/validation" - - computeBeta "google.golang.org/api/compute/v0.beta" - compute "google.golang.org/api/compute/v1" + "google.golang.org/api/compute/v1" ) func resourceComputeGlobalForwardingRule() *schema.Resource { @@ -19,7 +33,13 @@ func resourceComputeGlobalForwardingRule() *schema.Resource { Delete: resourceComputeGlobalForwardingRuleDelete, Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, + State: resourceComputeGlobalForwardingRuleImport, + }, + + Timeouts: &schema.ResourceTimeout{ + Create: schema.DefaultTimeout(240 * time.Second), + Update: schema.DefaultTimeout(240 * time.Second), + Delete: schema.DefaultTimeout(240 * time.Second), }, Schema: map[string]*schema.Schema{ @@ -28,72 +48,57 @@ func resourceComputeGlobalForwardingRule() *schema.Resource { Required: true, ForceNew: true, }, - "target": { Type: schema.TypeString, Required: true, DiffSuppressFunc: compareSelfLinkRelativePaths, }, - - "description": { - Type: schema.TypeString, - Optional: true, - ForceNew: true, - }, - "ip_address": { Type: schema.TypeString, + Computed: true, Optional: true, ForceNew: true, - Computed: true, }, - "ip_protocol": { + Type: schema.TypeString, + Computed: true, + Optional: true, + ForceNew: true, + ValidateFunc: validation.StringInSlice([]string{"TCP", "UDP", "ESP", "AH", "SCTP", "ICMP", ""}, false), + DiffSuppressFunc: caseDiffSuppress, + }, + "description": { Type: schema.TypeString, Optional: true, ForceNew: true, - Computed: true, }, - + "ip_version": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + ValidateFunc: validation.StringInSlice([]string{"IPV4", "IPV6", ""}, false), + }, "labels": { Type: schema.TypeMap, Optional: true, Elem: &schema.Schema{Type: schema.TypeString}, }, - - "label_fingerprint": { - Type: schema.TypeString, - Computed: true, - }, - "port_range": { Type: schema.TypeString, Optional: true, ForceNew: true, DiffSuppressFunc: portRangeDiffSuppress, }, - - "ip_version": { - Type: schema.TypeString, - Optional: true, - ForceNew: true, - ValidateFunc: validation.StringInSlice([]string{"IPV4", "IPV6"}, false), - }, - - "project": { + "label_fingerprint": { Type: schema.TypeString, - Optional: true, Computed: true, - ForceNew: true, }, - - "region": { + "project": { Type: schema.TypeString, Optional: true, + Computed: true, ForceNew: true, - Removed: "Please remove this attribute (it was never used)", }, - "self_link": { Type: schema.TypeString, Computed: true, @@ -105,89 +110,288 @@ func resourceComputeGlobalForwardingRule() *schema.Resource { func resourceComputeGlobalForwardingRuleCreate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) - project, err := getProject(d, config) + obj := make(map[string]interface{}) + descriptionProp, err := expandComputeGlobalForwardingRuleDescription(d.Get("description"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("description"); !isEmptyValue(reflect.ValueOf(descriptionProp)) && (ok || !reflect.DeepEqual(v, descriptionProp)) { + obj["description"] = descriptionProp + } + IPAddressProp, err := expandComputeGlobalForwardingRuleIPAddress(d.Get("ip_address"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("ip_address"); !isEmptyValue(reflect.ValueOf(IPAddressProp)) && (ok || !reflect.DeepEqual(v, IPAddressProp)) { + obj["IPAddress"] = IPAddressProp + } + IPProtocolProp, err := expandComputeGlobalForwardingRuleIPProtocol(d.Get("ip_protocol"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("ip_protocol"); !isEmptyValue(reflect.ValueOf(IPProtocolProp)) && (ok || !reflect.DeepEqual(v, IPProtocolProp)) { + obj["IPProtocol"] = IPProtocolProp + } + ipVersionProp, err := expandComputeGlobalForwardingRuleIpVersion(d.Get("ip_version"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("ip_version"); !isEmptyValue(reflect.ValueOf(ipVersionProp)) && (ok || !reflect.DeepEqual(v, ipVersionProp)) { + obj["ipVersion"] = ipVersionProp + } + labelsProp, err := expandComputeGlobalForwardingRuleLabels(d.Get("labels"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("labels"); !isEmptyValue(reflect.ValueOf(labelsProp)) && (ok || !reflect.DeepEqual(v, labelsProp)) { + obj["labels"] = labelsProp + } + labelFingerprintProp, err := expandComputeGlobalForwardingRuleLabelFingerprint(d.Get("label_fingerprint"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("label_fingerprint"); !isEmptyValue(reflect.ValueOf(labelFingerprintProp)) && (ok || !reflect.DeepEqual(v, labelFingerprintProp)) { + obj["labelFingerprint"] = labelFingerprintProp + } + nameProp, err := expandComputeGlobalForwardingRuleName(d.Get("name"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("name"); !isEmptyValue(reflect.ValueOf(nameProp)) && (ok || !reflect.DeepEqual(v, nameProp)) { + obj["name"] = nameProp + } + portRangeProp, err := expandComputeGlobalForwardingRulePortRange(d.Get("port_range"), d, config) if err != nil { return err + } else if v, ok := d.GetOkExists("port_range"); !isEmptyValue(reflect.ValueOf(portRangeProp)) && (ok || !reflect.DeepEqual(v, portRangeProp)) { + obj["portRange"] = portRangeProp + } + targetProp, err := expandComputeGlobalForwardingRuleTarget(d.Get("target"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("target"); !isEmptyValue(reflect.ValueOf(targetProp)) && (ok || !reflect.DeepEqual(v, targetProp)) { + obj["target"] = targetProp } - frule := &computeBeta.ForwardingRule{ - IPAddress: d.Get("ip_address").(string), - IPProtocol: d.Get("ip_protocol").(string), - IpVersion: d.Get("ip_version").(string), - Description: d.Get("description").(string), - Name: d.Get("name").(string), - PortRange: d.Get("port_range").(string), - Target: d.Get("target").(string), + url, err := replaceVars(d, config, "https://www.googleapis.com/compute/beta/projects/{{project}}/global/forwardingRules") + if err != nil { + return err } - op, err := config.clientComputeBeta.GlobalForwardingRules.Insert(project, frule).Do() + log.Printf("[DEBUG] Creating new GlobalForwardingRule: %#v", obj) + res, err := sendRequestWithTimeout(config, "POST", url, obj, d.Timeout(schema.TimeoutCreate)) if err != nil { - return fmt.Errorf("Error creating Global Forwarding Rule: %s", err) + return fmt.Errorf("Error creating GlobalForwardingRule: %s", err) } - // It probably maybe worked, so store the ID now - d.SetId(frule.Name) + // Store the ID now + id, err := replaceVars(d, config, "{{name}}") + if err != nil { + return fmt.Errorf("Error constructing id: %s", err) + } + d.SetId(id) - err = computeSharedOperationWait(config.clientCompute, op, project, "Creating Global Fowarding Rule") + project, err := getProject(d, config) + if err != nil { + return err + } + op := &compute.Operation{} + err = Convert(res, op) if err != nil { return err } - // If we have labels to set, try to set those too - if _, ok := d.GetOk("labels"); ok { - labels := expandLabels(d) - // Do a read to get the fingerprint value so we can update - fingerprint, err := resourceComputeGlobalForwardingRuleReadLabelFingerprint(config, project, frule.Name) + waitErr := computeOperationWaitTime( + config.clientCompute, op, project, "Creating GlobalForwardingRule", + int(d.Timeout(schema.TimeoutCreate).Minutes())) + + if waitErr != nil { + // The resource didn't actually create + d.SetId("") + return fmt.Errorf("Error waiting to create GlobalForwardingRule: %s", waitErr) + } + + log.Printf("[DEBUG] Finished creating GlobalForwardingRule %q: %#v", d.Id(), res) + + if v, ok := d.GetOkExists("labels"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, labelsProp)) { + // Labels cannot be set in a create. We'll have to set them here. + err = resourceComputeGlobalForwardingRuleRead(d, meta) + if err != nil { + return err + } + + obj := make(map[string]interface{}) + // d.Get("labels") will have been overridden by the Read call. + labelsProp, err := expandComputeGlobalForwardingRuleLabels(v, d, config) + if err != nil { + return err + } + obj["labels"] = labelsProp + labelFingerprintProp := d.Get("label_fingerprint") + obj["labelFingerprint"] = labelFingerprintProp + + url, err = replaceVars(d, config, "https://www.googleapis.com/compute/beta/projects/{{project}}/global/forwardingRules/{{name}}/setLabels") + if err != nil { + return err + } + res, err = sendRequest(config, "POST", url, obj) + if err != nil { + return fmt.Errorf("Error adding labels to ComputeGlobalForwardingRule %q: %s", d.Id(), err) + } + + err = Convert(res, op) if err != nil { return err } - err = resourceComputeGlobalForwardingRuleSetLabels(config, project, frule.Name, labels, fingerprint) + err = computeOperationWaitTime( + config.clientCompute, op, project, "Updating ComputeGlobalForwardingRule Labels", + int(d.Timeout(schema.TimeoutUpdate).Minutes())) + if err != nil { return err } + } return resourceComputeGlobalForwardingRuleRead(d, meta) } -func resourceComputeGlobalForwardingRuleUpdate(d *schema.ResourceData, meta interface{}) error { +func resourceComputeGlobalForwardingRuleRead(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + url, err := replaceVars(d, config, "https://www.googleapis.com/compute/beta/projects/{{project}}/global/forwardingRules/{{name}}") + if err != nil { + return err + } + + res, err := sendRequest(config, "GET", url, nil) + if err != nil { + return handleNotFoundError(err, d, fmt.Sprintf("ComputeGlobalForwardingRule %q", d.Id())) + } + project, err := getProject(d, config) if err != nil { return err } + if err := d.Set("project", project); err != nil { + return fmt.Errorf("Error reading GlobalForwardingRule: %s", err) + } + + if err := d.Set("description", flattenComputeGlobalForwardingRuleDescription(res["description"], d)); err != nil { + return fmt.Errorf("Error reading GlobalForwardingRule: %s", err) + } + if err := d.Set("ip_address", flattenComputeGlobalForwardingRuleIPAddress(res["IPAddress"], d)); err != nil { + return fmt.Errorf("Error reading GlobalForwardingRule: %s", err) + } + if err := d.Set("ip_protocol", flattenComputeGlobalForwardingRuleIPProtocol(res["IPProtocol"], d)); err != nil { + return fmt.Errorf("Error reading GlobalForwardingRule: %s", err) + } + if err := d.Set("ip_version", flattenComputeGlobalForwardingRuleIpVersion(res["ipVersion"], d)); err != nil { + return fmt.Errorf("Error reading GlobalForwardingRule: %s", err) + } + if err := d.Set("labels", flattenComputeGlobalForwardingRuleLabels(res["labels"], d)); err != nil { + return fmt.Errorf("Error reading GlobalForwardingRule: %s", err) + } + if err := d.Set("label_fingerprint", flattenComputeGlobalForwardingRuleLabelFingerprint(res["labelFingerprint"], d)); err != nil { + return fmt.Errorf("Error reading GlobalForwardingRule: %s", err) + } + if err := d.Set("name", flattenComputeGlobalForwardingRuleName(res["name"], d)); err != nil { + return fmt.Errorf("Error reading GlobalForwardingRule: %s", err) + } + if err := d.Set("port_range", flattenComputeGlobalForwardingRulePortRange(res["portRange"], d)); err != nil { + return fmt.Errorf("Error reading GlobalForwardingRule: %s", err) + } + if err := d.Set("target", flattenComputeGlobalForwardingRuleTarget(res["target"], d)); err != nil { + return fmt.Errorf("Error reading GlobalForwardingRule: %s", err) + } + if err := d.Set("self_link", ConvertSelfLinkToV1(res["selfLink"].(string))); err != nil { + return fmt.Errorf("Error reading GlobalForwardingRule: %s", err) + } + + return nil +} + +func resourceComputeGlobalForwardingRuleUpdate(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) d.Partial(true) - if d.HasChange("target") { - target := d.Get("target").(string) - targetRef := &compute.TargetReference{Target: target} + if d.HasChange("labels") || d.HasChange("label_fingerprint") { + obj := make(map[string]interface{}) + labelsProp, err := expandComputeGlobalForwardingRuleLabels(d.Get("labels"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("labels"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, labelsProp)) { + obj["labels"] = labelsProp + } + labelFingerprintProp, err := expandComputeGlobalForwardingRuleLabelFingerprint(d.Get("label_fingerprint"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("label_fingerprint"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, labelFingerprintProp)) { + obj["labelFingerprint"] = labelFingerprintProp + } + + url, err := replaceVars(d, config, "https://www.googleapis.com/compute/beta/projects/{{project}}/global/forwardingRules/{{name}}/setLabels") + if err != nil { + return err + } + res, err := sendRequestWithTimeout(config, "POST", url, obj, d.Timeout(schema.TimeoutUpdate)) + if err != nil { + return fmt.Errorf("Error updating GlobalForwardingRule %q: %s", d.Id(), err) + } - op, err := config.clientCompute.GlobalForwardingRules.SetTarget( - project, d.Id(), targetRef).Do() + project, err := getProject(d, config) if err != nil { - return fmt.Errorf("Error updating target: %s", err) + return err } + op := &compute.Operation{} + err = Convert(res, op) + if err != nil { + return err + } + + err = computeOperationWaitTime( + config.clientCompute, op, project, "Updating GlobalForwardingRule", + int(d.Timeout(schema.TimeoutUpdate).Minutes())) - err = computeSharedOperationWait(config.clientCompute, op, project, "Updating Global Forwarding Rule") if err != nil { return err } - d.SetPartial("target") + d.SetPartial("labels") + d.SetPartial("label_fingerprint") } - if d.HasChange("labels") { - labels := expandLabels(d) - fingerprint := d.Get("label_fingerprint").(string) + if d.HasChange("target") { + obj := make(map[string]interface{}) + targetProp, err := expandComputeGlobalForwardingRuleTarget(d.Get("target"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("target"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, targetProp)) { + obj["target"] = targetProp + } + + url, err := replaceVars(d, config, "https://www.googleapis.com/compute/beta/projects/{{project}}/global/forwardingRules/{{name}}/setTarget") + if err != nil { + return err + } + res, err := sendRequestWithTimeout(config, "POST", url, obj, d.Timeout(schema.TimeoutUpdate)) + if err != nil { + return fmt.Errorf("Error updating GlobalForwardingRule %q: %s", d.Id(), err) + } + + project, err := getProject(d, config) + if err != nil { + return err + } + op := &compute.Operation{} + err = Convert(res, op) + if err != nil { + return err + } + + err = computeOperationWaitTime( + config.clientCompute, op, project, "Updating GlobalForwardingRule", + int(d.Timeout(schema.TimeoutUpdate).Minutes())) - err = resourceComputeGlobalForwardingRuleSetLabels(config, project, d.Get("name").(string), labels, fingerprint) if err != nil { return err } - d.SetPartial("labels") + d.SetPartial("target") } d.Partial(false) @@ -195,83 +399,134 @@ func resourceComputeGlobalForwardingRuleUpdate(d *schema.ResourceData, meta inte return resourceComputeGlobalForwardingRuleRead(d, meta) } -func resourceComputeGlobalForwardingRuleRead(d *schema.ResourceData, meta interface{}) error { +func resourceComputeGlobalForwardingRuleDelete(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) - project, err := getProject(d, config) + url, err := replaceVars(d, config, "https://www.googleapis.com/compute/beta/projects/{{project}}/global/forwardingRules/{{name}}") if err != nil { return err } - frule, err := config.clientComputeBeta.GlobalForwardingRules.Get(project, d.Id()).Do() + var obj map[string]interface{} + log.Printf("[DEBUG] Deleting GlobalForwardingRule %q", d.Id()) + res, err := sendRequestWithTimeout(config, "DELETE", url, obj, d.Timeout(schema.TimeoutDelete)) if err != nil { - return handleNotFoundError(err, d, fmt.Sprintf("Global Forwarding Rule %q", d.Get("name").(string))) + return handleNotFoundError(err, d, "GlobalForwardingRule") } - d.Set("name", frule.Name) - d.Set("description", frule.Description) - d.Set("target", frule.Target) - d.Set("port_range", frule.PortRange) - d.Set("ip_address", frule.IPAddress) - d.Set("ip_protocol", frule.IPProtocol) - d.Set("ip_version", frule.IpVersion) - d.Set("self_link", ConvertSelfLinkToV1(frule.SelfLink)) - d.Set("labels", frule.Labels) - d.Set("label_fingerprint", frule.LabelFingerprint) - d.Set("project", project) - - return nil -} - -func resourceComputeGlobalForwardingRuleDelete(d *schema.ResourceData, meta interface{}) error { - config := meta.(*Config) - project, err := getProject(d, config) if err != nil { return err } - - // Delete the GlobalForwardingRule - log.Printf("[DEBUG] GlobalForwardingRule delete request") - op, err := config.clientCompute.GlobalForwardingRules.Delete(project, d.Id()).Do() + op := &compute.Operation{} + err = Convert(res, op) if err != nil { - return fmt.Errorf("Error deleting GlobalForwardingRule: %s", err) + return err } - err = computeSharedOperationWait(config.clientCompute, op, project, "Deleting GlobalForwarding Rule") + + err = computeOperationWaitTime( + config.clientCompute, op, project, "Deleting GlobalForwardingRule", + int(d.Timeout(schema.TimeoutDelete).Minutes())) + if err != nil { return err } - d.SetId("") + log.Printf("[DEBUG] Finished deleting GlobalForwardingRule %q: %#v", d.Id(), res) return nil } -// resourceComputeGlobalForwardingRuleReadLabelFingerprint performs a read on the remote resource and returns only the -// fingerprint. Used on create when setting labels as we don't know the label fingerprint initially. -func resourceComputeGlobalForwardingRuleReadLabelFingerprint(config *Config, project, name string) (string, error) { - frule, err := config.clientComputeBeta.GlobalForwardingRules.Get(project, name).Do() +func resourceComputeGlobalForwardingRuleImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + config := meta.(*Config) + if err := parseImportId([]string{"projects/(?P[^/]+)/global/forwardingRules/(?P[^/]+)", "(?P[^/]+)/(?P[^/]+)", "(?P[^/]+)"}, d, config); err != nil { + return nil, err + } + + // Replace import id for the resource id + id, err := replaceVars(d, config, "{{name}}") if err != nil { - return "", fmt.Errorf("Unable to read global forwarding rule to update labels: %s", err) + return nil, fmt.Errorf("Error constructing id: %s", err) } + d.SetId(id) + + return []*schema.ResourceData{d}, nil +} + +func flattenComputeGlobalForwardingRuleDescription(v interface{}, d *schema.ResourceData) interface{} { + return v +} - return frule.LabelFingerprint, nil +func flattenComputeGlobalForwardingRuleIPAddress(v interface{}, d *schema.ResourceData) interface{} { + return v } -// resourceComputeGlobalForwardingRuleSetLabels sets the Labels attribute on a forwarding rule. -func resourceComputeGlobalForwardingRuleSetLabels(config *Config, project, name string, labels map[string]string, fingerprint string) error { - setLabels := computeBeta.GlobalSetLabelsRequest{ - Labels: labels, - LabelFingerprint: fingerprint, +func flattenComputeGlobalForwardingRuleIPProtocol(v interface{}, d *schema.ResourceData) interface{} { + return v +} + +func flattenComputeGlobalForwardingRuleIpVersion(v interface{}, d *schema.ResourceData) interface{} { + return v +} + +func flattenComputeGlobalForwardingRuleLabels(v interface{}, d *schema.ResourceData) interface{} { + return v +} + +func flattenComputeGlobalForwardingRuleLabelFingerprint(v interface{}, d *schema.ResourceData) interface{} { + return v +} + +func flattenComputeGlobalForwardingRuleName(v interface{}, d *schema.ResourceData) interface{} { + return v +} + +func flattenComputeGlobalForwardingRulePortRange(v interface{}, d *schema.ResourceData) interface{} { + return v +} + +func flattenComputeGlobalForwardingRuleTarget(v interface{}, d *schema.ResourceData) interface{} { + return v +} + +func expandComputeGlobalForwardingRuleDescription(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { + return v, nil +} + +func expandComputeGlobalForwardingRuleIPAddress(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { + return v, nil +} + +func expandComputeGlobalForwardingRuleIPProtocol(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { + return v, nil +} + +func expandComputeGlobalForwardingRuleIpVersion(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { + return v, nil +} + +func expandComputeGlobalForwardingRuleLabels(v interface{}, d TerraformResourceData, config *Config) (map[string]string, error) { + if v == nil { + return map[string]string{}, nil } - op, err := config.clientComputeBeta.GlobalForwardingRules.SetLabels(project, name, &setLabels).Do() - if err != nil { - return err + m := make(map[string]string) + for k, val := range v.(map[string]interface{}) { + m[k] = val.(string) } + return m, nil +} - err = computeSharedOperationWait(config.clientCompute, op, project, "Setting labels on Global Forwarding Rule") - if err != nil { - return err - } +func expandComputeGlobalForwardingRuleLabelFingerprint(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { + return v, nil +} - return nil +func expandComputeGlobalForwardingRuleName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { + return v, nil +} + +func expandComputeGlobalForwardingRulePortRange(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { + return v, nil +} + +func expandComputeGlobalForwardingRuleTarget(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { + return v, nil } diff --git a/google-beta/resource_compute_global_forwarding_rule_generated_test.go b/google-beta/resource_compute_global_forwarding_rule_generated_test.go new file mode 100644 index 00000000000..34e05b99076 --- /dev/null +++ b/google-beta/resource_compute_global_forwarding_rule_generated_test.go @@ -0,0 +1,127 @@ +// ---------------------------------------------------------------------------- +// +// *** 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 TestAccComputeGlobalForwardingRule_globalForwardingRuleHttpExample(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: testAccCheckComputeGlobalForwardingRuleDestroy, + Steps: []resource.TestStep{ + { + Config: testAccComputeGlobalForwardingRule_globalForwardingRuleHttpExample(context), + }, + { + ResourceName: "google_compute_global_forwarding_rule.default", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func testAccComputeGlobalForwardingRule_globalForwardingRuleHttpExample(context map[string]interface{}) string { + return Nprintf(` +resource "google_compute_global_forwarding_rule" "default" { + name = "global-rule-%{random_suffix}" + target = "${google_compute_target_http_proxy.default.self_link}" + port_range = "80" +} + +resource "google_compute_target_http_proxy" "default" { + name = "target-proxy-%{random_suffix}" + description = "a description" + url_map = "${google_compute_url_map.default.self_link}" +} + +resource "google_compute_url_map" "default" { + name = "url-map-target-proxy-%{random_suffix}" + description = "a description" + default_service = "${google_compute_backend_service.default.self_link}" + + host_rule { + hosts = ["mysite.com"] + path_matcher = "allpaths" + } + + path_matcher { + name = "allpaths" + default_service = "${google_compute_backend_service.default.self_link}" + + path_rule { + paths = ["/*"] + service = "${google_compute_backend_service.default.self_link}" + } + } +} + +resource "google_compute_backend_service" "default" { + name = "backend-%{random_suffix}" + port_name = "http" + protocol = "HTTP" + timeout_sec = 10 + + health_checks = ["${google_compute_http_health_check.default.self_link}"] +} + +resource "google_compute_http_health_check" "default" { + name = "check-backend-%{random_suffix}" + request_path = "/" + check_interval_sec = 1 + timeout_sec = 1 +} +`, context) +} + +func testAccCheckComputeGlobalForwardingRuleDestroy(s *terraform.State) error { + for name, rs := range s.RootModule().Resources { + if rs.Type != "google_compute_global_forwarding_rule" { + continue + } + if strings.HasPrefix(name, "data.") { + continue + } + + config := testAccProvider.Meta().(*Config) + + url, err := replaceVarsForTest(rs, "https://www.googleapis.com/compute/beta/projects/{{project}}/global/forwardingRules/{{name}}") + if err != nil { + return err + } + + _, err = sendRequest(config, "GET", url, nil) + if err == nil { + return fmt.Errorf("ComputeGlobalForwardingRule still exists at %s", url) + } + } + + return nil +} diff --git a/google-beta/resource_compute_global_forwarding_rule_test.go b/google-beta/resource_compute_global_forwarding_rule_test.go index 8ea224e8708..abd55c9bb4b 100644 --- a/google-beta/resource_compute_global_forwarding_rule_test.go +++ b/google-beta/resource_compute_global_forwarding_rule_test.go @@ -2,21 +2,19 @@ package google import ( "fmt" + "regexp" "testing" "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" - "github.com/hashicorp/terraform/terraform" - - computeBeta "google.golang.org/api/compute/v0.beta" ) -func TestAccComputeGlobalForwardingRule_basic(t *testing.T) { +func TestAccComputeGlobalForwardingRule_updateTarget(t *testing.T) { t.Parallel() fr := fmt.Sprintf("forwardrule-test-%s", acctest.RandString(10)) - proxy1 := fmt.Sprintf("forwardrule-test-%s", acctest.RandString(10)) - proxy2 := fmt.Sprintf("forwardrule-test-%s", acctest.RandString(10)) + proxy := fmt.Sprintf("forwardrule-test-%s", acctest.RandString(10)) + proxyUpdated := fmt.Sprintf("forwardrule-test-%s", acctest.RandString(10)) backend := fmt.Sprintf("forwardrule-test-%s", acctest.RandString(10)) hc := fmt.Sprintf("forwardrule-test-%s", acctest.RandString(10)) urlmap := fmt.Sprintf("forwardrule-test-%s", acctest.RandString(10)) @@ -27,53 +25,28 @@ func TestAccComputeGlobalForwardingRule_basic(t *testing.T) { CheckDestroy: testAccCheckComputeGlobalForwardingRuleDestroy, Steps: []resource.TestStep{ { - Config: testAccComputeGlobalForwardingRule_basic1(fr, proxy1, proxy2, backend, hc, urlmap), + Config: testAccComputeGlobalForwardingRule_httpProxy(fr, "proxy", proxy, proxyUpdated, backend, hc, urlmap), Check: resource.ComposeTestCheckFunc( - testAccCheckComputeGlobalForwardingRuleExists( - "google_compute_global_forwarding_rule.foobar"), - - // Unlike GlobalAddress, IpVersion is "" instead of "IPV4" when this is made with a v1 API. - testAccCheckComputeBetaGlobalForwardingRuleIpVersion("google_compute_global_forwarding_rule.foobar", ""), + resource.TestMatchResourceAttr( + "google_compute_global_forwarding_rule.forwarding_rule", "target", regexp.MustCompile(proxy+"$")), ), }, { - ResourceName: "google_compute_global_forwarding_rule.foobar", + ResourceName: "google_compute_global_forwarding_rule.forwarding_rule", ImportState: true, ImportStateVerify: true, }, - }, - }) -} - -func TestAccComputeGlobalForwardingRule_update(t *testing.T) { - t.Parallel() - - fr := fmt.Sprintf("forwardrule-test-%s", acctest.RandString(10)) - proxy1 := fmt.Sprintf("forwardrule-test-%s", acctest.RandString(10)) - proxy2 := fmt.Sprintf("forwardrule-test-%s", acctest.RandString(10)) - backend := fmt.Sprintf("forwardrule-test-%s", acctest.RandString(10)) - hc := fmt.Sprintf("forwardrule-test-%s", acctest.RandString(10)) - urlmap := fmt.Sprintf("forwardrule-test-%s", acctest.RandString(10)) - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckComputeGlobalForwardingRuleDestroy, - Steps: []resource.TestStep{ { - Config: testAccComputeGlobalForwardingRule_basic1(fr, proxy1, proxy2, backend, hc, urlmap), + Config: testAccComputeGlobalForwardingRule_httpProxy(fr, "proxy2", proxy, proxyUpdated, backend, hc, urlmap), Check: resource.ComposeTestCheckFunc( - testAccCheckComputeGlobalForwardingRuleExists( - "google_compute_global_forwarding_rule.foobar"), + resource.TestMatchResourceAttr( + "google_compute_global_forwarding_rule.forwarding_rule", "target", regexp.MustCompile(proxyUpdated+"$")), ), }, - { - Config: testAccComputeGlobalForwardingRule_basic2(fr, proxy1, proxy2, backend, hc, urlmap), - Check: resource.ComposeTestCheckFunc( - testAccCheckComputeGlobalForwardingRuleExists( - "google_compute_global_forwarding_rule.foobar"), - ), + ResourceName: "google_compute_global_forwarding_rule.forwarding_rule", + ImportState: true, + ImportStateVerify: true, }, }, }) @@ -82,11 +55,8 @@ func TestAccComputeGlobalForwardingRule_update(t *testing.T) { func TestAccComputeGlobalForwardingRule_ipv6(t *testing.T) { t.Parallel() - var frule computeBeta.ForwardingRule - fr := fmt.Sprintf("forwardrule-test-%s", acctest.RandString(10)) - proxy1 := fmt.Sprintf("forwardrule-test-%s", acctest.RandString(10)) - proxy2 := fmt.Sprintf("forwardrule-test-%s", acctest.RandString(10)) + proxy := fmt.Sprintf("forwardrule-test-%s", acctest.RandString(10)) backend := fmt.Sprintf("forwardrule-test-%s", acctest.RandString(10)) hc := fmt.Sprintf("forwardrule-test-%s", acctest.RandString(10)) urlmap := fmt.Sprintf("forwardrule-test-%s", acctest.RandString(10)) @@ -97,15 +67,14 @@ func TestAccComputeGlobalForwardingRule_ipv6(t *testing.T) { CheckDestroy: testAccCheckComputeGlobalForwardingRuleDestroy, Steps: []resource.TestStep{ { - Config: testAccComputeGlobalForwardingRule_ipv6(fr, proxy1, proxy2, backend, hc, urlmap), + Config: testAccComputeGlobalForwardingRule_ipv6(fr, proxy, backend, hc, urlmap), Check: resource.ComposeTestCheckFunc( - testAccCheckComputeBetaGlobalForwardingRuleExists( - "google_compute_global_forwarding_rule.foobar", &frule), - testAccCheckComputeBetaGlobalForwardingRuleIpVersion("google_compute_global_forwarding_rule.foobar", "IPV6"), + resource.TestCheckResourceAttr( + "google_compute_global_forwarding_rule.forwarding_rule", "ip_version", "IPV6"), ), }, { - ResourceName: "google_compute_global_forwarding_rule.foobar", + ResourceName: "google_compute_global_forwarding_rule.forwarding_rule", ImportState: true, ImportStateVerify: true, }, @@ -116,11 +85,8 @@ func TestAccComputeGlobalForwardingRule_ipv6(t *testing.T) { func TestAccComputeGlobalForwardingRule_labels(t *testing.T) { t.Parallel() - var frule computeBeta.ForwardingRule - fr := fmt.Sprintf("forwardrule-test-%s", acctest.RandString(10)) - proxy1 := fmt.Sprintf("forwardrule-test-%s", acctest.RandString(10)) - proxy2 := fmt.Sprintf("forwardrule-test-%s", acctest.RandString(10)) + proxy := fmt.Sprintf("forwardrule-test-%s", acctest.RandString(10)) backend := fmt.Sprintf("forwardrule-test-%s", acctest.RandString(10)) hc := fmt.Sprintf("forwardrule-test-%s", acctest.RandString(10)) urlmap := fmt.Sprintf("forwardrule-test-%s", acctest.RandString(10)) @@ -131,179 +97,48 @@ func TestAccComputeGlobalForwardingRule_labels(t *testing.T) { CheckDestroy: testAccCheckComputeGlobalForwardingRuleDestroy, Steps: []resource.TestStep{ { - Config: testAccComputeGlobalForwardingRule_labels(fr, proxy1, proxy2, backend, hc, urlmap), - Check: resource.ComposeTestCheckFunc( - testAccCheckComputeBetaGlobalForwardingRuleExists( - "google_compute_global_forwarding_rule.foobar", &frule), - testAccCheckComputeBetaGlobalForwardingRuleHasLabel(&frule, "my-label", "my-label-value"), - testAccCheckComputeBetaGlobalForwardingRuleHasLabel(&frule, "my-second-label", "my-second-label-value"), - testAccCheckComputeBetaGlobalForwardingRuleHasCorrectLabelFingerprint(&frule, "google_compute_global_forwarding_rule.foobar"), - ), + Config: testAccComputeGlobalForwardingRule_labels(fr, proxy, backend, hc, urlmap), }, { - Config: testAccComputeGlobalForwardingRule_labelsUpdated(fr, proxy1, proxy2, backend, hc, urlmap), - Check: resource.ComposeTestCheckFunc( - testAccCheckComputeBetaGlobalForwardingRuleExists( - "google_compute_global_forwarding_rule.foobar", &frule), - testAccCheckComputeBetaGlobalForwardingRuleHasLabel(&frule, "my-label", "my-label-value"), - testAccCheckComputeBetaGlobalForwardingRuleHasLabel(&frule, "my-third-label", "my-third-label-value"), - testAccCheckComputeBetaGlobalForwardingRuleHasCorrectLabelFingerprint(&frule, "google_compute_global_forwarding_rule.foobar"), - ), + ResourceName: "google_compute_global_forwarding_rule.forwarding_rule", + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccComputeGlobalForwardingRule_labelsUpdated(fr, proxy, backend, hc, urlmap), + }, + { + ResourceName: "google_compute_global_forwarding_rule.forwarding_rule", + ImportState: true, + ImportStateVerify: true, }, }, }) } -func testAccCheckComputeGlobalForwardingRuleDestroy(s *terraform.State) error { - config := testAccProvider.Meta().(*Config) - - for _, rs := range s.RootModule().Resources { - if rs.Type != "google_compute_global_forwarding_rule" { - continue - } - - _, err := config.clientCompute.GlobalForwardingRules.Get( - config.Project, rs.Primary.ID).Do() - if err == nil { - return fmt.Errorf("Global Forwarding Rule still exists") - } - } - - return nil -} - -func testAccCheckComputeGlobalForwardingRuleExists(n string) resource.TestCheckFunc { - return func(s *terraform.State) error { - rs, ok := s.RootModule().Resources[n] - if !ok { - return fmt.Errorf("Not found: %s", n) - } - - if rs.Primary.ID == "" { - return fmt.Errorf("No ID is set") - } - - config := testAccProvider.Meta().(*Config) - - found, err := config.clientCompute.GlobalForwardingRules.Get( - config.Project, rs.Primary.ID).Do() - if err != nil { - return err - } - - if found.Name != rs.Primary.ID { - return fmt.Errorf("Global Forwarding Rule not found") - } - - return nil - } -} - -func testAccCheckComputeBetaGlobalForwardingRuleExists(n string, frule *computeBeta.ForwardingRule) resource.TestCheckFunc { - return func(s *terraform.State) error { - rs, ok := s.RootModule().Resources[n] - if !ok { - return fmt.Errorf("Not found: %s", n) - } - - if rs.Primary.ID == "" { - return fmt.Errorf("No ID is set") - } - - config := testAccProvider.Meta().(*Config) - - found, err := config.clientComputeBeta.GlobalForwardingRules.Get( - config.Project, rs.Primary.ID).Do() - if err != nil { - return err - } - - if found.Name != rs.Primary.ID { - return fmt.Errorf("Global Forwarding Rule not found") - } - - *frule = *found - - return nil - } -} - -func testAccCheckComputeBetaGlobalForwardingRuleIpVersion(n, version string) resource.TestCheckFunc { - return func(s *terraform.State) error { - rs, ok := s.RootModule().Resources[n] - if !ok { - return fmt.Errorf("Not found: %s", n) - } - - if rs.Primary.ID == "" { - return fmt.Errorf("No ID is set") - } - - config := testAccProvider.Meta().(*Config) - - frule, err := config.clientComputeBeta.GlobalForwardingRules.Get(config.Project, rs.Primary.ID).Do() - if err != nil { - return err - } - - if frule.IpVersion != version { - return fmt.Errorf("Expected IP version to be %s, got %s", version, frule.IpVersion) - } - - return nil - } -} -func testAccCheckComputeBetaGlobalForwardingRuleHasLabel(frule *computeBeta.ForwardingRule, key, value string) resource.TestCheckFunc { - return func(s *terraform.State) error { - val, ok := frule.Labels[key] - if !ok { - return fmt.Errorf("label with key %s not found", key) - } - - if val != value { - return fmt.Errorf("label value did not match for key %s: expected %s but found %s", key, value, val) - } - return nil - } -} - -func testAccCheckComputeBetaGlobalForwardingRuleHasCorrectLabelFingerprint( - frule *computeBeta.ForwardingRule, resourceName string) resource.TestCheckFunc { - return func(s *terraform.State) error { - tfLabelFingerprint := s.RootModule().Resources[resourceName].Primary.Attributes["label_fingerprint"] - remoteLabelFingerprint := frule.LabelFingerprint - - if tfLabelFingerprint != remoteLabelFingerprint { - return fmt.Errorf("Label fingerprint mismatch: remote has %#v but terraform has %#v", - remoteLabelFingerprint, tfLabelFingerprint) - } - return nil - } -} - -func testAccComputeGlobalForwardingRule_basic1(fr, proxy1, proxy2, backend, hc, urlmap string) string { +func testAccComputeGlobalForwardingRule_httpProxy(fr, targetProxy, proxy, proxy2, backend, hc, urlmap string) string { return fmt.Sprintf(` - resource "google_compute_global_forwarding_rule" "foobar" { + resource "google_compute_global_forwarding_rule" "forwarding_rule" { description = "Resource created for Terraform acceptance testing" ip_protocol = "TCP" name = "%s" port_range = "80" - target = "${google_compute_target_http_proxy.foobar1.self_link}" + target = "${google_compute_target_http_proxy.%s.self_link}" } - resource "google_compute_target_http_proxy" "foobar1" { + resource "google_compute_target_http_proxy" "proxy" { description = "Resource created for Terraform acceptance testing" name = "%s" - url_map = "${google_compute_url_map.foobar.self_link}" + url_map = "${google_compute_url_map.url_map.self_link}" } - resource "google_compute_target_http_proxy" "foobar2" { + resource "google_compute_target_http_proxy" "proxy2" { description = "Resource created for Terraform acceptance testing" name = "%s" - url_map = "${google_compute_url_map.foobar.self_link}" + url_map = "${google_compute_url_map.url_map.self_link}" } - resource "google_compute_backend_service" "foobar" { + resource "google_compute_backend_service" "backend" { name = "%s" health_checks = ["${google_compute_http_health_check.zero.self_link}"] } @@ -315,234 +150,155 @@ func testAccComputeGlobalForwardingRule_basic1(fr, proxy1, proxy2, backend, hc, timeout_sec = 1 } - resource "google_compute_url_map" "foobar" { + resource "google_compute_url_map" "url_map" { name = "%s" - default_service = "${google_compute_backend_service.foobar.self_link}" + default_service = "${google_compute_backend_service.backend.self_link}" host_rule { hosts = ["mysite.com", "myothersite.com"] path_matcher = "boop" } path_matcher { - default_service = "${google_compute_backend_service.foobar.self_link}" + default_service = "${google_compute_backend_service.backend.self_link}" name = "boop" path_rule { paths = ["/*"] - service = "${google_compute_backend_service.foobar.self_link}" + service = "${google_compute_backend_service.backend.self_link}" } } test { host = "mysite.com" path = "/*" - service = "${google_compute_backend_service.foobar.self_link}" + service = "${google_compute_backend_service.backend.self_link}" } - }`, fr, proxy1, proxy2, backend, hc, urlmap) + }`, fr, targetProxy, proxy, proxy2, backend, hc, urlmap) } -func testAccComputeGlobalForwardingRule_labels(fr, proxy1, proxy2, backend, hc, urlmap string) string { +func testAccComputeGlobalForwardingRule_labels(fr, proxy, backend, hc, urlmap string) string { return fmt.Sprintf(` - resource "google_compute_global_forwarding_rule" "foobar" { - description = "Resource created for Terraform acceptance testing" - ip_protocol = "TCP" - name = "%s" - port_range = "80" - target = "${google_compute_target_http_proxy.foobar1.self_link}" - - labels = { - my-label = "my-label-value" - my-second-label = "my-second-label-value" - } - } - - resource "google_compute_target_http_proxy" "foobar1" { - description = "Resource created for Terraform acceptance testing" - name = "%s" - url_map = "${google_compute_url_map.foobar.self_link}" - } - - resource "google_compute_target_http_proxy" "foobar2" { - description = "Resource created for Terraform acceptance testing" - name = "%s" - url_map = "${google_compute_url_map.foobar.self_link}" - } - - resource "google_compute_backend_service" "foobar" { - name = "%s" - health_checks = ["${google_compute_http_health_check.zero.self_link}"] - } - - resource "google_compute_http_health_check" "zero" { - name = "%s" - request_path = "/" - check_interval_sec = 1 - timeout_sec = 1 - } - - resource "google_compute_url_map" "foobar" { - name = "%s" - default_service = "${google_compute_backend_service.foobar.self_link}" - host_rule { - hosts = ["mysite.com", "myothersite.com"] - path_matcher = "boop" - } - path_matcher { - default_service = "${google_compute_backend_service.foobar.self_link}" - name = "boop" - path_rule { - paths = ["/*"] - service = "${google_compute_backend_service.foobar.self_link}" - } - } - test { - host = "mysite.com" - path = "/*" - service = "${google_compute_backend_service.foobar.self_link}" - } - }`, fr, proxy1, proxy2, backend, hc, urlmap) +resource "google_compute_global_forwarding_rule" "forwarding_rule" { + name = "%s" + target = "${google_compute_target_http_proxy.proxy.self_link}" + port_range = "80" + + labels = { + my-label = "a-value" + a-different-label = "my-second-label-value" + } } -func testAccComputeGlobalForwardingRule_labelsUpdated(fr, proxy1, proxy2, backend, hc, urlmap string) string { - return fmt.Sprintf(` - resource "google_compute_global_forwarding_rule" "foobar" { - description = "Resource created for Terraform acceptance testing" - ip_protocol = "TCP" - name = "%s" - port_range = "80" - target = "${google_compute_target_http_proxy.foobar1.self_link}" +resource "google_compute_target_http_proxy" "proxy" { + description = "Resource created for Terraform acceptance testing" + name = "%s" + url_map = "${google_compute_url_map.urlmap.self_link}" +} - labels = { - my-label = "my-label-value" - my-third-label = "my-third-label-value" - } - } +resource "google_compute_backend_service" "backend" { + name = "%s" + health_checks = ["${google_compute_http_health_check.zero.self_link}"] +} - resource "google_compute_target_http_proxy" "foobar1" { - description = "Resource created for Terraform acceptance testing" - name = "%s" - url_map = "${google_compute_url_map.foobar.self_link}" - } +resource "google_compute_http_health_check" "zero" { + name = "%s" + request_path = "/" + check_interval_sec = 1 + timeout_sec = 1 +} - resource "google_compute_target_http_proxy" "foobar2" { - description = "Resource created for Terraform acceptance testing" - name = "%s" - url_map = "${google_compute_url_map.foobar.self_link}" +resource "google_compute_url_map" "urlmap" { + name = "%s" + default_service = "${google_compute_backend_service.backend.self_link}" + host_rule { + hosts = ["mysite.com", "myothersite.com"] + path_matcher = "boop" } - - resource "google_compute_backend_service" "foobar" { - name = "%s" - health_checks = ["${google_compute_http_health_check.zero.self_link}"] + path_matcher { + default_service = "${google_compute_backend_service.backend.self_link}" + name = "boop" + path_rule { + paths = ["/*"] + service = "${google_compute_backend_service.backend.self_link}" + } } - - resource "google_compute_http_health_check" "zero" { - name = "%s" - request_path = "/" - check_interval_sec = 1 - timeout_sec = 1 + test { + host = "mysite.com" + path = "/*" + service = "${google_compute_backend_service.backend.self_link}" } - - resource "google_compute_url_map" "foobar" { - name = "%s" - default_service = "${google_compute_backend_service.foobar.self_link}" - host_rule { - hosts = ["mysite.com", "myothersite.com"] - path_matcher = "boop" - } - path_matcher { - default_service = "${google_compute_backend_service.foobar.self_link}" - name = "boop" - path_rule { - paths = ["/*"] - service = "${google_compute_backend_service.foobar.self_link}" - } - } - test { - host = "mysite.com" - path = "/*" - service = "${google_compute_backend_service.foobar.self_link}" - } - }`, fr, proxy1, proxy2, backend, hc, urlmap) +}`, fr, proxy, backend, hc, urlmap) } -func testAccComputeGlobalForwardingRule_basic2(fr, proxy1, proxy2, backend, hc, urlmap string) string { +func testAccComputeGlobalForwardingRule_labelsUpdated(fr, proxy, backend, hc, urlmap string) string { return fmt.Sprintf(` - resource "google_compute_global_forwarding_rule" "foobar" { - description = "Resource created for Terraform acceptance testing" - ip_protocol = "TCP" - name = "%s" - port_range = "80" - target = "${google_compute_target_http_proxy.foobar2.self_link}" - } +resource "google_compute_global_forwarding_rule" "forwarding_rule" { + name = "%s" + target = "${google_compute_target_http_proxy.proxy.self_link}" + port_range = "80" + + labels = { + my-label = "a-new-value" + a-different-label = "my-third-label-value" + } +} - resource "google_compute_target_http_proxy" "foobar1" { - description = "Resource created for Terraform acceptance testing" - name = "%s" - url_map = "${google_compute_url_map.foobar.self_link}" - } +resource "google_compute_target_http_proxy" "proxy" { + description = "Resource created for Terraform acceptance testing" + name = "%s" + url_map = "${google_compute_url_map.urlmap.self_link}" +} - resource "google_compute_target_http_proxy" "foobar2" { - description = "Resource created for Terraform acceptance testing" - name = "%s" - url_map = "${google_compute_url_map.foobar.self_link}" - } +resource "google_compute_backend_service" "backend" { + name = "%s" + health_checks = ["${google_compute_http_health_check.zero.self_link}"] +} - resource "google_compute_backend_service" "foobar" { - name = "%s" - health_checks = ["${google_compute_http_health_check.zero.self_link}"] - } +resource "google_compute_http_health_check" "zero" { + name = "%s" + request_path = "/" + check_interval_sec = 1 + timeout_sec = 1 +} - resource "google_compute_http_health_check" "zero" { - name = "%s" - request_path = "/" - check_interval_sec = 1 - timeout_sec = 1 +resource "google_compute_url_map" "urlmap" { + name = "%s" + default_service = "${google_compute_backend_service.backend.self_link}" + host_rule { + hosts = ["mysite.com", "myothersite.com"] + path_matcher = "boop" } - - resource "google_compute_url_map" "foobar" { - name = "%s" - default_service = "${google_compute_backend_service.foobar.self_link}" - host_rule { - hosts = ["mysite.com", "myothersite.com"] - path_matcher = "boop" + path_matcher { + default_service = "${google_compute_backend_service.backend.self_link}" + name = "boop" + path_rule { + paths = ["/*"] + service = "${google_compute_backend_service.backend.self_link}" } - path_matcher { - default_service = "${google_compute_backend_service.foobar.self_link}" - name = "boop" - path_rule { - paths = ["/*"] - service = "${google_compute_backend_service.foobar.self_link}" - } - } - test { - host = "mysite.com" - path = "/*" - service = "${google_compute_backend_service.foobar.self_link}" - } - }`, fr, proxy1, proxy2, backend, hc, urlmap) + } + test { + host = "mysite.com" + path = "/*" + service = "${google_compute_backend_service.backend.self_link}" + } +}`, fr, proxy, backend, hc, urlmap) } -func testAccComputeGlobalForwardingRule_ipv6(fr, proxy1, proxy2, backend, hc, urlmap string) string { +func testAccComputeGlobalForwardingRule_ipv6(fr, proxy, backend, hc, urlmap string) string { return fmt.Sprintf(` - resource "google_compute_global_forwarding_rule" "foobar" { + resource "google_compute_global_forwarding_rule" "forwarding_rule" { description = "Resource created for Terraform acceptance testing" ip_protocol = "TCP" name = "%s" port_range = "80" - target = "${google_compute_target_http_proxy.foobar1.self_link}" + target = "${google_compute_target_http_proxy.proxy.self_link}" ip_version = "IPV6" } - resource "google_compute_target_http_proxy" "foobar1" { - description = "Resource created for Terraform acceptance testing" - name = "%s" - url_map = "${google_compute_url_map.foobar.self_link}" - } - - resource "google_compute_target_http_proxy" "foobar2" { + resource "google_compute_target_http_proxy" "proxy" { description = "Resource created for Terraform acceptance testing" name = "%s" - url_map = "${google_compute_url_map.foobar.self_link}" + url_map = "${google_compute_url_map.urlmap.self_link}" } - resource "google_compute_backend_service" "foobar" { + resource "google_compute_backend_service" "backend" { name = "%s" health_checks = ["${google_compute_http_health_check.zero.self_link}"] } @@ -554,25 +310,25 @@ func testAccComputeGlobalForwardingRule_ipv6(fr, proxy1, proxy2, backend, hc, ur timeout_sec = 1 } - resource "google_compute_url_map" "foobar" { + resource "google_compute_url_map" "urlmap" { name = "%s" - default_service = "${google_compute_backend_service.foobar.self_link}" + default_service = "${google_compute_backend_service.backend.self_link}" host_rule { hosts = ["mysite.com", "myothersite.com"] path_matcher = "boop" } path_matcher { - default_service = "${google_compute_backend_service.foobar.self_link}" + default_service = "${google_compute_backend_service.backend.self_link}" name = "boop" path_rule { paths = ["/*"] - service = "${google_compute_backend_service.foobar.self_link}" + service = "${google_compute_backend_service.backend.self_link}" } } test { host = "mysite.com" path = "/*" - service = "${google_compute_backend_service.foobar.self_link}" + service = "${google_compute_backend_service.backend.self_link}" } - }`, fr, proxy1, proxy2, backend, hc, urlmap) + }`, fr, proxy, backend, hc, urlmap) }