kitchen-terraform 5.1.1 → 5.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (110) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/README.md +8 -10
  5. data/lib/kitchen/driver/terraform.rb +196 -514
  6. data/lib/kitchen/provisioner/terraform.rb +88 -86
  7. data/lib/kitchen/terraform.rb +5 -6
  8. data/lib/kitchen/terraform/breaking.rb +7 -5
  9. data/lib/kitchen/terraform/command.rb +7 -5
  10. data/lib/kitchen/terraform/command/apply.rb +87 -0
  11. data/lib/kitchen/terraform/command/destroy.rb +88 -0
  12. data/lib/kitchen/terraform/command/get.rb +32 -0
  13. data/lib/kitchen/terraform/command/init.rb +102 -0
  14. data/lib/kitchen/terraform/command/output.rb +11 -50
  15. data/lib/kitchen/terraform/command/validate.rb +63 -0
  16. data/lib/kitchen/terraform/command/version.rb +5 -42
  17. data/lib/kitchen/terraform/command/workspace_delete.rb +41 -0
  18. data/lib/kitchen/terraform/command/workspace_new.rb +43 -0
  19. data/lib/kitchen/terraform/command/workspace_select.rb +43 -0
  20. data/lib/kitchen/terraform/command_executor.rb +60 -0
  21. data/lib/kitchen/terraform/command_flag.rb +23 -0
  22. data/lib/kitchen/terraform/command_flag/backend_config.rb +43 -0
  23. data/lib/kitchen/terraform/command_flag/color.rb +45 -0
  24. data/lib/kitchen/terraform/command_flag/lock_timeout.rb +42 -0
  25. data/lib/kitchen/terraform/command_flag/plugin_dir.rb +47 -0
  26. data/lib/kitchen/terraform/command_flag/upgrade.rb +45 -0
  27. data/lib/kitchen/terraform/command_flag/var.rb +43 -0
  28. data/lib/kitchen/terraform/command_flag/var_file.rb +45 -0
  29. data/lib/kitchen/terraform/config_attribute.rb +58 -55
  30. data/lib/kitchen/terraform/config_attribute/backend_configurations.rb +1 -1
  31. data/lib/kitchen/terraform/config_attribute/client.rb +10 -3
  32. data/lib/kitchen/terraform/config_attribute/color.rb +1 -1
  33. data/lib/kitchen/terraform/config_attribute/command_timeout.rb +1 -1
  34. data/lib/kitchen/terraform/config_attribute/fail_fast.rb +1 -1
  35. data/lib/kitchen/terraform/config_attribute/lock.rb +1 -1
  36. data/lib/kitchen/terraform/config_attribute/lock_timeout.rb +1 -1
  37. data/lib/kitchen/terraform/config_attribute/parallelism.rb +1 -1
  38. data/lib/kitchen/terraform/config_attribute/plugin_directory.rb +3 -3
  39. data/lib/kitchen/terraform/config_attribute/root_module_directory.rb +4 -4
  40. data/lib/kitchen/terraform/config_attribute/systems.rb +1 -1
  41. data/lib/kitchen/terraform/config_attribute/variable_files.rb +4 -4
  42. data/lib/kitchen/terraform/config_attribute/variables.rb +1 -1
  43. data/lib/kitchen/terraform/config_attribute/verify_version.rb +2 -2
  44. data/lib/kitchen/terraform/config_attribute_cacher.rb +27 -25
  45. data/lib/kitchen/terraform/config_attribute_definer.rb +37 -43
  46. data/lib/kitchen/terraform/config_attribute_type.rb +7 -5
  47. data/lib/kitchen/terraform/config_attribute_type/hash_of_symbols_and_strings.rb +32 -27
  48. data/lib/kitchen/terraform/config_attribute_type/integer.rb +4 -4
  49. data/lib/kitchen/terraform/config_predicates.rb +7 -5
  50. data/lib/kitchen/terraform/config_predicates/hash_of_symbols_and_strings.rb +6 -6
  51. data/lib/kitchen/terraform/config_schemas.rb +7 -5
  52. data/lib/kitchen/terraform/config_schemas/array_of_strings.rb +20 -13
  53. data/lib/kitchen/terraform/config_schemas/boolean.rb +16 -9
  54. data/lib/kitchen/terraform/config_schemas/optional_string.rb +20 -13
  55. data/lib/kitchen/terraform/config_schemas/string.rb +13 -7
  56. data/lib/kitchen/terraform/config_schemas/system.rb +14 -12
  57. data/lib/kitchen/terraform/config_schemas/systems.rb +5 -2
  58. data/lib/kitchen/terraform/configurable.rb +41 -21
  59. data/lib/kitchen/terraform/debug_logger.rb +1 -1
  60. data/lib/kitchen/terraform/deprecating.rb +7 -5
  61. data/lib/kitchen/terraform/driver.rb +23 -0
  62. data/lib/kitchen/terraform/driver/create.rb +120 -0
  63. data/lib/kitchen/terraform/driver/destroy.rb +169 -0
  64. data/lib/kitchen/terraform/error.rb +7 -5
  65. data/lib/kitchen/terraform/file_path_config_attribute_definer.rb +30 -27
  66. data/lib/kitchen/terraform/inspec.rb +3 -57
  67. data/lib/kitchen/terraform/inspec/fail_fast_with_hosts.rb +59 -0
  68. data/lib/kitchen/terraform/inspec/fail_slow_with_hosts.rb +69 -0
  69. data/lib/kitchen/terraform/inspec/without_hosts.rb +52 -0
  70. data/lib/kitchen/terraform/inspec_factory.rb +68 -0
  71. data/lib/kitchen/terraform/inspec_options_factory.rb +67 -0
  72. data/lib/kitchen/terraform/inspec_runner.rb +88 -0
  73. data/lib/kitchen/terraform/outputs_manager.rb +64 -0
  74. data/lib/kitchen/terraform/outputs_parser.rb +39 -0
  75. data/lib/kitchen/terraform/outputs_reader.rb +63 -0
  76. data/lib/kitchen/terraform/provisioner.rb +23 -0
  77. data/lib/kitchen/terraform/provisioner/converge.rb +196 -0
  78. data/lib/kitchen/terraform/raise.rb +23 -0
  79. data/lib/kitchen/terraform/raise/action_failed.rb +49 -0
  80. data/lib/kitchen/terraform/raise/client_error.rb +49 -0
  81. data/lib/kitchen/terraform/shell_out.rb +90 -59
  82. data/lib/kitchen/terraform/system.rb +62 -94
  83. data/lib/kitchen/terraform/system_attrs_inputs_resolver.rb +49 -0
  84. data/lib/kitchen/terraform/system_attrs_outputs_resolver.rb +80 -0
  85. data/lib/kitchen/terraform/system_hosts_resolver.rb +39 -17
  86. data/lib/kitchen/terraform/system_inspec_map.rb +50 -0
  87. data/lib/kitchen/terraform/systems_verifier.rb +23 -0
  88. data/lib/kitchen/terraform/systems_verifier/fail_fast.rb +52 -0
  89. data/lib/kitchen/terraform/systems_verifier/fail_slow.rb +62 -0
  90. data/lib/kitchen/terraform/systems_verifier_factory.rb +50 -0
  91. data/lib/kitchen/terraform/unsupported_client_version_error.rb +26 -0
  92. data/lib/kitchen/terraform/variables_manager.rb +64 -0
  93. data/lib/kitchen/terraform/verify_version.rb +63 -20
  94. data/lib/kitchen/terraform/verify_version_rescue_strategy.rb +23 -0
  95. data/lib/kitchen/terraform/verify_version_rescue_strategy/permissive.rb +50 -0
  96. data/lib/kitchen/terraform/verify_version_rescue_strategy/strict.rb +47 -0
  97. data/lib/kitchen/terraform/verify_version_rescue_strategy_factory.rb +51 -0
  98. data/lib/kitchen/terraform/version.rb +59 -58
  99. data/lib/kitchen/terraform/version_verifier.rb +50 -0
  100. data/lib/kitchen/terraform/version_verifier_strategy.rb +25 -0
  101. data/lib/kitchen/terraform/version_verifier_strategy/supported.rb +33 -0
  102. data/lib/kitchen/terraform/version_verifier_strategy/unsupported.rb +34 -0
  103. data/lib/kitchen/terraform/version_verifier_strategy_factory.rb +50 -0
  104. data/lib/kitchen/verifier/terraform.rb +71 -62
  105. metadata +132 -42
  106. metadata.gz.sig +0 -0
  107. data/lib/kitchen/terraform/inspec_options_mapper.rb +0 -73
  108. data/lib/kitchen/terraform/inspec_with_hosts.rb +0 -50
  109. data/lib/kitchen/terraform/inspec_without_hosts.rb +0 -45
  110. data/lib/kitchen/terraform/system_attrs_resolver.rb +0 -60
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 87c376e5a70ddafbed0a060b327f76d89447a975131983a7f4593c0987d4fdac
4
- data.tar.gz: a41d6d0fc75aaec9de8cddc2adc236fe3e29b43da7765f7916a1bb5e1e9dd798
3
+ metadata.gz: df1c8cf5af302e8ff0157356cdcd17b629b4613a279f44f6776b1c83532fee55
4
+ data.tar.gz: ddf2fee8e8b4191bdae7305ca3c2fb1464c58aa9eff9edbfe14495051f31d757
5
5
  SHA512:
