Skip to content

Commit

Permalink
provider/google: Changed network argument in google_compute_instance_…
Browse files Browse the repository at this point in the history
…group as optional (#13493)
  • Loading branch information
tmshn authored and danawillow committed Jun 7, 2017
1 parent 6c0f6fa commit 0b6518a
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 3 deletions.
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 @@ -67,7 +67,9 @@ func resourceComputeInstanceGroup() *schema.Resource {

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

"project": {
Expand Down Expand Up @@ -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()
Expand Down
103 changes: 102 additions & 1 deletion builtin/providers/google/resource_compute_instance_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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" {
Expand Down Expand Up @@ -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"
}
Expand Down Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
}
```

Expand Down Expand Up @@ -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.
Expand All @@ -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.

0 comments on commit 0b6518a

Please sign in to comment.