kitchen-terraform 5.1.1 → 5.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (111) 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 +37 -173
  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 +38 -16
  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 +86 -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 +206 -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 +63 -93
  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_bastion_host_resolver.rb +72 -0
  86. data/lib/kitchen/terraform/system_hosts_resolver.rb +39 -17
  87. data/lib/kitchen/terraform/system_inspec_map.rb +49 -0
  88. data/lib/kitchen/terraform/systems_verifier.rb +23 -0
  89. data/lib/kitchen/terraform/systems_verifier/fail_fast.rb +52 -0
  90. data/lib/kitchen/terraform/systems_verifier/fail_slow.rb +62 -0
  91. data/lib/kitchen/terraform/systems_verifier_factory.rb +50 -0
  92. data/lib/kitchen/terraform/unsupported_client_version_error.rb +26 -0
  93. data/lib/kitchen/terraform/variables_manager.rb +64 -0
  94. data/lib/kitchen/terraform/verify_version.rb +63 -20
  95. data/lib/kitchen/terraform/verify_version_rescue_strategy.rb +23 -0
  96. data/lib/kitchen/terraform/verify_version_rescue_strategy/permissive.rb +50 -0
  97. data/lib/kitchen/terraform/verify_version_rescue_strategy/strict.rb +47 -0
  98. data/lib/kitchen/terraform/verify_version_rescue_strategy_factory.rb +51 -0
  99. data/lib/kitchen/terraform/version.rb +59 -58
  100. data/lib/kitchen/terraform/version_verifier.rb +50 -0
  101. data/lib/kitchen/terraform/version_verifier_strategy.rb +25 -0
  102. data/lib/kitchen/terraform/version_verifier_strategy/supported.rb +33 -0
  103. data/lib/kitchen/terraform/version_verifier_strategy/unsupported.rb +34 -0
  104. data/lib/kitchen/terraform/version_verifier_strategy_factory.rb +50 -0
  105. data/lib/kitchen/verifier/terraform.rb +71 -62
  106. metadata +143 -80
  107. metadata.gz.sig +0 -0
  108. data/lib/kitchen/terraform/inspec_options_mapper.rb +0 -73
  109. data/lib/kitchen/terraform/inspec_with_hosts.rb +0 -50
  110. data/lib/kitchen/terraform/inspec_without_hosts.rb +0 -45
  111. data/lib/kitchen/terraform/system_attrs_resolver.rb +0 -60
