github.phpd.cn/hashicorp/packer@v1.3.2/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  	SnapshotName   string `mapstructure:"image_name"`
    24  	DataCenterName string `mapstructure:"data_center_name"`
    25  	DataCenterId   string
    26  	Image          string              `mapstructure:"source_image_name"`
    27  	DiskSize       int                 `mapstructure:"disk_size"`
    28  	Retries        int                 `mapstructure:"retries"`
    29  	CommConfig     communicator.Config `mapstructure:",squash"`
    30  	ctx            interpolate.Context
    31  }
    32  
    33  func NewConfig(raws ...interface{}) (*Config, []string, error) {
    34  	var c Config
    35  
    36  	var md mapstructure.Metadata
    37  	err := config.Decode(&c, &config.DecodeOpts{
    38  		Metadata:           &md,
    39  		Interpolate:        true,
    40  		InterpolateContext: &c.ctx,
    41  		InterpolateFilter: &interpolate.RenderFilter{
    42  			Exclude: []string{
    43  				"run_command",
    44  			},
    45  		},
    46  	}, raws...)
    47  	if err != nil {
    48  		return nil, nil, err
    49  	}
    50  
    51  	var errs *packer.MultiError
    52  
    53  	if c.SnapshotName == "" {
    54  		def, err := interpolate.Render("packer-{{timestamp}}", nil)
    55  		if err != nil {
    56  			panic(err)
    57  		}
    58  
    59  		// Default to packer-{{ unix timestamp (utc) }}
    60  		c.SnapshotName = def
    61  	}
    62  
    63  	if c.Image == "" {
    64  		errs = packer.MultiErrorAppend(
    65  			errs, errors.New("1&1 'image' is required"))
    66  	}
    67  
    68  	if c.Token == "" {
    69  		c.Token = os.Getenv("ONEANDONE_TOKEN")
    70  	}
    71  
    72  	if c.Url == "" {
    73  		c.Url = oneandone.BaseUrl
    74  	}
    75  
    76  	if c.DiskSize == 0 {
    77  		c.DiskSize = 50
    78  	}
    79  
    80  	if c.Retries == 0 {
    81  		c.Retries = 600
    82  	}
    83  
    84  	if c.DataCenterName != "" {
    85  		token := oneandone.SetToken(c.Token)
    86  
    87  		//Create an API client
    88  		api := oneandone.New(token, c.Url)
    89  
    90  		dcs, err := api.ListDatacenters()
    91  
    92  		if err != nil {
    93  			errs = packer.MultiErrorAppend(
    94  				errs, err)
    95  		}
    96  		for _, dc := range dcs {
    97  			if strings.ToLower(dc.CountryCode) == strings.ToLower(c.DataCenterName) {
    98  				c.DataCenterId = dc.Id
    99  				break
   100  			}
   101  		}
   102  	}
   103  
   104  	if es := c.Comm.Prepare(&c.ctx); len(es) > 0 {
   105  		errs = packer.MultiErrorAppend(errs, es...)
   106  	}
   107  
   108  	if errs != nil && len(errs.Errors) > 0 {
   109  		return nil, nil, errs
   110  	}
   111  	packer.LogSecretFilter.Set(c.Token)
   112  	return &c, nil, nil
   113  }