6 "github.com/hashicorp/terraform/addrs"
7 "github.com/zclconf/go-cty/cty"
9 "github.com/hashicorp/terraform/config/hcl2shim"
10 "github.com/hashicorp/terraform/helper/schema"
12 "github.com/hashicorp/terraform/states"
13 "github.com/hashicorp/terraform/terraform"
16 // shimState takes a new *states.State and reverts it to a legacy state for the provider ACC tests
17 func shimNewState(newState *states.State, providers map[string]terraform.ResourceProvider) (*terraform.State, error) {
18 state := terraform.NewState()
20 // in the odd case of a nil state, let the helper packages handle it
25 for _, newMod := range newState.Modules {
26 mod := state.AddModule(newMod.Addr)
28 for name, out := range newMod.OutputValues {
30 val := hcl2shim.ConfigValueFromHCL2(out.Value)
31 ty := out.Value.Type()
33 case ty == cty.String:
35 case ty.IsTupleType() || ty.IsListType():
41 mod.Outputs[name] = &terraform.OutputState{
44 Sensitive: out.Sensitive,
48 for _, res := range newMod.Resources {
49 resType := res.Addr.Type
50 providerType := res.ProviderConfig.ProviderConfig.Type
52 resource := getResource(providers, providerType, res.Addr)
54 for key, i := range res.Instances {
55 flatmap, err := shimmedAttributes(i.Current, resource)
57 return nil, fmt.Errorf("error decoding state for %q: %s", resType, err)
60 resState := &terraform.ResourceState{
62 Primary: &terraform.InstanceState{
65 Tainted: i.Current.Status == states.ObjectTainted,
67 Provider: res.ProviderConfig.String(),
69 if i.Current.SchemaVersion != 0 {
70 resState.Primary.Meta = map[string]interface{}{
71 "schema_version": i.Current.SchemaVersion,
75 for _, dep := range i.Current.Dependencies {
76 resState.Dependencies = append(resState.Dependencies, dep.String())
79 // convert the indexes to the old style flapmap indexes
83 // don't add numeric index values to resources with a count of 0
84 if len(res.Instances) > 1 {
85 idx = fmt.Sprintf(".%d", key)
88 idx = "." + key.String()
91 mod.Resources[res.Addr.String()+idx] = resState
93 // add any deposed instances
94 for _, dep := range i.Deposed {
95 flatmap, err := shimmedAttributes(dep, resource)
97 return nil, fmt.Errorf("error decoding deposed state for %q: %s", resType, err)
100 deposed := &terraform.InstanceState{
103 Tainted: dep.Status == states.ObjectTainted,
105 if dep.SchemaVersion != 0 {
106 deposed.Meta = map[string]interface{}{
107 "schema_version": dep.SchemaVersion,
111 resState.Deposed = append(resState.Deposed, deposed)
120 func getResource(providers map[string]terraform.ResourceProvider, providerName string, addr addrs.Resource) *schema.Resource {
121 p := providers[providerName]
123 panic(fmt.Sprintf("provider %q not found in test step", providerName))
126 // this is only for tests, so should only see schema.Providers
127 provider := p.(*schema.Provider)
130 case addrs.ManagedResourceMode:
131 resource := provider.ResourcesMap[addr.Type]
135 case addrs.DataResourceMode:
136 resource := provider.DataSourcesMap[addr.Type]
142 panic(fmt.Sprintf("resource %s not found in test step", addr.Type))
145 func shimmedAttributes(instance *states.ResourceInstanceObjectSrc, res *schema.Resource) (map[string]string, error) {
146 flatmap := instance.AttrsFlat
151 // if we have json attrs, they need to be decoded
152 rio, err := instance.Decode(res.CoreConfigSchema().ImpliedType())
157 instanceState, err := res.ShimInstanceStateFromValue(rio.Value)
162 return instanceState.Attributes, nil