provider/openstack: Allow resizing when Flavor Name changes

Previously, resizing would only work if the flavor_id changed and
would create an error if the flavor_name changes. This commit fixes
this behavior.

It also quickly refactors the getFlavorID function to use
Gophercloud's IDFromName function. getFlavorID was the basis of
IDFromName so the exact same code is used.

Fixes #5780
This commit is contained in:
Joe Topjian 2016-04-05 15:09:41 +00:00
parent cd6d7328d4
commit 23b953eea3
1 changed files with 13 additions and 32 deletions

View File

@ -725,12 +725,20 @@ func resourceComputeInstanceV2Update(d *schema.ResourceData, meta interface{}) e
}
if d.HasChange("flavor_id") || d.HasChange("flavor_name") {
flavorId, err := getFlavorID(computeClient, d)
if err != nil {
return err
var newFlavorId string
var err error
if d.HasChange("flavor_id") {
newFlavorId = d.Get("flavor_id").(string)
} else {
newFlavorName := d.Get("flavor_name").(string)
newFlavorId, err = flavors.IDFromName(computeClient, newFlavorName)
if err != nil {
return err
}
}
resizeOpts := &servers.ResizeOpts{
FlavorRef: flavorId,
FlavorRef: newFlavorId,
}
log.Printf("[DEBUG] Resize configuration: %#v", resizeOpts)
err = servers.Resize(computeClient, d.Id(), resizeOpts).ExtractErr()
@ -1258,35 +1266,8 @@ func getFlavorID(client *gophercloud.ServiceClient, d *schema.ResourceData) (str
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 a flavor ID nor a flavor name were able to be determined.")
return flavors.IDFromName(client, flavorName)
}
func resourceComputeVolumeAttachmentHash(v interface{}) int {