Skip to content

Commit

Permalink
maintain private data through provider ACC tests
Browse files Browse the repository at this point in the history
The private->meta data was lost in the test harness.
  • Loading branch information
jbardin committed Jun 5, 2019
1 parent ad970a4 commit 750e7e5
Showing 1 changed file with 51 additions and 27 deletions.
78 changes: 51 additions & 27 deletions helper/resource/state_shim.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package resource

import (
"encoding/json"
"fmt"

"github.com/hashicorp/terraform/addrs"
Expand Down Expand Up @@ -52,43 +53,57 @@ func shimNewState(newState *states.State, providers map[string]terraform.Resourc
resource := getResource(providers, providerType, res.Addr)

for key, i := range res.Instances {
flatmap, err := shimmedAttributes(i.Current, resource)
if err != nil {
return nil, fmt.Errorf("error decoding state for %q: %s", resType, err)
resState := &terraform.ResourceState{
Type: resType,
Provider: res.ProviderConfig.String(),
}

resState := &terraform.ResourceState{
Type: resType,
Primary: &terraform.InstanceState{
// We should always have a Current instance here, but be safe about checking.
if i.Current != nil {
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"],
Attributes: flatmap,
Tainted: i.Current.Status == states.ObjectTainted,
},
Provider: res.ProviderConfig.String(),
}
if i.Current.SchemaVersion != 0 {
resState.Primary.Meta = map[string]interface{}{
"schema_version": i.Current.SchemaVersion,
Meta: meta,
}
}

for _, dep := range i.Current.Dependencies {
resState.Dependencies = append(resState.Dependencies, dep.String())
}
if i.Current.SchemaVersion != 0 {
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)
for _, dep := range i.Current.Dependencies {
resState.Dependencies = append(resState.Dependencies, dep.String())
}
case addrs.StringKey:
idx = "." + key.String()
}

mod.Resources[res.Addr.String()+idx] = resState
// 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
for _, dep := range i.Deposed {
Expand All @@ -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)
}

var meta map[string]interface{}
if dep.Private != nil {
err := json.Unmarshal(dep.Private, &meta)
if err != nil {
return nil, err
}
}

deposed := &terraform.InstanceState{
ID: flatmap["id"],
Attributes: flatmap,
Tainted: dep.Status == states.ObjectTainted,
Meta: meta,
}
if dep.SchemaVersion != 0 {
deposed.Meta = map[string]interface{}{
Expand Down

0 comments on commit 750e7e5

Please sign in to comment.