From 23b953eea3985f962b65d8a285cdf4a023cd7619 Mon Sep 17 00:00:00 2001 From: Joe Topjian Date: Tue, 5 Apr 2016 15:09:41 +0000 Subject: [PATCH] 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 --- .../resource_openstack_compute_instance_v2.go | 45 ++++++------------- 1 file changed, 13 insertions(+), 32 deletions(-) diff --git a/builtin/providers/openstack/resource_openstack_compute_instance_v2.go b/builtin/providers/openstack/resource_openstack_compute_instance_v2.go index 2b621c92d..b863c5114 100644 --- a/builtin/providers/openstack/resource_openstack_compute_instance_v2.go +++ b/builtin/providers/openstack/resource_openstack_compute_instance_v2.go @@ -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 {