github.com/hashicorp/packer@v1.14.3/datasource/null/data.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: BUSL-1.1 3 4 //go:generate packer-sdc struct-markdown 5 //go:generate packer-sdc mapstructure-to-hcl2 -type DatasourceOutput,Config 6 package null 7 8 import ( 9 "fmt" 10 11 "github.com/zclconf/go-cty/cty" 12 13 "github.com/hashicorp/hcl/v2/hcldec" 14 "github.com/hashicorp/packer-plugin-sdk/common" 15 "github.com/hashicorp/packer-plugin-sdk/hcl2helper" 16 packersdk "github.com/hashicorp/packer-plugin-sdk/packer" 17 "github.com/hashicorp/packer-plugin-sdk/template/config" 18 ) 19 20 type Datasource struct { 21 config Config 22 } 23 24 // The Null data source is designed to demonstrate how data sources work, and 25 // to provide a test plugin. It does not do anything useful; you assign an 26 // input string and it gets returned as an output string. 27 type Config struct { 28 common.PackerConfig `mapstructure:",squash"` 29 // This variable will get stored as "output" in the output spec. 30 Input string `mapstructure:"input" required:"true"` 31 } 32 33 func (d *Datasource) ConfigSpec() hcldec.ObjectSpec { 34 return d.config.FlatMapstructure().HCL2Spec() 35 } 36 37 func (d *Datasource) Configure(raws ...interface{}) error { 38 err := config.Decode(&d.config, nil, raws...) 39 if err != nil { 40 return err 41 } 42 43 var errs *packersdk.MultiError 44 45 if d.config.Input == "" { 46 errs = packersdk.MultiErrorAppend(errs, fmt.Errorf("The `input` must be specified")) 47 } 48 49 if errs != nil && len(errs.Errors) > 0 { 50 return errs 51 } 52 return nil 53 } 54 55 type DatasourceOutput struct { 56 // Output will return the input variable, as output. 57 Output string `mapstructure:"output"` 58 } 59 60 func (d *Datasource) OutputSpec() hcldec.ObjectSpec { 61 return (&DatasourceOutput{}).FlatMapstructure().HCL2Spec() 62 } 63 64 func (d *Datasource) Execute() (cty.Value, error) { 65 // Pass input variable through to output. 66 output := DatasourceOutput{ 67 Output: d.config.Input, 68 } 69 70 return hcl2helper.HCL2ValueFromConfig(output, d.OutputSpec()), nil 71 }