2019-05-31 08:27:41 -04:00
//go:generate struct-markdown
2017-04-06 05:19:17 -04:00
package scaleway
import (
"errors"
"fmt"
2018-11-09 11:47:22 -05:00
"log"
2017-04-06 10:37:06 -04:00
"os"
2017-04-06 05:19:17 -04:00
"github.com/hashicorp/packer/common"
"github.com/hashicorp/packer/common/uuid"
"github.com/hashicorp/packer/helper/communicator"
"github.com/hashicorp/packer/helper/config"
2018-04-04 22:24:21 -04:00
"github.com/hashicorp/packer/helper/useragent"
2017-04-06 05:19:17 -04:00
"github.com/hashicorp/packer/packer"
"github.com/hashicorp/packer/template/interpolate"
"github.com/mitchellh/mapstructure"
)
type Config struct {
common . PackerConfig ` mapstructure:",squash" `
Comm communicator . Config ` mapstructure:",squash" `
2019-05-28 11:50:58 -04:00
// The token to use to authenticate with your account.
2019-06-06 10:29:25 -04:00
// It can also be specified via environment variable SCALEWAY_API_TOKEN. You
// can see and generate tokens in the "Credentials"
// section of the control panel.
Token string ` mapstructure:"api_token" required:"true" `
2019-05-28 11:50:58 -04:00
// The organization id to use to identify your
2019-06-06 10:29:25 -04:00
// organization. It can also be specified via environment variable
// SCALEWAY_ORGANIZATION. Your organization id is available in the
// "Account" section of the
// control panel.
// Previously named: api_access_key with environment variable: SCALEWAY_API_ACCESS_KEY
2019-05-28 11:50:58 -04:00
Organization string ` mapstructure:"organization_id" required:"true" `
// The name of the region to launch the server in (par1
2019-06-06 10:29:25 -04:00
// or ams1). Consequently, this is the region where the snapshot will be
// available.
Region string ` mapstructure:"region" required:"true" `
2019-05-28 11:50:58 -04:00
// The UUID of the base image to use. This is the image
2019-06-06 10:29:25 -04:00
// that will be used to launch a new server and provision it. See
// the images list
// get the complete list of the accepted image UUID.
Image string ` mapstructure:"image" required:"true" `
2019-05-28 11:50:58 -04:00
// The name of the server commercial type:
2019-06-06 10:29:25 -04:00
// ARM64-128GB, ARM64-16GB, ARM64-2GB, ARM64-32GB, ARM64-4GB,
// ARM64-64GB, ARM64-8GB, C1, C2L, C2M, C2S, START1-L,
// START1-M, START1-S, START1-XS, X64-120GB, X64-15GB, X64-30GB,
// X64-60GB
2019-05-28 11:50:58 -04:00
CommercialType string ` mapstructure:"commercial_type" required:"true" `
// The name of the resulting snapshot that will
2019-06-06 10:29:25 -04:00
// appear in your account. Default packer-TIMESTAMP
2019-05-28 11:50:58 -04:00
SnapshotName string ` mapstructure:"snapshot_name" required:"false" `
// The name of the resulting image that will appear in
2019-06-06 10:29:25 -04:00
// your account. Default packer-TIMESTAMP
ImageName string ` mapstructure:"image_name" required:"false" `
2019-05-28 11:50:58 -04:00
// The name assigned to the server. Default
2019-06-06 10:29:25 -04:00
// packer-UUID
ServerName string ` mapstructure:"server_name" required:"false" `
2019-05-28 11:50:58 -04:00
// The id of an existing bootscript to use when
2019-06-06 10:29:25 -04:00
// booting the server.
Bootscript string ` mapstructure:"bootscript" required:"false" `
2019-05-28 11:50:58 -04:00
// The type of boot, can be either local or
2019-06-06 10:29:25 -04:00
// bootscript, Default bootscript
BootType string ` mapstructure:"boottype" required:"false" `
2017-04-06 05:19:17 -04:00
2019-07-18 16:42:18 -04:00
RemoveVolume bool ` mapstructure:"remove_volume" `
2017-04-06 05:19:17 -04:00
UserAgent string
ctx interpolate . Context
}
func NewConfig ( raws ... interface { } ) ( * Config , [ ] string , error ) {
c := new ( Config )
var md mapstructure . Metadata
err := config . Decode ( c , & config . DecodeOpts {
Metadata : & md ,
Interpolate : true ,
InterpolateContext : & c . ctx ,
InterpolateFilter : & interpolate . RenderFilter {
Exclude : [ ] string {
"run_command" ,
} ,
} ,
} , raws ... )
if err != nil {
return nil , nil , err
}
2018-04-04 22:24:21 -04:00
c . UserAgent = useragent . String ( )
2017-04-06 05:19:17 -04:00
2017-04-06 10:37:06 -04:00
if c . Organization == "" {
2018-11-09 11:47:22 -05:00
if os . Getenv ( "SCALEWAY_ORGANIZATION" ) != "" {
c . Organization = os . Getenv ( "SCALEWAY_ORGANIZATION" )
} else {
log . Printf ( "Deprecation warning: Use SCALEWAY_ORGANIZATION environment variable and organization_id argument instead of api_access_key argument and SCALEWAY_API_ACCESS_KEY environment variable." )
c . Organization = os . Getenv ( "SCALEWAY_API_ACCESS_KEY" )
}
2017-04-06 10:37:06 -04:00
}
if c . Token == "" {
c . Token = os . Getenv ( "SCALEWAY_API_TOKEN" )
}
2017-04-06 05:19:17 -04:00
if c . SnapshotName == "" {
2017-04-11 06:19:28 -04:00
def , err := interpolate . Render ( "snapshot-packer-{{timestamp}}" , nil )
2017-04-06 05:19:17 -04:00
if err != nil {
panic ( err )
}
c . SnapshotName = def
}
2017-04-11 06:19:28 -04:00
if c . ImageName == "" {
def , err := interpolate . Render ( "image-packer-{{timestamp}}" , nil )
if err != nil {
panic ( err )
}
c . ImageName = def
}
2017-04-06 05:19:17 -04:00
if c . ServerName == "" {
// Default to packer-[time-ordered-uuid]
c . ServerName = fmt . Sprintf ( "packer-%s" , uuid . TimeOrderedUUID ( ) )
}
2018-09-27 10:27:33 -04:00
if c . BootType == "" {
c . BootType = "bootscript"
}
2017-04-06 05:19:17 -04:00
var errs * packer . MultiError
if es := c . Comm . Prepare ( & c . ctx ) ; len ( es ) > 0 {
errs = packer . MultiErrorAppend ( errs , es ... )
}
if c . Organization == "" {
errs = packer . MultiErrorAppend (
errs , errors . New ( "Scaleway Organization ID must be specified" ) )
}
if c . Token == "" {
errs = packer . MultiErrorAppend (
errs , errors . New ( "Scaleway Token must be specified" ) )
}
if c . Region == "" {
errs = packer . MultiErrorAppend (
errs , errors . New ( "region is required" ) )
}
if c . CommercialType == "" {
errs = packer . MultiErrorAppend (
errs , errors . New ( "commercial type is required" ) )
}
if c . Image == "" {
errs = packer . MultiErrorAppend (
errs , errors . New ( "image is required" ) )
}
if errs != nil && len ( errs . Errors ) > 0 {
return nil , nil , errs
}
2018-08-10 17:25:14 -04:00
packer . LogSecretFilter . Set ( c . Token )
2017-04-06 05:19:17 -04:00
return c , nil , nil
}