@@ -0,0 +1,102 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2016-2019 New Context, Inc.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ require "kitchen/terraform/command_flag/backend_config"
18
+ require "kitchen/terraform/command_flag/color"
19
+ require "kitchen/terraform/command_flag/lock_timeout"
20
+ require "kitchen/terraform/command_flag/plugin_dir"
21
+ require "kitchen/terraform/command_flag/upgrade"
22
+
23
+ module Kitchen
24
+ module Terraform
25
+ module Command
26
+ # The working directory is initialized by running a command like the following example:
27
+ # terraform init \
28
+ # -input=false \
29
+ # -lock=<lock> \
30
+ # -lock-timeout=<lock_timeout>s \
31
+ # [-no-color] \
32
+ # [-upgrade] \
33
+ # -force-copy \
34
+ # -backend=true \
35
+ # [-backend-config=<backend_configurations[0]> ...] \
36
+ # -get=true \
37
+ # -get-plugins=true \
38
+ # [-plugin-dir=<plugin_directory>] \
39
+ # -verify-plugins=true \
40
+ # <root_module_directory>
41
+ class Init
42
+ # #initialize prepares a new instance of the class.
43
+ #
44
+ # @param config [Hash] the configuration of the driver.
45
+ # @option config [Hash{String=>String}] :backend_configurations Terraform backend configuration arguments to
46
+ # complete a partial backend configuration.
47
+ # @option config [Boolean] :color a toggle of colored output from the Terraform client.
48
+ # @option config [Integer] :command_timeout the the number of seconds to wait for the command to finish running.
49
+ # @option config [Boolean] :lock a toggle of locking for the Terraform state file.
50
+ # @option config [Integer] :lock_timeout the number of seconds that the Terraform client will wait for a lock
51
+ # on the state to be obtained during operations.
52
+ # @option config [String] :plugin_directory the pathname of the directory which contains
53
+ # customized Terraform provider plugins to install in place of the official Terraform provider plugins.
54
+ # @option config [String] :root_module_directory the pathname of the directory which contains the root
55
+ # Terraform module.
56
+ # @option config [Boolean] :upgrade_during_init a toggle for upgrading modules and plugins.
57
+ # @return [Kitchen::Terraform::Command::Init]
58
+ def initialize(config:)
59
+ self.backend_config = ::Kitchen::Terraform::CommandFlag::BackendConfig.new arguments: config.fetch(
60
+ :backend_configurations
61
+ )
62
+ self.color = ::Kitchen::Terraform::CommandFlag::Color.new enabled: config.fetch(:color)
63
+ self.lock = config.fetch :lock
64
+ self.lock_timeout = ::Kitchen::Terraform::CommandFlag::LockTimeout.new duration: config.fetch(:lock_timeout)
65
+ self.plugin_dir = ::Kitchen::Terraform::CommandFlag::PluginDir.new pathname: config.fetch(
66
+ :plugin_directory
67
+ )
68
+ self.upgrade = ::Kitchen::Terraform::CommandFlag::Upgrade.new enabled: config.fetch(:upgrade_during_init)
69
+ end
70
+
71
+ # @return [String] the command with flags.
72
+ def to_s
73
+ "init " \
74
+ "-input=false " \
75
+ "-lock=#{lock} " \
76
+ "#{lock_timeout} " \
77
+ "#{color} " \
78
+ "#{upgrade} " \
79
+ "-force-copy " \
80
+ "-backend=true " \
81
+ "#{backend_config} " \
82
+ "-get=true " \
83
+ "-get-plugins=true " \
84
+ "#{plugin_dir} " \
85
+ "-verify-plugins=true"
86
+ end
87
+
88
+ private
89
+
90
+ attr_accessor(
91
+ :backend_config,
92
+ :color,
93
+ :lock,
94
+ :lock_timeout,
95
+ :options,
96
+ :plugin_dir,
97
+ :upgrade,
98
+ )
99
+ end
100
+ end
101
+ end
102
+ end
@@ -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.
@@ -14,55 +14,16 @@
14
14
  # See the License for the specific language governing permissions and
15
15
  # limitations under the License.
16
16
 
17
- require "json"
18
- require "kitchen"
19
- require "kitchen/terraform/command"
20
- require "kitchen/terraform/error"
21
- require "kitchen/terraform/shell_out"
22
-
23
- # Behaviour to run the `terraform output` command.
24
- module ::Kitchen::Terraform::Command::Output
25
- class << self
26
- # Runs the command with JSON foramtting.
27
- #
28
- # @option options [::String] :cwd the directory in which to run the command.
29
- # @option options [::Kitchen::Logger] :live_stream a Test Kitchen logger to capture the output from running the
30
- # command.
31
- # @option options [::Integer] :timeout the maximum duration in seconds to run the command.
32
- # @param client [::String] the pathname of the Terraform client.
33
- # @param options [::Hash] options which adjust the execution of the command.
34
- # @yieldparam output [::Hash] the standard output of the command parsed as JSON.
35
- def run(client:, options:, &block)
36
- run_shell_out client: client, options: options, &block
37
- rescue ::JSON::ParserError => error
38
- handle_json_parser error: error
39
- rescue ::Kitchen::Terraform::Error => error
40
- handle_kitchen_terraform(
41
- error: error,
42
- &block
43
- )
44
- end
45
-
46
- private
47
-
48
- # @api private
49
- def handle_json_parser(error:)
50
- raise(
51
- ::Kitchen::Terraform::Error,
52
- "Parsing Terraform output as JSON failed: #{error.message}"
53
- )
54
- end
55
-
56
- # @api private
57
- def handle_kitchen_terraform(error:)
58
- /no\\ outputs\\ defined/.match ::Regexp.escape error.to_s or raise error
59
- yield outputs: {}
60
- end
61
-
62
- # @api private
63
- def run_shell_out(client:, options:)
64
- ::Kitchen::Terraform::ShellOut.run client: client, command: "output -json", options: options do |standard_output:|
65
- yield outputs: ::Kitchen::Util.stringified_hash(::JSON.parse(standard_output))
17
+ module Kitchen
18
+ module Terraform
19
+ module Command
20
+ # The outputs are retrieved by running a command like the following example:
21
+ # terraform output -json
22
+ class Output
23
+ # @return [String] the command with flags.
24
+ def to_s
25
+ "output -json"
26
+ end
66
27
  end
