diff --git a/builder/openstack/access_config.go b/builder/openstack/access_config.go index a4bab2b35..04e7c854d 100644 --- a/builder/openstack/access_config.go +++ b/builder/openstack/access_config.go @@ -110,6 +110,13 @@ func (c *AccessConfig) computeV2Client() (*gophercloud.ServiceClient, error) { }) } +func (c *AccessConfig) imageV2Client() (*gophercloud.ServiceClient, error) { + return openstack.NewImageServiceV2(c.osClient, gophercloud.EndpointOpts{ + Region: c.Region, + Availability: c.getEndpointType(), + }) +} + func (c *AccessConfig) getEndpointType() gophercloud.Availability { if c.EndpointType == "internal" || c.EndpointType == "internalURL" { return gophercloud.AvailabilityInternal diff --git a/builder/openstack/image_config.go b/builder/openstack/image_config.go index 6a40296b1..4d6f9de4f 100644 --- a/builder/openstack/image_config.go +++ b/builder/openstack/image_config.go @@ -3,13 +3,16 @@ package openstack import ( "fmt" + imageservice "github.com/gophercloud/gophercloud/openstack/imageservice/v2/images" "github.com/mitchellh/packer/template/interpolate" ) // ImageConfig is for common configuration related to creating Images. type ImageConfig struct { - ImageName string `mapstructure:"image_name"` - ImageMetadata map[string]string `mapstructure:"metadata"` + ImageName string `mapstructure:"image_name"` + + ImageMetadata map[string]string `mapstructure:"metadata"` + Visibility imageservice.ImageVisibility `mapstructure:"image_visibility"` } func (c *ImageConfig) Prepare(ctx *interpolate.Context) []error { diff --git a/builder/openstack/step_create_image.go b/builder/openstack/step_create_image.go index c5b12b85e..6f6aef99e 100644 --- a/builder/openstack/step_create_image.go +++ b/builder/openstack/step_create_image.go @@ -8,6 +8,7 @@ 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" ) @@ -53,6 +54,30 @@ 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 }