2016-12-13 19:32:30 -05:00
|
|
|
package openstack
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2016-12-13 19:32:30 -05:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/gophercloud/gophercloud/openstack/imageservice/v2/members"
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2016-12-13 19:32:30 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type stepAddImageMembers struct{}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepAddImageMembers) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2018-09-18 10:05:49 -04:00
|
|
|
config := state.Get("config").(*Config)
|
2016-12-13 19:32:30 -05:00
|
|
|
|
2021-01-19 04:43:18 -05:00
|
|
|
if config.SkipCreateImage {
|
|
|
|
ui.Say("Skipping image add members...")
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
imageId := state.Get("image").(string)
|
|
|
|
|
2016-12-13 19:32:30 -05:00
|
|
|
if len(config.ImageMembers) == 0 {
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
imageClient, err := config.imageV2Client()
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Error initializing image service client: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, member := range config.ImageMembers {
|
|
|
|
ui.Say(fmt.Sprintf("Adding member '%s' to image %s", member, imageId))
|
|
|
|
r := members.Create(imageClient, imageId, member)
|
|
|
|
if _, err = r.Extract(); err != nil {
|
|
|
|
err = fmt.Errorf("Error adding member to image: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-23 17:07:00 -04:00
|
|
|
if config.ImageAutoAcceptMembers {
|
|
|
|
for _, member := range config.ImageMembers {
|
|
|
|
ui.Say(fmt.Sprintf("Accepting image %s for member '%s'", imageId, member))
|
|
|
|
r := members.Update(imageClient, imageId, member, members.UpdateOpts{Status: "accepted"})
|
|
|
|
if _, err = r.Extract(); err != nil {
|
|
|
|
err = fmt.Errorf("Error accepting image for member: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-13 19:32:30 -05:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepAddImageMembers) Cleanup(multistep.StateBag) {
|
|
|
|
// No cleanup...
|
|
|
|
}
|