From 0b6518a29b21e15a1f5b3e590c6d9480871eddd3 Mon Sep 17 00:00:00 2001 From: Shinichi TAMURA Date: Thu, 8 Jun 2017 03:20:17 +0900 Subject: [PATCH] provider/google: Changed network argument in google_compute_instance_group as optional (#13493) --- .../google/resource_compute_instance_group.go | 6 + .../resource_compute_instance_group_test.go | 103 +++++++++++++++++- .../r/compute_instance_group.html.markdown | 8 +- 3 files changed, 114 insertions(+), 3 deletions(-) diff --git a/builtin/providers/google/resource_compute_instance_group.go b/builtin/providers/google/resource_compute_instance_group.go index 787297eeb..0a7c49547 100644 --- a/builtin/providers/google/resource_compute_instance_group.go +++ b/builtin/providers/google/resource_compute_instance_group.go @@ -67,7 +67,9 @@ func resourceComputeInstanceGroup() *schema.Resource { "network": { Type: schema.TypeString, + Optional: true, Computed: true, + ForceNew: true, }, "project": { @@ -129,6 +131,10 @@ func resourceComputeInstanceGroupCreate(d *schema.ResourceData, meta interface{} instanceGroup.NamedPorts = getNamedPorts(v.([]interface{})) } + if v, ok := d.GetOk("network"); ok { + instanceGroup.Network = v.(string) + } + log.Printf("[DEBUG] InstanceGroup insert request: %#v", instanceGroup) op, err := config.clientCompute.InstanceGroups.Insert( project, d.Get("zone").(string), instanceGroup).Do() diff --git a/builtin/providers/google/resource_compute_instance_group_test.go b/builtin/providers/google/resource_compute_instance_group_test.go index 13e2f1c28..50d956fcd 100644 --- a/builtin/providers/google/resource_compute_instance_group_test.go +++ b/builtin/providers/google/resource_compute_instance_group_test.go @@ -90,6 +90,32 @@ func TestAccComputeInstanceGroup_outOfOrderInstances(t *testing.T) { }) } +func TestAccComputeInstanceGroup_network(t *testing.T) { + var instanceGroup compute.InstanceGroup + var instanceName = fmt.Sprintf("instancegroup-test-%s", acctest.RandString(10)) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccComputeInstanceGroup_destroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccComputeInstanceGroup_network(instanceName), + Check: resource.ComposeTestCheckFunc( + testAccComputeInstanceGroup_exists( + "google_compute_instance_group.with_instance", &instanceGroup), + testAccComputeInstanceGroup_hasCorrectNetwork( + "google_compute_instance_group.with_instance", "google_compute_network.ig_network", &instanceGroup), + testAccComputeInstanceGroup_exists( + "google_compute_instance_group.without_instance", &instanceGroup), + testAccComputeInstanceGroup_hasCorrectNetwork( + "google_compute_instance_group.without_instance", "google_compute_network.ig_network", &instanceGroup), + ), + }, + }, + }) +} + func testAccComputeInstanceGroup_destroy(s *terraform.State) error { config := testAccProvider.Meta().(*Config) @@ -201,6 +227,44 @@ func testAccComputeInstanceGroup_named_ports(n string, np map[string]int64, inst } } +func testAccComputeInstanceGroup_hasCorrectNetwork(nInstanceGroup string, nNetwork string, instanceGroup *compute.InstanceGroup) resource.TestCheckFunc { + return func(s *terraform.State) error { + config := testAccProvider.Meta().(*Config) + + rsInstanceGroup, ok := s.RootModule().Resources[nInstanceGroup] + if !ok { + return fmt.Errorf("Not found: %s", nInstanceGroup) + } + if rsInstanceGroup.Primary.ID == "" { + return fmt.Errorf("No ID is set") + } + instanceGroup, err := config.clientCompute.InstanceGroups.Get( + config.Project, rsInstanceGroup.Primary.Attributes["zone"], rsInstanceGroup.Primary.ID).Do() + if err != nil { + return err + } + + rsNetwork, ok := s.RootModule().Resources[nNetwork] + if !ok { + return fmt.Errorf("Not found: %s", nNetwork) + } + if rsNetwork.Primary.ID == "" { + return fmt.Errorf("No ID is set") + } + network, err := config.clientCompute.Networks.Get( + config.Project, rsNetwork.Primary.ID).Do() + if err != nil { + return err + } + + if instanceGroup.Network != network.SelfLink { + return fmt.Errorf("network incorrect: actual=%s vs expected=%s", instanceGroup.Network, network.SelfLink) + } + + return nil + } +} + func testAccComputeInstanceGroup_basic(instance string) string { return fmt.Sprintf(` resource "google_compute_instance" "ig_instance" { @@ -237,7 +301,7 @@ func testAccComputeInstanceGroup_basic(instance string) string { description = "Terraform test instance group empty" name = "%s-empty" zone = "us-central1-c" - named_port { + named_port { name = "http" port = "8080" } @@ -365,3 +429,40 @@ func testAccComputeInstanceGroup_outOfOrderInstances(instance string) string { } }`, instance, instance, instance) } + +func testAccComputeInstanceGroup_network(instance string) string { + return fmt.Sprintf(` + resource "google_compute_network" "ig_network" { + name = "%[1]s" + auto_create_subnetworks = true + } + + resource "google_compute_instance" "ig_instance" { + name = "%[1]s" + machine_type = "n1-standard-1" + can_ip_forward = false + zone = "us-central1-c" + + disk { + image = "debian-8-jessie-v20160803" + } + + network_interface { + network = "${google_compute_network.ig_network.name}" + } + } + + resource "google_compute_instance_group" "with_instance" { + description = "Terraform test instance group" + name = "%[1]s-with-instance" + zone = "us-central1-c" + instances = [ "${google_compute_instance.ig_instance.self_link}" ] + } + + resource "google_compute_instance_group" "without_instance" { + description = "Terraform test instance group" + name = "%[1]s-without-instance" + zone = "us-central1-c" + network = "${google_compute_network.ig_network.self_link}" + }`, instance) +} diff --git a/website/source/docs/providers/google/r/compute_instance_group.html.markdown b/website/source/docs/providers/google/r/compute_instance_group.html.markdown index b7ed3db1e..766365ddb 100644 --- a/website/source/docs/providers/google/r/compute_instance_group.html.markdown +++ b/website/source/docs/providers/google/r/compute_instance_group.html.markdown @@ -22,6 +22,7 @@ resource "google_compute_instance_group" "test" { name = "terraform-test" description = "Terraform test instance group" zone = "us-central1-a" + network = "${google_compute_network.default.self_link}" } ``` @@ -77,6 +78,11 @@ The following arguments are supported: * `project` - (Optional) The project in which the resource belongs. If it is not provided, the provider project is used. +* `network` - (Optional) The URL of the network the instance group is in. If + this is different from the network where the instances are in, the creation + fails. Defaults to the network where the instances are in (if neither + `network` nor `instances` is specified, this field will be blank). + The `named_port` block supports: * `name` - (Required) The name which the port will be mapped to. @@ -88,8 +94,6 @@ The `named_port` block supports: In addition to the arguments listed above, the following computed attributes are exported: -* `network` - The network the instance group is in. - * `self_link` - The URI of the created resource. * `size` - The number of instances in the group.