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-08-19 18:21:32 -04:00
|
|
|
SourceImage string
|
|
|
|
SourceImageName string
|
|
|
|
SourceImageOpts images.ListOpts
|
|
|
|
SourceMostRecent bool
|
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)
|
|
|
|
|
2018-07-20 07:51:00 -04:00
|
|
|
client, err := config.imageV2Client()
|
2018-07-14 00:25:26 -04:00
|
|
|
|
2018-08-19 18:21:32 -04:00
|
|
|
log.Printf("Using Image Filters %v", s.SourceImageOpts)
|
2018-07-14 00:25:26 -04:00
|
|
|
image := &images.Image{}
|
2018-08-19 18:21:32 -04:00
|
|
|
err = images.List(client, s.SourceImageOpts).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 1:
|
|
|
|
*image = i[0]
|
2018-08-21 06:46:42 -04:00
|
|
|
return false, nil
|
2018-07-14 00:25:26 -04:00
|
|
|
default:
|
2018-08-19 18:21:32 -04:00
|
|
|
if s.SourceMostRecent {
|
2018-07-20 18:09:48 -04:00
|
|
|
*image = i[0]
|
2018-08-21 06:46:42 -04:00
|
|
|
return false, nil
|
2018-07-20 18:09:48 -04:00
|
|
|
}
|
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",
|
2018-08-19 18:21:32 -04:00
|
|
|
s.SourceImageOpts)
|
2018-07-14 00:25:26 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-08-21 06:46:42 -04:00
|
|
|
if image.ID == "" {
|
|
|
|
err := fmt.Errorf("No image was found matching filters: %v", s.SourceImageOpts)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
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))
|
|
|
|
|
2018-07-20 07:51:00 -04:00
|
|
|
state.Put("source_image", image.ID)
|
2018-07-14 00:25:26 -04:00
|
|
|
return multistep.ActionContinue
|
2018-07-15 23:35:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepSourceImageInfo) Cleanup(state multistep.StateBag) {
|
|
|
|
// No cleanup required for backout
|
|
|
|
}
|