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

     1  package scaleway
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"os"
     7  
     8  	"github.com/hashicorp/packer/common"
     9  	"github.com/hashicorp/packer/common/uuid"
    10  	"github.com/hashicorp/packer/helper/communicator"
    11  	"github.com/hashicorp/packer/helper/config"
    12  	"github.com/hashicorp/packer/helper/useragent"
    13  	"github.com/hashicorp/packer/packer"
    14  	"github.com/hashicorp/packer/template/interpolate"
    15  	"github.com/mitchellh/mapstructure"
    16  )
    17  
    18  type Config struct {
    19  	common.PackerConfig `mapstructure:",squash"`
    20  	Comm                communicator.Config `mapstructure:",squash"`
    21  
    22  	Token        string `mapstructure:"api_token"`
    23  	Organization string `mapstructure:"api_access_key"`
    24  
    25  	Region         string `mapstructure:"region"`
    26  	Image          string `mapstructure:"image"`
    27  	CommercialType string `mapstructure:"commercial_type"`
    28  
    29  	SnapshotName string `mapstructure:"snapshot_name"`
    30  	ImageName    string `mapstructure:"image_name"`
    31  	ServerName   string `mapstructure:"server_name"`
    32  	Bootscript   string `mapstructure:"bootscript"`
    33  
    34  	UserAgent string
    35  	ctx       interpolate.Context
    36  }
    37  
    38  func NewConfig(raws ...interface{}) (*Config, []string, error) {
    39  	c := new(Config)
    40  
    41  	var md mapstructure.Metadata
    42  	err := config.Decode(c, &config.DecodeOpts{
    43  		Metadata:           &md,
    44  		Interpolate:        true,
    45  		InterpolateContext: &c.ctx,
    46  		InterpolateFilter: &interpolate.RenderFilter{
    47  			Exclude: []string{
    48  				"run_command",
    49  			},
    50  		},
    51  	}, raws...)
    52  	if err != nil {
    53  		return nil, nil, err
    54  	}
    55  
    56  	c.UserAgent = useragent.String()
    57  
    58  	if c.Organization == "" {
    59  		c.Organization = os.Getenv("SCALEWAY_API_ACCESS_KEY")
    60  	}
    61  
    62  	if c.Token == "" {
    63  		c.Token = os.Getenv("SCALEWAY_API_TOKEN")
    64  	}
    65  
    66  	if c.SnapshotName == "" {
    67  		def, err := interpolate.Render("snapshot-packer-{{timestamp}}", nil)
    68  		if err != nil {
    69  			panic(err)
    70  		}
    71  
    72  		c.SnapshotName = def
    73  	}
    74  
    75  	if c.ImageName == "" {
    76  		def, err := interpolate.Render("image-packer-{{timestamp}}", nil)
    77  		if err != nil {
    78  			panic(err)
    79  		}
    80  
    81  		c.ImageName = def
    82  	}
    83  
    84  	if c.ServerName == "" {
    85  		// Default to packer-[time-ordered-uuid]
    86  		c.ServerName = fmt.Sprintf("packer-%s", uuid.TimeOrderedUUID())
    87  	}
    88  
    89  	var errs *packer.MultiError
    90  	if es := c.Comm.Prepare(&c.ctx); len(es) > 0 {
    91  		errs = packer.MultiErrorAppend(errs, es...)
    92  	}
    93  	if c.Organization == "" {
    94  		errs = packer.MultiErrorAppend(
    95  			errs, errors.New("Scaleway Organization ID must be specified"))
    96  	}
    97  
    98  	if c.Token == "" {
    99  		errs = packer.MultiErrorAppend(
   100  			errs, errors.New("Scaleway Token must be specified"))
   101  	}
   102  
   103  	if c.Region == "" {
   104  		errs = packer.MultiErrorAppend(
   105  			errs, errors.New("region is required"))
   106  	}
   107  
   108  	if c.CommercialType == "" {
   109  		errs = packer.MultiErrorAppend(
   110  			errs, errors.New("commercial type is required"))
   111  	}
   112  
   113  	if c.Image == "" {
   114  		errs = packer.MultiErrorAppend(
   115  			errs, errors.New("image is required"))
   116  	}
   117  
   118  	if errs != nil && len(errs.Errors) > 0 {
   119  		return nil, nil, errs
   120  	}
   121  
   122  	common.ScrubConfig(c, c.Token)
   123  	return c, nil, nil
   124  }