github.com/StackPointCloud/packer@v0.10.2-0.20180716202532-b28098e0f79b/builder/oneandone/config.go (about) 1 package oneandone 2 3 import ( 4 "errors" 5 "os" 6 "strings" 7 8 "github.com/1and1/oneandone-cloudserver-sdk-go" 9 "github.com/hashicorp/packer/common" 10 "github.com/hashicorp/packer/helper/communicator" 11 "github.com/hashicorp/packer/helper/config" 12 "github.com/hashicorp/packer/packer" 13 "github.com/hashicorp/packer/template/interpolate" 14 "github.com/mitchellh/mapstructure" 15 ) 16 17 type Config struct { 18 common.PackerConfig `mapstructure:",squash"` 19 Comm communicator.Config `mapstructure:",squash"` 20 21 Token string `mapstructure:"token"` 22 Url string `mapstructure:"url"` 23 SSHKey string 24 SnapshotName string `mapstructure:"image_name"` 25 DataCenterName string `mapstructure:"data_center_name"` 26 DataCenterId string 27 Image string `mapstructure:"source_image_name"` 28 DiskSize int `mapstructure:"disk_size"` 29 Retries int `mapstructure:"retries"` 30 CommConfig communicator.Config `mapstructure:",squash"` 31 ctx interpolate.Context 32 } 33 34 func NewConfig(raws ...interface{}) (*Config, []string, error) { 35 var c Config 36 37 var md mapstructure.Metadata 38 err := config.Decode(&c, &config.DecodeOpts{ 39 Metadata: &md, 40 Interpolate: true, 41 InterpolateContext: &c.ctx, 42 InterpolateFilter: &interpolate.RenderFilter{ 43 Exclude: []string{ 44 "run_command", 45 }, 46 }, 47 }, raws...) 48 if err != nil { 49 return nil, nil, err 50 } 51 52 var errs *packer.MultiError 53 54 if c.SnapshotName == "" { 55 def, err := interpolate.Render("packer-{{timestamp}}", nil) 56 if err != nil { 57 panic(err) 58 } 59 60 // Default to packer-{{ unix timestamp (utc) }} 61 c.SnapshotName = def 62 } 63 64 if c.Image == "" { 65 errs = packer.MultiErrorAppend( 66 errs, errors.New("1&1 'image' is required")) 67 } 68 69 if c.Token == "" { 70 c.Token = os.Getenv("ONEANDONE_TOKEN") 71 } 72 73 if c.Url == "" { 74 c.Url = oneandone.BaseUrl 75 } 76 77 if c.DiskSize == 0 { 78 c.DiskSize = 50 79 } 80 81 if c.Retries == 0 { 82 c.Retries = 600 83 } 84 85 if c.DataCenterName != "" { 86 token := oneandone.SetToken(c.Token) 87 88 //Create an API client 89 api := oneandone.New(token, c.Url) 90 91 dcs, err := api.ListDatacenters() 92 93 if err != nil { 94 errs = packer.MultiErrorAppend( 95 errs, err) 96 } 97 for _, dc := range dcs { 98 if strings.ToLower(dc.CountryCode) == strings.ToLower(c.DataCenterName) { 99 c.DataCenterId = dc.Id 100 break 101 } 102 } 103 } 104 105 if es := c.Comm.Prepare(&c.ctx); len(es) > 0 { 106 errs = packer.MultiErrorAppend(errs, es...) 107 } 108 109 if errs != nil && len(errs.Errors) > 0 { 110 return nil, nil, errs 111 } 112 common.ScrubConfig(c, c.Token) 113 114 return &c, nil, nil 115 }