provider/google: Support named_port on instance_group_manager
This allows HTTP and HTTPs load-balancers to direct traffic to ports other than tcp/80 and tcp/443.
This commit is contained in:
parent
c42f25df74
commit
9aa8bbda93
|
@ -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,
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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() {
|
||||
|
@ -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)
|
||||
|
@ -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 {
|
||||
|
|
|
@ -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{
|
||||
|
@ -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),
|
||||
),
|
||||
},
|
||||
},
|
||||
|
@ -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" {
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -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:
|
||||
|
|
Loading…
Reference in New Issue