terraform: make sure the "id" attribute is in the state
This commit is contained in:
parent
611e0668e7
commit
eda3cb009c
|
@ -460,6 +460,15 @@ func (c *Context) applyWalkFn() depgraph.WalkFunc {
|
||||||
// Force the resource state type to be our type
|
// Force the resource state type to be our type
|
||||||
rs.Type = r.State.Type
|
rs.Type = r.State.Type
|
||||||
|
|
||||||
|
// Force the "id" attribute to be our ID
|
||||||
|
if rs.ID != "" {
|
||||||
|
if rs.Attributes == nil {
|
||||||
|
rs.Attributes = make(map[string]string)
|
||||||
|
}
|
||||||
|
|
||||||
|
rs.Attributes["id"] = rs.ID
|
||||||
|
}
|
||||||
|
|
||||||
var errs []error
|
var errs []error
|
||||||
for ak, av := range rs.Attributes {
|
for ak, av := range rs.Attributes {
|
||||||
// If the value is the unknown variable value, then it is an error.
|
// If the value is the unknown variable value, then it is an error.
|
||||||
|
|
|
@ -542,6 +542,56 @@ func TestContextApply_hook(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestContextApply_idAttr(t *testing.T) {
|
||||||
|
c := testConfig(t, "apply-idattr")
|
||||||
|
p := testProvider("aws")
|
||||||
|
ctx := testContext(t, &ContextOpts{
|
||||||
|
Config: c,
|
||||||
|
Providers: map[string]ResourceProviderFactory{
|
||||||
|
"aws": testProviderFuncFixed(p),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
p.ApplyFn = func(s *ResourceState, d *ResourceDiff) (*ResourceState, error) {
|
||||||
|
result := s.MergeDiff(d)
|
||||||
|
result.ID = "foo"
|
||||||
|
result.Attributes = map[string]string{
|
||||||
|
"id": "bar",
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
p.DiffFn = func(*ResourceState, *ResourceConfig) (*ResourceDiff, error) {
|
||||||
|
return &ResourceDiff{
|
||||||
|
Attributes: map[string]*ResourceAttrDiff{
|
||||||
|
"num": &ResourceAttrDiff{
|
||||||
|
New: "bar",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := ctx.Plan(nil); err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
state, err := ctx.Apply()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
rs, ok := state.Resources["aws_instance.foo"]
|
||||||
|
if !ok {
|
||||||
|
t.Fatal("not in state")
|
||||||
|
}
|
||||||
|
if rs.ID != "foo" {
|
||||||
|
t.Fatalf("bad: %#v", rs.ID)
|
||||||
|
}
|
||||||
|
if rs.Attributes["id"] != "foo" {
|
||||||
|
t.Fatalf("bad: %#v", rs.Attributes)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestContextApply_output(t *testing.T) {
|
func TestContextApply_output(t *testing.T) {
|
||||||
c := testConfig(t, "apply-output")
|
c := testConfig(t, "apply-output")
|
||||||
p := testProvider("aws")
|
p := testProvider("aws")
|
||||||
|
|
|
@ -92,6 +92,10 @@ func (s *State) String() string {
|
||||||
|
|
||||||
attrKeys := make([]string, 0, len(rs.Attributes))
|
attrKeys := make([]string, 0, len(rs.Attributes))
|
||||||
for ak, _ := range rs.Attributes {
|
for ak, _ := range rs.Attributes {
|
||||||
|
if ak == "id" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
attrKeys = append(attrKeys, ak)
|
attrKeys = append(attrKeys, ak)
|
||||||
}
|
}
|
||||||
sort.Strings(attrKeys)
|
sort.Strings(attrKeys)
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
resource "aws_instance" "foo" {
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
resource "aws_instance" "foo" {
|
resource "aws_instance" "foo" {
|
||||||
num = "2"
|
num = "2"
|
||||||
compute = "id"
|
compute = "foo"
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue