2019-09-25 07:27:24 -04:00
|
|
|
//go:generate struct-markdown
|
2019-10-14 10:43:59 -04:00
|
|
|
//go:generate mapstructure-to-hcl2 -type Config
|
|
|
|
|
2019-03-26 08:29:15 -04:00
|
|
|
package yandex
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/common"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/communicator"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/template/config"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
|
2019-03-26 08:29:15 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
|
|
|
Communicator communicator.Config `mapstructure:",squash"`
|
2020-08-31 09:29:20 -04:00
|
|
|
AccessConfig `mapstructure:",squash"`
|
2019-03-26 08:29:15 -04:00
|
|
|
|
2020-11-30 05:27:45 -05:00
|
|
|
CommonConfig `mapstructure:",squash"`
|
|
|
|
ImageConfig `mapstructure:",squash"`
|
|
|
|
|
2021-01-15 04:51:44 -05:00
|
|
|
SourceImageConfig `mapstructure:",squash"`
|
2020-08-31 09:29:20 -04:00
|
|
|
// Service account identifier to assign to instance.
|
2020-02-10 10:36:19 -05:00
|
|
|
ServiceAccountID string `mapstructure:"service_account_id" required:"false"`
|
2020-11-30 05:27:45 -05:00
|
|
|
|
2020-04-20 09:20:05 -04:00
|
|
|
// The ID of the folder to save built image in.
|
|
|
|
// This defaults to value of 'folder_id'.
|
|
|
|
TargetImageFolderID string `mapstructure:"target_image_folder_id" required:"false"`
|
2019-03-26 08:29:15 -04:00
|
|
|
|
2019-06-06 10:29:25 -04:00
|
|
|
ctx interpolate.Context
|
2019-03-26 08:29:15 -04:00
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
func (c *Config) Prepare(raws ...interface{}) ([]string, error) {
|
2019-03-26 08:29:15 -04:00
|
|
|
c.ctx.Funcs = TemplateFuncs
|
|
|
|
err := config.Decode(c, &config.DecodeOpts{
|
|
|
|
Interpolate: true,
|
|
|
|
InterpolateContext: &c.ctx,
|
|
|
|
}, raws...)
|
|
|
|
if err != nil {
|
2019-12-17 05:25:56 -05:00
|
|
|
return nil, err
|
2019-03-26 08:29:15 -04:00
|
|
|
}
|
|
|
|
|
2020-08-31 09:29:20 -04:00
|
|
|
// Accumulate any errors
|
2020-11-19 15:07:02 -05:00
|
|
|
var errs *packersdk.MultiError
|
2019-03-26 08:29:15 -04:00
|
|
|
|
2020-11-19 15:07:02 -05:00
|
|
|
errs = packersdk.MultiErrorAppend(errs, c.AccessConfig.Prepare(&c.ctx)...)
|
2020-08-31 09:29:20 -04:00
|
|
|
|
2020-11-30 05:27:45 -05:00
|
|
|
errs = c.CommonConfig.Prepare(errs)
|
|
|
|
errs = c.ImageConfig.Prepare(errs)
|
2021-01-15 04:51:44 -05:00
|
|
|
errs = c.SourceImageConfig.Prepare(errs)
|
2019-03-26 08:29:15 -04:00
|
|
|
|
2020-07-16 10:29:45 -04:00
|
|
|
if c.ImageMinDiskSizeGb == 0 {
|
|
|
|
c.ImageMinDiskSizeGb = c.DiskSizeGb
|
2020-07-16 10:10:22 -04:00
|
|
|
}
|
|
|
|
|
2020-07-16 10:29:45 -04:00
|
|
|
if c.ImageMinDiskSizeGb < c.DiskSizeGb {
|
2020-11-19 15:07:02 -05:00
|
|
|
errs = packersdk.MultiErrorAppend(errs,
|
2020-07-16 10:10:22 -04:00
|
|
|
fmt.Errorf("Invalid image_min_disk_size value (%d): Must be equal or greate than disk_size_gb (%d)",
|
2020-07-16 10:29:45 -04:00
|
|
|
c.ImageMinDiskSizeGb, c.DiskSizeGb))
|
2020-07-16 10:10:22 -04:00
|
|
|
}
|
|
|
|
|
2019-03-26 08:29:15 -04:00
|
|
|
if c.DiskName == "" {
|
|
|
|
c.DiskName = c.InstanceName + "-disk"
|
|
|
|
}
|
|
|
|
|
|
|
|
if es := c.Communicator.Prepare(&c.ctx); len(es) > 0 {
|
2020-11-19 15:07:02 -05:00
|
|
|
errs = packersdk.MultiErrorAppend(errs, es...)
|
2019-03-26 08:29:15 -04:00
|
|
|
}
|
|
|
|
|
2020-04-20 09:20:05 -04:00
|
|
|
if c.TargetImageFolderID == "" {
|
|
|
|
c.TargetImageFolderID = c.FolderID
|
|
|
|
}
|
|
|
|
|
2019-03-26 08:29:15 -04:00
|
|
|
// Check for any errors.
|
|
|
|
if errs != nil && len(errs.Errors) > 0 {
|
2019-12-17 05:25:56 -05:00
|
|
|
return nil, errs
|
2019-03-26 08:29:15 -04:00
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
return nil, nil
|
2019-03-26 08:29:15 -04:00
|
|
|
}
|