Merge pull request #6020 from jtopjian/openstack-resize-flavor-name

provider/openstack: Allow resizing when Flavor Name changes
This commit is contained in:
Joe Topjian 2016-04-07 20:37:25 -06:00
commit b002ce68cd
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") { if d.HasChange("flavor_id") || d.HasChange("flavor_name") {
flavorId, err := getFlavorID(computeClient, d) 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 { if err != nil {
return err return err
} }
}
resizeOpts := &servers.ResizeOpts{ resizeOpts := &servers.ResizeOpts{
FlavorRef: flavorId, FlavorRef: newFlavorId,
} }
log.Printf("[DEBUG] Resize configuration: %#v", resizeOpts) log.Printf("[DEBUG] Resize configuration: %#v", resizeOpts)
err = servers.Resize(computeClient, d.Id(), resizeOpts).ExtractErr() err = servers.Resize(computeClient, d.Id(), resizeOpts).ExtractErr()
@ -1258,35 +1266,8 @@ func getFlavorID(client *gophercloud.ServiceClient, d *schema.ResourceData) (str
return flavorId, nil return flavorId, nil
} }
flavorCount := 0
flavorName := d.Get("flavor_name").(string) flavorName := d.Get("flavor_name").(string)
if flavorName != "" { return flavors.IDFromName(client, 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.")
} }
func resourceComputeVolumeAttachmentHash(v interface{}) int { func resourceComputeVolumeAttachmentHash(v interface{}) int {