2021-04-16 05:52:03 -04:00
|
|
|
//go:generate packer-sdc mapstructure-to-hcl2 -type MockConfig,NestedMockConfig,MockTag
|
2019-12-17 05:25:56 -05:00
|
|
|
|
|
|
|
package hcl2template
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/hcl/v2/hcldec"
|
2021-01-20 04:37:16 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/hcl2helper"
|
2020-12-17 16:29:25 -05:00
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/template/config"
|
2020-02-17 09:53:05 -05:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
2020-02-18 05:31:52 -05:00
|
|
|
"github.com/zclconf/go-cty/cty/json"
|
2019-12-17 05:25:56 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type NestedMockConfig struct {
|
2020-01-07 06:45:42 -05:00
|
|
|
String string `mapstructure:"string"`
|
|
|
|
Int int `mapstructure:"int"`
|
|
|
|
Int64 int64 `mapstructure:"int64"`
|
|
|
|
Bool bool `mapstructure:"bool"`
|
|
|
|
Trilean config.Trilean `mapstructure:"trilean"`
|
|
|
|
Duration time.Duration `mapstructure:"duration"`
|
|
|
|
MapStringString map[string]string `mapstructure:"map_string_string"`
|
|
|
|
SliceString []string `mapstructure:"slice_string"`
|
2020-02-03 11:03:28 -05:00
|
|
|
SliceSliceString [][]string `mapstructure:"slice_slice_string"`
|
2020-01-07 06:45:42 -05:00
|
|
|
NamedMapStringString NamedMapStringString `mapstructure:"named_map_string_string"`
|
|
|
|
NamedString NamedString `mapstructure:"named_string"`
|
2020-02-20 04:51:34 -05:00
|
|
|
Tags []MockTag `mapstructure:"tag"`
|
2021-01-20 04:37:16 -05:00
|
|
|
Datasource string `mapstructure:"data_source"`
|
2020-02-20 04:51:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type MockTag struct {
|
|
|
|
Key string `mapstructure:"key"`
|
|
|
|
Value string `mapstructure:"value"`
|
2019-12-17 05:25:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type MockConfig struct {
|
Hcl2 input variables, local variables and functions (#8588)
Mainly redefine or reused what Terraform did.
* allow to used `variables`, `variable` and `local` blocks
* import the following functions and their docs from Terraform: abs, abspath, basename, base64decode, base64encode, bcrypt, can, ceil, chomp, chunklist, cidrhost, cidrnetmask, cidrsubnet, cidrsubnets, coalesce, coalescelist, compact, concat, contains, convert, csvdecode, dirname, distinct, element, file, fileexists, fileset, flatten, floor, format, formatdate, formatlist, indent, index, join, jsondecode, jsonencode, keys, length, log, lookup, lower, max, md5, merge, min, parseint, pathexpand, pow, range, reverse, rsadecrypt, setintersection, setproduct, setunion, sha1, sha256, sha512, signum, slice, sort, split, strrev, substr, timestamp, timeadd, title, trim, trimprefix, trimspace, trimsuffix, try, upper, urlencode, uuidv4, uuidv5, values, yamldecode, yamlencode, zipmap.
2020-02-06 05:49:21 -05:00
|
|
|
NotSquashed string `mapstructure:"not_squashed"`
|
2019-12-17 05:25:56 -05:00
|
|
|
NestedMockConfig `mapstructure:",squash"`
|
|
|
|
Nested NestedMockConfig `mapstructure:"nested"`
|
|
|
|
NestedSlice []NestedMockConfig `mapstructure:"nested_slice"`
|
|
|
|
}
|
|
|
|
|
2020-02-17 09:47:31 -05:00
|
|
|
func (b *MockConfig) Prepare(raws ...interface{}) error {
|
2020-02-17 09:53:05 -05:00
|
|
|
for i, raw := range raws {
|
|
|
|
cval, ok := raw.(cty.Value)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
b, err := json.Marshal(cval, cty.DynamicPseudoType)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ccval, err := json.Unmarshal(b, cty.DynamicPseudoType)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
raws[i] = ccval
|
|
|
|
}
|
2020-02-17 09:47:31 -05:00
|
|
|
return config.Decode(b, &config.DecodeOpts{
|
|
|
|
Interpolate: true,
|
|
|
|
}, raws...)
|
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
//////
|
|
|
|
// MockBuilder
|
|
|
|
//////
|
|
|
|
|
|
|
|
type MockBuilder struct {
|
|
|
|
Config MockConfig
|
|
|
|
}
|
|
|
|
|
2020-12-01 16:42:11 -05:00
|
|
|
var _ packersdk.Builder = new(MockBuilder)
|
2019-12-17 05:25:56 -05:00
|
|
|
|
|
|
|
func (b *MockBuilder) ConfigSpec() hcldec.ObjectSpec { return b.Config.FlatMapstructure().HCL2Spec() }
|
|
|
|
|
2019-12-17 10:31:22 -05:00
|
|
|
func (b *MockBuilder) Prepare(raws ...interface{}) ([]string, []string, error) {
|
2020-07-02 09:56:49 -04:00
|
|
|
return []string{"ID"}, nil, b.Config.Prepare(raws...)
|
2019-12-17 05:25:56 -05:00
|
|
|
}
|
|
|
|
|
2020-11-19 18:10:00 -05:00
|
|
|
func (b *MockBuilder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) (packersdk.Artifact, error) {
|
2019-12-17 05:25:56 -05:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//////
|
|
|
|
// MockProvisioner
|
|
|
|
//////
|
|
|
|
|
|
|
|
type MockProvisioner struct {
|
|
|
|
Config MockConfig
|
|
|
|
}
|
|
|
|
|
2020-12-01 17:25:14 -05:00
|
|
|
var _ packersdk.Provisioner = new(MockProvisioner)
|
2019-12-17 05:25:56 -05:00
|
|
|
|
|
|
|
func (b *MockProvisioner) ConfigSpec() hcldec.ObjectSpec {
|
|
|
|
return b.Config.FlatMapstructure().HCL2Spec()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *MockProvisioner) Prepare(raws ...interface{}) error {
|
2020-02-17 09:47:31 -05:00
|
|
|
return b.Config.Prepare(raws...)
|
2019-12-17 05:25:56 -05:00
|
|
|
}
|
|
|
|
|
2020-11-19 18:10:00 -05:00
|
|
|
func (b *MockProvisioner) Provision(ctx context.Context, ui packersdk.Ui, comm packersdk.Communicator, _ map[string]interface{}) error {
|
2019-12-17 05:25:56 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-20 04:37:16 -05:00
|
|
|
//////
|
|
|
|
// MockDatasource
|
|
|
|
//////
|
|
|
|
|
|
|
|
type MockDatasource struct {
|
|
|
|
Config MockConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ packersdk.Datasource = new(MockDatasource)
|
|
|
|
|
|
|
|
func (d *MockDatasource) ConfigSpec() hcldec.ObjectSpec {
|
|
|
|
return d.Config.FlatMapstructure().HCL2Spec()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *MockDatasource) OutputSpec() hcldec.ObjectSpec {
|
|
|
|
return d.Config.FlatMapstructure().HCL2Spec()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *MockDatasource) Configure(raws ...interface{}) error {
|
|
|
|
return d.Config.Prepare(raws...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *MockDatasource) Execute() (cty.Value, error) {
|
|
|
|
return hcl2helper.HCL2ValueFromConfig(d.Config, d.OutputSpec()), nil
|
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
//////
|
|
|
|
// MockPostProcessor
|
|
|
|
//////
|
|
|
|
|
|
|
|
type MockPostProcessor struct {
|
|
|
|
Config MockConfig
|
|
|
|
}
|
|
|
|
|
2020-12-01 17:48:55 -05:00
|
|
|
var _ packersdk.PostProcessor = new(MockPostProcessor)
|
2019-12-17 05:25:56 -05:00
|
|
|
|
|
|
|
func (b *MockPostProcessor) ConfigSpec() hcldec.ObjectSpec {
|
|
|
|
return b.Config.FlatMapstructure().HCL2Spec()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *MockPostProcessor) Configure(raws ...interface{}) error {
|
2020-02-17 09:47:31 -05:00
|
|
|
return b.Config.Prepare(raws...)
|
2019-12-17 05:25:56 -05:00
|
|
|
}
|
|
|
|
|
2020-11-19 15:17:11 -05:00
|
|
|
func (b *MockPostProcessor) PostProcess(ctx context.Context, ui packersdk.Ui, a packersdk.Artifact) (packersdk.Artifact, bool, bool, error) {
|
2019-12-17 05:25:56 -05:00
|
|
|
return nil, false, false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//////
|
|
|
|
// MockCommunicator
|
|
|
|
//////
|
|
|
|
|
|
|
|
type MockCommunicator struct {
|
|
|
|
Config MockConfig
|
2020-11-19 18:10:00 -05:00
|
|
|
packersdk.Communicator
|
2019-12-17 05:25:56 -05:00
|
|
|
}
|
|
|
|
|
2020-11-19 18:10:00 -05:00
|
|
|
var _ packersdk.ConfigurableCommunicator = new(MockCommunicator)
|
2019-12-17 05:25:56 -05:00
|
|
|
|
|
|
|
func (b *MockCommunicator) ConfigSpec() hcldec.ObjectSpec {
|
|
|
|
return b.Config.FlatMapstructure().HCL2Spec()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *MockCommunicator) Configure(raws ...interface{}) ([]string, error) {
|
2020-02-17 09:47:31 -05:00
|
|
|
return nil, b.Config.Prepare(raws...)
|
2019-12-17 05:25:56 -05:00
|
|
|
}
|
2020-01-07 06:45:42 -05:00
|
|
|
|
|
|
|
//////
|
|
|
|
// Utils
|
|
|
|
//////
|
|
|
|
|
|
|
|
type NamedMapStringString map[string]string
|
|
|
|
type NamedString string
|