first attempt at modifying visibility

This commit is contained in:
Matthew Hooker 2016-12-13 15:21:20 -08:00
parent 98051a5207
commit 7bd211cf61
No known key found for this signature in database
GPG Key ID: 7B5F933D9CE8C6A1
3 changed files with 37 additions and 2 deletions

View File

@ -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

View File

@ -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 {

View File

@ -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
}