terraform/builtin/providers/openstack/resource_openstack_compute_...

453 lines
13 KiB
Go
Raw Normal View History

2014-10-29 22:52:36 +01:00
package openstack
import (
"fmt"
"log"
"time"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/rackspace/gophercloud"
2015-01-04 17:52:49 +01:00
"github.com/rackspace/gophercloud/openstack/compute/v2/extensions/keypairs"
"github.com/rackspace/gophercloud/openstack/compute/v2/extensions/secgroups"
2014-10-29 22:52:36 +01:00
"github.com/rackspace/gophercloud/openstack/compute/v2/servers"
"github.com/rackspace/gophercloud/pagination"
2014-10-29 22:52:36 +01:00
)
2015-01-26 19:09:27 +01:00
func resourceComputeInstanceV2() *schema.Resource {
2014-10-29 22:52:36 +01:00
return &schema.Resource{
2015-01-26 19:09:27 +01:00
Create: resourceComputeInstanceV2Create,
Read: resourceComputeInstanceV2Read,
Update: resourceComputeInstanceV2Update,
Delete: resourceComputeInstanceV2Delete,
2014-10-29 22:52:36 +01:00
Schema: map[string]*schema.Schema{
"region": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
2015-01-27 02:28:01 +01:00
DefaultFunc: envDefaultFunc("OS_REGION_NAME"),
},
2014-10-29 22:52:36 +01:00
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: false,
},
"image_ref": &schema.Schema{
2015-01-27 18:19:11 +01:00
Type: schema.TypeString,
Required: true,
ForceNew: false,
DefaultFunc: envDefaultFunc("OS_IMAGE_ID"),
2014-10-29 22:52:36 +01:00
},
"flavor_ref": &schema.Schema{
2015-01-27 18:19:11 +01:00
Type: schema.TypeString,
Required: true,
ForceNew: false,
DefaultFunc: envDefaultFunc("OS_FLAVOR_ID"),
2014-10-29 22:52:36 +01:00
},
"security_groups": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
ForceNew: false,
Elem: &schema.Schema{Type: schema.TypeString},
Set: func(v interface{}) int {
return hashcode.String(v.(string))
},
},
"availability_zone": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
2015-02-01 01:21:57 +01:00
"network": &schema.Schema{
2014-10-29 22:52:36 +01:00
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"uuid": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"port": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"fixed_ip": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
},
},
},
"metadata": &schema.Schema{
Type: schema.TypeMap,
Optional: true,
2015-01-04 17:52:49 +01:00
ForceNew: false,
2014-10-29 22:52:36 +01:00
},
"config_drive": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
},
"admin_pass": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: false,
},
2014-10-29 22:52:36 +01:00
"access_ip_v4": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: false,
},
"access_ip_v6": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: false,
},
2015-01-04 17:52:49 +01:00
"key_pair": &schema.Schema{
Type: schema.TypeString,
2015-01-04 17:52:49 +01:00
Optional: true,
ForceNew: true,
},
2014-10-29 22:52:36 +01:00
},
}
}
2015-01-26 19:09:27 +01:00
func resourceComputeInstanceV2Create(d *schema.ResourceData, meta interface{}) error {
2014-10-29 22:52:36 +01:00
config := meta.(*Config)
2015-01-31 22:33:54 +01:00
computeClient, err := config.computeV2Client(d.Get("region").(string))
if err != nil {
return fmt.Errorf("Error creating OpenStack compute client: %s", err)
}
2015-01-04 17:52:49 +01:00
var createOpts servers.CreateOptsBuilder
createOpts = &servers.CreateOpts{
Name: d.Get("name").(string),
ImageRef: d.Get("image_ref").(string),
FlavorRef: d.Get("flavor_ref").(string),
2015-01-26 19:09:27 +01:00
SecurityGroups: resourceInstanceSecGroupsV2(d),
2014-10-29 22:52:36 +01:00
AvailabilityZone: d.Get("availability_zone").(string),
2015-01-26 19:09:27 +01:00
Networks: resourceInstanceNetworksV2(d),
Metadata: resourceInstanceMetadataV2(d),
2014-10-29 22:52:36 +01:00
ConfigDrive: d.Get("config_drive").(bool),
AdminPass: d.Get("admin_pass").(string),
2014-10-29 22:52:36 +01:00
}
if keyName, ok := d.Get("key_pair").(string); ok && keyName != "" {
createOpts = &keypairs.CreateOptsExt{
createOpts,
keyName,
2015-01-04 17:52:49 +01:00
}
}
2014-10-29 22:52:36 +01:00
log.Printf("[INFO] Requesting instance creation")
server, err := servers.Create(computeClient, createOpts).Extract()
2014-10-29 22:52:36 +01:00
if err != nil {
return fmt.Errorf("Error creating OpenStack server: %s", err)
}
log.Printf("[INFO] Instance ID: %s", server.ID)
// Store the ID now
d.SetId(server.ID)
// Wait for the instance to become running so we can get some attributes
// that aren't available until later.
log.Printf(
"[DEBUG] Waiting for instance (%s) to become running",
server.ID)
stateConf := &resource.StateChangeConf{
Pending: []string{"BUILD"},
Target: "ACTIVE",
2015-01-26 19:09:27 +01:00
Refresh: ServerV2StateRefreshFunc(computeClient, server.ID),
2014-10-29 22:52:36 +01:00
Timeout: 10 * time.Minute,
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
}
_, err = stateConf.WaitForState()
2014-10-29 22:52:36 +01:00
if err != nil {
return fmt.Errorf(
"Error waiting for instance (%s) to become ready: %s",
server.ID, err)
}
2015-01-26 19:09:27 +01:00
return resourceComputeInstanceV2Read(d, meta)
2014-10-29 22:52:36 +01:00
}
2015-01-26 19:09:27 +01:00
func resourceComputeInstanceV2Read(d *schema.ResourceData, meta interface{}) error {
2014-10-29 22:52:36 +01:00
config := meta.(*Config)
2015-01-31 22:33:54 +01:00
computeClient, err := config.computeV2Client(d.Get("region").(string))
if err != nil {
return fmt.Errorf("Error creating OpenStack compute client: %s", err)
}
2014-10-29 22:52:36 +01:00
server, err := servers.Get(computeClient, d.Id()).Extract()
2014-10-29 22:52:36 +01:00
if err != nil {
return fmt.Errorf("Error retrieving OpenStack server: %s", err)
}
log.Printf("[DEBUG] Retreived Server %s: %+v", d.Id(), server)
2015-01-26 19:58:19 +01:00
d.Set("region", d.Get("region").(string))
2014-10-29 22:52:36 +01:00
d.Set("name", server.Name)
d.Set("access_ip_v4", server.AccessIPv4)
d.Set("access_ip_v6", server.AccessIPv6)
host := server.AccessIPv4
if host == "" {
if publicAddressesRaw, ok := server.Addresses["public"]; ok {
publicAddresses := publicAddressesRaw.([]interface{})
for _, paRaw := range publicAddresses {
pa := paRaw.(map[string]interface{})
if pa["version"].(float64) == 4 {
host = pa["addr"].(string)
2015-01-14 19:58:52 +01:00
d.Set("access_ip_v4", host)
2014-10-29 22:52:36 +01:00
}
}
}
}
2015-01-14 19:58:52 +01:00
d.Set("host", host)
2014-10-29 22:52:36 +01:00
log.Printf("host: %s", host)
// Initialize the connection info
d.SetConnInfo(map[string]string{
"type": "ssh",
"host": host,
})
d.Set("metadata", server.Metadata)
2015-01-27 02:28:01 +01:00
secGrpNum := 0
err = secgroups.ListByServer(computeClient, d.Id()).EachPage(func(page pagination.Page) (bool, error) {
secGrpList, err := secgroups.ExtractSecurityGroups(page)
if err != nil {
2015-01-26 23:45:05 +01:00
return false, fmt.Errorf("Error getting security groups for OpenStack server: %s", err)
}
2015-01-27 02:28:01 +01:00
for _, sg := range secGrpList {
d.Set(fmt.Sprintf("security_groups.%d", secGrpNum), sg.Name)
secGrpNum++
}
return true, nil
})
2015-01-27 02:28:01 +01:00
d.Set("security_groups.#", secGrpNum)
2015-01-05 18:05:25 +01:00
newFlavor, ok := server.Flavor["id"].(string)
if !ok {
2015-01-10 22:59:59 +01:00
return fmt.Errorf("Error setting OpenStack server's flavor: %v", server.Flavor)
2015-01-05 18:05:25 +01:00
}
d.Set("flavor_ref", newFlavor)
2014-10-29 22:52:36 +01:00
return nil
}
2015-01-26 19:09:27 +01:00
func resourceComputeInstanceV2Update(d *schema.ResourceData, meta interface{}) error {
2014-10-29 22:52:36 +01:00
config := meta.(*Config)
2015-01-31 22:33:54 +01:00
computeClient, err := config.computeV2Client(d.Get("region").(string))
if err != nil {
return fmt.Errorf("Error creating OpenStack compute client: %s", err)
}
2014-10-29 22:52:36 +01:00
var updateOpts servers.UpdateOpts
if d.HasChange("name") {
updateOpts.Name = d.Get("name").(string)
}
if d.HasChange("access_ip_v4") {
updateOpts.AccessIPv4 = d.Get("access_ip_v4").(string)
}
if d.HasChange("access_ip_v6") {
updateOpts.AccessIPv4 = d.Get("access_ip_v6").(string)
}
2015-01-26 23:45:05 +01:00
if updateOpts != (servers.UpdateOpts{}) {
_, err := servers.Update(computeClient, d.Id(), updateOpts).Extract()
if err != nil {
return fmt.Errorf("Error updating OpenStack server: %s", err)
}
2015-01-02 20:40:42 +01:00
}
if d.HasChange("metadata") {
var metadataOpts servers.MetadataOpts
metadataOpts = make(servers.MetadataOpts)
newMetadata := d.Get("metadata").(map[string]interface{})
for k, v := range newMetadata {
metadataOpts[k] = v.(string)
}
_, err := servers.UpdateMetadata(computeClient, d.Id(), metadataOpts).Extract()
2015-01-02 20:40:42 +01:00
if err != nil {
return fmt.Errorf("Error updating OpenStack server (%s) metadata: %s", d.Id(), err)
2014-10-29 22:52:36 +01:00
}
}
if d.HasChange("security_groups") {
oldSGRaw, newSGRaw := d.GetChange("security_groups")
oldSGSet, newSGSet := oldSGRaw.(*schema.Set), newSGRaw.(*schema.Set)
secgroupsToAdd := newSGSet.Difference(oldSGSet)
secgroupsToRemove := oldSGSet.Difference(newSGSet)
log.Printf("[DEBUG] Security groups to add: %v", secgroupsToAdd)
log.Printf("[DEBUG] Security groups to remove: %v", secgroupsToRemove)
for _, g := range secgroupsToAdd.List() {
err := secgroups.AddServerToGroup(computeClient, d.Id(), g.(string)).ExtractErr()
if err != nil {
return fmt.Errorf("Error adding security group to OpenStack server (%s): %s", d.Id(), err)
}
log.Printf("[DEBUG] Added security group (%s) to instance (%s)", g.(string), d.Id())
}
for _, g := range secgroupsToRemove.List() {
err := secgroups.RemoveServerFromGroup(computeClient, d.Id(), g.(string)).ExtractErr()
if err != nil {
return fmt.Errorf("Error removing security group from OpenStack server (%s): %s", d.Id(), err)
}
log.Printf("[DEBUG] Removed security group (%s) from instance (%s)", g.(string), d.Id())
}
}
if d.HasChange("admin_pass") {
if newPwd, ok := d.Get("admin_pass").(string); ok {
err := servers.ChangeAdminPassword(computeClient, d.Id(), newPwd).ExtractErr()
if err != nil {
return fmt.Errorf("Error changing admin password of OpenStack server (%s): %s", d.Id(), err)
}
}
}
2015-01-05 18:05:25 +01:00
if d.HasChange("flavor_ref") {
resizeOpts := &servers.ResizeOpts{
FlavorRef: d.Get("flavor_ref").(string),
}
err := servers.Resize(computeClient, d.Id(), resizeOpts).ExtractErr()
2015-01-05 18:05:25 +01:00
if err != nil {
return fmt.Errorf("Error resizing OpenStack server: %s", err)
}
// Wait for the instance to finish resizing.
log.Printf("[DEBUG] Waiting for instance (%s) to finish resizing", d.Id())
stateConf := &resource.StateChangeConf{
Pending: []string{"RESIZE"},
Target: "VERIFY_RESIZE",
2015-01-26 19:09:27 +01:00
Refresh: ServerV2StateRefreshFunc(computeClient, d.Id()),
2015-01-05 18:05:25 +01:00
Timeout: 3 * time.Minute,
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
}
_, err = stateConf.WaitForState()
if err != nil {
return fmt.Errorf("Error waiting for instance (%s) to resize: %s", d.Id(), err)
}
// Confirm resize.
log.Printf("[DEBUG] Confirming resize")
err = servers.ConfirmResize(computeClient, d.Id()).ExtractErr()
2015-01-05 18:05:25 +01:00
if err != nil {
return fmt.Errorf("Error confirming resize of OpenStack server: %s", err)
}
stateConf = &resource.StateChangeConf{
Pending: []string{"VERIFY_RESIZE"},
Target: "ACTIVE",
2015-01-26 19:09:27 +01:00
Refresh: ServerV2StateRefreshFunc(computeClient, d.Id()),
2015-01-05 18:05:25 +01:00
Timeout: 3 * time.Minute,
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
}
_, err = stateConf.WaitForState()
if err != nil {
return fmt.Errorf("Error waiting for instance (%s) to confirm resize: %s", d.Id(), err)
}
}
2015-01-26 19:09:27 +01:00
return resourceComputeInstanceV2Read(d, meta)
2014-10-29 22:52:36 +01:00
}
2015-01-26 19:09:27 +01:00
func resourceComputeInstanceV2Delete(d *schema.ResourceData, meta interface{}) error {
2014-10-29 22:52:36 +01:00
config := meta.(*Config)
2015-01-31 22:33:54 +01:00
computeClient, err := config.computeV2Client(d.Get("region").(string))
if err != nil {
return fmt.Errorf("Error creating OpenStack compute client: %s", err)
}
2014-10-29 22:52:36 +01:00
err = servers.Delete(computeClient, d.Id()).ExtractErr()
2014-10-29 22:52:36 +01:00
if err != nil {
return fmt.Errorf("Error deleting OpenStack server: %s", err)
}
// Wait for the instance to delete before moving on.
2015-01-05 18:05:25 +01:00
log.Printf("[DEBUG] Waiting for instance (%s) to delete", d.Id())
2014-10-29 22:52:36 +01:00
stateConf := &resource.StateChangeConf{
Target: "",
2015-01-26 19:09:27 +01:00
Refresh: ServerV2StateRefreshFunc(computeClient, d.Id()),
2014-10-29 22:52:36 +01:00
Timeout: 10 * time.Minute,
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
}
_, err = stateConf.WaitForState()
if err != nil {
return fmt.Errorf(
"Error waiting for instance (%s) to delete: %s",
d.Id(), err)
}
d.SetId("")
return nil
}
// ServerStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
// an OpenStack instance.
2015-01-26 19:09:27 +01:00
func ServerV2StateRefreshFunc(client *gophercloud.ServiceClient, instanceID string) resource.StateRefreshFunc {
2014-10-29 22:52:36 +01:00
return func() (interface{}, string, error) {
s, err := servers.Get(client, instanceID).Extract()
if err != nil {
return nil, "", err
}
return s, s.Status, nil
}
}
2015-01-26 19:09:27 +01:00
func resourceInstanceSecGroupsV2(d *schema.ResourceData) []string {
rawSecGroups := d.Get("security_groups").(*schema.Set)
secgroups := make([]string, rawSecGroups.Len())
for i, raw := range rawSecGroups.List() {
secgroups[i] = raw.(string)
}
return secgroups
}
2015-01-26 19:09:27 +01:00
func resourceInstanceNetworksV2(d *schema.ResourceData) []servers.Network {
2015-02-01 01:21:57 +01:00
rawNetworks := d.Get("network").([]interface{})
2014-10-29 22:52:36 +01:00
networks := make([]servers.Network, len(rawNetworks))
for i, raw := range rawNetworks {
rawMap := raw.(map[string]interface{})
networks[i] = servers.Network{
UUID: rawMap["uuid"].(string),
Port: rawMap["port"].(string),
FixedIP: rawMap["fixed_ip"].(string),
}
}
return networks
}
2015-01-26 19:09:27 +01:00
func resourceInstanceMetadataV2(d *schema.ResourceData) map[string]string {
2014-10-29 22:52:36 +01:00
m := make(map[string]string)
for key, val := range d.Get("metadata").(map[string]interface{}) {
m[key] = val.(string)
}
return m
}