update image visibility own step

This commit is contained in:
Matthew Hooker 2016-12-13 16:28:54 -08:00
parent 9d7df95cdf
commit 99667df17e
No known key found for this signature in database
GPG Key ID: 7B5F933D9CE8C6A1
4 changed files with 64 additions and 27 deletions

View File

@ -114,6 +114,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&common.StepProvision{},
&StepStopServer{},
&stepCreateImage{},
&stepUpdateImageVisibility{},
}
// Run!

View File

@ -11,8 +11,9 @@ import (
type ImageConfig struct {
ImageName string `mapstructure:"image_name"`
ImageMetadata map[string]string `mapstructure:"metadata"`
Visibility imageservice.ImageVisibility `mapstructure:"image_visibility"`
ImageMetadata map[string]string `mapstructure:"metadata"`
ImageVisibility imageservice.ImageVisibility `mapstructure:"image_visibility"`
ImageMembers []string `mapstructure:"image_members"`
}
func (c *ImageConfig) Prepare(ctx *interpolate.Context) []error {
@ -32,6 +33,18 @@ func (c *ImageConfig) Prepare(ctx *interpolate.Context) []error {
c.ImageMetadata["image_type"] = "image"
}
// ImageVisibility values
// https://wiki.openstack.org/wiki/Glance-v2-community-image-visibility-design
if c.ImageVisibility != "" {
valid := []string{"public", "private", "shared", "community"}
for _, val := range valid {
if string(c.ImageVisibility) == val {
break
}
}
errs = append(errs, fmt.Errorf("Unknown visibility value %s", c.ImageVisibility))
}
if len(errs) > 0 {
return errs
}

View File

@ -8,7 +8,6 @@ import (
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/compute/v2/images"
"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
imageservice "github.com/gophercloud/gophercloud/openstack/imageservice/v2/images"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
)
@ -54,30 +53,6 @@ func (s *stepCreateImage) Run(state multistep.StateBag) multistep.StepAction {
return multistep.ActionHalt
}
if config.Visibility != "" {
imageClient, err := config.imageV2Client()
if err != nil {
err = fmt.Errorf("Error initializing image service client: %s", err)
state.Put("error", err)
return multistep.ActionHalt
}
r := imageservice.Update(
imageClient,
imageId,
imageservice.UpdateOpts{
imageservice.UpdateVisibility{
Visibility: config.Visibility,
},
},
)
if _, err = r.Extract(); err != nil {
err = fmt.Errorf("Error updating image visibility: %s", err)
state.Put("error", err)
return multistep.ActionHalt
}
}
return multistep.ActionContinue
}

View File

@ -0,0 +1,48 @@
package openstack
import (
"fmt"
imageservice "github.com/gophercloud/gophercloud/openstack/imageservice/v2/images"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
)
type stepUpdateImageVisibility struct{}
func (s *stepUpdateImageVisibility) Run(state multistep.StateBag) multistep.StepAction {
imageId := state.Get("image").(string)
ui := state.Get("ui").(packer.Ui)
config := state.Get("config").(Config)
if config.ImageVisibility != "" {
imageClient, err := config.imageV2Client()
if err != nil {
err = fmt.Errorf("Error initializing image service client: %s", err)
state.Put("error", err)
return multistep.ActionHalt
}
ui.Say(fmt.Sprintf("Updating image visibility to %s", config.ImageVisibility))
r := imageservice.Update(
imageClient,
imageId,
imageservice.UpdateOpts{
imageservice.UpdateVisibility{
Visibility: config.ImageVisibility,
},
},
)
if _, err = r.Extract(); err != nil {
err = fmt.Errorf("Error updating image visibility: %s", err)
state.Put("error", err)
return multistep.ActionHalt
}
}
return multistep.ActionContinue
}
func (s *stepUpdateImageVisibility) Cleanup(multistep.StateBag) {
// No cleanup...
}