-
Notifications
You must be signed in to change notification settings - Fork 9.6k
/
state_shim.go
218 lines (184 loc) · 6.02 KB
/
state_shim.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package resource
import (
"encoding/json"
"fmt"
"github.com/hashicorp/terraform/addrs"
"github.com/zclconf/go-cty/cty"
"github.com/hashicorp/terraform/configs/hcl2shim"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/states"
"github.com/hashicorp/terraform/terraform"
)
// shimState takes a new *states.State and reverts it to a legacy state for the provider ACC tests
func shimNewState(newState *states.State, providers map[string]terraform.ResourceProvider) (*terraform.State, error) {
state := terraform.NewState()
// in the odd case of a nil state, let the helper packages handle it
if newState == nil {
return nil, nil
}
for _, newMod := range newState.Modules {
mod := state.AddModule(newMod.Addr)
for name, out := range newMod.OutputValues {
outputType := ""
val := hcl2shim.ConfigValueFromHCL2(out.Value)
ty := out.Value.Type()
switch {
case ty == cty.String:
outputType = "string"
case ty.IsTupleType() || ty.IsListType():
outputType = "list"
case ty.IsMapType():
outputType = "map"
}
mod.Outputs[name] = &terraform.OutputState{
Type: outputType,
Value: val,
Sensitive: out.Sensitive,
}
}
for _, res := range newMod.Resources {
resType := res.Addr.Resource.Type
providerType := res.ProviderConfig.Provider.Type
resource := getResource(providers, providerType, res.Addr.Resource)
for key, i := range res.Instances {
resState := &terraform.ResourceState{
Type: resType,
Provider: legacyProviderConfigString(res.ProviderConfig),
}
// 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,
Meta: meta,
}
if i.Current.SchemaVersion != 0 {
if resState.Primary.Meta == nil {
resState.Primary.Meta = map[string]interface{}{}
}
resState.Primary.Meta["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.Resource.String()+idx] = resState
}
// add any deposed instances
for _, dep := range i.Deposed {
flatmap, err := shimmedAttributes(dep, resource)
if err != nil {
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{}{
"schema_version": dep.SchemaVersion,
}
}
resState.Deposed = append(resState.Deposed, deposed)
}
}
}
}
return state, nil
}
func getResource(providers map[string]terraform.ResourceProvider, providerName string, addr addrs.Resource) *schema.Resource {
p := providers[providerName]
if p == nil {
panic(fmt.Sprintf("provider %q not found in test step", providerName))
}
// this is only for tests, so should only see schema.Providers
provider := p.(*schema.Provider)
switch addr.Mode {
case addrs.ManagedResourceMode:
resource := provider.ResourcesMap[addr.Type]
if resource != nil {
return resource
}
case addrs.DataResourceMode:
resource := provider.DataSourcesMap[addr.Type]
if resource != nil {
return resource
}
}
panic(fmt.Sprintf("resource %s not found in test step", addr.Type))
}
func shimmedAttributes(instance *states.ResourceInstanceObjectSrc, res *schema.Resource) (map[string]string, error) {
flatmap := instance.AttrsFlat
if flatmap != nil {
return flatmap, nil
}
// if we have json attrs, they need to be decoded
rio, err := instance.Decode(res.CoreConfigSchema().ImpliedType())
if err != nil {
return nil, err
}
instanceState, err := res.ShimInstanceStateFromValue(rio.Value)
if err != nil {
return nil, err
}
return instanceState.Attributes, nil
}
func shimLegacyState(legacy *terraform.State) (*states.State, error) {
state, err := terraform.ShimLegacyState(legacy)
if err != nil {
return nil, err
}
if state.HasResources() {
for _, module := range state.Modules {
for name, resource := range module.Resources {
module.Resources[name].ProviderConfig.Provider = addrs.ImpliedProviderForUnqualifiedType(resource.Addr.Resource.ImpliedProvider())
}
}
}
return state, err
}
// legacyProviderConfigString was copied from addrs.Provider.LegacyString() to
// create a legacy-style string from a non-legacy provider. This is only
// necessary as this package shims back and forth between legacy and modern
// state, neither of which encode the addrs.Provider for a resource.
func legacyProviderConfigString(pc addrs.AbsProviderConfig) string {
if pc.Alias != "" {
if len(pc.Module) == 0 {
return fmt.Sprintf("%s.%s.%s", "provider", pc.Provider.Type, pc.Alias)
} else {
return fmt.Sprintf("%s.%s.%s.%s", pc.Module.String(), "provider", pc.Provider.LegacyString(), pc.Alias)
}
}
if len(pc.Module) == 0 {
return fmt.Sprintf("%s.%s", "provider", pc.Provider.Type)
}
return fmt.Sprintf("%s.%s.%s", pc.Module.String(), "provider", pc.Provider.Type)
}