2015-02-16 17:06:23 +01:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
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/hashcode"
|
|
|
|
"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{
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"description": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"base_instance_name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"fingerprint": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2015-07-28 02:47:10 +02:00
|
|
|
"instance_group": &schema.Schema{
|
2015-02-16 17:06:23 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"instance_template": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"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},
|
|
|
|
Set: func(v interface{}) int {
|
|
|
|
return hashcode.String(v.(string))
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
"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
|
|
|
},
|
|
|
|
|
|
|
|
"zone": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"self_link": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-28 02:47:10 +02:00
|
|
|
func waitOpZone(config *Config, op *compute.Operation, zone string,
|
|
|
|
resource string, action string) (*compute.Operation, error) {
|
2015-02-16 17:06:23 +01:00
|
|
|
|
2015-07-28 02:47:10 +02:00
|
|
|
w := &OperationWaiter{
|
|
|
|
Service: config.clientCompute,
|
2015-02-16 17:06:23 +01:00
|
|
|
Op: op,
|
|
|
|
Project: config.Project,
|
|
|
|
Zone: zone,
|
2015-07-28 02:47:10 +02:00
|
|
|
Type: OperationWaitZone,
|
2015-02-16 17:06:23 +01:00
|
|
|
}
|
|
|
|
state := w.Conf()
|
2015-07-28 20:09:43 +02:00
|
|
|
state.Timeout = 8 * time.Minute
|
2015-02-16 17:06:23 +01:00
|
|
|
state.MinTimeout = 1 * time.Second
|
|
|
|
opRaw, err := state.WaitForState()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error waiting for %s to %s: %s", resource, action, err)
|
|
|
|
}
|
2015-07-28 02:47:10 +02:00
|
|
|
return opRaw.(*compute.Operation), nil
|
2015-02-16 17:06:23 +01:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] InstanceGroupManager insert request: %#v", manager)
|
2015-07-28 02:47:10 +02:00
|
|
|
op, err := config.clientCompute.InstanceGroupManagers.Insert(
|
|
|
|
config.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
|
|
|
|
op, err = waitOpZone(config, op, d.Get("zone").(string), "InstanceGroupManager", "create")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if op.Error != nil {
|
|
|
|
// The resource didn't actually create
|
|
|
|
d.SetId("")
|
|
|
|
// Return the error
|
2015-07-28 02:47:10 +02:00
|
|
|
return OperationError(*op.Error)
|
2015-02-16 17:06:23 +01:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
2015-07-28 02:47:10 +02:00
|
|
|
manager, err := config.clientCompute.InstanceGroupManagers.Get(
|
2015-02-16 17:06:23 +01:00
|
|
|
config.Project, d.Get("zone").(string), d.Id()).Do()
|
|
|
|
if err != nil {
|
|
|
|
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
|
|
|
|
// The resource doesn't exist anymore
|
|
|
|
d.SetId("")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("Error reading instance group manager: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set computed fields
|
|
|
|
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)
|
|
|
|
|
|
|
|
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(
|
2015-02-16 17:06:23 +01:00
|
|
|
config.Project, d.Get("zone").(string), d.Id(), setTargetPools).Do()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error updating InstanceGroupManager: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the operation to complete
|
|
|
|
op, err = waitOpZone(config, op, d.Get("zone").(string), "InstanceGroupManager", "update TargetPools")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if op.Error != nil {
|
2015-07-28 02:47:10 +02:00
|
|
|
return OperationError(*op.Error)
|
2015-02-16 17:06:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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(
|
2015-02-16 17:06:23 +01:00
|
|
|
config.Project, d.Get("zone").(string), d.Id(), setInstanceTemplate).Do()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error updating InstanceGroupManager: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the operation to complete
|
|
|
|
op, err = waitOpZone(config, op, d.Get("zone").(string), "InstanceGroupManager", "update instance template")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if op.Error != nil {
|
2015-07-28 02:47:10 +02:00
|
|
|
return OperationError(*op.Error)
|
2015-02-16 17:06:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
d.SetPartial("instance_template")
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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(
|
2015-05-17 02:14:38 +02:00
|
|
|
config.Project, d.Get("zone").(string), d.Id(), target_size).Do()
|
|
|
|
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
|
|
|
|
op, err = waitOpZone(config, op, d.Get("zone").(string), "InstanceGroupManager", "update target_size")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if op.Error != nil {
|
2015-07-28 02:47:10 +02:00
|
|
|
return OperationError(*op.Error)
|
2015-05-17 02:14:38 +02:00
|
|
|
}
|
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)
|
|
|
|
|
|
|
|
zone := d.Get("zone").(string)
|
2015-07-28 02:47:10 +02:00
|
|
|
op, err := config.clientCompute.InstanceGroupManagers.Delete(config.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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the operation to complete
|
2015-07-28 02:47:10 +02:00
|
|
|
op, err = waitOpZone(config, op, d.Get("zone").(string), "InstanceGroupManager", "delete")
|
2015-02-16 17:06:23 +01:00
|
|
|
if err != nil {
|
2015-07-28 02:47:10 +02:00
|
|
|
return err
|
2015-02-16 17:06:23 +01:00
|
|
|
}
|
|
|
|
if op.Error != nil {
|
|
|
|
// The resource didn't actually create
|
|
|
|
d.SetId("")
|
|
|
|
|
|
|
|
// Return the error
|
2015-07-28 02:47:10 +02:00
|
|
|
return OperationError(*op.Error)
|
2015-02-16 17:06:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|