Accounting for multiple results of an image name
If multiple results are found, an error will be returned to the user.
This commit is contained in:
parent
f51a53000f
commit
bad2c9f18d
|
@ -673,34 +673,43 @@ func getFloatingIPs(networkingClient *gophercloud.ServiceClient) ([]floatingips.
|
||||||
}
|
}
|
||||||
|
|
||||||
func getImageID(client *gophercloud.ServiceClient, d *schema.ResourceData) (string, error) {
|
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)
|
imageName := d.Get("image_name").(string)
|
||||||
if imageID == "" {
|
imageCount := 0
|
||||||
pager := images.ListDetail(client, nil)
|
|
||||||
|
|
||||||
|
if imageId == "" && imageName != "" {
|
||||||
|
pager := images.ListDetail(client, &images.ListOpts{
|
||||||
|
Name: imageName,
|
||||||
|
})
|
||||||
pager.EachPage(func(page pagination.Page) (bool, error) {
|
pager.EachPage(func(page pagination.Page) (bool, error) {
|
||||||
imageList, err := images.ExtractImages(page)
|
imageList, err := images.ExtractImages(page)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, i := range imageList {
|
for _, i := range imageList {
|
||||||
if i.Name == imageName {
|
if i.Name == imageName {
|
||||||
imageID = i.ID
|
imageCount++
|
||||||
|
imageId = i.ID
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true, nil
|
return true, nil
|
||||||
})
|
})
|
||||||
|
|
||||||
if imageID == "" {
|
switch imageCount {
|
||||||
return "", fmt.Errorf("Unable to find image: %v", imageName)
|
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 "", fmt.Errorf("Neither an image ID nor an image name were able to be determined.")
|
||||||
}
|
}
|
||||||
|
|
||||||
return imageID, nil
|
return imageId, nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue