github.com/StackPointCloud/packer@v0.10.2-0.20180716202532-b28098e0f79b/builder/openstack/image_config.go (about)

     1  package openstack
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	imageservice "github.com/gophercloud/gophercloud/openstack/imageservice/v2/images"
     8  	"github.com/hashicorp/packer/template/interpolate"
     9  )
    10  
    11  // ImageConfig is for common configuration related to creating Images.
    12  type ImageConfig struct {
    13  	ImageName       string                       `mapstructure:"image_name"`
    14  	ImageMetadata   map[string]string            `mapstructure:"metadata"`
    15  	ImageVisibility imageservice.ImageVisibility `mapstructure:"image_visibility"`
    16  	ImageMembers    []string                     `mapstructure:"image_members"`
    17  }
    18  
    19  func (c *ImageConfig) Prepare(ctx *interpolate.Context) []error {
    20  	errs := make([]error, 0)
    21  	if c.ImageName == "" {
    22  		errs = append(errs, fmt.Errorf("An image_name must be specified"))
    23  	}
    24  
    25  	// By default, OpenStack seems to create the image with an image_type of
    26  	// "snapshot", since it came from snapshotting a VM. A "snapshot" looks
    27  	// slightly different in the OpenStack UI and OpenStack won't show "snapshot"
    28  	// images as a choice in the list of images to boot from for a new instance.
    29  	// See https://github.com/hashicorp/packer/issues/3038
    30  	if c.ImageMetadata == nil {
    31  		c.ImageMetadata = map[string]string{"image_type": "image"}
    32  	} else if c.ImageMetadata["image_type"] == "" {
    33  		c.ImageMetadata["image_type"] = "image"
    34  	}
    35  
    36  	// ImageVisibility values
    37  	// https://wiki.openstack.org/wiki/Glance-v2-community-image-visibility-design
    38  	if c.ImageVisibility != "" {
    39  		validVals := []imageservice.ImageVisibility{"public", "private", "shared", "community"}
    40  		valid := false
    41  		for _, val := range validVals {
    42  			if strings.EqualFold(string(c.ImageVisibility), string(val)) {
    43  				valid = true
    44  				c.ImageVisibility = val
    45  				break
    46  			}
    47  		}
    48  		if !valid {
    49  			errs = append(errs, fmt.Errorf("Unknown visibility value %s", c.ImageVisibility))
    50  		}
    51  	}
    52  
    53  	if len(errs) > 0 {
    54  		return errs
    55  	}
    56  
    57  	return nil
    58  }