forked from hashicorp/packer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow datasources to use other datasources
create a null data source for testing. We can choose to document if we want to, but it's a convenience for us add a test to catch cyclic datasource dependency, update tests to include out of order data sources, and update the code to clean up the returned diagnostics generated from the recursive evaluation PR review comments
- Loading branch information
1 parent
9cab184
commit 48de1fc
Showing
13 changed files
with
432 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
//go:generate packer-sdc struct-markdown | ||
//go:generate packer-sdc mapstructure-to-hcl2 -type DatasourceOutput,Config | ||
package null | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/zclconf/go-cty/cty" | ||
|
||
"github.com/hashicorp/hcl/v2/hcldec" | ||
"github.com/hashicorp/packer-plugin-sdk/common" | ||
"github.com/hashicorp/packer-plugin-sdk/hcl2helper" | ||
packersdk "github.com/hashicorp/packer-plugin-sdk/packer" | ||
"github.com/hashicorp/packer-plugin-sdk/template/config" | ||
) | ||
|
||
type Datasource struct { | ||
config Config | ||
} | ||
|
||
// The Null data source is designed to demonstrate how data sources work, and | ||
// to provide a test plugin. It does not do anything useful; you assign an | ||
// input string and it gets returned as an output string. | ||
type Config struct { | ||
common.PackerConfig `mapstructure:",squash"` | ||
// This variable will get stored as "output" in the output spec. | ||
Input string `mapstructure:"input" required:"true"` | ||
} | ||
|
||
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 | ||
} | ||
|
||
var errs *packersdk.MultiError | ||
|
||
if d.config.Input == "" { | ||
errs = packersdk.MultiErrorAppend(errs, fmt.Errorf("The `input` must be specified")) | ||
} | ||
|
||
if errs != nil && len(errs.Errors) > 0 { | ||
return errs | ||
} | ||
return nil | ||
} | ||
|
||
type DatasourceOutput struct { | ||
// Output will return the input variable, as output. | ||
Output string `mapstructure:"output"` | ||
} | ||
|
||
func (d *Datasource) OutputSpec() hcldec.ObjectSpec { | ||
return (&DatasourceOutput{}).FlatMapstructure().HCL2Spec() | ||
} | ||
|
||
func (d *Datasource) Execute() (cty.Value, error) { | ||
// Pass input variable through to output. | ||
output := DatasourceOutput{ | ||
Output: d.config.Input, | ||
} | ||
|
||
return hcl2helper.HCL2ValueFromConfig(output, d.OutputSpec()), nil | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
data "null" "gummy" { | ||
input = "${data.null.bear.output}" | ||
} | ||
data "null" "bear" { | ||
input = "${data.null.gummy.output}" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
data "null" "foo" { | ||
input = "chocolate" | ||
} | ||
|
||
data "null" "yummy" { | ||
input = "${data.null.bang.output}-and-sprinkles" | ||
} | ||
|
||
data "null" "bar" { | ||
input = "vanilla" | ||
} | ||
|
||
data "null" "baz" { | ||
input = "${data.null.foo.output}-${data.null.bar.output}-swirl" | ||
} | ||
|
||
data "null" "bang" { | ||
input = "${data.null.baz.output}-with-marshmallows" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.