diff options
author | Nathan Dench <ndenc2@gmail.com> | 2019-05-24 15:16:44 +1000 |
---|---|---|
committer | Nathan Dench <ndenc2@gmail.com> | 2019-05-24 15:16:44 +1000 |
commit | 107c1cdb09c575aa2f61d97f48d8587eb6bada4c (patch) | |
tree | ca7d008643efc555c388baeaf1d986e0b6b3e28c /vendor/github.com/hashicorp/terraform/helper/resource/testing_import_state.go | |
parent | 844b5a68d8af4791755b8f0ad293cc99f5959183 (diff) | |
download | terraform-provider-statuscake-107c1cdb09c575aa2f61d97f48d8587eb6bada4c.tar.gz terraform-provider-statuscake-107c1cdb09c575aa2f61d97f48d8587eb6bada4c.tar.zst terraform-provider-statuscake-107c1cdb09c575aa2f61d97f48d8587eb6bada4c.zip |
Upgrade to 0.12
Diffstat (limited to 'vendor/github.com/hashicorp/terraform/helper/resource/testing_import_state.go')
-rw-r--r-- | vendor/github.com/hashicorp/terraform/helper/resource/testing_import_state.go | 113 |
1 files changed, 98 insertions, 15 deletions
diff --git a/vendor/github.com/hashicorp/terraform/helper/resource/testing_import_state.go b/vendor/github.com/hashicorp/terraform/helper/resource/testing_import_state.go index 94fef3c..e1b7aea 100644 --- a/vendor/github.com/hashicorp/terraform/helper/resource/testing_import_state.go +++ b/vendor/github.com/hashicorp/terraform/helper/resource/testing_import_state.go | |||
@@ -7,6 +7,12 @@ import ( | |||
7 | "strings" | 7 | "strings" |
8 | 8 | ||
9 | "github.com/davecgh/go-spew/spew" | 9 | "github.com/davecgh/go-spew/spew" |
10 | "github.com/hashicorp/hcl2/hcl" | ||
11 | "github.com/hashicorp/hcl2/hcl/hclsyntax" | ||
12 | |||
13 | "github.com/hashicorp/terraform/addrs" | ||
14 | "github.com/hashicorp/terraform/helper/schema" | ||
15 | "github.com/hashicorp/terraform/states" | ||
10 | "github.com/hashicorp/terraform/terraform" | 16 | "github.com/hashicorp/terraform/terraform" |
11 | ) | 17 | ) |
12 | 18 | ||
@@ -15,6 +21,7 @@ func testStepImportState( | |||
15 | opts terraform.ContextOpts, | 21 | opts terraform.ContextOpts, |
16 | state *terraform.State, | 22 | state *terraform.State, |
17 | step TestStep) (*terraform.State, error) { | 23 | step TestStep) (*terraform.State, error) { |
24 | |||
18 | // Determine the ID to import | 25 | // Determine the ID to import |
19 | var importId string | 26 | var importId string |
20 | switch { | 27 | switch { |
@@ -41,33 +48,53 @@ func testStepImportState( | |||
41 | 48 | ||
42 | // Setup the context. We initialize with an empty state. We use the | 49 | // Setup the context. We initialize with an empty state. We use the |
43 | // full config for provider configurations. | 50 | // full config for provider configurations. |
44 | mod, err := testModule(opts, step) | 51 | cfg, err := testConfig(opts, step) |
45 | if err != nil { | 52 | if err != nil { |
46 | return state, err | 53 | return state, err |
47 | } | 54 | } |
48 | 55 | ||
49 | opts.Module = mod | 56 | opts.Config = cfg |
50 | opts.State = terraform.NewState() | 57 | |
51 | ctx, err := terraform.NewContext(&opts) | 58 | // import tests start with empty state |
52 | if err != nil { | 59 | opts.State = states.NewState() |
53 | return state, err | 60 | |
61 | ctx, stepDiags := terraform.NewContext(&opts) | ||
62 | if stepDiags.HasErrors() { | ||
63 | return state, stepDiags.Err() | ||
54 | } | 64 | } |
55 | 65 | ||
56 | // Do the import! | 66 | // The test step provides the resource address as a string, so we need |
57 | newState, err := ctx.Import(&terraform.ImportOpts{ | 67 | // to parse it to get an addrs.AbsResourceAddress to pass in to the |
68 | // import method. | ||
69 | traversal, hclDiags := hclsyntax.ParseTraversalAbs([]byte(step.ResourceName), "", hcl.Pos{}) | ||
70 | if hclDiags.HasErrors() { | ||
71 | return nil, hclDiags | ||
72 | } | ||
73 | importAddr, stepDiags := addrs.ParseAbsResourceInstance(traversal) | ||
74 | if stepDiags.HasErrors() { | ||
75 | return nil, stepDiags.Err() | ||
76 | } | ||
77 | |||
78 | // Do the import | ||
79 | importedState, stepDiags := ctx.Import(&terraform.ImportOpts{ | ||
58 | // Set the module so that any provider config is loaded | 80 | // Set the module so that any provider config is loaded |
59 | Module: mod, | 81 | Config: cfg, |
60 | 82 | ||
61 | Targets: []*terraform.ImportTarget{ | 83 | Targets: []*terraform.ImportTarget{ |
62 | &terraform.ImportTarget{ | 84 | &terraform.ImportTarget{ |
63 | Addr: step.ResourceName, | 85 | Addr: importAddr, |
64 | ID: importId, | 86 | ID: importId, |
65 | }, | 87 | }, |
66 | }, | 88 | }, |
67 | }) | 89 | }) |
90 | if stepDiags.HasErrors() { | ||
91 | log.Printf("[ERROR] Test: ImportState failure: %s", stepDiags.Err()) | ||
92 | return state, stepDiags.Err() | ||
93 | } | ||
94 | |||
95 | newState, err := shimNewState(importedState, step.providers) | ||
68 | if err != nil { | 96 | if err != nil { |
69 | log.Printf("[ERROR] Test: ImportState failure: %s", err) | 97 | return nil, err |
70 | return state, err | ||
71 | } | 98 | } |
72 | 99 | ||
73 | // Go through the new state and verify | 100 | // Go through the new state and verify |
@@ -75,7 +102,9 @@ func testStepImportState( | |||
75 | var states []*terraform.InstanceState | 102 | var states []*terraform.InstanceState |
76 | for _, r := range newState.RootModule().Resources { | 103 | for _, r := range newState.RootModule().Resources { |
77 | if r.Primary != nil { | 104 | if r.Primary != nil { |
78 | states = append(states, r.Primary) | 105 | is := r.Primary.DeepCopy() |
106 | is.Ephemeral.Type = r.Type // otherwise the check function cannot see the type | ||
107 | states = append(states, is) | ||
79 | } | 108 | } |
80 | } | 109 | } |
81 | if err := step.ImportStateCheck(states); err != nil { | 110 | if err := step.ImportStateCheck(states); err != nil { |
@@ -102,30 +131,84 @@ func testStepImportState( | |||
102 | r.Primary.ID) | 131 | r.Primary.ID) |
103 | } | 132 | } |
104 | 133 | ||
134 | // We'll try our best to find the schema for this resource type | ||
135 | // so we can ignore Removed fields during validation. If we fail | ||
136 | // to find the schema then we won't ignore them and so the test | ||
137 | // will need to rely on explicit ImportStateVerifyIgnore, though | ||
138 | // this shouldn't happen in any reasonable case. | ||
139 | var rsrcSchema *schema.Resource | ||
140 | if providerAddr, diags := addrs.ParseAbsProviderConfigStr(r.Provider); !diags.HasErrors() { | ||
141 | providerType := providerAddr.ProviderConfig.Type | ||
142 | if provider, ok := step.providers[providerType]; ok { | ||
143 | if provider, ok := provider.(*schema.Provider); ok { | ||
144 | rsrcSchema = provider.ResourcesMap[r.Type] | ||
145 | } | ||
146 | } | ||
147 | } | ||
148 | |||
149 | // don't add empty flatmapped containers, so we can more easily | ||
150 | // compare the attributes | ||
151 | skipEmpty := func(k, v string) bool { | ||
152 | if strings.HasSuffix(k, ".#") || strings.HasSuffix(k, ".%") { | ||
153 | if v == "0" { | ||
154 | return true | ||
155 | } | ||
156 | } | ||
157 | return false | ||
158 | } | ||
159 | |||
105 | // Compare their attributes | 160 | // Compare their attributes |
106 | actual := make(map[string]string) | 161 | actual := make(map[string]string) |
107 | for k, v := range r.Primary.Attributes { | 162 | for k, v := range r.Primary.Attributes { |
163 | if skipEmpty(k, v) { | ||
164 | continue | ||
165 | } | ||
108 | actual[k] = v | 166 | actual[k] = v |
109 | } | 167 | } |
168 | |||
110 | expected := make(map[string]string) | 169 | expected := make(map[string]string) |
111 | for k, v := range oldR.Primary.Attributes { | 170 | for k, v := range oldR.Primary.Attributes { |
171 | if skipEmpty(k, v) { | ||
172 | continue | ||
173 | } | ||
112 | expected[k] = v | 174 | expected[k] = v |
113 | } | 175 | } |
114 | 176 | ||
115 | // Remove fields we're ignoring | 177 | // Remove fields we're ignoring |
116 | for _, v := range step.ImportStateVerifyIgnore { | 178 | for _, v := range step.ImportStateVerifyIgnore { |
117 | for k, _ := range actual { | 179 | for k := range actual { |
118 | if strings.HasPrefix(k, v) { | 180 | if strings.HasPrefix(k, v) { |
119 | delete(actual, k) | 181 | delete(actual, k) |
120 | } | 182 | } |
121 | } | 183 | } |
122 | for k, _ := range expected { | 184 | for k := range expected { |
123 | if strings.HasPrefix(k, v) { | 185 | if strings.HasPrefix(k, v) { |
124 | delete(expected, k) | 186 | delete(expected, k) |
125 | } | 187 | } |
126 | } | 188 | } |
127 | } | 189 | } |
128 | 190 | ||
191 | // Also remove any attributes that are marked as "Removed" in the | ||
192 | // schema, if we have a schema to check that against. | ||
193 | if rsrcSchema != nil { | ||
194 | for k := range actual { | ||
195 | for _, schema := range rsrcSchema.SchemasForFlatmapPath(k) { | ||
196 | if schema.Removed != "" { | ||
197 | delete(actual, k) | ||
198 | break | ||
199 | } | ||
200 | } | ||
201 | } | ||
202 | for k := range expected { | ||
203 | for _, schema := range rsrcSchema.SchemasForFlatmapPath(k) { | ||
204 | if schema.Removed != "" { | ||
205 | delete(expected, k) | ||
206 | break | ||
207 | } | ||
208 | } | ||
209 | } | ||
210 | } | ||
211 | |||
129 | if !reflect.DeepEqual(actual, expected) { | 212 | if !reflect.DeepEqual(actual, expected) { |
130 | // Determine only the different attributes | 213 | // Determine only the different attributes |
131 | for k, v := range expected { | 214 | for k, v := range expected { |