github.phpd.cn/hashicorp/packer@v1.3.2/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  	ImageDiskFormat string                       `mapstructure:"image_disk_format"`
    18  }
    19  
    20  func (c *ImageConfig) Prepare(ctx *interpolate.Context) []error {
    21  	errs := make([]error, 0)
    22  	if c.ImageName == "" {
    23  		errs = append(errs, fmt.Errorf("An image_name must be specified"))
    24  	}
    25  
    26  	// By default, OpenStack seems to create the image with an image_type of
    27  	// "snapshot", since it came from snapshotting a VM. A "snapshot" looks
    28  	// slightly different in the OpenStack UI and OpenStack won't show "snapshot"
    29  	// images as a choice in the list of images to boot from for a new instance.
    30  	// See https://github.com/hashicorp/packer/issues/3038
    31  	if c.ImageMetadata == nil {
    32  		c.ImageMetadata = map[string]string{"image_type": "image"}
    33  	} else if c.ImageMetadata["image_type"] == "" {
    34  		c.ImageMetadata["image_type"] = "image"
    35  	}
    36  
    37  	// ImageVisibility values
    38  	// https://wiki.openstack.org/wiki/Glance-v2-community-image-visibility-design
    39  	if c.ImageVisibility != "" {
    40  		validVals := []imageservice.ImageVisibility{"public", "private", "shared", "community"}
    41  		valid := false
    42  		for _, val := range validVals {
    43  			if strings.EqualFold(string(c.ImageVisibility), string(val)) {
    44  				valid = true
    45  				c.ImageVisibility = val
    46  				break
    47  			}
    48  		}
    49  		if !valid {
    50  			errs = append(errs, fmt.Errorf("Unknown visibility value %s", c.ImageVisibility))
    51  		}
    52  	}
    53  
    54  	if len(errs) > 0 {
    55  		return errs
    56  	}
    57  
    58  	return nil
    59  }