2019-05-31 08:27:41 -04:00
|
|
|
//go:generate struct-markdown
|
|
|
|
|
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"
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
|
2013-08-27 00:57:23 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// ImageConfig is for common configuration related to creating Images.
|
|
|
|
type ImageConfig struct {
|
2019-05-28 11:50:58 -04:00
|
|
|
// The name of the resulting image.
|
2019-06-06 10:29:25 -04:00
|
|
|
ImageName string `mapstructure:"image_name" required:"true"`
|
2019-06-20 09:24:10 -04:00
|
|
|
// Glance metadata that will be applied to the image.
|
2019-06-06 10:29:25 -04:00
|
|
|
ImageMetadata map[string]string `mapstructure:"metadata" required:"false"`
|
2019-06-20 09:24:10 -04:00
|
|
|
// One of "public", "private", "shared", or "community".
|
2019-05-28 11:50:58 -04:00
|
|
|
ImageVisibility imageservice.ImageVisibility `mapstructure:"image_visibility" required:"false"`
|
2019-06-20 09:24:10 -04:00
|
|
|
// List of members to add to the image after creation. An image member is
|
|
|
|
// usually a project (also called the "tenant") with whom the image is
|
|
|
|
// shared.
|
2019-06-06 10:29:25 -04:00
|
|
|
ImageMembers []string `mapstructure:"image_members" required:"false"`
|
2020-03-23 17:07:00 -04:00
|
|
|
// When true, perform the image accept so the members can see the image in their
|
|
|
|
// project. This requires a user with priveleges both in the build project and
|
|
|
|
// in the members provided. Defaults to false.
|
|
|
|
ImageAutoAcceptMembers bool `mapstructure:"image_auto_accept_members" required:"false"`
|
2019-06-20 09:24:10 -04:00
|
|
|
// Disk format of the resulting image. This option works if
|
|
|
|
// use_blockstorage_volume is true.
|
2019-06-06 10:29:25 -04:00
|
|
|
ImageDiskFormat string `mapstructure:"image_disk_format" required:"false"`
|
2019-06-20 09:24:10 -04:00
|
|
|
// List of tags to add to the image after creation.
|
2019-06-06 10:29:25 -04:00
|
|
|
ImageTags []string `mapstructure:"image_tags" required:"false"`
|
2019-08-21 06:28:34 -04:00
|
|
|
// Minimum disk size needed to boot image, in gigabytes.
|
|
|
|
ImageMinDisk int `mapstructure:"image_min_disk" required:"false"`
|
2021-01-19 04:43:18 -05:00
|
|
|
// Skip creating the image. Useful for setting to `true` during a build test stage. Defaults to `false`.
|
|
|
|
SkipCreateImage bool `mapstructure:"skip_create_image" required:"false"`
|
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
|
2019-06-20 09:24:10 -04:00
|
|
|
// 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. 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
|
|
|
}
|
|
|
|
|
2019-02-07 00:54:12 -05:00
|
|
|
if c.ImageMinDisk < 0 {
|
|
|
|
errs = append(errs, fmt.Errorf("An image min disk size must be greater than or equal to 0"))
|
|
|
|
}
|
|
|
|
|
2013-08-27 00:57:23 -04:00
|
|
|
if len(errs) > 0 {
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|