2019-10-14 10:43:59 -04:00
|
|
|
//go:generate mapstructure-to-hcl2 -type Config
|
|
|
|
|
2016-11-13 17:34:36 -05:00
|
|
|
package oneandone
|
|
|
|
|
|
|
|
import (
|
2016-11-15 18:17:30 -05:00
|
|
|
"errors"
|
2018-01-22 20:21:10 -05:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2016-11-13 17:34:36 -05:00
|
|
|
"github.com/1and1/oneandone-cloudserver-sdk-go"
|
2020-11-12 17:44:02 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/common"
|
2020-12-01 18:30:31 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/communicator"
|
2020-11-19 15:07:02 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2020-11-18 13:34:59 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/template/config"
|
2020-11-11 13:21:37 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/template/interpolate"
|
2016-11-13 17:34:36 -05:00
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
|
|
|
Comm communicator.Config `mapstructure:",squash"`
|
|
|
|
|
2016-11-15 18:17:30 -05:00
|
|
|
Token string `mapstructure:"token"`
|
|
|
|
Url string `mapstructure:"url"`
|
|
|
|
SnapshotName string `mapstructure:"image_name"`
|
|
|
|
DataCenterName string `mapstructure:"data_center_name"`
|
|
|
|
DataCenterId string
|
2019-10-14 10:43:59 -04:00
|
|
|
Image string `mapstructure:"source_image_name"`
|
|
|
|
DiskSize int `mapstructure:"disk_size"`
|
|
|
|
Retries int `mapstructure:"retries"`
|
2016-11-15 18:17:30 -05:00
|
|
|
ctx interpolate.Context
|
2016-11-13 17:34:36 -05:00
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
func (c *Config) Prepare(raws ...interface{}) ([]string, error) {
|
2016-11-13 17:34:36 -05:00
|
|
|
|
|
|
|
var md mapstructure.Metadata
|
2019-12-17 05:25:56 -05:00
|
|
|
err := config.Decode(c, &config.DecodeOpts{
|
2016-11-13 17:34:36 -05:00
|
|
|
Metadata: &md,
|
|
|
|
Interpolate: true,
|
|
|
|
InterpolateContext: &c.ctx,
|
|
|
|
InterpolateFilter: &interpolate.RenderFilter{
|
|
|
|
Exclude: []string{
|
|
|
|
"run_command",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, raws...)
|
|
|
|
if err != nil {
|
2019-12-17 05:25:56 -05:00
|
|
|
return nil, err
|
2016-11-13 17:34:36 -05:00
|
|
|
}
|
|
|
|
|
2020-11-19 15:07:02 -05:00
|
|
|
var errs *packersdk.MultiError
|
2016-11-13 17:34:36 -05:00
|
|
|
|
2016-11-15 18:17:30 -05:00
|
|
|
if c.SnapshotName == "" {
|
|
|
|
def, err := interpolate.Render("packer-{{timestamp}}", nil)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default to packer-{{ unix timestamp (utc) }}
|
|
|
|
c.SnapshotName = def
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Image == "" {
|
2020-11-19 15:07:02 -05:00
|
|
|
errs = packersdk.MultiErrorAppend(
|
2016-11-15 18:17:30 -05:00
|
|
|
errs, errors.New("1&1 'image' is required"))
|
|
|
|
}
|
|
|
|
|
2016-11-13 17:34:36 -05:00
|
|
|
if c.Token == "" {
|
|
|
|
c.Token = os.Getenv("ONEANDONE_TOKEN")
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Url == "" {
|
|
|
|
c.Url = oneandone.BaseUrl
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.DiskSize == 0 {
|
|
|
|
c.DiskSize = 50
|
|
|
|
}
|
|
|
|
|
2016-11-15 18:17:30 -05:00
|
|
|
if c.Retries == 0 {
|
|
|
|
c.Retries = 600
|
2016-11-13 17:34:36 -05:00
|
|
|
}
|
|
|
|
|
2016-11-15 18:17:30 -05:00
|
|
|
if c.DataCenterName != "" {
|
|
|
|
token := oneandone.SetToken(c.Token)
|
|
|
|
|
|
|
|
//Create an API client
|
|
|
|
api := oneandone.New(token, c.Url)
|
|
|
|
|
|
|
|
dcs, err := api.ListDatacenters()
|
|
|
|
|
|
|
|
if err != nil {
|
2020-11-19 15:07:02 -05:00
|
|
|
errs = packersdk.MultiErrorAppend(
|
2016-11-15 18:17:30 -05:00
|
|
|
errs, err)
|
|
|
|
}
|
|
|
|
for _, dc := range dcs {
|
2020-08-31 09:44:42 -04:00
|
|
|
if strings.EqualFold(dc.CountryCode, c.DataCenterName) {
|
2016-11-15 18:17:30 -05:00
|
|
|
c.DataCenterId = dc.Id
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2016-11-13 17:34:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if es := c.Comm.Prepare(&c.ctx); len(es) > 0 {
|
2020-11-19 15:07:02 -05:00
|
|
|
errs = packersdk.MultiErrorAppend(errs, es...)
|
2016-11-13 17:34:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if errs != nil && len(errs.Errors) > 0 {
|
2019-12-17 05:25:56 -05:00
|
|
|
return nil, errs
|
2016-11-13 17:34:36 -05:00
|
|
|
}
|
2020-11-19 17:03:11 -05:00
|
|
|
packersdk.LogSecretFilter.Set(c.Token)
|
2019-12-17 05:25:56 -05:00
|
|
|
return nil, nil
|
2016-11-13 17:34:36 -05:00
|
|
|
}
|