67
28
  end
68
29
  end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2016-2019 New Context, Inc.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ require "kitchen/terraform/command_flag/color"
18
+ require "kitchen/terraform/command_flag/var_file"
19
+ require "kitchen/terraform/command_flag/var"
20
+ require "shellwords"
21
+
22
+ module Kitchen
23
+ module Terraform
24
+ module Command
25
+ # The root module is validated by running a command like the following example:
26
+ # terraform validate \
27
+ # [-no-color] \
28
+ # [-var=<variables.first>...] \
29
+ # [-var-file=<variable_files.first>...] \
30
+ # <directory>
31
+ class Validate
32
+ # #initialize prepares a new instance of the class.
33
+ #
34
+ # @param config [Hash] the configuration of the driver.
35
+ # @option config [Boolean] :color a toggle of colored output from the Terraform client.
36
+ # @option config [Array<String>] :variable_files a list of pathnames of Terraform variable files to evaluate.
37
+ # @option config [Hash{String=>String}] :variables a mapping of Terraform variables to evaluate.
38
+ # @return [Kitchen::Terraform::Command::Validate]
39
+ def initialize(config:)
40
+ self.color = ::Kitchen::Terraform::CommandFlag::Color.new enabled: config.fetch(:color)
41
+ self.var_file = ::Kitchen::Terraform::CommandFlag::VarFile.new pathnames: config.fetch(:variable_files)
42
+ self.var = ::Kitchen::Terraform::CommandFlag::Var.new arguments: config.fetch(:variables)
43
+ end
44
+
45
+ # @return [String] the command with flags.
46
+ def to_s
47
+ "validate " \
48
+ "#{color} " \
49
+ "#{var} " \
50
+ "#{var_file}"
51
+ end
52
+
53
+ private
54
+
55
+ attr_accessor(
56
+ :color,
57
+ :var_file,
58
+ :var,
59
+ )
60
+ end
61
+ end
62
+ end
63
+ end
@@ -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.
@@ -14,51 +14,14 @@
14
14
  # See the License for the specific language governing permissions and
15
15
  # limitations under the License.
16
16
 
17
- require "kitchen"
18
- require "kitchen/terraform/error"
19
- require "rubygems"
20
-
21
17
  module Kitchen
22
18
  module Terraform
23
19
  module Command
24
20
  # Version is the class of objects which represent the <tt>terraform version</tt> command.
25
- class Version < ::Gem::Version
26
- extend ::Kitchen::Logging
27
- extend ::Kitchen::ShellOut
28
-
29
- class << self
30
- # The command is run by shelling out in an environment which is optimized for automating Terraform.
31
- #
32
- # @raise [::Kitchen::Terraform::Error] if the result of running the command is a failure.
33
- # @return [::Kitchen::Terraform::Command::Version] an instance initialized with the output of the command.
34
- # @yieldparam version [::Kitchen::Terraform::Command::Version] an instance initialized with the output of the
35
- # command.
36
- def run
37
- new(
38
- run_command(
39
- "terraform version",
40
- environment: {
41
- "LC_ALL" => nil,
42
- "TF_IN_AUTOMATION" => "true",
43
- "TF_WARN_OUTPUT_ERRORS" => "true",
44
- },
45
- )
46
- ).tap do |version|
47
- yield version: version
48
- end
49
- rescue ::Kitchen::ShellOut::ShellCommandFailed, ::Kitchen::Error => error
50
- raise ::Kitchen::Terraform::Error, error.message
51
- end
52
-
53
- def logger
54
- ::Kitchen.logger
55
- end
56
- end
57
-
58
- private
59
-
60
- def initialize(version)
61
- super version.slice(/Terraform v(\d+\.\d+\.\d+)/, 1)
21
+ class Version
22
+ # @return [String] the command.
23
+ def to_s
24
+ "version"
62
25
  end
