From 652b227542295245b6b53bfd7e1f1c16ae6c7429 Mon Sep 17 00:00:00 2001 From: Ty Larrabee Date: Mon, 13 Jan 2020 19:00:25 +0000 Subject: [PATCH] Allow target to accept other resource types Signed-off-by: Modular Magician --- .../resource_compute_forwarding_rule.go | 10 +- ..._compute_forwarding_rule_generated_test.go | 212 ++++++++++++++++++ ...resource_compute_region_backend_service.go | 7 +- ...e_region_backend_service_generated_test.go | 108 +++++++++ .../r/compute_forwarding_rule.html.markdown | 203 ++++++++++++++++- ...mpute_region_backend_service.html.markdown | 96 ++++++++ 6 files changed, 624 insertions(+), 12 deletions(-) diff --git a/google-beta/resource_compute_forwarding_rule.go b/google-beta/resource_compute_forwarding_rule.go index 69059e339a..23bbb14cab 100644 --- a/google-beta/resource_compute_forwarding_rule.go +++ b/google-beta/resource_compute_forwarding_rule.go @@ -259,9 +259,8 @@ subnetwork must be specified.`, Type: schema.TypeString, Optional: true, DiffSuppressFunc: compareSelfLinkRelativePaths, - Description: `This field is only used for EXTERNAL load balancing. -A reference to a TargetPool resource to receive the matched traffic. -This target must live in the same region as the forwarding rule. + Description: `The URL of the target resource to receive the matched traffic. +The target must live in the same region as the forwarding rule. The forwarded traffic must be of a type appropriate to the target object.`, }, @@ -794,10 +793,7 @@ func flattenComputeForwardingRuleSubnetwork(v interface{}, d *schema.ResourceDat } func flattenComputeForwardingRuleTarget(v interface{}, d *schema.ResourceData) interface{} { - if v == nil { - return v - } - return ConvertSelfLinkToV1(v.(string)) + return v } func flattenComputeForwardingRuleAllowGlobalAccess(v interface{}, d *schema.ResourceData) interface{} { diff --git a/google-beta/resource_compute_forwarding_rule_generated_test.go b/google-beta/resource_compute_forwarding_rule_generated_test.go index 96fd469be3..22a5394d73 100644 --- a/google-beta/resource_compute_forwarding_rule_generated_test.go +++ b/google-beta/resource_compute_forwarding_rule_generated_test.go @@ -193,6 +193,218 @@ resource "google_compute_subnetwork" "default" { `, context) } +func TestAccComputeForwardingRule_forwardingRuleHttpLbExample(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "random_suffix": acctest.RandString(10), + } + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProvidersOiCS, + CheckDestroy: testAccCheckComputeForwardingRuleDestroy, + Steps: []resource.TestStep{ + { + Config: testAccComputeForwardingRule_forwardingRuleHttpLbExample(context), + }, + }, + }) +} + +func testAccComputeForwardingRule_forwardingRuleHttpLbExample(context map[string]interface{}) string { + return Nprintf(` +// Forwarding rule for Internal Load Balancing +resource "google_compute_forwarding_rule" "default" { + provider = google-beta + depends_on = [google_compute_subnetwork.proxy] + name = "website-forwarding-rule%{random_suffix}" + region = "us-central1" + + ip_protocol = "TCP" + load_balancing_scheme = "INTERNAL_MANAGED" + port_range = "80" + target = google_compute_region_target_http_proxy.default.self_link + network = google_compute_network.default.self_link + subnetwork = google_compute_subnetwork.default.self_link + network_tier = "PREMIUM" +} + +resource "google_compute_region_target_http_proxy" "default" { + provider = google-beta + + region = "us-central1" + name = "website-proxy%{random_suffix}" + url_map = google_compute_region_url_map.default.self_link +} + +resource "google_compute_region_url_map" "default" { + provider = google-beta + + region = "us-central1" + name = "website-map%{random_suffix}" + default_service = google_compute_region_backend_service.default.self_link +} + +resource "google_compute_region_backend_service" "default" { + provider = google-beta + + load_balancing_scheme = "INTERNAL_MANAGED" + + backend { + group = google_compute_region_instance_group_manager.rigm.instance_group + balancing_mode = "UTILIZATION" + } + + region = "us-central1" + name = "website-backend%{random_suffix}" + protocol = "HTTP" + timeout_sec = 10 + + health_checks = [google_compute_region_health_check.default.self_link] +} + +data "google_compute_image" "debian_image" { + provider = google-beta + family = "debian-9" + project = "debian-cloud" +} + +resource "google_compute_region_instance_group_manager" "rigm" { + provider = google-beta + region = "us-central1" + name = "rigm-internal" + version { + instance_template = google_compute_instance_template.instance_template.self_link + name = "primary" + } + base_instance_name = "internal-glb" + target_size = 1 +} + +resource "google_compute_instance_template" "instance_template" { + provider = google-beta + name = "template-website-backend%{random_suffix}" + machine_type = "n1-standard-1" + + network_interface { + network = google_compute_network.default.self_link + subnetwork = google_compute_subnetwork.default.self_link + } + + disk { + source_image = data.google_compute_image.debian_image.self_link + auto_delete = true + boot = true + } + + tags = ["allow-ssh", "load-balanced-backend"] +} + +resource "google_compute_region_health_check" "default" { + depends_on = [google_compute_firewall.fw4] + provider = google-beta + + region = "us-central1" + name = "website-hc%{random_suffix}" + http_health_check { + port_specification = "USE_SERVING_PORT" + } +} + +resource "google_compute_firewall" "fw1" { + provider = google-beta + name = "website-fw%{random_suffix}-1" + network = google_compute_network.default.self_link + source_ranges = ["10.1.2.0/24"] + allow { + protocol = "tcp" + } + allow { + protocol = "udp" + } + allow { + protocol = "icmp" + } + direction = "INGRESS" +} + +resource "google_compute_firewall" "fw2" { + depends_on = [google_compute_firewall.fw1] + provider = google-beta + name = "website-fw%{random_suffix}-2" + network = google_compute_network.default.self_link + source_ranges = ["0.0.0.0/0"] + allow { + protocol = "tcp" + ports = ["22"] + } + target_tags = ["allow-ssh"] + direction = "INGRESS" +} + +resource "google_compute_firewall" "fw3" { + depends_on = [google_compute_firewall.fw2] + provider = google-beta + name = "website-fw%{random_suffix}-3" + network = google_compute_network.default.self_link + source_ranges = ["130.211.0.0/22", "35.191.0.0/16"] + allow { + protocol = "tcp" + } + target_tags = ["load-balanced-backend"] + direction = "INGRESS" +} + +resource "google_compute_firewall" "fw4" { + depends_on = [google_compute_firewall.fw3] + provider = google-beta + name = "website-fw%{random_suffix}-4" + network = google_compute_network.default.self_link + source_ranges = ["10.129.0.0/26"] + target_tags = ["load-balanced-backend"] + allow { + protocol = "tcp" + ports = ["80"] + } + allow { + protocol = "tcp" + ports = ["443"] + } + allow { + protocol = "tcp" + ports = ["8000"] + } + direction = "INGRESS" +} + +resource "google_compute_network" "default" { + provider = google-beta + name = "website-net%{random_suffix}" + auto_create_subnetworks = false + routing_mode = "REGIONAL" +} + +resource "google_compute_subnetwork" "default" { + provider = google-beta + name = "website-net%{random_suffix}-default" + ip_cidr_range = "10.1.2.0/24" + region = "us-central1" + network = google_compute_network.default.self_link +} + +resource "google_compute_subnetwork" "proxy" { + provider = google-beta + name = "website-net%{random_suffix}-proxy" + ip_cidr_range = "10.129.0.0/26" + region = "us-central1" + network = google_compute_network.default.self_link + purpose = "INTERNAL_HTTPS_LOAD_BALANCER" + role = "ACTIVE" +} +`, context) +} + func testAccCheckComputeForwardingRuleDestroy(s *terraform.State) error { for name, rs := range s.RootModule().Resources { if rs.Type != "google_compute_forwarding_rule" { diff --git a/google-beta/resource_compute_region_backend_service.go b/google-beta/resource_compute_region_backend_service.go index 58d3e994f7..5289def3c4 100644 --- a/google-beta/resource_compute_region_backend_service.go +++ b/google-beta/resource_compute_region_backend_service.go @@ -641,13 +641,15 @@ partial URL.`, }, "capacity_scaler": { Type: schema.TypeFloat, - Computed: true, Optional: true, Description: `A multiplier applied to the group's maximum servicing capacity (based on UTILIZATION, RATE or CONNECTION). +Default value is 1, which means the group will serve up to 100% +of its configured capacity (depending on balancingMode). A setting of 0 means the group is completely drained, offering 0% of its available Capacity. Valid range is [0.0,1.0].`, + Default: 1.0, }, "description": { Type: schema.TypeString, @@ -657,6 +659,7 @@ Provide this property when you create the resource.`, }, "failover": { Type: schema.TypeBool, + Computed: true, Optional: true, Description: `This field designates whether this is a failover backend. More than one failover backend can be configured for a given RegionBackendService.`, @@ -1850,7 +1853,7 @@ func expandComputeRegionBackendServiceBackend(v interface{}, d TerraformResource transformedCapacityScaler, err := expandComputeRegionBackendServiceBackendCapacityScaler(original["capacity_scaler"], d, config) if err != nil { return nil, err - } else if val := reflect.ValueOf(transformedCapacityScaler); val.IsValid() && !isEmptyValue(val) { + } else { transformed["capacityScaler"] = transformedCapacityScaler } diff --git a/google-beta/resource_compute_region_backend_service_generated_test.go b/google-beta/resource_compute_region_backend_service_generated_test.go index 743055f3a8..1cf49a8a31 100644 --- a/google-beta/resource_compute_region_backend_service_generated_test.go +++ b/google-beta/resource_compute_region_backend_service_generated_test.go @@ -172,6 +172,114 @@ resource "google_compute_health_check" "health_check" { `, context) } +func TestAccComputeRegionBackendService_regionBackendServiceBalancingModeExample(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "random_suffix": acctest.RandString(10), + } + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProvidersOiCS, + CheckDestroy: testAccCheckComputeRegionBackendServiceDestroy, + Steps: []resource.TestStep{ + { + Config: testAccComputeRegionBackendService_regionBackendServiceBalancingModeExample(context), + }, + }, + }) +} + +func testAccComputeRegionBackendService_regionBackendServiceBalancingModeExample(context map[string]interface{}) string { + return Nprintf(` +resource "google_compute_region_backend_service" "default" { + provider = google-beta + + load_balancing_scheme = "INTERNAL_MANAGED" + + backend { + group = google_compute_region_instance_group_manager.rigm.instance_group + balancing_mode = "UTILIZATION" + } + + region = "us-central1" + name = "region-backend-service%{random_suffix}" + protocol = "HTTP" + timeout_sec = 10 + + health_checks = [google_compute_region_health_check.default.self_link] +} + +data "google_compute_image" "debian_image" { + provider = google-beta + + family = "debian-9" + project = "debian-cloud" +} + +resource "google_compute_region_instance_group_manager" "rigm" { + provider = google-beta + + region = "us-central1" + name = "rigm-internal" + version { + instance_template = google_compute_instance_template.instance_template.self_link + name = "primary" + } + base_instance_name = "internal-glb" + target_size = 1 +} + +resource "google_compute_instance_template" "instance_template" { + provider = google-beta + + name = "template-region-backend-service%{random_suffix}" + machine_type = "n1-standard-1" + + network_interface { + network = google_compute_network.default.self_link + subnetwork = google_compute_subnetwork.default.self_link + } + + disk { + source_image = data.google_compute_image.debian_image.self_link + auto_delete = true + boot = true + } + + tags = ["allow-ssh", "load-balanced-backend"] +} + +resource "google_compute_region_health_check" "default" { + provider = google-beta + + region = "us-central1" + name = "health-check%{random_suffix}" + http_health_check { + port_specification = "USE_SERVING_PORT" + } +} + +resource "google_compute_network" "default" { + provider = google-beta + + name = "net%{random_suffix}" + auto_create_subnetworks = false + routing_mode = "REGIONAL" +} + +resource "google_compute_subnetwork" "default" { + provider = google-beta + + name = "net%{random_suffix}-default" + ip_cidr_range = "10.1.2.0/24" + region = "us-central1" + network = google_compute_network.default.self_link +} +`, context) +} + func testAccCheckComputeRegionBackendServiceDestroy(s *terraform.State) error { for name, rs := range s.RootModule().Resources { if rs.Type != "google_compute_region_backend_service" { diff --git a/website/docs/r/compute_forwarding_rule.html.markdown b/website/docs/r/compute_forwarding_rule.html.markdown index df4beb5385..38fa632acb 100644 --- a/website/docs/r/compute_forwarding_rule.html.markdown +++ b/website/docs/r/compute_forwarding_rule.html.markdown @@ -150,6 +150,204 @@ resource "google_compute_subnetwork" "default" { network = google_compute_network.default.self_link } ``` + +## Example Usage - Forwarding Rule Http Lb + + +```hcl +// Forwarding rule for Internal Load Balancing +resource "google_compute_forwarding_rule" "default" { + provider = google-beta + depends_on = [google_compute_subnetwork.proxy] + name = "website-forwarding-rule" + region = "us-central1" + + ip_protocol = "TCP" + load_balancing_scheme = "INTERNAL_MANAGED" + port_range = "80" + target = google_compute_region_target_http_proxy.default.self_link + network = google_compute_network.default.self_link + subnetwork = google_compute_subnetwork.default.self_link + network_tier = "PREMIUM" +} + +resource "google_compute_region_target_http_proxy" "default" { + provider = google-beta + + region = "us-central1" + name = "website-proxy" + url_map = google_compute_region_url_map.default.self_link +} + +resource "google_compute_region_url_map" "default" { + provider = google-beta + + region = "us-central1" + name = "website-map" + default_service = google_compute_region_backend_service.default.self_link +} + +resource "google_compute_region_backend_service" "default" { + provider = google-beta + + load_balancing_scheme = "INTERNAL_MANAGED" + + backend { + group = google_compute_region_instance_group_manager.rigm.instance_group + balancing_mode = "UTILIZATION" + } + + region = "us-central1" + name = "website-backend" + protocol = "HTTP" + timeout_sec = 10 + + health_checks = [google_compute_region_health_check.default.self_link] +} + +data "google_compute_image" "debian_image" { + provider = google-beta + family = "debian-9" + project = "debian-cloud" +} + +resource "google_compute_region_instance_group_manager" "rigm" { + provider = google-beta + region = "us-central1" + name = "rigm-internal" + version { + instance_template = google_compute_instance_template.instance_template.self_link + name = "primary" + } + base_instance_name = "internal-glb" + target_size = 1 +} + +resource "google_compute_instance_template" "instance_template" { + provider = google-beta + name = "template-website-backend" + machine_type = "n1-standard-1" + + network_interface { + network = google_compute_network.default.self_link + subnetwork = google_compute_subnetwork.default.self_link + } + + disk { + source_image = data.google_compute_image.debian_image.self_link + auto_delete = true + boot = true + } + + tags = ["allow-ssh", "load-balanced-backend"] +} + +resource "google_compute_region_health_check" "default" { + depends_on = [google_compute_firewall.fw4] + provider = google-beta + + region = "us-central1" + name = "website-hc" + http_health_check { + port_specification = "USE_SERVING_PORT" + } +} + +resource "google_compute_firewall" "fw1" { + provider = google-beta + name = "website-fw-1" + network = google_compute_network.default.self_link + source_ranges = ["10.1.2.0/24"] + allow { + protocol = "tcp" + } + allow { + protocol = "udp" + } + allow { + protocol = "icmp" + } + direction = "INGRESS" +} + +resource "google_compute_firewall" "fw2" { + depends_on = [google_compute_firewall.fw1] + provider = google-beta + name = "website-fw-2" + network = google_compute_network.default.self_link + source_ranges = ["0.0.0.0/0"] + allow { + protocol = "tcp" + ports = ["22"] + } + target_tags = ["allow-ssh"] + direction = "INGRESS" +} + +resource "google_compute_firewall" "fw3" { + depends_on = [google_compute_firewall.fw2] + provider = google-beta + name = "website-fw-3" + network = google_compute_network.default.self_link + source_ranges = ["130.211.0.0/22", "35.191.0.0/16"] + allow { + protocol = "tcp" + } + target_tags = ["load-balanced-backend"] + direction = "INGRESS" +} + +resource "google_compute_firewall" "fw4" { + depends_on = [google_compute_firewall.fw3] + provider = google-beta + name = "website-fw-4" + network = google_compute_network.default.self_link + source_ranges = ["10.129.0.0/26"] + target_tags = ["load-balanced-backend"] + allow { + protocol = "tcp" + ports = ["80"] + } + allow { + protocol = "tcp" + ports = ["443"] + } + allow { + protocol = "tcp" + ports = ["8000"] + } + direction = "INGRESS" +} + +resource "google_compute_network" "default" { + provider = google-beta + name = "website-net" + auto_create_subnetworks = false + routing_mode = "REGIONAL" +} + +resource "google_compute_subnetwork" "default" { + provider = google-beta + name = "website-net-default" + ip_cidr_range = "10.1.2.0/24" + region = "us-central1" + network = google_compute_network.default.self_link +} + +resource "google_compute_subnetwork" "proxy" { + provider = google-beta + name = "website-net-proxy" + ip_cidr_range = "10.129.0.0/26" + region = "us-central1" + network = google_compute_network.default.self_link + purpose = "INTERNAL_HTTPS_LOAD_BALANCER" + role = "ACTIVE" +} +``` ## Argument Reference @@ -266,9 +464,8 @@ The following arguments are supported: * `target` - (Optional) - This field is only used for EXTERNAL load balancing. - A reference to a TargetPool resource to receive the matched traffic. - This target must live in the same region as the forwarding rule. + The URL of the target resource to receive the matched traffic. + The target must live in the same region as the forwarding rule. The forwarded traffic must be of a type appropriate to the target object. diff --git a/website/docs/r/compute_region_backend_service.html.markdown b/website/docs/r/compute_region_backend_service.html.markdown index 987d4b38ab..b8528acd78 100644 --- a/website/docs/r/compute_region_backend_service.html.markdown +++ b/website/docs/r/compute_region_backend_service.html.markdown @@ -134,6 +134,100 @@ resource "google_compute_health_check" "health_check" { } } ``` + +## Example Usage - Region Backend Service Balancing Mode + + +```hcl +resource "google_compute_region_backend_service" "default" { + provider = google-beta + + load_balancing_scheme = "INTERNAL_MANAGED" + + backend { + group = google_compute_region_instance_group_manager.rigm.instance_group + balancing_mode = "UTILIZATION" + } + + region = "us-central1" + name = "region-backend-service" + protocol = "HTTP" + timeout_sec = 10 + + health_checks = [google_compute_region_health_check.default.self_link] +} + +data "google_compute_image" "debian_image" { + provider = google-beta + + family = "debian-9" + project = "debian-cloud" +} + +resource "google_compute_region_instance_group_manager" "rigm" { + provider = google-beta + + region = "us-central1" + name = "rigm-internal" + version { + instance_template = google_compute_instance_template.instance_template.self_link + name = "primary" + } + base_instance_name = "internal-glb" + target_size = 1 +} + +resource "google_compute_instance_template" "instance_template" { + provider = google-beta + + name = "template-region-backend-service" + machine_type = "n1-standard-1" + + network_interface { + network = google_compute_network.default.self_link + subnetwork = google_compute_subnetwork.default.self_link + } + + disk { + source_image = data.google_compute_image.debian_image.self_link + auto_delete = true + boot = true + } + + tags = ["allow-ssh", "load-balanced-backend"] +} + +resource "google_compute_region_health_check" "default" { + provider = google-beta + + region = "us-central1" + name = "health-check" + http_health_check { + port_specification = "USE_SERVING_PORT" + } +} + +resource "google_compute_network" "default" { + provider = google-beta + + name = "net" + auto_create_subnetworks = false + routing_mode = "REGIONAL" +} + +resource "google_compute_subnetwork" "default" { + provider = google-beta + + name = "net-default" + ip_cidr_range = "10.1.2.0/24" + region = "us-central1" + network = google_compute_network.default.self_link +} +``` ## Argument Reference @@ -288,6 +382,8 @@ The `backend` block supports: (Optional) A multiplier applied to the group's maximum servicing capacity (based on UTILIZATION, RATE or CONNECTION). + Default value is 1, which means the group will serve up to 100% + of its configured capacity (depending on balancingMode). A setting of 0 means the group is completely drained, offering 0% of its available Capacity. Valid range is [0.0,1.0].