Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provider/google: Changed network argument in google_compute_instance_group as optional #13493

Merged
merged 7 commits into from
Jun 7, 2017
6 changes: 6 additions & 0 deletions builtin/providers/google/resource_compute_instance_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ func resourceComputeInstanceGroup() *schema.Resource {

"network": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},

"project": &schema.Schema{
Expand Down Expand Up @@ -128,6 +130,10 @@ func resourceComputeInstanceGroupCreate(d *schema.ResourceData, meta interface{}
instanceGroup.NamedPorts = getNamedPorts(v.([]interface{}))
}

if v, ok := d.GetOk("network"); ok {
instanceGroup.Network = v.(string)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In other terraform resources, we have the network parameter be the name of the network and then do a lookup. For consistency, I think we should do that here too. See https://github.com/hashicorp/terraform/blob/master/builtin/providers/google/resource_compute_instance.go#L479 for an example.

}

log.Printf("[DEBUG] InstanceGroup insert request: %#v", instanceGroup)
op, err := config.clientCompute.InstanceGroups.Insert(
project, d.Get("zone").(string), instanceGroup).Do()
Expand Down
58 changes: 55 additions & 3 deletions builtin/providers/google/resource_compute_instance_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,29 @@ import (
func TestAccComputeInstanceGroup_basic(t *testing.T) {
var instanceGroup compute.InstanceGroup
var instanceName = fmt.Sprintf("instancegroup-test-%s", acctest.RandString(10))
projs := []string{
"GOOGLE_PROJECT",
"GCLOUD_PROJECT",
"CLOUDSDK_CORE_PROJECT",
}
var network = fmt.Sprintf("https://www.googleapis.com/compute/v1/projects/%s/global/networks/default", multiEnvSearch(projs))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccComputeInstanceGroup_destroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeInstanceGroup_basic(instanceName),
Config: testAccComputeInstanceGroup_basic(instanceName, network),
Check: resource.ComposeTestCheckFunc(
testAccComputeInstanceGroup_exists(
"google_compute_instance_group.basic", &instanceGroup),
testAccComputeInstanceGroup_exists(
"google_compute_instance_group.empty", &instanceGroup),
testAccComputeInstanceGroup_exists(
"google_compute_instance_group.empty-with-network", &instanceGroup),
testAccComputeInstanceGroup_network(
"google_compute_instance_group.empty-with-network", network, &instanceGroup),
),
},
},
Expand Down Expand Up @@ -201,7 +211,34 @@ func testAccComputeInstanceGroup_named_ports(n string, np map[string]int64, inst
}
}

func testAccComputeInstanceGroup_basic(instance string) string {
func testAccComputeInstanceGroup_network(n string, network string, instanceGroup *compute.InstanceGroup) resource.TestCheckFunc {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: can you rename this to something like testAccComputeInstanceGroup_networkExists? Check functions (in my opinion) should be more boolean-like in name to make it clear that it's a check and not a config.

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)

instanceGroup, err := config.clientCompute.InstanceGroups.Get(
config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do()
if err != nil {
return err
}

if instanceGroup.Network != network {
return fmt.Errorf("network incorrect: actual=%s vs expected=%s", instanceGroup.Network, network)
}

return nil
}
}

func testAccComputeInstanceGroup_basic(instance, network string) string {
return fmt.Sprintf(`
resource "google_compute_instance" "ig_instance" {
name = "%s"
Expand Down Expand Up @@ -245,7 +282,22 @@ func testAccComputeInstanceGroup_basic(instance string) string {
name = "https"
port = "8443"
}
}`, instance, instance, instance)
}

resource "google_compute_instance_group" "empty-with-network" {
description = "Terraform test instance group empty-with-network"
name = "%s-empty-with-network"
network = "%s"
zone = "us-central1-c"
named_port {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: indentation

name = "http"
port = "8080"
}
named_port {
name = "https"
port = "8443"
}
}`, instance, instance, instance, instance, network)
}

func testAccComputeInstanceGroup_update(instance string) string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,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}"
}
```

Expand Down Expand Up @@ -75,6 +76,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.
Expand All @@ -86,8 +92,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.