From bad2c9f18d5038c9b6377c916dec2f46cc500479 Mon Sep 17 00:00:00 2001 From: Joe Topjian Date: Tue, 10 Feb 2015 16:58:27 +0000 Subject: [PATCH] Accounting for multiple results of an image name If multiple results are found, an error will be returned to the user. --- .../resource_openstack_compute_instance_v2.go | 27 ++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/builtin/providers/openstack/resource_openstack_compute_instance_v2.go b/builtin/providers/openstack/resource_openstack_compute_instance_v2.go index c56f86792..b4f51cc1d 100644 --- a/builtin/providers/openstack/resource_openstack_compute_instance_v2.go +++ b/builtin/providers/openstack/resource_openstack_compute_instance_v2.go @@ -673,34 +673,43 @@ func getFloatingIPs(networkingClient *gophercloud.ServiceClient) ([]floatingips. } func getImageID(client *gophercloud.ServiceClient, d *schema.ResourceData) (string, error) { - imageID := d.Get("image_id").(string) + imageId := d.Get("image_id").(string) imageName := d.Get("image_name").(string) - if imageID == "" { - pager := images.ListDetail(client, nil) + imageCount := 0 + if imageId == "" && imageName != "" { + pager := images.ListDetail(client, &images.ListOpts{ + Name: imageName, + }) pager.EachPage(func(page pagination.Page) (bool, error) { imageList, err := images.ExtractImages(page) - if err != nil { return false, err } for _, i := range imageList { if i.Name == imageName { - imageID = i.ID + imageCount++ + imageId = i.ID } } return true, nil }) - if imageID == "" { - return "", fmt.Errorf("Unable to find image: %v", imageName) + switch imageCount { + case 0: + return "", fmt.Errorf("Unable to find image: %s", imageName) + case 1: + return imageId, nil + default: + return "", fmt.Errorf("Found %d images matching %s", imageCount, imageName) } } - if imageID == "" && imageName == "" { + if imageId == "" && imageName == "" { return "", fmt.Errorf("Neither an image ID nor an image name were able to be determined.") } - return imageID, nil + return imageId, nil + }