terraform: make sure the "id" attribute is in the state

This commit is contained in:
Mitchell Hashimoto 2014-07-08 17:15:41 -07:00
parent 611e0668e7
commit eda3cb009c
5 changed files with 66 additions and 1 deletions

View File

@ -460,6 +460,15 @@ func (c *Context) applyWalkFn() depgraph.WalkFunc {
// Force the resource state type to be our 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
for ak, av := range rs.Attributes {
// If the value is the unknown variable value, then it is an error.

View File

@ -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) {
c := testConfig(t, "apply-output")
p := testProvider("aws")

View File

@ -92,6 +92,10 @@ func (s *State) String() string {
attrKeys := make([]string, 0, len(rs.Attributes))
for ak, _ := range rs.Attributes {
if ak == "id" {
continue
}
attrKeys = append(attrKeys, ak)
}
sort.Strings(attrKeys)

View File

@ -0,0 +1,2 @@
resource "aws_instance" "foo" {
}

View File

@ -1,4 +1,4 @@
resource "aws_instance" "foo" {
num = "2"
compute = "id"
compute = "foo"
}