63
26
  end
64
27
  end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2016-2019 New Context, Inc.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ module Kitchen
18
+ module Terraform
19
+ module Command
20
+ # The workspace is deleted by running a command like the following example:
21
+ # terraform workspace delete <name>
22
+ class WorkspaceDelete
23
+ # @param config [Hash] the configuration of the driver.
24
+ # @option config [String] :workspace_name the name of the Terraform workspace.
25
+ # @return [Kitchen::Terraform::Command::WorkspaceDelete]
26
+ def initialize(config:)
27
+ self.workspace_name = config.fetch :workspace_name
28
+ end
29
+
30
+ # @return [String] the command.
31
+ def to_s
32
+ "workspace delete #{workspace_name}"
33
+ end
34
+
35
+ private
36
+
37
+ attr_accessor :workspace_name
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2016-2019 New Context, Inc.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ module Kitchen
18
+ module Terraform
19
+ module Command
20
+ # The workspace is created by running a command like the following example:
21
+ # terraform workspace new <name>
22
+ class WorkspaceNew
23
+ # #initialize prepares a new instance of the class.
24
+ #
25
+ # @param config [Hash] the configuration of the driver.
26
+ # @option config [String] :workspace_name the name of the Terraform workspace.
27
+ # @return [Kitchen::Terraform::Command::WorkspaceNew]
28
+ def initialize(config:)
29
+ self.workspace_name = config.fetch :workspace_name
30
+ end
31
+
32
+ # @return [String] the command.
33
+ def to_s
34
+ "workspace new #{workspace_name}"
35
+ end
36
+
37
+ private
38
+
39
+ attr_accessor :workspace_name
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2016-2019 New Context, Inc.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ module Kitchen
18
+ module Terraform
19
+ module Command
20
+ # The workspace is selected by running a command like the following example:
21
+ # terraform workspace select <name>
22
+ class WorkspaceSelect
23
+ # #initialize prepares a new instance of the class.
24
+ #
25
+ # @param config [Hash] the configuration of the driver.
26
+ # @option config [String] :workspace_name the name of the Terraform workspace.
27
+ # @return [Kitchen::Terraform::Command::WorkspaceSelect]
28
+ def initialize(config:)
29
+ self.workspace_name = config.fetch :workspace_name
30
+ end
31
+
32
+ # @return [String] the command.
33
+ def to_s
34
+ "workspace select #{workspace_name}"
35
+ end
36
+
37
+ private
38
+
39
+ attr_accessor :workspace_name
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2016-2019 New Context, Inc.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ require "kitchen"
18
+ require "kitchen/terraform/shell_out"
19
+
20
+ module Kitchen
21
+ module Terraform
22
+ # CommandExecutor is the class of objects which execute Terraform CLI commands.
23
+ class CommandExecutor
24
+ # #initialize prepares a new instance of the class.
25
+ #
26
+ # @param client [String] the pathname of the Terraform client.
27
+ # @param logger [Kitchen::Logger] a logger for logging messages.
28
+ # @return [Kitchen::Terraform::CommandExecutor]
29
+ def initialize(client:, logger:)
30
+ self.client = client
31
+ self.logger = logger
32
+ end
33
+
34
+ # #run executes a client command.
35
+ #
36
+ # @param command [String] the command to run.
37
+ # @param options [Hash] options which adjust the execution of the command.
38
+ # @option options [Integer] :timeout the maximum duration in seconds to run the command.
39
+ # @option options [String] :cwd the directory in which to run the command.
40
+ # @yieldparam standard_output [String] the standard output of the command.
41
+ # @raise [Kitchen::TransientFailure] if running the command results in failure.
42
+ # @return [self]
43
+ def run(command:, options:)
44
+ ::Kitchen::Terraform::ShellOut.new(
45
+ command: "#{client} #{command}",
46
+ logger: logger,
47
+ options: options,
48
+ ).run do |standard_output:|
49
+ yield standard_output: standard_output
50
+ end
51
+
52
+ self
53
+ end
54
+
55
+ private
56
+
57
+ attr_accessor :client, :logger
58
+ end
59
+ end
60
+ end