-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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
provider/google: Support for unmanaged instance groups (google_compute_instance_group) #4087
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
317 changes: 317 additions & 0 deletions
317
builtin/providers/google/resource_compute_instance_group.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,317 @@ | ||
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, | ||
|
||
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, | ||
}, | ||
|
||
"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, | ||
}, | ||
}, | ||
}, | ||
}, | ||
|
||
"instances": &schema.Schema{ | ||
Type: schema.TypeList, | ||
Optional: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
}, | ||
|
||
"network": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"size": &schema.Schema{ | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
|
||
"zone": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"self_link": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
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) | ||
|
||
// 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( | ||
config.Project, d.Get("zone").(string), instanceGroup).Do() | ||
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 | ||
err = computeOperationWaitZone(config, op, d.Get("zone").(string), "Creating InstanceGroup") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if v, ok := d.GetOk("instances"); ok { | ||
instanceUrls := convertStringArr(v.([]interface{})) | ||
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( | ||
config.Project, d.Get("zone").(string), d.Id(), addInstanceReq).Do() | ||
if err != nil { | ||
return fmt.Errorf("Error adding instances to InstanceGroup: %s", err) | ||
} | ||
|
||
// Wait for the operation to complete | ||
err = computeOperationWaitZone(config, op, d.Get("zone").(string), "Adding instances to InstanceGroup") | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return resourceComputeInstanceGroupRead(d, meta) | ||
} | ||
|
||
func resourceComputeInstanceGroupRead(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
|
||
// retreive instance group | ||
instanceGroup, err := config.clientCompute.InstanceGroups.Get( | ||
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 InstanceGroup: %s", err) | ||
} | ||
|
||
// retreive instance group members | ||
var memberUrls []string | ||
members, err := config.clientCompute.InstanceGroups.ListInstances( | ||
config.Project, d.Get("zone").(string), d.Id(), &compute.InstanceGroupsListInstancesRequest{ | ||
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) | ||
|
||
// refresh the state incase referenced instances have been removed earlier in the run | ||
err := resourceComputeInstanceGroupRead(d, meta) | ||
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") | ||
|
||
from := convertStringArr(from_.([]interface{})) | ||
to := convertStringArr(to_.([]interface{})) | ||
|
||
if !validInstanceURLs(from) { | ||
return fmt.Errorf("Error invalid instance URLs: %v", from) | ||
} | ||
if !validInstanceURLs(to) { | ||
return fmt.Errorf("Error invalid instance URLs: %v", from) | ||
} | ||
|
||
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( | ||
config.Project, d.Get("zone").(string), d.Id(), removeReq).Do() | ||
if err != nil { | ||
return fmt.Errorf("Error removing instances from InstanceGroup: %s", err) | ||
} | ||
|
||
// Wait for the operation to complete | ||
err = computeOperationWaitZone(config, removeOp, d.Get("zone").(string), "Updating InstanceGroup") | ||
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( | ||
config.Project, d.Get("zone").(string), d.Id(), addReq).Do() | ||
if err != nil { | ||
return fmt.Errorf("Error adding instances from InstanceGroup: %s", err) | ||
} | ||
|
||
// Wait for the operation to complete | ||
err = computeOperationWaitZone(config, addOp, d.Get("zone").(string), "Updating InstanceGroup") | ||
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( | ||
config.Project, d.Get("zone").(string), d.Id(), namedPortsReq).Do() | ||
if err != nil { | ||
return fmt.Errorf("Error updating named ports for InstanceGroup: %s", err) | ||
} | ||
|
||
err = computeOperationWaitZone(config, op, d.Get("zone").(string), "Updating InstanceGroup") | ||
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) | ||
|
||
zone := d.Get("zone").(string) | ||
op, err := config.clientCompute.InstanceGroups.Delete(config.Project, zone, d.Id()).Do() | ||
if err != nil { | ||
return fmt.Errorf("Error deleting InstanceGroup: %s", err) | ||
} | ||
|
||
err = computeOperationWaitZone(config, op, zone, "Deleting InstanceGroup") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d.SetId("") | ||
return nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the API docs, this is not computed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is strange, the library docs list it as
[Output Only]
see https://godoc.org/google.golang.org/api/compute/v1#InstanceGroupFrom my testing the adding of instances will populate the network into the state. or leave it blank should no instance be defined when it's created.
If you later add instance the network is populated correctly. However the network remains even after all instances are removed, this could be something to change?