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 1 commit
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
103 changes: 103 additions & 0 deletions builtin/providers/google/resource_compute_instance_group_manager.go
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 @@ -118,6 +137,29 @@ func resourceComputeInstanceGroupManagerCreate(d *schema.ResourceData, meta inte
manager.TargetPools = s
}

// Prepare the list of named ports
namedPortsCount := d.Get("named_port.#").(int)

var namedPorts []*compute.NamedPort

if namedPortsCount > 0 {
// Build up the list of namedPorts
namedPorts = make([]*compute.NamedPort, 0, namedPortsCount)
for i := 0; i < namedPortsCount; i++ {
prefix := fmt.Sprintf("named_port.%d", i)

// Build a namedPort
namedPort := compute.NamedPort{
Name: d.Get(prefix + ".name").(string),
Port: int64(d.Get(prefix + ".port").(int)),
}

// Add it to the list of namedPorts
namedPorts = append(namedPorts, &namedPort)
}
manager.NamedPorts = namedPorts
}

updateStrategy := d.Get("update_strategy").(string)
if !(updateStrategy == "NONE" || updateStrategy == "RESTART") {
return fmt.Errorf("Update strategy must be \"NONE\" or \"RESTART\"")
Expand Down Expand Up @@ -165,6 +207,22 @@ func resourceComputeInstanceGroupManagerRead(d *schema.ResourceData, meta interf
d.Set("target_size", manager.TargetSize)
d.Set("self_link", manager.SelfLink)

// Prepare the list of named ports
namedPortsCount := d.Get("named_port.#").(int)

namedPorts := make([]map[string]interface{}, 0, 1)
Copy link
Contributor

Choose a reason for hiding this comment

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

if these are returned in a different order than the schema stored them in, will reflect.DeepEqual() as used in helper/schema/resource_data.go see them as equal?

e.g.
{0: {"name": "http", "port": 8080}, 1: {"name": "https", "port" 8443}}
{0: {"name": "https", "port": 8443}, 1: {"name": "http", "port" 8080}}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've not encountered issues with it yet, and I've used this branch to build instance_group_managers on 2 different GCE projects (in some tests with up to 10 named-ports). The order which seems to matter is within the .tf files themselves. I've repeatedly run "terraform apply" after the initial run and never seen anything being re-ordered.

Is this what you mean? I understand that it would be a problem if TF considered things to have changed if they came back in the wrong order, but it's certainly not the behaviour I've seen in testing this.

if namedPortsCount > 0 {
for _, namedPort := range manager.NamedPorts {

named_port := make(map[string]interface{})
named_port["name"] = namedPort.Name
named_port["port"] = namedPort.Port

namedPorts = append(namedPorts, named_port)
}
}
d.Set("named_port", namedPorts)

return nil
}
func resourceComputeInstanceGroupManagerUpdate(d *schema.ResourceData, meta interface{}) error {
Expand Down Expand Up @@ -253,6 +311,51 @@ func resourceComputeInstanceGroupManagerUpdate(d *schema.ResourceData, meta inte
d.SetPartial("instance_template")
}

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

// Prepare the list of named ports
namedPortsCount := d.Get("named_port.#").(int)

var namedPorts []*compute.NamedPort

if namedPortsCount > 0 {
// Build up the list of namedPorts
namedPorts = make([]*compute.NamedPort, 0, namedPortsCount)
for i := 0; i < namedPortsCount; i++ {
prefix := fmt.Sprintf("named_port.%d", i)

// Build a namedPort
namedPort := compute.NamedPort{
Name: d.Get(prefix + ".name").(string),
Port: int64(d.Get(prefix + ".port").(int)),
}

// Add it to the list of namedPorts
namedPorts = append(namedPorts, &namedPort)
}
}

// Build the parameter
setNamedPorts := &compute.InstanceGroupsSetNamedPortsRequest{
NamedPorts: namedPorts,
}

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 @@ -252,6 +252,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 = 8888
}
}`, template, target, igm)
}

Expand Down Expand Up @@ -322,5 +326,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 = 8888
}
named_port {
name = "customHTTPs"
port = 8889
}
}`, 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