2015-02-16 17:06:23 +01:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2015-10-23 21:06:46 +02:00
|
|
|
"strings"
|
2015-02-16 17:06:23 +01:00
|
|
|
|
2015-07-28 02:47:10 +02:00
|
|
|
"google.golang.org/api/compute/v1"
|
2015-08-14 13:06:06 +02:00
|
|
|
"google.golang.org/api/googleapi"
|
2015-02-16 17:06:23 +01:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
2015-07-28 02:47:10 +02:00
|
|
|
func resourceComputeInstanceGroupManager() *schema.Resource {
|
2015-02-16 17:06:23 +01:00
|
|
|
return &schema.Resource{
|
2015-07-28 02:47:10 +02:00
|
|
|
Create: resourceComputeInstanceGroupManagerCreate,
|
|
|
|
Read: resourceComputeInstanceGroupManagerRead,
|
|
|
|
Update: resourceComputeInstanceGroupManagerUpdate,
|
|
|
|
Delete: resourceComputeInstanceGroupManagerDelete,
|
2015-02-16 17:06:23 +01:00
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
2016-04-10 23:34:15 +02:00
|
|
|
"base_instance_name": &schema.Schema{
|
2015-02-16 17:06:23 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2016-04-10 23:34:15 +02:00
|
|
|
"instance_template": &schema.Schema{
|
2015-02-16 17:06:23 +01:00
|
|
|
Type: schema.TypeString,
|
2016-04-10 23:34:15 +02:00
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
2015-02-16 17:06:23 +01:00
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2016-04-10 23:34:15 +02:00
|
|
|
"zone": &schema.Schema{
|
2015-02-16 17:06:23 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2016-04-10 23:34:15 +02:00
|
|
|
"description": &schema.Schema{
|
2015-02-16 17:06:23 +01:00
|
|
|
Type: schema.TypeString,
|
2016-04-10 23:34:15 +02:00
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
2015-02-16 17:06:23 +01:00
|
|
|
},
|
|
|
|
|
2016-04-10 23:34:15 +02:00
|
|
|
"fingerprint": &schema.Schema{
|
2015-02-16 17:06:23 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2016-04-10 23:34:15 +02:00
|
|
|
"instance_group": &schema.Schema{
|
2015-02-16 17:06:23 +01:00
|
|
|
Type: schema.TypeString,
|
2016-04-10 23:34:15 +02:00
|
|
|
Computed: true,
|
2015-02-16 17:06:23 +01:00
|
|
|
},
|
|
|
|
|
2016-01-10 15:09:05 +01:00
|
|
|
"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,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2016-04-10 23:34:15 +02:00
|
|
|
"project": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"self_link": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2015-11-12 21:44:31 +01:00
|
|
|
"update_strategy": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Default: "RESTART",
|
|
|
|
},
|
|
|
|
|
2015-02-16 17:06:23 +01:00
|
|
|
"target_pools": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
2015-07-30 16:46:16 +02:00
|
|
|
Optional: true,
|
2015-02-16 17:06:23 +01:00
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
2016-02-08 00:51:26 +01:00
|
|
|
Set: schema.HashString,
|
2015-02-16 17:06:23 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
"target_size": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Computed: true,
|
2015-05-17 02:14:38 +02:00
|
|
|
Optional: true,
|
2015-02-16 17:06:23 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-10 15:09:05 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-07-28 02:47:10 +02:00
|
|
|
func resourceComputeInstanceGroupManagerCreate(d *schema.ResourceData, meta interface{}) error {
|
2015-02-16 17:06:23 +01:00
|
|
|
config := meta.(*Config)
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-17 02:14:38 +02:00
|
|
|
// Get group size, default to 1 if not given
|
|
|
|
var target_size int64 = 1
|
|
|
|
if v, ok := d.GetOk("target_size"); ok {
|
|
|
|
target_size = int64(v.(int))
|
2015-02-16 17:06:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build the parameter
|
2015-07-28 02:47:10 +02:00
|
|
|
manager := &compute.InstanceGroupManager{
|
2015-02-16 17:06:23 +01:00
|
|
|
Name: d.Get("name").(string),
|
|
|
|
BaseInstanceName: d.Get("base_instance_name").(string),
|
|
|
|
InstanceTemplate: d.Get("instance_template").(string),
|
2015-08-14 13:06:06 +02:00
|
|
|
TargetSize: target_size,
|
2015-02-16 17:06:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set optional fields
|
|
|
|
if v, ok := d.GetOk("description"); ok {
|
|
|
|
manager.Description = v.(string)
|
|
|
|
}
|
|
|
|
|
2016-01-10 15:09:05 +01:00
|
|
|
if v, ok := d.GetOk("named_port"); ok {
|
|
|
|
manager.NamedPorts = getNamedPorts(v.([]interface{}))
|
|
|
|
}
|
|
|
|
|
2015-02-16 17:06:23 +01:00
|
|
|
if attr := d.Get("target_pools").(*schema.Set); attr.Len() > 0 {
|
|
|
|
var s []string
|
|
|
|
for _, v := range attr.List() {
|
|
|
|
s = append(s, v.(string))
|
|
|
|
}
|
|
|
|
manager.TargetPools = s
|
|
|
|
}
|
|
|
|
|
2015-11-12 21:44:31 +01:00
|
|
|
updateStrategy := d.Get("update_strategy").(string)
|
|
|
|
if !(updateStrategy == "NONE" || updateStrategy == "RESTART") {
|
|
|
|
return fmt.Errorf("Update strategy must be \"NONE\" or \"RESTART\"")
|
|
|
|
}
|
|
|
|
|
2015-02-16 17:06:23 +01:00
|
|
|
log.Printf("[DEBUG] InstanceGroupManager insert request: %#v", manager)
|
2015-07-28 02:47:10 +02:00
|
|
|
op, err := config.clientCompute.InstanceGroupManagers.Insert(
|
2016-04-10 18:59:57 +02:00
|
|
|
project, d.Get("zone").(string), manager).Do()
|
2015-02-16 17:06:23 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating InstanceGroupManager: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// It probably maybe worked, so store the ID now
|
|
|
|
d.SetId(manager.Name)
|
|
|
|
|
|
|
|
// Wait for the operation to complete
|
2016-06-06 19:35:13 +02:00
|
|
|
err = computeOperationWaitZone(config, op, project, d.Get("zone").(string), "Creating InstanceGroupManager")
|
2015-02-16 17:06:23 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-07-28 02:47:10 +02:00
|
|
|
return resourceComputeInstanceGroupManagerRead(d, meta)
|
2015-02-16 17:06:23 +01:00
|
|
|
}
|
|
|
|
|
2015-07-28 02:47:10 +02:00
|
|
|
func resourceComputeInstanceGroupManagerRead(d *schema.ResourceData, meta interface{}) error {
|
2015-02-16 17:06:23 +01:00
|
|
|
config := meta.(*Config)
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-07-28 02:47:10 +02:00
|
|
|
manager, err := config.clientCompute.InstanceGroupManagers.Get(
|
2016-04-10 18:59:57 +02:00
|
|
|
project, d.Get("zone").(string), d.Id()).Do()
|
2015-02-16 17:06:23 +01:00
|
|
|
if err != nil {
|
|
|
|
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
|
2015-11-13 21:36:03 +01:00
|
|
|
log.Printf("[WARN] Removing Instance Group Manager %q because it's gone", d.Get("name").(string))
|
2015-02-16 17:06:23 +01:00
|
|
|
// The resource doesn't exist anymore
|
|
|
|
d.SetId("")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("Error reading instance group manager: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set computed fields
|
2016-01-10 15:09:05 +01:00
|
|
|
d.Set("named_port", manager.NamedPorts)
|
2015-02-16 17:06:23 +01:00
|
|
|
d.Set("fingerprint", manager.Fingerprint)
|
2015-07-28 02:47:10 +02:00
|
|
|
d.Set("instance_group", manager.InstanceGroup)
|
2015-02-16 17:06:23 +01:00
|
|
|
d.Set("target_size", manager.TargetSize)
|
|
|
|
d.Set("self_link", manager.SelfLink)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2015-07-28 02:47:10 +02:00
|
|
|
func resourceComputeInstanceGroupManagerUpdate(d *schema.ResourceData, meta interface{}) error {
|
2015-02-16 17:06:23 +01:00
|
|
|
config := meta.(*Config)
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-02-16 17:06:23 +01:00
|
|
|
d.Partial(true)
|
|
|
|
|
|
|
|
// If target_pools changes then update
|
|
|
|
if d.HasChange("target_pools") {
|
|
|
|
var targetPools []string
|
|
|
|
if attr := d.Get("target_pools").(*schema.Set); attr.Len() > 0 {
|
|
|
|
for _, v := range attr.List() {
|
|
|
|
targetPools = append(targetPools, v.(string))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the parameter
|
2015-07-28 02:47:10 +02:00
|
|
|
setTargetPools := &compute.InstanceGroupManagersSetTargetPoolsRequest{
|
2015-02-16 17:06:23 +01:00
|
|
|
Fingerprint: d.Get("fingerprint").(string),
|
|
|
|
TargetPools: targetPools,
|
|
|
|
}
|
|
|
|
|
2015-07-28 02:47:10 +02:00
|
|
|
op, err := config.clientCompute.InstanceGroupManagers.SetTargetPools(
|
2016-04-10 18:59:57 +02:00
|
|
|
project, d.Get("zone").(string), d.Id(), setTargetPools).Do()
|
2015-02-16 17:06:23 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error updating InstanceGroupManager: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the operation to complete
|
2016-06-06 19:35:13 +02:00
|
|
|
err = computeOperationWaitZone(config, op, project, d.Get("zone").(string), "Updating InstanceGroupManager")
|
2015-02-16 17:06:23 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetPartial("target_pools")
|
|
|
|
}
|
|
|
|
|
|
|
|
// If instance_template changes then update
|
|
|
|
if d.HasChange("instance_template") {
|
|
|
|
// Build the parameter
|
2015-07-28 02:47:10 +02:00
|
|
|
setInstanceTemplate := &compute.InstanceGroupManagersSetInstanceTemplateRequest{
|
2015-02-16 17:06:23 +01:00
|
|
|
InstanceTemplate: d.Get("instance_template").(string),
|
|
|
|
}
|
|
|
|
|
2015-07-28 02:47:10 +02:00
|
|
|
op, err := config.clientCompute.InstanceGroupManagers.SetInstanceTemplate(
|
2016-04-10 18:59:57 +02:00
|
|
|
project, d.Get("zone").(string), d.Id(), setInstanceTemplate).Do()
|
2015-02-16 17:06:23 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error updating InstanceGroupManager: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the operation to complete
|
2016-06-06 19:35:13 +02:00
|
|
|
err = computeOperationWaitZone(config, op, project, d.Get("zone").(string), "Updating InstanceGroupManager")
|
2015-02-16 17:06:23 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-11-12 21:44:31 +01:00
|
|
|
if d.Get("update_strategy").(string) == "RESTART" {
|
|
|
|
managedInstances, err := config.clientCompute.InstanceGroupManagers.ListManagedInstances(
|
2016-04-10 18:59:57 +02:00
|
|
|
project, d.Get("zone").(string), d.Id()).Do()
|
2015-11-12 21:44:31 +01:00
|
|
|
|
|
|
|
managedInstanceCount := len(managedInstances.ManagedInstances)
|
|
|
|
instances := make([]string, managedInstanceCount)
|
|
|
|
for i, v := range managedInstances.ManagedInstances {
|
|
|
|
instances[i] = v.Instance
|
|
|
|
}
|
|
|
|
|
|
|
|
recreateInstances := &compute.InstanceGroupManagersRecreateInstancesRequest{
|
|
|
|
Instances: instances,
|
|
|
|
}
|
|
|
|
|
|
|
|
op, err = config.clientCompute.InstanceGroupManagers.RecreateInstances(
|
2016-04-10 18:59:57 +02:00
|
|
|
project, d.Get("zone").(string), d.Id(), recreateInstances).Do()
|
2015-11-12 21:44:31 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error restarting instance group managers instances: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the operation to complete
|
2016-06-06 19:35:13 +02:00
|
|
|
err = computeOperationWaitZoneTime(config, op, project, d.Get("zone").(string),
|
2015-12-17 00:33:00 +01:00
|
|
|
managedInstanceCount*4, "Restarting InstanceGroupManagers instances")
|
2015-11-12 21:44:31 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-16 17:06:23 +01:00
|
|
|
d.SetPartial("instance_template")
|
|
|
|
}
|
|
|
|
|
2016-01-10 15:09:05 +01:00
|
|
|
// 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(
|
2016-04-10 18:59:57 +02:00
|
|
|
project, d.Get("zone").(string), d.Id(), setNamedPorts).Do()
|
2016-01-10 15:09:05 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error updating InstanceGroupManager: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the operation to complete:
|
2016-06-06 19:35:13 +02:00
|
|
|
err = computeOperationWaitZone(config, op, project, d.Get("zone").(string), "Updating InstanceGroupManager")
|
2016-01-10 15:09:05 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetPartial("named_port")
|
|
|
|
}
|
|
|
|
|
2015-02-16 17:06:23 +01:00
|
|
|
// If size changes trigger a resize
|
2015-05-17 02:14:38 +02:00
|
|
|
if d.HasChange("target_size") {
|
|
|
|
if v, ok := d.GetOk("target_size"); ok {
|
|
|
|
// Only do anything if the new size is set
|
|
|
|
target_size := int64(v.(int))
|
|
|
|
|
2015-07-28 02:47:10 +02:00
|
|
|
op, err := config.clientCompute.InstanceGroupManagers.Resize(
|
2016-04-10 18:59:57 +02:00
|
|
|
project, d.Get("zone").(string), d.Id(), target_size).Do()
|
2015-05-17 02:14:38 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error updating InstanceGroupManager: %s", err)
|
|
|
|
}
|
2015-02-16 17:06:23 +01:00
|
|
|
|
2015-05-17 02:14:38 +02:00
|
|
|
// Wait for the operation to complete
|
2016-06-06 19:35:13 +02:00
|
|
|
err = computeOperationWaitZone(config, op, project, d.Get("zone").(string), "Updating InstanceGroupManager")
|
2015-05-17 02:14:38 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-02-16 17:06:23 +01:00
|
|
|
}
|
|
|
|
|
2015-05-17 02:14:38 +02:00
|
|
|
d.SetPartial("target_size")
|
2015-02-16 17:06:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
d.Partial(false)
|
|
|
|
|
2015-07-28 02:47:10 +02:00
|
|
|
return resourceComputeInstanceGroupManagerRead(d, meta)
|
2015-02-16 17:06:23 +01:00
|
|
|
}
|
|
|
|
|
2015-07-28 02:47:10 +02:00
|
|
|
func resourceComputeInstanceGroupManagerDelete(d *schema.ResourceData, meta interface{}) error {
|
2015-02-16 17:06:23 +01:00
|
|
|
config := meta.(*Config)
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-02-16 17:06:23 +01:00
|
|
|
zone := d.Get("zone").(string)
|
2016-04-10 18:59:57 +02:00
|
|
|
op, err := config.clientCompute.InstanceGroupManagers.Delete(project, zone, d.Id()).Do()
|
2015-02-16 17:06:23 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting instance group manager: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-10-23 21:06:46 +02:00
|
|
|
currentSize := int64(d.Get("target_size").(int))
|
|
|
|
|
2015-02-16 17:06:23 +01:00
|
|
|
// Wait for the operation to complete
|
2016-06-06 19:35:13 +02:00
|
|
|
err = computeOperationWaitZone(config, op, project, d.Get("zone").(string), "Deleting InstanceGroupManager")
|
2015-10-23 21:06:46 +02:00
|
|
|
|
|
|
|
for err != nil && currentSize > 0 {
|
|
|
|
if !strings.Contains(err.Error(), "timeout") {
|
2015-10-29 23:10:44 +01:00
|
|
|
return err
|
2015-10-23 21:06:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
instanceGroup, err := config.clientCompute.InstanceGroups.Get(
|
2016-04-10 18:59:57 +02:00
|
|
|
project, d.Get("zone").(string), d.Id()).Do()
|
2015-10-23 21:06:46 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2015-10-29 23:10:44 +01:00
|
|
|
return fmt.Errorf("Error getting instance group size: %s", err)
|
2015-10-23 21:06:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if instanceGroup.Size >= currentSize {
|
|
|
|
return fmt.Errorf("Error, instance group isn't shrinking during delete")
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[INFO] timeout occured, but instance group is shrinking (%d < %d)", instanceGroup.Size, currentSize)
|
|
|
|
|
|
|
|
currentSize = instanceGroup.Size
|
|
|
|
|
2016-06-06 19:35:13 +02:00
|
|
|
err = computeOperationWaitZone(config, op, project, d.Get("zone").(string), "Deleting InstanceGroupManager")
|
2015-02-16 17:06:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|