6
- metadata.gz: f4ff76d4c4a2357a0651b29524b371358b9f5f15bf9d0db494c6f4141061b97e22e0ea8da8ca7783f9514810089589d820d38edc0252f79309828205ca4cf3a7
7
- data.tar.gz: 8a5301f8d490038734cda34b3a637d24596ed42ec310692581075f4befb3533ff637e417256316a010f512e59a1cb914bd6132d977171ad22008e9becce0a2c5
6
+ metadata.gz: 179e8d7217c594ad88c2bcfe3c81537a7ac18a4b9a0ff9645a56261ec6143468e08d026e99fdd105c253a8567e19f0df9293a1b91a0143a81687ae7b69c963ac
7
+ data.tar.gz: f32b9ebb0be2ca34102d51650bf0d73e83807a6d49ab38de21aac89c188be9850ff9d2c1a995d9a154c4a83fe044a3529f2ee53f3f8153158b77049bbd655aee
Binary file
data.tar.gz.sig CHANGED
Binary file
data/README.md CHANGED
@@ -75,10 +75,7 @@ the semantic versioning of the Ruby gem.
75
75
 
76
76
  ```ruby
77
77
  source "https://rubygems.org/" do
78
- gem(
79
- "kitchen-terraform",
80
- "~> 4.0"
81
- )
78
+ gem "kitchen-terraform", "~> 5.2"
82
79
  end
83
80
  ```
