Merge pull request #4446 from TimeIncOSS/f-schema-new-resource

helper/schema: Allow identification of a new resource in update func
This commit is contained in:
Radek Simko 2016-02-29 20:07:00 +00:00
commit 8bdd92187c
3 changed files with 91 additions and 0 deletions

View File

@ -142,6 +142,7 @@ func (r *Resource) Apply(
err = nil
if data.Id() == "" {
// We're creating, it is a new resource.
data.MarkNewResource()
err = r.Create(data, meta)
} else {
if r.Update == nil {

View File

@ -30,6 +30,7 @@ type ResourceData struct {
partial bool
partialMap map[string]struct{}
once sync.Once
isNew bool
}
// getResult is the internal structure that is generated when a Get
@ -171,6 +172,14 @@ func (d *ResourceData) SetPartial(k string) {
}
}
func (d *ResourceData) MarkNewResource() {
d.isNew = true
}
func (d *ResourceData) IsNewResource() bool {
return d.isNew
}
// Id returns the ID of the resource.
func (d *ResourceData) Id() string {
var result string

View File

@ -312,6 +312,87 @@ func TestResourceApply_updateNoCallback(t *testing.T) {
}
}
func TestResourceApply_isNewResource(t *testing.T) {
r := &Resource{
Schema: map[string]*Schema{
"foo": &Schema{
Type: TypeString,
Optional: true,
},
},
}
updateFunc := func(d *ResourceData, m interface{}) error {
d.Set("foo", "updated")
if d.IsNewResource() {
d.Set("foo", "new-resource")
}
return nil
}
r.Create = func(d *ResourceData, m interface{}) error {
d.SetId("foo")
d.Set("foo", "created")
return updateFunc(d, m)
}
r.Update = updateFunc
d := &terraform.InstanceDiff{
Attributes: map[string]*terraform.ResourceAttrDiff{
"foo": &terraform.ResourceAttrDiff{
New: "bla-blah",
},
},
}
// positive test
var s *terraform.InstanceState = nil
actual, err := r.Apply(s, d, nil)
if err != nil {
t.Fatalf("err: %s", err)
}
expected := &terraform.InstanceState{
ID: "foo",
Attributes: map[string]string{
"id": "foo",
"foo": "new-resource",
},
}
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("actual: %#v\nexpected: %#v",
actual, expected)
}
// negative test
s = &terraform.InstanceState{
ID: "foo",
Attributes: map[string]string{
"id": "foo",
"foo": "new-resource",
},
}
actual, err = r.Apply(s, d, nil)
if err != nil {
t.Fatalf("err: %s", err)
}
expected = &terraform.InstanceState{
ID: "foo",
Attributes: map[string]string{
"id": "foo",
"foo": "updated",
},
}
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("actual: %#v\nexpected: %#v",
actual, expected)
}
}
func TestResourceInternalValidate(t *testing.T) {
cases := []struct {
In *Resource