-
Notifications
You must be signed in to change notification settings - Fork 29
/
data.go
49 lines (40 loc) · 1.11 KB
/
data.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
//go:generate packer-sdc mapstructure-to-hcl2 -type Config,DatasourceOutput
package scaffolding
import (
"github.com/hashicorp/hcl/v2/hcldec"
"github.com/hashicorp/packer-plugin-sdk/hcl2helper"
"github.com/hashicorp/packer-plugin-sdk/template/config"
"github.com/zclconf/go-cty/cty"
)
type Config struct {
MockOption string `mapstructure:"mock"`
}
type Datasource struct {
config Config
}
type DatasourceOutput struct {
Foo string `mapstructure:"foo"`
Bar string `mapstructure:"bar"`
}
func (d *Datasource) ConfigSpec() hcldec.ObjectSpec {
return d.config.FlatMapstructure().HCL2Spec()
}
func (d *Datasource) Configure(raws ...interface{}) error {
err := config.Decode(&d.config, nil, raws...)
if err != nil {
return err
}
return nil
}
func (d *Datasource) OutputSpec() hcldec.ObjectSpec {
return (&DatasourceOutput{}).FlatMapstructure().HCL2Spec()
}
func (d *Datasource) Execute() (cty.Value, error) {
output := DatasourceOutput{
Foo: "foo-value",
Bar: "bar-value",
}
return hcl2helper.HCL2ValueFromConfig(output, d.OutputSpec()), nil
}