84
81
 
@@ -105,7 +102,7 @@ example.
105
102
  > Installing Kitchen-Terraform with RubyGems
106
103
 
107
104
  ```sh
108
- gem install kitchen-terraform --version 4.0.0
105
+ gem install kitchen-terraform --version 5.2.0
109
106
  ```
110
107
 
111
108
  This approach is not recommended as it requires more effort to install
@@ -183,10 +180,10 @@ hierarchy of files comprising the following blocks.
183
180
  ```ruby
184
181
  source "https://rubygems.org/"
185
182
 
186
- gem 'kitchen-terraform'
183
+ gem 'kitchen-terraform', '~> 5.1'
187
184
  ```
188
185
 
189
- > ./.kitchen.yml (Test Kitchen configuration)
186
+ > ./kitchen.yml (Test Kitchen configuration)
190
187
 
191
188
  ```yaml
192
189
  driver:
@@ -212,11 +209,12 @@ suites:
212
209
  - name: example
213
210
  ```
214
211
 
212
+ Although Kitchen-Terraform supports multiple versions of Terraform, below snippets are compatible with v0.12:
215
213
  > ./main.tf
216
214
 
217
215
  ```hcl
218
216
  provider "docker" {
219
- host = "unix://localhost/var/run/docker.sock"
217
+ host = "unix:///var/run/docker.sock"
220
218
  }
221
219
 
222
220
  data "docker_registry_image" "ubuntu" {
@@ -224,12 +222,12 @@ data "docker_registry_image" "ubuntu" {
224
222
  }
225
223
 
226
224
  resource "docker_image" "ubuntu" {
227
- name = "${data.docker_registry_image.ubuntu.name}"
225
+ name = data.docker_registry_image.ubuntu.name
228
226
  pull_triggers = ["${data.docker_registry_image.ubuntu.sha256_digest}"]
229
227
  }
230
228
 
231
229
  resource "docker_container" "ubuntu" {
232
- image = "${docker_image.ubuntu.name}"
230
+ image = docker_image.ubuntu.name
233
231
  must_run = true
234
232
  name = "ubuntu_container"
235
233
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2016 New Context Services, Inc.
3
+ # Copyright 2016-2019 New Context, Inc.
4
4
  #
5
5
  # Licensed under the Apache License, Version 2.0 (the "License");
6
6
  # you may not use this file except in compliance with the License.
@@ -15,14 +15,13 @@
15
15
  # limitations under the License.
16
16
 
17
17
  require "kitchen"
18
- require "kitchen/terraform/command/output"
19
- require "kitchen/terraform/command/version"
18
+ require "kitchen/terraform/raise/action_failed"
20
19
  require "kitchen/terraform/config_attribute/backend_configurations"
21
20
  require "kitchen/terraform/config_attribute/client"
22
21
  require "kitchen/terraform/config_attribute/color"
23
22
  require "kitchen/terraform/config_attribute/command_timeout"
24
- require "kitchen/terraform/config_attribute/lock"
25
23
  require "kitchen/terraform/config_attribute/lock_timeout"
24
+ require "kitchen/terraform/config_attribute/lock"
26
25
  require "kitchen/terraform/config_attribute/parallelism"
27
26
  require "kitchen/terraform/config_attribute/plugin_directory"
28
27
  require "kitchen/terraform/config_attribute/root_module_directory"
@@ -30,521 +29,204 @@ require "kitchen/terraform/config_attribute/variable_files"
30
29
  require "kitchen/terraform/config_attribute/variables"
31
30
  require "kitchen/terraform/config_attribute/verify_version"
32
31
  require "kitchen/terraform/configurable"
33
- require "kitchen/terraform/debug_logger"
34
- require "kitchen/terraform/shell_out"
35
- require "kitchen/terraform/verify_version"
32
+ require "kitchen/terraform/driver/create"
33
+ require "kitchen/terraform/driver/destroy"
34
+ require "kitchen/terraform/version_verifier"
35
+ require "rubygems"
36
36
  require "shellwords"
37
37
 
38
38
  # This namespace is defined by Kitchen.
39
39
  #
40
- # @see http://www.rubydoc.info/gems/test-kitchen/Kitchen/Driver
41
- module ::Kitchen::Driver
42
- end
43
-
44
- # The driver is the bridge between Test Kitchen and Terraform. It manages the
45
- # {https://www.terraform.io/docs/state/index.html state} of the Terraform root module by shelling out and running
46
- # Terraform commands.
47
- #
48
- # === Commands
49
- #
50
- # The following command-line commands are provided by the driver.
51
- #
52
- # ==== kitchen create
53
- #
54
- # A Test Kitchen instance is created through the following steps.
55
- #
56
- # ===== Initializing the Terraform Working Directory
57
- #
58
- # terraform init \
59
- # -input=false \
60
- # -lock=<lock> \
61
- # -lock-timeout=<lock_timeout>s \
62
- # [-no-color] \
63
- # -upgrade \
64
- # -force-copy \
65
- # -backend=true \
66
- # [-backend-config=<backend_configurations.first> ...] \
67
- # -get=true \
68
- # -get-plugins=true \
69
- # [-plugin-dir=<plugin_directory>] \
70
- # -verify-plugins=true \
71
- # <root_module_directory>
72
- #
73
- # ===== Creating a Test Terraform Workspace
74
- #
75
- # terraform workspace <new|select> kitchen-terraform-<instance>
76
- #
77
- # ==== kitchen destroy
78
- #
79
- # A Test Kitchen instance is destroyed through the following steps.
80
- #
81
- # ===== Initializing the Terraform Working Directory
82
- #
83
- # terraform init \
84
- # -input=false \
85
- # -lock=<lock> \
86
- # -lock-timeout=<lock_timeout>s \
87
- # [-no-color] \
88
- # -force-copy \
89
- # -backend=true \
90
- # [-backend-config=<backend_configurations.first>...] \
91
- # -get=true \
92
- # -get-plugins=true \
93
- # [-plugin-dir=<plugin_directory>] \
94
- # -verify-plugins=true \
95
- # <root_module_directory>
96
- #
97
- # ===== Selecting the Test Terraform Workspace
98
- #
99
- # terraform workspace <select|new> kitchen-terraform-<instance>
100
- #
101
- # ===== Destroying the Terraform State
102
- #
103
- # terraform destroy \
104
- # -auto-approve \
105
- # -lock=<lock> \
106
- # -lock-timeout=<lock_timeout>s \
107
- # -input=false \
108
- # [-no-color] \
109
- # -parallelism=<parallelism> \
110
- # -refresh=true \
111
- # [-var=<variables.first>...] \
112
- # [-var-file=<variable_files.first>...] \
113
- # <root_module_directory>
114
- #
115
- # ===== Selecting the Default Terraform Workspace
116
- #
117
- # terraform workspace select default
118
- #
119
- # ===== Deleting the Test Terraform Workspace
120
- #
121
- # terraform workspace delete kitchen-terraform-<instance>
122
- #
123
- # === Shelling Out
124
- #
125
- # {include:Kitchen::Terraform::ShellOut}
126
- #
127
- # === Configuration Attributes
128
- #
129
- # The configuration attributes of the driver control the behaviour of the Terraform commands that are run. Within the
130
- # {http://kitchen.ci/docs/getting-started/kitchen-yml Test Kitchen configuration file}, these attributes must be
131
- # declared in the +driver+ mapping along with the plugin name.
132
- #
133
- # driver:
134
- # name: terraform
135
- # a_configuration_attribute: some value
136
- #
137
- # ==== backend_configurations
138
- #
139
- # {include:Kitchen::Terraform::ConfigAttribute::BackendConfigurations}
140
- #
141
- # ==== client
142
- #
143
- # {include:Kitchen::Terraform::ConfigAttribute::Client}
144
- #
145
- # ==== color
146
- #
147
- # {include:Kitchen::Terraform::ConfigAttribute::Color}
148
- #
149
- # ==== command_timeout
150
- #
151
- # {include:Kitchen::Terraform::ConfigAttribute::CommandTimeout}
152
- #
153
- # ==== lock
154
- #
155
- # {include:Kitchen::Terraform::ConfigAttribute::Lock}
156
- #
157
- # ==== lock_timeout
158
- #
159
- # {include:Kitchen::Terraform::ConfigAttribute::LockTimeout}
160
- #
161
- # ==== parallelism
162
- #
163
- # {include:Kitchen::Terraform::ConfigAttribute::Parallelism}
164
- #
165
- # ==== plugin_directory
166
- #
167
- # {include:Kitchen::Terraform::ConfigAttribute::PluginDirectory}
168
- #
169
- # ==== root_module_directory
170
- #
171
- # {include:Kitchen::Terraform::ConfigAttribute::RootModuleDirectory}
172
- #
173
- # ==== variable_files
174
- #
175
- # {include:Kitchen::Terraform::ConfigAttribute::VariableFiles}
176
- #
177
- # ==== variables
178
- #
179
- # {include:Kitchen::Terraform::ConfigAttribute::Variables}
180
- #
181
- # ==== verify_version
182
- #
183
- # {include:Kitchen::Terraform::ConfigAttribute::VerifyVersion}
184
- #
185
- # @example Describe the create command
186
- # kitchen help create
187
- # @example Create a Test Kitchen instance
188
- # kitchen create default-ubuntu
189
- # @example Describe the destroy command
190
- # kitchen help destroy
191
- # @example Destroy a Test Kitchen instance
192
- # kitchen destroy default-ubuntu
193
- # @version 2
194
- class ::Kitchen::Driver::Terraform < ::Kitchen::Driver::Base
195
- kitchen_driver_api_version 2
196
-
197
- no_parallel_for(
198
- :create,
199
- :converge,
200
- :setup,
201
- :destroy
202
- )
203
-
204
- include ::Kitchen::Terraform::ConfigAttribute::BackendConfigurations
205
-
206
- include ::Kitchen::Terraform::ConfigAttribute::Client
207
-
208
- include ::Kitchen::Terraform::ConfigAttribute::Color
209
-
210
- include ::Kitchen::Terraform::ConfigAttribute::CommandTimeout
211
-
212
- include ::Kitchen::Terraform::ConfigAttribute::Lock
213
-
214
- include ::Kitchen::Terraform::ConfigAttribute::LockTimeout
215
-
216
- include ::Kitchen::Terraform::ConfigAttribute::Parallelism
217
-
218
- include ::Kitchen::Terraform::ConfigAttribute::PluginDirectory
219
-
220
- include ::Kitchen::Terraform::ConfigAttribute::RootModuleDirectory
221
-
222
- include ::Kitchen::Terraform::ConfigAttribute::VariableFiles
223
-
224
- include ::Kitchen::Terraform::ConfigAttribute::Variables
225
-
226
- include ::Kitchen::Terraform::ConfigAttribute::VerifyVersion
227
-
228
- include ::Kitchen::Terraform::Configurable
229
-
230
- # Applies changes to the state by selecting the test workspace, updating the dependency modules, validating the root
231
- # module, and applying the state changes.
232
- #
233
- # @raise [::Kitchen::ActionFailed] if the result of the action is a failure.
234
- # @return [void]
235
- def apply(&block)
236
- verify_version
237
- run_workspace_select_instance
238
- apply_run
239
- rescue ::Kitchen::Terraform::Error => error
240
- raise ::Kitchen::ActionFailed, error.message
241
- end
242
-
243
- # Creates a Test Kitchen instance by initializing the working directory and creating a test workspace.
244
- #
245
- # @param _state [::Hash] the mutable instance and driver state.
246
- # @raise [::Kitchen::ActionFailed] if the result of the action is a failure.
247
- # @return [void]
248
- def create(_state)
249
- verify_version
250
- create_run_init
251
- run_workspace_select_instance
252
- rescue ::Kitchen::Terraform::Error => error
253
- raise ::Kitchen::ActionFailed, error.message
254
- end
255
-
256
- # Destroys a Test Kitchen instance by initializing the working directory, selecting the test workspace,
257
- # deleting the state, selecting the default workspace, and deleting the test workspace.
258
- #
259
- # @param _state [::Hash] the mutable instance and driver state.
260
- # @raise [::Kitchen::ActionFailed] if the result of the action is a failure.
261
- # @return [void]
262
- def destroy(_state)
263
- verify_version
264
- destroy_run_init
265
- run_workspace_select_instance
266
- destroy_run_destroy
267
- destroy_run_workspace_select_default
268
- destroy_run_workspace_delete_instance
269
- rescue ::Kitchen::Terraform::Error => error
270
- raise ::Kitchen::ActionFailed, error.message
271
- end
272
-
273
- # Retrieves the Terraform input variables for a Kitchen instance provided by the configuration.
274
- #
275
- # @return [self]
276
- # @yieldparam inputs [::Hash] the input variables.
277
- def retrieve_inputs
278
- yield inputs: config_variables
279
-
280
- self
281
- end
282
-
283
- # Retrieves the Terraform state outputs for a Kitchen instance by selecting the test workspace and fetching the
284
- # outputs.
40
+ # @see http://www.rubydoc.info/gems/test-kitchen/Kitchen
41
+ module Kitchen
42
+ # This namespace is defined by Kitchen.
285
43
  #
286
- # @raise [::Kitchen::ActionFailed] if the result of the action is a failure.
287
- # @return [self]
288
- # @yieldparam outputs [::Hash] the state output.
289
- def retrieve_outputs(&block)
290
- run_workspace_select_instance
291
- ::Kitchen::Terraform::Command::Output.run(
292
- client: config_client,
293
- options: { cwd: config_root_module_directory, live_stream: debug_logger, timeout: config_command_timeout },
294
- &block
295
- )
296
-
297
- self
298
- rescue ::Kitchen::Terraform::Error => error
299
- raise ::Kitchen::ActionFailed, error.message
300
- end
301
-
302
- private
303
-
304
- attr_accessor :debug_logger
305
-
306
- def apply_run
307
- apply_run_get
308
- apply_run_validate
309
- apply_run_apply
310
- end
311
-
312
- # @api private
313
- def apply_run_apply
314
- ::Kitchen::Terraform::ShellOut.run(
315
- client: config_client,
316
- command: "apply " \
317
- "#{lock_flag} " \
318
- "#{lock_timeout_flag} " \
319
- "-input=false " \
320
- "-auto-approve=true " \
321
- "#{color_flag} " \
322
- "#{parallelism_flag} " \
323
- "-refresh=true " \
324
- "#{variables_flags} " \
325
- "#{variable_files_flags}",
326
- options: {
327
- cwd: config_root_module_directory,
328
- live_stream: logger,
329
- timeout: config_command_timeout,
330
- },
331
- )
332
- end
333
-
334
- # @api private
335
- def apply_run_get
336
- ::Kitchen::Terraform::ShellOut.run(
337
- client: config_client,
338
- command: "get -update",
339
- options: {
340
- cwd: config_root_module_directory,
341
- live_stream: logger,
342
- timeout: config_command_timeout,
343
- },
344
- )
345
- end
346
-
347
- # @api private
348
- def apply_run_validate
349
- ::Kitchen::Terraform::ShellOut.run(
350
- client: config_client,
351
- command: "validate " \
352
- "#{color_flag} " \
353
- "#{variables_flags} " \
354
- "#{variable_files_flags}",
355
- options: {
356
- cwd: config_root_module_directory,
357
- live_stream: logger,
358
- timeout: config_command_timeout,
359
- },
360
- )
361
- end
362
-
363
- # @api private
364
- def backend_configurations_flags
365
- config_backend_configurations.map do |key, value|
366
- "-backend-config=\"#{key}=#{value}\""
367
- end.join " "
368
- end
369
-
370
- # api private
371
- def color_flag
372
- config_color and "" or "-no-color"
373
- end
374
-
375
- # @api private
376
- def create_run_init
377
- ::Kitchen::Terraform::ShellOut.run(
378
- client: config_client,
379
- command: "init " \
380
- "-input=false " \
381
- "#{lock_flag} " \
382
- "#{lock_timeout_flag} " \
383
- "#{color_flag} " \
384
- "-upgrade " \
385
- "-force-copy " \
386
- "-backend=true " \
387
- "#{backend_configurations_flags} " \
388
- "-get=true " \
389
- "-get-plugins=true " \
390
- "#{plugin_directory_flag}" \
391
- "-verify-plugins=true",
392
- options: {
393
- cwd: config_root_module_directory,
394
- live_stream: logger,
395
- timeout: config_command_timeout,
396
- },
397
- )
398
- end
399
-
400
- # @api private
401
- def destroy_run_destroy
402
- ::Kitchen::Terraform::ShellOut.run(
403
- client: config_client,
404
- command: "destroy " \
405
- "-auto-approve " \
406
- "#{lock_flag} " \
407
- "#{lock_timeout_flag} " \
408
- "-input=false " \
409
- "#{color_flag} " \
410
- "#{parallelism_flag} " \
411
- "-refresh=true " \
412
- "#{variables_flags} " \
413
- "#{variable_files_flags}",
414
- options: {
415
- cwd: config_root_module_directory,
416
- environment: { "TF_WARN_OUTPUT_ERRORS" => "true" },
417
- live_stream: logger,
418
- timeout: config_command_timeout,
419
- },
420
- )
421
- end
422
-
423
- # @api private
424
- def destroy_run_init
425
- ::Kitchen::Terraform::ShellOut.run(
426
- client: config_client,
427
- command: "init " \
428
- "-input=false " \
429
- "#{lock_flag} " \
430
- "#{lock_timeout_flag} " \
431
- "#{color_flag} " \
432
- "-force-copy " \
433
- "-backend=true " \
434
- "#{backend_configurations_flags} " \
435
- "-get=true " \
436
- "-get-plugins=true " \
437
- "#{plugin_directory_flag}" \
438
- "-verify-plugins=true",
439
- options: {
440
- cwd: config_root_module_directory,
441
- live_stream: logger,
442
- timeout: config_command_timeout,
443
- },
444
- )
445
- end
446
-
447
- # @api private
448
- def destroy_run_workspace_delete_instance
449
- ::Kitchen::Terraform::ShellOut.run(
450
- client: config_client,
451
- command: "workspace delete #{workspace_name}",
452
- options: {
453
- cwd: config_root_module_directory,
454
- live_stream: logger,
455
- timeout: config_command_timeout,
456
- },
457
- )
458
- end
459
-
460
- # @api private
461
- def destroy_run_workspace_select_default
462
- ::Kitchen::Terraform::ShellOut.run(
463
- client: config_client,
464
- command: "workspace select default",
465
- options: {
466
- cwd: config_root_module_directory,
467
- live_stream: logger,
468
- timeout: config_command_timeout,
469
- },
470
- )
471
- end
472
-
473
- def initialize(config = {})
474
- super
475
- self.debug_logger = ::Kitchen::Terraform::DebugLogger.new logger
476
- end
477
-
478
- # @api private
479
- def lock_flag
480
- "-lock=#{config_lock}"
481
- end
482
-
483
- # @api private
484
- def lock_timeout_flag
485
- "-lock-timeout=#{config_lock_timeout}s"
486
- end
487
-
488
- # @api private
489
- def parallelism_flag
490
- "-parallelism=#{config_parallelism}"
491
- end
492
-
493
- # @api private
494
- def plugin_directory_flag
495
- if config_plugin_directory
496
- "-plugin-dir=\"#{::Shellwords.shelljoin ::Shellwords.shellsplit config_plugin_directory}\" "
497
- else
498
- ""
499
- end
500
- end
501
-
502
- # @api private
503
- def run_workspace_select_instance
504
- ::Kitchen::Terraform::ShellOut.run(
505
- client: config_client,
506
- command: "workspace select #{workspace_name}",
507
- options: {
508
- cwd: config_root_module_directory,
509
- live_stream: logger,
510
- timeout: config_command_timeout,
511
- },
512
- )
513
- rescue ::Kitchen::Terraform::Error
514
- ::Kitchen::Terraform::ShellOut.run(
515
- client: config_client,
516
- command: "workspace new #{workspace_name}",
517
- options: {
518
- cwd: config_root_module_directory,
519
- live_stream: logger,
520
- timeout: config_command_timeout,
521
- },
522
- )
523
- end
524
-
525
- # @api private
526
- def variable_files_flags
527
- config_variable_files.map do |path|
528
- "-var-file=\"#{::Shellwords.shelljoin ::Shellwords.shellsplit path}\""
529
- end.join " "
530
- end
531
-
532
- # @api private
533
- def variables_flags
534
- config_variables.map do |key, value|
535
- "-var=\"#{key}=#{value}\""
536
- end.join " "
537
- end
538
-
539
- def verify_version
540
- if config_verify_version
541
- ::Kitchen::Terraform::VerifyVersion.call
542
- else
543
- logger.warn "Verification of support for the available version of Terraform is disabled"
44
+ # @see http://www.rubydoc.info/gems/test-kitchen/Kitchen/Driver
45
+ module Driver
46
+
47
+ # The driver is the bridge between Test Kitchen and Terraform. It manages the
48
+ # {https://www.terraform.io/docs/state/index.html state} of the Terraform root module by shelling out and running
49
+ # Terraform commands.
50
+ #
51
+ # === Commands
52
+ #
53
+ # The following command-line commands are provided by the driver.
54
+ #
55
+ # ==== kitchen create
56
+ #
57
+ # {include:Kitchen::Terraform::Driver::Create}
58
+ #
59
+ # ==== kitchen destroy
60
+ #
61
+ # {include:Kitchen::Terraform::Driver::Destroy}
62
+ #
63
+ # === Configuration Attributes
64
+ #
65
+ # The configuration attributes of the driver control the behaviour of the Terraform commands that are run. Within the
66
+ # {http://kitchen.ci/docs/getting-started/kitchen-yml Test Kitchen configuration file}, these attributes must be
67
+ # declared in the +driver+ mapping along with the plugin name.
68
+ #
69
+ # driver:
70
+ # name: terraform
71
+ # a_configuration_attribute: some value
72
+ #
73
+ # ==== backend_configurations
74
+ #
75
+ # {include:Kitchen::Terraform::ConfigAttribute::BackendConfigurations}
76
+ #
77
+ # ==== client
78
+ #
79
+ # {include:Kitchen::Terraform::ConfigAttribute::Client}
80
+ #
81
+ # ==== color
82
+ #
83
+ # {include:Kitchen::Terraform::ConfigAttribute::Color}
84
+ #
85
+ # ==== command_timeout
86
+ #
87
+ # {include:Kitchen::Terraform::ConfigAttribute::CommandTimeout}
88
+ #
89
+ # ==== lock
90
+ #
91
+ # {include:Kitchen::Terraform::ConfigAttribute::Lock}
92
+ #
93
+ # ==== lock_timeout
94
+ #
95
+ # {include:Kitchen::Terraform::ConfigAttribute::LockTimeout}
96
+ #
97
+ # ==== parallelism
98
+ #
99
+ # {include:Kitchen::Terraform::ConfigAttribute::Parallelism}
100
+ #
101
+ # ==== plugin_directory
102
+ #
103
+ # {include:Kitchen::Terraform::ConfigAttribute::PluginDirectory}
104
+ #
105
+ # ==== root_module_directory
106
+ #
107
+ # {include:Kitchen::Terraform::ConfigAttribute::RootModuleDirectory}
108
+ #
109
+ # ==== variable_files
110
+ #
111
+ # {include:Kitchen::Terraform::ConfigAttribute::VariableFiles}
112
+ #
113
+ # ==== variables
114
+ #
115
+ # {include:Kitchen::Terraform::ConfigAttribute::Variables}
116
+ #
117
+ # ==== verify_version
118
+ #
119
+ # {include:Kitchen::Terraform::ConfigAttribute::VerifyVersion}
120
+ #
121
+ # === Ruby Interface
122
+ #
123
+ # This class implements the interface of Kitchen::Configurable which requires the following Reek suppressions:
124
+ # :reek:MissingSafeMethod { exclude: [ finalize_config! ] }
125
+ #
126
+ # @example Describe the create command
127
+ # kitchen help create
128
+ # @example Create a Test Kitchen instance
129
+ # kitchen create default-ubuntu
130
+ # @example Describe the destroy command
131
+ # kitchen help destroy
132
+ # @example Destroy a Test Kitchen instance
133
+ # kitchen destroy default-ubuntu
134
+ # @version 2
135
+ class Terraform < ::Kitchen::Driver::Base
136
+ kitchen_driver_api_version 2
137
+
138
+ no_parallel_for(
139
+ :create,
140
+ :converge,
141
+ :setup,
142
+ :destroy
143
+ )
144
+
145
+ include ::Kitchen::Terraform::ConfigAttribute::BackendConfigurations
146
+
147
+ include ::Kitchen::Terraform::ConfigAttribute::Client
148
+
149
+ include ::Kitchen::Terraform::ConfigAttribute::Color
150
+
151
+ include ::Kitchen::Terraform::ConfigAttribute::CommandTimeout
152
+
153
+ include ::Kitchen::Terraform::ConfigAttribute::Lock
154
+
155
+ include ::Kitchen::Terraform::ConfigAttribute::LockTimeout
156
+
157
+ include ::Kitchen::Terraform::ConfigAttribute::Parallelism
158
+
159
+ include ::Kitchen::Terraform::ConfigAttribute::PluginDirectory
160
+
161
+ include ::Kitchen::Terraform::ConfigAttribute::RootModuleDirectory
162
+
163
+ include ::Kitchen::Terraform::ConfigAttribute::VariableFiles
164
+
165
+ include ::Kitchen::Terraform::ConfigAttribute::Variables
166
+
167
+ include ::Kitchen::Terraform::ConfigAttribute::VerifyVersion
168
+
169
+ include ::Kitchen::Terraform::Configurable
170
+
171
+ # Creates a Test Kitchen instance by initializing the working directory and creating a test workspace.
172
+ #
173
+ # @param _state [Hash] the mutable instance and driver state.
174
+ # @raise [Kitchen::ActionFailed] if the result of the action is a failure.
175
+ # @return [void]
176
+ def create(_state)
177
+ create_strategy.call
178
+ rescue => error
179
+ action_failed.call message: error.message
180
+ end
181
+
182
+ # Destroys a Test Kitchen instance by initializing the working directory, selecting the test workspace,
183
+ # deleting the state, selecting the default workspace, and deleting the test workspace.
184
+ #
185
+ # @param _state [Hash] the mutable instance and driver state.
186
+ # @raise [Kitchen::ActionFailed] if the result of the action is a failure.
187
+ # @return [void]
188
+ def destroy(_state)
189
+ destroy_strategy.call
190
+ rescue => error
191
+ action_failed.call message: error.message
192
+ end
193
+
194
+ # #finalize_config! invokes the super implementation and then initializes the strategies.
195
+ #
196
+ # @param instance [Kitchen::Instance] an associated instance.
197
+ # @raise [Kitchen::ClientError] if the instance is nil.
198
+ # @return [self]
199
+ # @see Kitchen::Configurable#finalize_config!
200
+ def finalize_config!(instance)
201
+ super instance
202
+ self.create_strategy = ::Kitchen::Terraform::Driver::Create.new(
203
+ config: config,
204
+ logger: logger,
205
+ version_requirement: version_requirement,
206
+ workspace_name: workspace_name,
207
+ )
208
+ self.destroy_strategy = ::Kitchen::Terraform::Driver::Destroy.new(
209
+ config: config,
210
+ logger: logger,
211
+ version_requirement: version_requirement,
212
+ workspace_name: workspace_name,
213
+ )
214
+
215
+ self
216
+ end
217
+
218
+ # #initialize prepares a new instance of the class.
219
+ #
220
+ # @param config [Hash] the driver configuration.
221
+ # @return [Kitchen::Driver::Terraform]
222
+ def initialize(config = {})
223
+ super config
224
+ self.action_failed = ::Kitchen::Terraform::Raise::ActionFailed.new logger: logger
225
+ end
226
+
227
+ private
228
+
229
+ attr_accessor :action_failed, :create_strategy, :destroy_strategy
544
230
  end
545
231
  end
546
-
547
- def workspace_name
548
- @workspace_name ||= "kitchen-terraform-#{::Shellwords.escape instance.name}"
549
- end
550
232
  end