maintain private data through provider ACC tests

The private->meta data was lost in the test harness.
This commit is contained in:
James Bardin 2019-06-05 11:11:04 -04:00
parent 49fee6ba78
commit 4cb6ebe22c
1 changed files with 52 additions and 28 deletions

View File

@ -1,6 +1,7 @@
package resource package resource
import ( import (
"encoding/json"
"fmt" "fmt"
"github.com/hashicorp/terraform/addrs" "github.com/hashicorp/terraform/addrs"
@ -52,43 +53,57 @@ func shimNewState(newState *states.State, providers map[string]terraform.Resourc
resource := getResource(providers, providerType, res.Addr) resource := getResource(providers, providerType, res.Addr)
for key, i := range res.Instances { for key, i := range res.Instances {
flatmap, err := shimmedAttributes(i.Current, resource) resState := &terraform.ResourceState{
if err != nil { Type: resType,
return nil, fmt.Errorf("error decoding state for %q: %s", resType, err) Provider: res.ProviderConfig.String(),
} }
resState := &terraform.ResourceState{ // We should always have a Current instance here, but be safe about checking.
Type: resType, if i.Current != nil {
Primary: &terraform.InstanceState{ flatmap, err := shimmedAttributes(i.Current, resource)
if err != nil {
return nil, fmt.Errorf("error decoding state for %q: %s", resType, err)
}
var meta map[string]interface{}
if i.Current.Private != nil {
err := json.Unmarshal(i.Current.Private, &meta)
if err != nil {
return nil, err
}
}
resState.Primary = &terraform.InstanceState{
ID: flatmap["id"], ID: flatmap["id"],
Attributes: flatmap, Attributes: flatmap,
Tainted: i.Current.Status == states.ObjectTainted, Tainted: i.Current.Status == states.ObjectTainted,
}, Meta: meta,
Provider: res.ProviderConfig.String(),
}
if i.Current.SchemaVersion != 0 {
resState.Primary.Meta = map[string]interface{}{
"schema_version": i.Current.SchemaVersion,
} }
}
for _, dep := range i.Current.Dependencies { if i.Current.SchemaVersion != 0 {
resState.Dependencies = append(resState.Dependencies, dep.String()) resState.Primary.Meta = map[string]interface{}{
} "schema_version": i.Current.SchemaVersion,
}
// convert the indexes to the old style flapmap indexes
idx := ""
switch key.(type) {
case addrs.IntKey:
// don't add numeric index values to resources with a count of 0
if len(res.Instances) > 1 {
idx = fmt.Sprintf(".%d", key)
} }
case addrs.StringKey:
idx = "." + key.String()
}
mod.Resources[res.Addr.String()+idx] = resState for _, dep := range i.Current.Dependencies {
resState.Dependencies = append(resState.Dependencies, dep.String())
}
// convert the indexes to the old style flapmap indexes
idx := ""
switch key.(type) {
case addrs.IntKey:
// don't add numeric index values to resources with a count of 0
if len(res.Instances) > 1 {
idx = fmt.Sprintf(".%d", key)
}
case addrs.StringKey:
idx = "." + key.String()
}
mod.Resources[res.Addr.String()+idx] = resState
}
// add any deposed instances // add any deposed instances
for _, dep := range i.Deposed { for _, dep := range i.Deposed {
@ -97,10 +112,19 @@ func shimNewState(newState *states.State, providers map[string]terraform.Resourc
return nil, fmt.Errorf("error decoding deposed state for %q: %s", resType, err) return nil, fmt.Errorf("error decoding deposed state for %q: %s", resType, err)
} }
var meta map[string]interface{}
if dep.Private != nil {
err := json.Unmarshal(dep.Private, &meta)
if err != nil {
return nil, err
}
}
deposed := &terraform.InstanceState{ deposed := &terraform.InstanceState{
ID: flatmap["id"], ID: flatmap["id"],
Attributes: flatmap, Attributes: flatmap,
Tainted: dep.Status == states.ObjectTainted, Tainted: dep.Status == states.ObjectTainted,
Meta: meta,
} }
if dep.SchemaVersion != 0 { if dep.SchemaVersion != 0 {
deposed.Meta = map[string]interface{}{ deposed.Meta = map[string]interface{}{