|
|
|
|
@ -9,6 +9,7 @@ import (
|
|
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/addrs"
|
|
|
|
|
"github.com/hashicorp/terraform/configs"
|
|
|
|
|
"github.com/hashicorp/terraform/configs/configschema"
|
|
|
|
|
"github.com/hashicorp/terraform/plans"
|
|
|
|
|
"github.com/hashicorp/terraform/states"
|
|
|
|
|
"github.com/hashicorp/terraform/tfdiags"
|
|
|
|
|
@ -124,6 +125,178 @@ func TestEvaluatorGetInputVariable(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestEvaluatorGetResource(t *testing.T) {
|
|
|
|
|
stateSync := states.BuildState(func(ss *states.SyncState) {
|
|
|
|
|
ss.SetResourceInstanceCurrent(
|
|
|
|
|
addrs.Resource{
|
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
|
Type: "test_resource",
|
|
|
|
|
Name: "foo",
|
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
|
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
|
AttrsJSON: []byte(`{"id":"foo", "nesting_list": [{"sensitive_value":"abc"}], "nesting_map": {"foo":{"foo":"x"}}, "nesting_set": [{"baz":"abc"}], "nesting_single": {"boop":"abc"}, "nesting_nesting": {"nesting_list":[{"sensitive_value":"abc"}]}, "value":"hello"}`),
|
|
|
|
|
},
|
|
|
|
|
addrs.AbsProviderConfig{
|
|
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
|
|
|
|
Module: addrs.RootModule,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
}).SyncWrapper()
|
|
|
|
|
|
|
|
|
|
rc := &configs.Resource{
|
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
|
Type: "test_resource",
|
|
|
|
|
Name: "foo",
|
|
|
|
|
Config: configs.SynthBody("", map[string]cty.Value{
|
|
|
|
|
"id": cty.StringVal("foo"),
|
|
|
|
|
}),
|
|
|
|
|
Provider: addrs.Provider{
|
|
|
|
|
Hostname: addrs.DefaultRegistryHost,
|
|
|
|
|
Namespace: "hashicorp",
|
|
|
|
|
Type: "test",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
evaluator := &Evaluator{
|
|
|
|
|
Meta: &ContextMeta{
|
|
|
|
|
Env: "foo",
|
|
|
|
|
},
|
|
|
|
|
Changes: plans.NewChanges().SyncWrapper(),
|
|
|
|
|
Config: &configs.Config{
|
|
|
|
|
Module: &configs.Module{
|
|
|
|
|
ManagedResources: map[string]*configs.Resource{
|
|
|
|
|
"test_resource.foo": rc,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
State: stateSync,
|
|
|
|
|
Schemas: &Schemas{
|
|
|
|
|
Providers: map[addrs.Provider]*ProviderSchema{
|
|
|
|
|
addrs.NewDefaultProvider("test"): {
|
|
|
|
|
Provider: &configschema.Block{},
|
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
|
"test_resource": {
|
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
|
"id": {
|
|
|
|
|
Type: cty.String,
|
|
|
|
|
Computed: true,
|
|
|
|
|
},
|
|
|
|
|
"value": {
|
|
|
|
|
Type: cty.String,
|
|
|
|
|
Computed: true,
|
|
|
|
|
Sensitive: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
BlockTypes: map[string]*configschema.NestedBlock{
|
|
|
|
|
"nesting_list": {
|
|
|
|
|
Block: configschema.Block{
|
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
|
"value": {Type: cty.String, Optional: true},
|
|
|
|
|
"sensitive_value": {Type: cty.String, Optional: true, Sensitive: true},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
Nesting: configschema.NestingList,
|
|
|
|
|
},
|
|
|
|
|
"nesting_map": {
|
|
|
|
|
Block: configschema.Block{
|
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
|
"foo": {Type: cty.String, Optional: true, Sensitive: true},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
Nesting: configschema.NestingMap,
|
|
|
|
|
},
|
|
|
|
|
"nesting_set": {
|
|
|
|
|
Block: configschema.Block{
|
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
|
"baz": {Type: cty.String, Optional: true, Sensitive: true},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
Nesting: configschema.NestingSet,
|
|
|
|
|
},
|
|
|
|
|
"nesting_single": {
|
|
|
|
|
Block: configschema.Block{
|
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
|
"boop": {Type: cty.String, Optional: true, Sensitive: true},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
Nesting: configschema.NestingSingle,
|
|
|
|
|
},
|
|
|
|
|
"nesting_nesting": {
|
|
|
|
|
Block: configschema.Block{
|
|
|
|
|
BlockTypes: map[string]*configschema.NestedBlock{
|
|
|
|
|
"nesting_list": {
|
|
|
|
|
Block: configschema.Block{
|
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
|
"value": {Type: cty.String, Optional: true},
|
|
|
|
|
"sensitive_value": {Type: cty.String, Optional: true, Sensitive: true},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
Nesting: configschema.NestingList,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
Nesting: configschema.NestingSingle,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data := &evaluationStateData{
|
|
|
|
|
Evaluator: evaluator,
|
|
|
|
|
}
|
|
|
|
|
scope := evaluator.Scope(data, nil)
|
|
|
|
|
|
|
|
|
|
want := cty.ObjectVal(map[string]cty.Value{
|
|
|
|
|
"id": cty.StringVal("foo"),
|
|
|
|
|
"nesting_list": cty.ListVal([]cty.Value{
|
|
|
|
|
cty.ObjectVal(map[string]cty.Value{
|
|
|
|
|
"sensitive_value": cty.StringVal("abc").Mark("sensitive"),
|
|
|
|
|
"value": cty.NullVal(cty.String),
|
|
|
|
|
}),
|
|
|
|
|
}),
|
|
|
|
|
"nesting_map": cty.MapVal(map[string]cty.Value{
|
|
|
|
|
"foo": cty.ObjectVal(map[string]cty.Value{"foo": cty.StringVal("x").Mark("sensitive")}),
|
|
|
|
|
}),
|
|
|
|
|
"nesting_nesting": cty.ObjectVal(map[string]cty.Value{
|
|
|
|
|
"nesting_list": cty.ListVal([]cty.Value{
|
|
|
|
|
cty.ObjectVal(map[string]cty.Value{
|
|
|
|
|
"sensitive_value": cty.StringVal("abc").Mark("sensitive"),
|
|
|
|
|
"value": cty.NullVal(cty.String),
|
|
|
|
|
}),
|
|
|
|
|
}),
|
|
|
|
|
}),
|
|
|
|
|
"nesting_set": cty.SetVal([]cty.Value{
|
|
|
|
|
cty.ObjectVal(map[string]cty.Value{
|
|
|
|
|
"baz": cty.StringVal("abc").Mark("sensitive"),
|
|
|
|
|
}),
|
|
|
|
|
}),
|
|
|
|
|
"nesting_single": cty.ObjectVal(map[string]cty.Value{
|
|
|
|
|
"boop": cty.StringVal("abc").Mark("sensitive"),
|
|
|
|
|
}),
|
|
|
|
|
"value": cty.StringVal("hello").Mark("sensitive"),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
addr := addrs.Resource{
|
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
|
Type: "test_resource",
|
|
|
|
|
Name: "foo",
|
|
|
|
|
}
|
|
|
|
|
got, diags := scope.Data.GetResource(addr, tfdiags.SourceRange{})
|
|
|
|
|
|
|
|
|
|
if len(diags) != 0 {
|
|
|
|
|
t.Errorf("unexpected diagnostics %s", spew.Sdump(diags))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !got.RawEquals(want) {
|
|
|
|
|
t.Errorf("wrong result:\ngot: %#v\nwant: %#v", got, want)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestEvaluatorGetModule(t *testing.T) {
|
|
|
|
|
// Create a new evaluator with an existing state
|
|
|
|
|
stateSync := states.BuildState(func(ss *states.SyncState) {
|
|
|
|
|
|