2018-07-14 00:25:26 -04:00
|
|
|
package openstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-07-15 23:35:02 -04:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2018-07-14 00:25:26 -04:00
|
|
|
|
|
|
|
"github.com/gophercloud/gophercloud/openstack/imageservice/v2/images"
|
|
|
|
"github.com/gophercloud/gophercloud/pagination"
|
2018-07-15 23:35:02 -04:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
2018-07-14 00:25:26 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type StepSourceImageInfo struct {
|
2018-07-15 23:35:02 -04:00
|
|
|
SourceImage string
|
|
|
|
SourceImageName string
|
|
|
|
ImageFilters ImageFilterOptions
|
2018-07-14 00:25:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type ImageFilterOptions struct {
|
2018-07-16 13:00:02 -04:00
|
|
|
Filters map[string]interface{} `mapstructure:"filters"`
|
2018-07-16 13:26:09 -04:00
|
|
|
MostRecent bool `mapstructure:"most_recent"`
|
2018-07-14 00:25:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepSourceImageInfo) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
|
|
|
config := state.Get("config").(Config)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
client, err := config.computeV2Client()
|
|
|
|
|
2018-07-16 13:00:02 -04:00
|
|
|
// if an ID is provided we skip the filter since that will return a single or no image
|
2018-07-14 00:25:26 -04:00
|
|
|
if s.SourceImage != "" {
|
|
|
|
state.Put("source_image", s.SourceImage)
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
params := &images.ListOpts{}
|
|
|
|
|
|
|
|
// build ListOpts from filters
|
|
|
|
if len(s.ImageFilters.Filters) > 0 {
|
|
|
|
err = buildImageFilters(s.ImageFilters.Filters, params)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Errors encountered in filter parsing.\n%s" + err.Error())
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-16 13:00:02 -04:00
|
|
|
// override the "name" provided in filters if "s.SourceImageName" was filled
|
|
|
|
if s.SourceImageName != "" {
|
|
|
|
params.Name = s.SourceImageName
|
|
|
|
}
|
|
|
|
|
|
|
|
// apply "most_recent" logic to the sort fields and allow OpenStack to return the latest qualified image
|
2018-07-14 00:25:26 -04:00
|
|
|
if s.ImageFilters.MostRecent {
|
|
|
|
applyMostRecent(params)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Using Image Filters %v", params)
|
|
|
|
image := &images.Image{}
|
2018-07-15 23:35:02 -04:00
|
|
|
err = images.List(client, params).EachPage(func(page pagination.Page) (bool, error) {
|
2018-07-14 00:25:26 -04:00
|
|
|
i, err := images.ExtractImages(page)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch len(i) {
|
|
|
|
case 0:
|
2018-07-15 23:35:02 -04:00
|
|
|
return false, fmt.Errorf("No image was found matching filters: %v", params)
|
2018-07-14 00:25:26 -04:00
|
|
|
case 1:
|
|
|
|
*image = i[0]
|
|
|
|
return true, nil
|
|
|
|
default:
|
2018-07-15 23:35:02 -04:00
|
|
|
return false, fmt.Errorf(
|
|
|
|
"Your query returned more than one result. Please try a more specific search, or set most_recent to true. Search filters: %v",
|
|
|
|
params)
|
2018-07-14 00:25:26 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error querying image: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Message(fmt.Sprintf("Found Image ID: %s", image.ID))
|
|
|
|
|
|
|
|
state.Put("source_image", image)
|
|
|
|
return multistep.ActionContinue
|
2018-07-15 23:35:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepSourceImageInfo) Cleanup(state multistep.StateBag) {
|
|
|
|
// No cleanup required for backout
|
|
|
|
}
|