diff --git a/builder/openstack/step_create_image.go b/builder/openstack/step_create_image.go index 27dce4a1b..1b8b02b9e 100644 --- a/builder/openstack/step_create_image.go +++ b/builder/openstack/step_create_image.go @@ -45,9 +45,10 @@ func (s *stepCreateImage) Run(ctx context.Context, state multistep.StateBag) mul // Block Storage service volume or regular Compute service local volume. ui.Say(fmt.Sprintf("Creating the image: %s", config.ImageName)) var imageId string + var blockStorageClient *gophercloud.ServiceClient if s.UseBlockStorageVolume { // We need the v3 block storage client. - blockStorageClient, err := config.blockStorageV3Client() + blockStorageClient, err = config.blockStorageV3Client() if err != nil { err = fmt.Errorf("Error initializing block storage client: %s", err) state.Put("error", err) @@ -64,15 +65,6 @@ func (s *stepCreateImage) Run(ctx context.Context, state multistep.StateBag) mul ui.Error(err.Error()) return multistep.ActionHalt } - err = volumeactions.SetImageMetadata(blockStorageClient, volume, volumeactions.ImageMetadataOpts{ - Metadata: config.ImageMetadata, - }).ExtractErr() - if err != nil { - err := fmt.Errorf("Error setting image metadata: %s", err) - state.Put("error", err) - ui.Error(err.Error()) - return multistep.ActionHalt - } imageId = image.ImageID } else { imageId, err = servers.CreateImage(computeClient, server.ID, servers.CreateImageOpts{ @@ -100,6 +92,19 @@ func (s *stepCreateImage) Run(ctx context.Context, state multistep.StateBag) mul return multistep.ActionHalt } + volume := state.Get("volume_id").(string) + if len(config.ImageMetadata) > 0 && s.UseBlockStorageVolume { + err = volumeactions.SetImageMetadata(blockStorageClient, volume, volumeactions.ImageMetadataOpts{ + Metadata: config.ImageMetadata, + }).ExtractErr() + if err != nil { + err := fmt.Errorf("Error setting image metadata: %s", err) + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } + } + return multistep.ActionContinue }