2013-08-27 00:57:23 -04:00
|
|
|
package openstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-12-14 19:08:36 -05:00
|
|
|
"strings"
|
2015-05-27 16:02:57 -04:00
|
|
|
|
2016-12-13 18:21:20 -05:00
|
|
|
imageservice "github.com/gophercloud/gophercloud/openstack/imageservice/v2/images"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
2013-08-27 00:57:23 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// ImageConfig is for common configuration related to creating Images.
|
|
|
|
type ImageConfig struct {
|
2016-12-14 19:08:36 -05:00
|
|
|
ImageName string `mapstructure:"image_name"`
|
2016-12-13 19:28:54 -05:00
|
|
|
ImageMetadata map[string]string `mapstructure:"metadata"`
|
|
|
|
ImageVisibility imageservice.ImageVisibility `mapstructure:"image_visibility"`
|
|
|
|
ImageMembers []string `mapstructure:"image_members"`
|
2018-09-13 05:53:54 -04:00
|
|
|
ImageDiskFormat string `mapstructure:"image_disk_format"`
|
2018-11-23 19:27:28 -05:00
|
|
|
ImageTags []string `mapstructure:"image_tags"`
|
2013-08-27 00:57:23 -04:00
|
|
|
}
|
|
|
|
|
2015-05-27 16:02:57 -04:00
|
|
|
func (c *ImageConfig) Prepare(ctx *interpolate.Context) []error {
|
2013-08-27 00:57:23 -04:00
|
|
|
errs := make([]error, 0)
|
|
|
|
if c.ImageName == "" {
|
|
|
|
errs = append(errs, fmt.Errorf("An image_name must be specified"))
|
|
|
|
}
|
|
|
|
|
2016-01-06 17:49:39 -05:00
|
|
|
// By default, OpenStack seems to create the image with an image_type of
|
|
|
|
// "snapshot", since it came from snapshotting a VM. A "snapshot" looks
|
|
|
|
// slightly different in the OpenStack UI and OpenStack won't show "snapshot"
|
|
|
|
// images as a choice in the list of images to boot from for a new instance.
|
2017-04-04 16:39:01 -04:00
|
|
|
// See https://github.com/hashicorp/packer/issues/3038
|
2016-01-06 17:49:39 -05:00
|
|
|
if c.ImageMetadata == nil {
|
|
|
|
c.ImageMetadata = map[string]string{"image_type": "image"}
|
|
|
|
} else if c.ImageMetadata["image_type"] == "" {
|
|
|
|
c.ImageMetadata["image_type"] = "image"
|
|
|
|
}
|
|
|
|
|
2016-12-13 19:28:54 -05:00
|
|
|
// ImageVisibility values
|
|
|
|
// https://wiki.openstack.org/wiki/Glance-v2-community-image-visibility-design
|
|
|
|
if c.ImageVisibility != "" {
|
2016-12-14 19:08:36 -05:00
|
|
|
validVals := []imageservice.ImageVisibility{"public", "private", "shared", "community"}
|
2016-12-14 04:01:57 -05:00
|
|
|
valid := false
|
|
|
|
for _, val := range validVals {
|
2016-12-14 19:08:36 -05:00
|
|
|
if strings.EqualFold(string(c.ImageVisibility), string(val)) {
|
2016-12-14 04:01:57 -05:00
|
|
|
valid = true
|
2016-12-14 19:08:36 -05:00
|
|
|
c.ImageVisibility = val
|
2016-12-13 19:28:54 -05:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2016-12-14 04:01:57 -05:00
|
|
|
if !valid {
|
|
|
|
errs = append(errs, fmt.Errorf("Unknown visibility value %s", c.ImageVisibility))
|
|
|
|
}
|
2016-12-13 19:28:54 -05:00
|
|
|
}
|
|
|
|
|
2013-08-27 00:57:23 -04:00
|
|
|
if len(errs) > 0 {
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|