github.phpd.cn/hashicorp/packer@v1.3.2/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 BootType string `mapstructure:"boottype"` 34 35 UserAgent string 36 ctx interpolate.Context 37 } 38 39 func NewConfig(raws ...interface{}) (*Config, []string, error) { 40 c := new(Config) 41 42 var md mapstructure.Metadata 43 err := config.Decode(c, &config.DecodeOpts{ 44 Metadata: &md, 45 Interpolate: true, 46 InterpolateContext: &c.ctx, 47 InterpolateFilter: &interpolate.RenderFilter{ 48 Exclude: []string{ 49 "run_command", 50 }, 51 }, 52 }, raws...) 53 if err != nil { 54 return nil, nil, err 55 } 56 57 c.UserAgent = useragent.String() 58 59 if c.Organization == "" { 60 c.Organization = os.Getenv("SCALEWAY_API_ACCESS_KEY") 61 } 62 63 if c.Token == "" { 64 c.Token = os.Getenv("SCALEWAY_API_TOKEN") 65 } 66 67 if c.SnapshotName == "" { 68 def, err := interpolate.Render("snapshot-packer-{{timestamp}}", nil) 69 if err != nil { 70 panic(err) 71 } 72 73 c.SnapshotName = def 74 } 75 76 if c.ImageName == "" { 77 def, err := interpolate.Render("image-packer-{{timestamp}}", nil) 78 if err != nil { 79 panic(err) 80 } 81 82 c.ImageName = def 83 } 84 85 if c.ServerName == "" { 86 // Default to packer-[time-ordered-uuid] 87 c.ServerName = fmt.Sprintf("packer-%s", uuid.TimeOrderedUUID()) 88 } 89 90 if c.BootType == "" { 91 c.BootType = "bootscript" 92 } 93 94 var errs *packer.MultiError 95 if es := c.Comm.Prepare(&c.ctx); len(es) > 0 { 96 errs = packer.MultiErrorAppend(errs, es...) 97 } 98 if c.Organization == "" { 99 errs = packer.MultiErrorAppend( 100 errs, errors.New("Scaleway Organization ID must be specified")) 101 } 102 103 if c.Token == "" { 104 errs = packer.MultiErrorAppend( 105 errs, errors.New("Scaleway Token must be specified")) 106 } 107 108 if c.Region == "" { 109 errs = packer.MultiErrorAppend( 110 errs, errors.New("region is required")) 111 } 112 113 if c.CommercialType == "" { 114 errs = packer.MultiErrorAppend( 115 errs, errors.New("commercial type is required")) 116 } 117 118 if c.Image == "" { 119 errs = packer.MultiErrorAppend( 120 errs, errors.New("image is required")) 121 } 122 123 if errs != nil && len(errs.Errors) > 0 { 124 return nil, nil, errs 125 } 126 127 packer.LogSecretFilter.Set(c.Token) 128 return c, nil, nil 129 }