Skip to content

Commit

Permalink
non-NIC0 ILB support (#1418)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored and chrisst committed Nov 23, 2019
1 parent 30a179e commit c302a51
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 0 deletions.
37 changes: 37 additions & 0 deletions google-beta/resource_compute_region_backend_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,13 @@ The default value is 1.0.`,
},
},
},
"network": {
Type: schema.TypeString,
Optional: true,
DiffSuppressFunc: compareSelfLinkOrResourceName,
Description: `The URL of the network to which this backend service belongs.
This field can only be specified when the load balancing scheme is set to INTERNAL.`,
},
"outlier_detection": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -828,6 +835,12 @@ func resourceComputeRegionBackendServiceCreate(d *schema.ResourceData, meta inte
} else if v, ok := d.GetOkExists("log_config"); !isEmptyValue(reflect.ValueOf(logConfigProp)) && (ok || !reflect.DeepEqual(v, logConfigProp)) {
obj["logConfig"] = logConfigProp
}
networkProp, err := expandComputeRegionBackendServiceNetwork(d.Get("network"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("network"); !isEmptyValue(reflect.ValueOf(networkProp)) && (ok || !reflect.DeepEqual(v, networkProp)) {
obj["network"] = networkProp
}
regionProp, err := expandComputeRegionBackendServiceRegion(d.Get("region"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -954,6 +967,9 @@ func resourceComputeRegionBackendServiceRead(d *schema.ResourceData, meta interf
if err := d.Set("log_config", flattenComputeRegionBackendServiceLogConfig(res["logConfig"], d)); err != nil {
return fmt.Errorf("Error reading RegionBackendService: %s", err)
}
if err := d.Set("network", flattenComputeRegionBackendServiceNetwork(res["network"], d)); err != nil {
return fmt.Errorf("Error reading RegionBackendService: %s", err)
}
if err := d.Set("region", flattenComputeRegionBackendServiceRegion(res["region"], d)); err != nil {
return fmt.Errorf("Error reading RegionBackendService: %s", err)
}
Expand Down Expand Up @@ -1075,6 +1091,12 @@ func resourceComputeRegionBackendServiceUpdate(d *schema.ResourceData, meta inte
} else if v, ok := d.GetOkExists("log_config"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, logConfigProp)) {
obj["logConfig"] = logConfigProp
}
networkProp, err := expandComputeRegionBackendServiceNetwork(d.Get("network"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("network"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, networkProp)) {
obj["network"] = networkProp
}
regionProp, err := expandComputeRegionBackendServiceRegion(d.Get("region"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -1789,6 +1811,13 @@ func flattenComputeRegionBackendServiceLogConfigSampleRate(v interface{}, d *sch
return v
}

func flattenComputeRegionBackendServiceNetwork(v interface{}, d *schema.ResourceData) interface{} {
if v == nil {
return v
}
return ConvertSelfLinkToV1(v.(string))
}

func flattenComputeRegionBackendServiceRegion(v interface{}, d *schema.ResourceData) interface{} {
if v == nil {
return v
Expand Down Expand Up @@ -2497,6 +2526,14 @@ func expandComputeRegionBackendServiceLogConfigSampleRate(v interface{}, d Terra
return v, nil
}

func expandComputeRegionBackendServiceNetwork(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
f, err := parseGlobalFieldValue("networks", v.(string), "project", d, config, true)
if err != nil {
return nil, fmt.Errorf("Invalid value for network: %s", err)
}
return f.RelativeLink(), nil
}

func expandComputeRegionBackendServiceRegion(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
f, err := parseGlobalFieldValue("regions", v.(string), "project", d, config, true)
if err != nil {
Expand Down
126 changes: 126 additions & 0 deletions google-beta/resource_compute_region_backend_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,33 @@ func TestAccComputeRegionBackendService_withBackendAndUpdate(t *testing.T) {
})
}

func TestAccComputeRegionBackendService_withBackendMultiNic(t *testing.T) {
t.Parallel()

serviceName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
net1Name := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
net2Name := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
igName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
itName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
checkName := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeRegionBackendServiceDestroy,
Steps: []resource.TestStep{
{
Config: testAccComputeRegionBackendService_withBackendMultiNic(
serviceName, net1Name, net2Name, igName, itName, checkName, 10),
},
{
ResourceName: "google_compute_region_backend_service.lipsum",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccComputeRegionBackendService_withConnectionDrainingAndUpdate(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -502,6 +529,105 @@ resource "google_compute_health_check" "default" {
`, serviceName, timeout, igName, itName, checkName)
}

func testAccComputeRegionBackendService_withBackendMultiNic(
serviceName, net1Name, net2Name, igName, itName, checkName string, timeout int64) string {
return fmt.Sprintf(`
data "google_compute_image" "my_image" {
family = "debian-9"
project = "debian-cloud"
}
resource "google_compute_region_backend_service" "lipsum" {
name = "%s"
description = "Hello World 1234"
protocol = "TCP"
region = "us-central1"
timeout_sec = %v
backend {
group = google_compute_instance_group_manager.foobar.instance_group
failover = true
}
failover_policy {
disable_connection_drain_on_failover = true
drop_traffic_if_unhealthy = true
failover_ratio = 0.4
}
network = google_compute_network.network2.self_link
health_checks = [google_compute_health_check.default.self_link]
}
resource "google_compute_network" "network1" {
name = "%s"
auto_create_subnetworks = false
}
resource "google_compute_subnetwork" "subnet1" {
name = "%s"
ip_cidr_range = "10.0.1.0/24"
region = "us-central1"
private_ip_google_access = true
network = google_compute_network.network1.self_link
}
resource "google_compute_network" "network2" {
name = "%s"
auto_create_subnetworks = false
}
resource "google_compute_subnetwork" "subnet2" {
name = "%s"
ip_cidr_range = "10.0.2.0/24"
region = "us-central1"
private_ip_google_access = true
network = google_compute_network.network2.self_link
}
resource "google_compute_instance_group_manager" "foobar" {
name = "%s"
version {
instance_template = google_compute_instance_template.foobar.self_link
name = "primary"
}
base_instance_name = "foobar"
zone = "us-central1-f"
target_size = 1
}
resource "google_compute_instance_template" "foobar" {
name = "%s"
machine_type = "n1-standard-1"
network_interface {
subnetwork = google_compute_subnetwork.subnet1.self_link
}
network_interface {
subnetwork = google_compute_subnetwork.subnet2.self_link
}
disk {
source_image = data.google_compute_image.my_image.self_link
auto_delete = true
boot = true
}
}
resource "google_compute_health_check" "default" {
name = "%s"
check_interval_sec = 1
timeout_sec = 1
tcp_health_check {
port = 443
}
}
`, serviceName, timeout, net1Name, net1Name, net2Name, net2Name, igName, itName, checkName)
}

func testAccComputeRegionBackendService_withConnectionDraining(serviceName, checkName string, drainingTimeout int64) string {
return fmt.Sprintf(`
resource "google_compute_region_backend_service" "foobar" {
Expand Down
5 changes: 5 additions & 0 deletions website/docs/r/compute_region_backend_service.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,11 @@ The following arguments are supported:
This field denotes the logging options for the load balancer traffic served by this backend service.
If logging is enabled, logs will be exported to Stackdriver. Structure is documented below.

* `network` -
(Optional, [Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html))
The URL of the network to which this backend service belongs.
This field can only be specified when the load balancing scheme is set to INTERNAL.

* `region` -
(Optional)
The Region in which the created backend service should reside.
Expand Down

0 comments on commit c302a51

Please sign in to comment.