2020-02-20 04:51:34 -05:00
|
|
|
//go:generate 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"
|
|
|
|
"github.com/hashicorp/packer/helper/config"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
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"`
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ packer.Builder = new(MockBuilder)
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (b *MockBuilder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (packer.Artifact, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//////
|
|
|
|
// MockProvisioner
|
|
|
|
//////
|
|
|
|
|
|
|
|
type MockProvisioner struct {
|
|
|
|
Config MockConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ packer.Provisioner = new(MockProvisioner)
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-12-17 10:31:22 -05:00
|
|
|
func (b *MockProvisioner) Provision(ctx context.Context, ui packer.Ui, comm packer.Communicator, _ map[string]interface{}) error {
|
2019-12-17 05:25:56 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//////
|
|
|
|
// MockPostProcessor
|
|
|
|
//////
|
|
|
|
|
|
|
|
type MockPostProcessor struct {
|
|
|
|
Config MockConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ packer.PostProcessor = new(MockPostProcessor)
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (b *MockPostProcessor) PostProcess(ctx context.Context, ui packer.Ui, a packer.Artifact) (packer.Artifact, bool, bool, error) {
|
|
|
|
return nil, false, false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//////
|
|
|
|
// MockCommunicator
|
|
|
|
//////
|
|
|
|
|
|
|
|
type MockCommunicator struct {
|
|
|
|
Config MockConfig
|
|
|
|
packer.Communicator
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ packer.ConfigurableCommunicator = new(MockCommunicator)
|
|
|
|
|
|
|
|
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
|