Support for flavor_name
This commit renames flavor_ref to flavor_id and adds the flavor_name parameter. Users can now specify either a flavor ID or name when launching instances.
This commit is contained in:
parent
52102624c6
commit
768292c069
|
@ -58,8 +58,9 @@ func testAccPreCheck(t *testing.T) {
|
|||
}
|
||||
OS_POOL_NAME = v
|
||||
|
||||
v = os.Getenv("OS_FLAVOR_ID")
|
||||
if v == "" {
|
||||
t.Fatal("OS_FLAVOR_ID must be set for acceptance tests")
|
||||
v1 = os.Getenv("OS_FLAVOR_ID")
|
||||
v2 = os.Getenv("OS_FLAVOR_NAME")
|
||||
if v1 == "" && v2 == "" {
|
||||
t.Fatal("OS_FLAVOR_ID or OS_FLAVOR_NAME must be set for acceptance tests")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,6 +13,7 @@ import (
|
|||
"github.com/rackspace/gophercloud/openstack/compute/v2/extensions/bootfromvolume"
|
||||
"github.com/rackspace/gophercloud/openstack/compute/v2/extensions/keypairs"
|
||||
"github.com/rackspace/gophercloud/openstack/compute/v2/extensions/secgroups"
|
||||
"github.com/rackspace/gophercloud/openstack/compute/v2/flavors"
|
||||
"github.com/rackspace/gophercloud/openstack/compute/v2/images"
|
||||
"github.com/rackspace/gophercloud/openstack/compute/v2/servers"
|
||||
"github.com/rackspace/gophercloud/openstack/networking/v2/extensions/layer3/floatingips"
|
||||
|
@ -52,12 +53,20 @@ func resourceComputeInstanceV2() *schema.Resource {
|
|||
ForceNew: true,
|
||||
DefaultFunc: envDefaultFunc("OS_IMAGE_NAME"),
|
||||
},
|
||||
"flavor_ref": &schema.Schema{
|
||||
"flavor_id": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
ForceNew: false,
|
||||
Computed: true,
|
||||
DefaultFunc: envDefaultFunc("OS_FLAVOR_ID"),
|
||||
},
|
||||
"flavor_name": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
ForceNew: false,
|
||||
Computed: true,
|
||||
DefaultFunc: envDefaultFunc("OS_FLAVOR_NAME"),
|
||||
},
|
||||
"floating_ip": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
|
@ -177,10 +186,15 @@ func resourceComputeInstanceV2Create(d *schema.ResourceData, meta interface{}) e
|
|||
return err
|
||||
}
|
||||
|
||||
flavorId, err := getFlavorID(computeClient, d)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
createOpts = &servers.CreateOpts{
|
||||
Name: d.Get("name").(string),
|
||||
ImageRef: imageId,
|
||||
FlavorRef: d.Get("flavor_ref").(string),
|
||||
FlavorRef: flavorId,
|
||||
SecurityGroups: resourceInstanceSecGroupsV2(d),
|
||||
AvailabilityZone: d.Get("availability_zone").(string),
|
||||
Networks: resourceInstanceNetworksV2(d),
|
||||
|
@ -327,11 +341,17 @@ func resourceComputeInstanceV2Read(d *schema.ResourceData, meta interface{}) err
|
|||
})
|
||||
d.Set("security_groups.#", secGrpNum)
|
||||
|
||||
newFlavor, ok := server.Flavor["id"].(string)
|
||||
flavorId, ok := server.Flavor["id"].(string)
|
||||
if !ok {
|
||||
return fmt.Errorf("Error setting OpenStack server's flavor: %v", server.Flavor)
|
||||
}
|
||||
d.Set("flavor_ref", newFlavor)
|
||||
d.Set("flavor_id", flavorId)
|
||||
|
||||
flavor, err := flavors.Get(computeClient, flavorId).Extract()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
d.Set("flavor_name", flavor.Name)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -711,3 +731,41 @@ func getImageID(client *gophercloud.ServiceClient, d *schema.ResourceData) (stri
|
|||
}
|
||||
return "", fmt.Errorf("Neither an image ID nor an image name were able to be determined.")
|
||||
}
|
||||
|
||||
func getFlavorID(client *gophercloud.ServiceClient, d *schema.ResourceData) (string, error) {
|
||||
flavorId := d.Get("flavor_id").(string)
|
||||
|
||||
if flavorId != "" {
|
||||
return flavorId, nil
|
||||
}
|
||||
|
||||
flavorCount := 0
|
||||
flavorName := d.Get("flavor_name").(string)
|
||||
if flavorName != "" {
|
||||
pager := flavors.ListDetail(client, nil)
|
||||
pager.EachPage(func(page pagination.Page) (bool, error) {
|
||||
flavorList, err := flavors.ExtractFlavors(page)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
for _, f := range flavorList {
|
||||
if f.Name == flavorName {
|
||||
flavorCount++
|
||||
flavorId = f.ID
|
||||
}
|
||||
}
|
||||
return true, nil
|
||||
})
|
||||
|
||||
switch flavorCount {
|
||||
case 0:
|
||||
return "", fmt.Errorf("Unable to find flavor: %s", flavorName)
|
||||
case 1:
|
||||
return flavorId, nil
|
||||
default:
|
||||
return "", fmt.Errorf("Found %d flavors matching %s", flavorCount, flavorName)
|
||||
}
|
||||
}
|
||||
return "", fmt.Errorf("Neither an flavor ID nor an flavor name were able to be determined.")
|
||||
}
|
||||
|
|
|
@ -16,7 +16,7 @@ Manages a V2 VM instance resource within OpenStack.
|
|||
resource "openstack_compute_instance_v2" "test-server" {
|
||||
name = "tf-test"
|
||||
image_id = "ad091b52-742f-469e-8f3c-fd81cadf0743"
|
||||
flavor_ref = "3"
|
||||
flavor_id = "3"
|
||||
metadata {
|
||||
this = "that"
|
||||
}
|
||||
|
@ -41,8 +41,11 @@ The following arguments are supported:
|
|||
* `image_name` - (Optional; Required if `image_id` is empty) The name of the
|
||||
desired image for the server. Changing this creates a new server.
|
||||
|
||||
* `flavor_ref` - (Required) The flavor reference (ID) for the desired flavor
|
||||
for the server. Changing this resizes the existing server.
|
||||
* `flavor_id` - (Optional; Required if `flavor_name` is empty) The flavor ID of
|
||||
the desired flavor for the server. Changing this resizes the existing server.
|
||||
|
||||
* `flavor_name` - (Optional; Required if `flavor_id` is empty) The name of the
|
||||
desired flavor for the server. Changing this resizes the existing server.
|
||||
|
||||
* `security_groups` - (Optional) An array of one or more security group names
|
||||
to associate with the server. Changing this results in adding/removing
|
||||
|
|
Loading…
Reference in New Issue