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

Adding "named_port" support to Google's instance_group_manager. #4605

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,25 @@ func resourceComputeInstanceGroupManager() *schema.Resource {
Required: true,
},

"named_port": &schema.Schema{
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{

"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},

"port": &schema.Schema{
Type: schema.TypeInt,
Required: true,
},
},
},
},

"update_strategy": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -88,6 +107,18 @@ func resourceComputeInstanceGroupManager() *schema.Resource {
}
}

func getNamedPorts(nps []interface{}) []*compute.NamedPort {
namedPorts := make([]*compute.NamedPort, 0, len(nps))
for _, v := range nps {
np := v.(map[string]interface{})
namedPorts = append(namedPorts, &compute.NamedPort{
Name: np["name"].(string),
Port: int64(np["port"].(int)),
})
}
return namedPorts
}

func resourceComputeInstanceGroupManagerCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

Expand All @@ -110,6 +141,10 @@ func resourceComputeInstanceGroupManagerCreate(d *schema.ResourceData, meta inte
manager.Description = v.(string)
}

if v, ok := d.GetOk("named_port"); ok {
manager.NamedPorts = getNamedPorts(v.([]interface{}))
}

if attr := d.Get("target_pools").(*schema.Set); attr.Len() > 0 {
var s []string
for _, v := range attr.List() {
Expand Down Expand Up @@ -160,6 +195,7 @@ func resourceComputeInstanceGroupManagerRead(d *schema.ResourceData, meta interf
}

// Set computed fields
d.Set("named_port", manager.NamedPorts)
d.Set("fingerprint", manager.Fingerprint)
d.Set("instance_group", manager.InstanceGroup)
d.Set("target_size", manager.TargetSize)
Expand Down Expand Up @@ -253,6 +289,31 @@ func resourceComputeInstanceGroupManagerUpdate(d *schema.ResourceData, meta inte
d.SetPartial("instance_template")
}

// If named_port changes then update:
if d.HasChange("named_port") {

// Build the parameters for a "SetNamedPorts" request:
namedPorts := getNamedPorts(d.Get("named_port").([]interface{}))
setNamedPorts := &compute.InstanceGroupsSetNamedPortsRequest{
NamedPorts: namedPorts,
}

// Make the request:
op, err := config.clientCompute.InstanceGroups.SetNamedPorts(
config.Project, d.Get("zone").(string), d.Id(), setNamedPorts).Do()
if err != nil {
return fmt.Errorf("Error updating InstanceGroupManager: %s", err)
}

// Wait for the operation to complete:
err = computeOperationWaitZone(config, op, d.Get("zone").(string), "Updating InstanceGroupManager")
if err != nil {
return err
}

d.SetPartial("named_port")
}

// If size changes trigger a resize
if d.HasChange("target_size") {
if v, ok := d.GetOk("target_size"); ok {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ func TestAccInstanceGroupManager_update(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckInstanceGroupManagerExists(
"google_compute_instance_group_manager.igm-update", &manager),
testAccCheckInstanceGroupManagerNamedPorts(
"google_compute_instance_group_manager.igm-update",
map[string]int64{"customhttp": 8080},
&manager),
),
},
resource.TestStep{
Expand All @@ -65,6 +69,10 @@ func TestAccInstanceGroupManager_update(t *testing.T) {
testAccCheckInstanceGroupManagerUpdated(
"google_compute_instance_group_manager.igm-update", 3,
"google_compute_target_pool.igm-update", template2),
testAccCheckInstanceGroupManagerNamedPorts(
"google_compute_instance_group_manager.igm-update",
map[string]int64{"customhttp": 8080, "customhttps": 8443},
&manager),
),
},
},
Expand Down Expand Up @@ -157,6 +165,42 @@ func testAccCheckInstanceGroupManagerUpdated(n string, size int64, targetPool st
}
}

func testAccCheckInstanceGroupManagerNamedPorts(n string, np map[string]int64, instanceGroupManager *compute.InstanceGroupManager) resource.TestCheckFunc {
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)

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

var found bool
for _, namedPort := range manager.NamedPorts {
found = false
for name, port := range np {
if namedPort.Name == name && namedPort.Port == port {
found = true
}
}
if !found {
return fmt.Errorf("named port incorrect")
}
}

return nil
}
}

func testAccInstanceGroupManager_basic(template, target, igm1, igm2 string) string {
return fmt.Sprintf(`
resource "google_compute_instance_template" "igm-basic" {
Expand Down Expand Up @@ -252,6 +296,10 @@ func testAccInstanceGroupManager_update(template, target, igm string) string {
base_instance_name = "igm-update"
zone = "us-central1-c"
target_size = 2
named_port {
name = "customhttp"
port = 8080
}
}`, template, target, igm)
}

Expand Down Expand Up @@ -322,5 +370,13 @@ func testAccInstanceGroupManager_update2(template1, target, template2, igm strin
base_instance_name = "igm-update"
zone = "us-central1-c"
target_size = 3
named_port {
name = "customhttp"
port = 8080
}
named_port {
name = "customhttps"
port = 8443
}
}`, template1, target, template2, igm)
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ resource "google_compute_instance_group_manager" "foobar" {
base_instance_name = "foobar"
zone = "us-central1-a"
target_size = 2

named_port {
name = "customHTTP"
port = 8888
}

}
```

Expand Down Expand Up @@ -63,6 +69,12 @@ affect existing instances.

* `zone` - (Required) The zone that instances in this group should be created in.

The `named_port` block supports: (Include a named_port block for each named-port required).

* `name` - (Required) The name of the port.

* `port` - (Required) The port number.

## Attributes Reference

The following attributes are exported:
Expand Down