2016-02-26 19:41:35 +01:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"google.golang.org/api/compute/v1"
|
|
|
|
"google.golang.org/api/googleapi"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceComputeInstanceGroup() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceComputeInstanceGroupCreate,
|
|
|
|
Read: resourceComputeInstanceGroupRead,
|
|
|
|
Update: resourceComputeInstanceGroupUpdate,
|
|
|
|
Delete: resourceComputeInstanceGroupDelete,
|
|
|
|
|
2017-03-23 01:47:41 +01:00
|
|
|
SchemaVersion: 1,
|
|
|
|
|
2016-02-26 19:41:35 +01:00
|
|
|
Schema: map[string]*schema.Schema{
|
2017-06-02 21:15:10 +02:00
|
|
|
"name": {
|
2016-02-26 19:41:35 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2017-06-02 21:15:10 +02:00
|
|
|
"zone": {
|
2016-04-10 23:34:15 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2017-06-02 21:15:10 +02:00
|
|
|
"description": {
|
2016-02-26 19:41:35 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2017-06-02 21:15:10 +02:00
|
|
|
"instances": {
|
2017-03-23 01:47:41 +01:00
|
|
|
Type: schema.TypeSet,
|
2016-04-10 23:34:15 +02:00
|
|
|
Optional: true,
|
2017-06-02 21:15:10 +02:00
|
|
|
Computed: true,
|
2016-04-10 23:34:15 +02:00
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
2017-03-23 01:47:41 +01:00
|
|
|
Set: schema.HashString,
|
2016-04-10 23:34:15 +02:00
|
|
|
},
|
|
|
|
|
2017-06-02 21:15:10 +02:00
|
|
|
"named_port": {
|
2016-02-26 19:41:35 +01:00
|
|
|
Type: schema.TypeList,
|
|
|
|
Optional: true,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
2017-06-02 21:15:10 +02:00
|
|
|
"name": {
|
2016-02-26 19:41:35 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
2017-06-02 21:15:10 +02:00
|
|
|
"port": {
|
2016-02-26 19:41:35 +01:00
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2017-06-02 21:15:10 +02:00
|
|
|
"network": {
|
2016-02-26 19:41:35 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2017-06-02 21:15:10 +02:00
|
|
|
"project": {
|
2016-02-26 19:41:35 +01:00
|
|
|
Type: schema.TypeString,
|
2016-04-10 23:34:15 +02:00
|
|
|
Optional: true,
|
2016-02-26 19:41:35 +01:00
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2017-06-02 21:15:10 +02:00
|
|
|
"self_link": {
|
2016-02-26 19:41:35 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2016-04-10 18:59:57 +02:00
|
|
|
|
2017-06-02 21:15:10 +02:00
|
|
|
"size": {
|
2016-04-10 23:34:15 +02:00
|
|
|
Type: schema.TypeInt,
|
|
|
|
Computed: true,
|
2016-04-10 18:59:57 +02:00
|
|
|
},
|
2016-02-26 19:41:35 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getInstanceReferences(instanceUrls []string) (refs []*compute.InstanceReference) {
|
|
|
|
for _, v := range instanceUrls {
|
|
|
|
refs = append(refs, &compute.InstanceReference{
|
|
|
|
Instance: v,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return refs
|
|
|
|
}
|
|
|
|
|
|
|
|
func validInstanceURLs(instanceUrls []string) bool {
|
|
|
|
for _, v := range instanceUrls {
|
|
|
|
if !strings.HasPrefix(v, "https://www.googleapis.com/compute/v1/") {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeInstanceGroupCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-02-26 19:41:35 +01:00
|
|
|
// Build the parameter
|
|
|
|
instanceGroup := &compute.InstanceGroup{
|
|
|
|
Name: d.Get("name").(string),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set optional fields
|
|
|
|
if v, ok := d.GetOk("description"); ok {
|
|
|
|
instanceGroup.Description = v.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("named_port"); ok {
|
|
|
|
instanceGroup.NamedPorts = getNamedPorts(v.([]interface{}))
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] InstanceGroup insert request: %#v", instanceGroup)
|
|
|
|
op, err := config.clientCompute.InstanceGroups.Insert(
|
2016-04-10 18:59:57 +02:00
|
|
|
project, d.Get("zone").(string), instanceGroup).Do()
|
2016-02-26 19:41:35 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating InstanceGroup: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// It probably maybe worked, so store the ID now
|
|
|
|
d.SetId(instanceGroup.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 InstanceGroup")
|
2016-02-26 19:41:35 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("instances"); ok {
|
2017-03-23 01:47:41 +01:00
|
|
|
instanceUrls := convertStringArr(v.(*schema.Set).List())
|
2016-02-26 19:41:35 +01:00
|
|
|
if !validInstanceURLs(instanceUrls) {
|
|
|
|
return fmt.Errorf("Error invalid instance URLs: %v", instanceUrls)
|
|
|
|
}
|
|
|
|
|
|
|
|
addInstanceReq := &compute.InstanceGroupsAddInstancesRequest{
|
|
|
|
Instances: getInstanceReferences(instanceUrls),
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] InstanceGroup add instances request: %#v", addInstanceReq)
|
|
|
|
op, err := config.clientCompute.InstanceGroups.AddInstances(
|
2016-04-10 18:59:57 +02:00
|
|
|
project, d.Get("zone").(string), d.Id(), addInstanceReq).Do()
|
2016-02-26 19:41:35 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error adding instances to InstanceGroup: %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), "Adding instances to InstanceGroup")
|
2016-02-26 19:41:35 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resourceComputeInstanceGroupRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeInstanceGroupRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-02-26 19:41:35 +01:00
|
|
|
// retreive instance group
|
|
|
|
instanceGroup, err := config.clientCompute.InstanceGroups.Get(
|
2016-04-10 18:59:57 +02:00
|
|
|
project, d.Get("zone").(string), d.Id()).Do()
|
2016-02-26 19:41:35 +01:00
|
|
|
if err != nil {
|
2017-05-10 01:00:47 +02:00
|
|
|
return handleNotFoundError(err, d, fmt.Sprintf("Instance Group %q", d.Get("name").(string)))
|
2016-02-26 19:41:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// retreive instance group members
|
|
|
|
var memberUrls []string
|
|
|
|
members, err := config.clientCompute.InstanceGroups.ListInstances(
|
2016-04-10 18:59:57 +02:00
|
|
|
project, d.Get("zone").(string), d.Id(), &compute.InstanceGroupsListInstancesRequest{
|
2016-02-26 19:41:35 +01:00
|
|
|
InstanceState: "ALL",
|
|
|
|
}).Do()
|
|
|
|
if err != nil {
|
|
|
|
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
|
|
|
|
// The resource doesn't have any instances
|
|
|
|
d.Set("instances", nil)
|
|
|
|
} else {
|
|
|
|
// any other errors return them
|
|
|
|
return fmt.Errorf("Error reading InstanceGroup Members: %s", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for _, member := range members.Items {
|
|
|
|
memberUrls = append(memberUrls, member.Instance)
|
|
|
|
}
|
|
|
|
log.Printf("[DEBUG] InstanceGroup members: %v", memberUrls)
|
|
|
|
d.Set("instances", memberUrls)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set computed fields
|
|
|
|
d.Set("network", instanceGroup.Network)
|
|
|
|
d.Set("size", instanceGroup.Size)
|
|
|
|
d.Set("self_link", instanceGroup.SelfLink)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func resourceComputeInstanceGroupUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-02-26 19:41:35 +01:00
|
|
|
// refresh the state incase referenced instances have been removed earlier in the run
|
2016-04-10 18:59:57 +02:00
|
|
|
err = resourceComputeInstanceGroupRead(d, meta)
|
2016-02-26 19:41:35 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error reading InstanceGroup: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Partial(true)
|
|
|
|
|
|
|
|
if d.HasChange("instances") {
|
|
|
|
// to-do check for no instances
|
|
|
|
from_, to_ := d.GetChange("instances")
|
|
|
|
|
2017-03-23 01:47:41 +01:00
|
|
|
from := convertStringArr(from_.(*schema.Set).List())
|
|
|
|
to := convertStringArr(to_.(*schema.Set).List())
|
2016-02-26 19:41:35 +01:00
|
|
|
|
|
|
|
if !validInstanceURLs(from) {
|
|
|
|
return fmt.Errorf("Error invalid instance URLs: %v", from)
|
|
|
|
}
|
|
|
|
if !validInstanceURLs(to) {
|
2016-02-27 21:23:40 +01:00
|
|
|
return fmt.Errorf("Error invalid instance URLs: %v", to)
|
2016-02-26 19:41:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
add, remove := calcAddRemove(from, to)
|
|
|
|
|
|
|
|
if len(remove) > 0 {
|
|
|
|
removeReq := &compute.InstanceGroupsRemoveInstancesRequest{
|
|
|
|
Instances: getInstanceReferences(remove),
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] InstanceGroup remove instances request: %#v", removeReq)
|
|
|
|
removeOp, err := config.clientCompute.InstanceGroups.RemoveInstances(
|
2016-04-10 18:59:57 +02:00
|
|
|
project, d.Get("zone").(string), d.Id(), removeReq).Do()
|
2016-02-26 19:41:35 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error removing instances from InstanceGroup: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the operation to complete
|
2016-06-06 19:35:13 +02:00
|
|
|
err = computeOperationWaitZone(config, removeOp, project, d.Get("zone").(string), "Updating InstanceGroup")
|
2016-02-26 19:41:35 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(add) > 0 {
|
|
|
|
|
|
|
|
addReq := &compute.InstanceGroupsAddInstancesRequest{
|
|
|
|
Instances: getInstanceReferences(add),
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] InstanceGroup adding instances request: %#v", addReq)
|
|
|
|
addOp, err := config.clientCompute.InstanceGroups.AddInstances(
|
2016-04-10 18:59:57 +02:00
|
|
|
project, d.Get("zone").(string), d.Id(), addReq).Do()
|
2016-02-26 19:41:35 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error adding instances from InstanceGroup: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the operation to complete
|
2016-06-06 19:35:13 +02:00
|
|
|
err = computeOperationWaitZone(config, addOp, project, d.Get("zone").(string), "Updating InstanceGroup")
|
2016-02-26 19:41:35 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetPartial("instances")
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.HasChange("named_port") {
|
|
|
|
namedPorts := getNamedPorts(d.Get("named_port").([]interface{}))
|
|
|
|
|
|
|
|
namedPortsReq := &compute.InstanceGroupsSetNamedPortsRequest{
|
|
|
|
NamedPorts: namedPorts,
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] InstanceGroup updating named ports request: %#v", namedPortsReq)
|
|
|
|
op, err := config.clientCompute.InstanceGroups.SetNamedPorts(
|
2016-04-10 18:59:57 +02:00
|
|
|
project, d.Get("zone").(string), d.Id(), namedPortsReq).Do()
|
2016-02-26 19:41:35 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error updating named ports for InstanceGroup: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-06-06 19:35:13 +02:00
|
|
|
err = computeOperationWaitZone(config, op, project, d.Get("zone").(string), "Updating InstanceGroup")
|
2016-02-26 19:41:35 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d.SetPartial("named_port")
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Partial(false)
|
|
|
|
|
|
|
|
return resourceComputeInstanceGroupRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeInstanceGroupDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-02-26 19:41:35 +01:00
|
|
|
zone := d.Get("zone").(string)
|
2016-04-10 18:59:57 +02:00
|
|
|
op, err := config.clientCompute.InstanceGroups.Delete(project, zone, d.Id()).Do()
|
2016-02-26 19:41:35 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting InstanceGroup: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-06-06 19:35:13 +02:00
|
|
|
err = computeOperationWaitZone(config, op, project, zone, "Deleting InstanceGroup")
|
2016-02-26 19:41:35 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|