helper/schema: cleaner way to store Ephemeral

This commit is contained in:
Mitchell Hashimoto 2016-04-26 12:25:50 -07:00
parent 45c3fd58d8
commit 1685054a9a
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
3 changed files with 26 additions and 2 deletions

View File

@ -213,3 +213,7 @@ func (p *Provider) Resources() []terraform.ResourceType {
return result
}
func (p *Provider) ImportState(info *terraform.InstanceInfo) ([]*terraform.InstanceState, error) {
return nil, nil
}

View File

@ -221,6 +221,13 @@ func (d *ResourceData) SetConnInfo(v map[string]string) {
d.newState.Ephemeral.ConnInfo = v
}
// SetType sets the ephemeral type for the data. This is only required
// for importing.
func (d *ResourceData) SetType(t string) {
d.once.Do(d.init)
d.newState.Ephemeral.Type = t
}
// State returns the new InstanceState after the diff and any Set
// calls.
func (d *ResourceData) State() *terraform.InstanceState {
@ -260,7 +267,9 @@ func (d *ResourceData) State() *terraform.InstanceState {
}
result.Attributes = mapW.Map()
result.Ephemeral.ConnInfo = d.ConnInfo()
if d.newState != nil {
result.Ephemeral = d.newState.Ephemeral
}
// TODO: This is hacky and we can remove this when we have a proper
// state writer. We should instead have a proper StateFieldWriter
@ -286,7 +295,7 @@ func (d *ResourceData) init() {
// Initialize the field that will store our new state
var copyState terraform.InstanceState
if d.state != nil {
copyState = *d.state
copyState = *d.state.DeepCopy()
}
d.newState = &copyState

View File

@ -2930,6 +2930,17 @@ func TestResourceDataSetId_override(t *testing.T) {
}
}
func TestResourceDataSetType(t *testing.T) {
d := &ResourceData{}
d.SetId("foo")
d.SetType("bar")
actual := d.State()
if v := actual.Ephemeral.Type; v != "bar" {
t.Fatalf("bad: %#v", actual)
}
}
func testPtrTo(raw interface{}) interface{} {
return &raw
}