kitchen-terraform 5.5.0 → 5.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,65 @@
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
+ module Validate
26
+ # The root module is validated by running a command like the following example:
27
+ # terraform validate \
28
+ # [-no-color] \
29
+ # [-var=<variables.first>...] \
30
+ # [-var-file=<variable_files.first>...] \
31
+ # <directory>
32
+ class PreZeroFifteenZero
33
+ # #initialize prepares a new instance of the class.
34
+ #
35
+ # @param config [Hash] the configuration of the driver.
36
+ # @option config [Boolean] :color a toggle of colored output from the Terraform client.
37
+ # @option config [Array<String>] :variable_files a list of pathnames of Terraform variable files to evaluate.
38
+ # @option config [Hash{String=>String}] :variables a mapping of Terraform variables to evaluate.
39
+ # @return [Kitchen::Terraform::Command::Validate]
40
+ def initialize(config:)
41
+ self.color = ::Kitchen::Terraform::CommandFlag::Color.new enabled: config.fetch(:color)
42
+ self.var_file = ::Kitchen::Terraform::CommandFlag::VarFile.new pathnames: config.fetch(:variable_files)
43
+ self.var = ::Kitchen::Terraform::CommandFlag::Var.new arguments: config.fetch(:variables)
44
+ end
45
+
46
+ # @return [String] the command with flags.
47
+ def to_s
48
+ "validate " \
49
+ "#{color} " \
50
+ "#{var} " \
51
+ "#{var_file}"
52
+ end
53
+
54
+ private
55
+
56
+ attr_accessor(
57
+ :color,
58
+ :var_file,
59
+ :var,
60
+ )
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,54 @@
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/validate/post_zero_fifteen_zero"
18
+ require "kitchen/terraform/command/validate/pre_zero_fifteen_zero"
19
+ require "rubygems"
20
+
21
+ module Kitchen
22
+ module Terraform
23
+ module Command
24
+ # ValidateFactory is the class of objects which build Validate objects.
25
+ class ValidateFactory
26
+ # #build creates a new instance of an Validate object.
27
+ #
28
+ # @param config [Hash] the configuration of the driver.
29
+ # @return [Kitchen::Terraform::Command::Validate::PreZeroFifteenZero,
30
+ # Kitchen::Terraform::Command::Validate::PostZeroFifteenZero]
31
+ def build(config:)
32
+ if requirement.satisfied_by? version
33
+ return ::Kitchen::Terraform::Command::Validate::PreZeroFifteenZero.new config: config
34
+ end
35
+
36
+ ::Kitchen::Terraform::Command::Validate::PostZeroFifteenZero.new config: config
37
+ end
38
+
39
+ # #initialize prepares a new instance of the class
40
+ #
41
+ # @param version [Gem::Version] a client version.
42
+ # @return [Kitchen::Terraform::Command::ValidateFactory]
43
+ def initialize(version:)
44
+ self.requirement = ::Gem::Requirement.new "< 0.15.0"
45
+ self.version = version
46
+ end
47
+
48
+ private
49
+
50
+ attr_accessor :requirement, :version
51
+ end
52
+ end
53
+ end
54
+ end
@@ -30,7 +30,7 @@ module Kitchen
30
30
  # @return [String] the upgrade flag.
31
31
  def to_s
32
32
  if enabled
33
- "-upgrade"
33
+ "-upgrade=true"
34
34
  else
35
35
  ""
36
36
  end
@@ -42,7 +42,7 @@ module Kitchen
42
42
  # @see Kitchen::Configurable#finalize_config!
43
43
  def finalize_config!(instance)
44
44
  super instance
45
- self.version_requirement = ::Gem::Requirement.new ">= 0.11.4", "< 0.14.0"
45
+ self.version_requirement = ::Gem::Requirement.new ">= 0.11.4", "< 1.1.0"
46
46
  self.workspace_name = "kitchen-terraform-#{::Shellwords.escape instance.name}"
47
47
  end
48
48
 
@@ -15,12 +15,13 @@
15
15
  # limitations under the License.
16
16
 
17
17
  require "kitchen"
18
- require "kitchen/terraform/command/init"
18
+ require "kitchen/terraform/command/init_factory"
19
19
  require "kitchen/terraform/command/version"
20
20
  require "kitchen/terraform/command/workspace_new"
21
21
  require "kitchen/terraform/command/workspace_select"
22
22
  require "kitchen/terraform/command_executor"
23
23
  require "kitchen/terraform/verify_version"
24
+ require "rubygems"
24
25
 
25
26
  module Kitchen
26
27
  module Terraform
@@ -29,7 +30,13 @@ module Kitchen
29
30
  #
30
31
  # ===== Initializing the Terraform Working Directory
31
32
  #
32
- # {include:Kitchen::Terraform::Command::Init}
33
+ # ====== Terraform >= 0.15.0
34
+ #
35
+ # {include:Kitchen::Terraform::Command::Init::PostZeroFifteenZero}
36
+ #
37
+ # ====== Terraform < 0.15.0
38
+ #
39
+ # {include:Kitchen::Terraform::Command::Init::PreZeroFifteenZero}
33
40
  #
34
41
  # ===== Creating or Selecting the Test Terraform Workspace
35
42
  #
@@ -42,7 +49,8 @@ module Kitchen
42
49
  # @raise [Kitchen::TransientFailure] if a command fails.
43
50
  # @return [self]
44
51
  def call
45
- verify_version.call command: version, options: options
52
+ read_client_version
53
+ verify_version.call version: client_version
46
54
  initialize_directory
47
55
  create_or_select_workspace
48
56
 
@@ -58,20 +66,22 @@ module Kitchen
58
66
  # @option config [String] :client the pathname of the Terraform client.
59
67
  # @return [Kitchen::Terraform::Driver::Create]
60
68
  def initialize(config:, logger:, version_requirement:, workspace_name:)
61
- hash_config = config.to_hash.merge upgrade_during_init: true, workspace_name: workspace_name
69
+ self.complete_config = config.to_hash.merge upgrade_during_init: true, workspace_name: workspace_name
70
+ self.client_version = ::Gem::Version.new "0.0.0"
62
71
  self.command_executor = ::Kitchen::Terraform::CommandExecutor.new(
63
- client: config.fetch(:client),
72
+ client: complete_config.fetch(:client),
64
73
  logger: logger,
65
74
  )
66
- self.init = ::Kitchen::Terraform::Command::Init.new config: hash_config
67
75
  self.logger = logger
68
- self.options = { cwd: config.fetch(:root_module_directory), timeout: config.fetch(:command_timeout) }
76
+ self.options = {
77
+ cwd: complete_config.fetch(:root_module_directory),
78
+ timeout: complete_config.fetch(:command_timeout),
79
+ }
69
80
  self.workspace_name = workspace_name
70
- self.workspace_new = ::Kitchen::Terraform::Command::WorkspaceNew.new config: hash_config
71
- self.workspace_select = ::Kitchen::Terraform::Command::WorkspaceSelect.new config: hash_config
81
+ self.workspace_new = ::Kitchen::Terraform::Command::WorkspaceNew.new config: complete_config
82
+ self.workspace_select = ::Kitchen::Terraform::Command::WorkspaceSelect.new config: complete_config
72
83
  self.verify_version = ::Kitchen::Terraform::VerifyVersion.new(
73
- command_executor: command_executor,
74
- config: config,
84
+ config: complete_config,
75
85
  logger: logger,
76
86
  version_requirement: version_requirement,
77
87
  )
@@ -81,8 +91,9 @@ module Kitchen
81
91
  private
82
92
 
83
93
  attr_accessor(
94
+ :client_version,
84
95
  :command_executor,
85
- :init,
96
+ :complete_config,
86
97
  :logger,
87
98
  :options,
88
99
  :verify_version,
@@ -103,11 +114,23 @@ module Kitchen
103
114
 
104
115
  def initialize_directory
105
116
  logger.warn "Initializing the Terraform working directory..."
106
- command_executor.run command: init, options: options do |standard_output:|
117
+ command_executor.run(
118
+ command: ::Kitchen::Terraform::Command::InitFactory.new(version: client_version)
119
+ .build(config: complete_config),
120
+ options: options,
121
+ ) do |standard_output:|
107
122
  end
108
123
  logger.warn "Finished initializing the Terraform working directory."
109
124
  end
110
125
 
126
+ def read_client_version
127
+ logger.warn "Reading the Terraform client version..."
128
+ command_executor.run command: version, options: options do |standard_output:|
129
+ self.client_version = ::Gem::Version.new standard_output.slice /Terraform v(\d+\.\d+\.\d+)/, 1
130
+ end
131
+ logger.warn "Finished reading the Terraform client version."
132
+ end
133
+
111
134
  def select_workspace
112
135
  logger.warn "Selecting the #{workspace_name} Terraform workspace..."
113
136
  command_executor.run command: workspace_select, options: options do |standard_output:|
@@ -17,12 +17,13 @@
17
17
  require "kitchen"
18
18
  require "kitchen/terraform/command_executor"
19
19
  require "kitchen/terraform/command/destroy"
20
- require "kitchen/terraform/command/init"
20
+ require "kitchen/terraform/command/init_factory"
21
21
  require "kitchen/terraform/command/version"
22
22
  require "kitchen/terraform/command/workspace_delete"
23
23
  require "kitchen/terraform/command/workspace_new"
24
24
  require "kitchen/terraform/command/workspace_select"
25
25
  require "kitchen/terraform/verify_version"
26
+ require "rubygems"
26
27
 
27
28
  module Kitchen
28
29
  module Terraform
@@ -31,7 +32,13 @@ module Kitchen
31
32
  #
32
33
  # ===== Initializing the Terraform Working Directory
33
34
  #
34
- # {include:Kitchen::Terraform::Command::Init}
35
+ # ====== Terraform >= 0.15.0
36
+ #
37
+ # {include:Kitchen::Terraform::Command::Init::PostZeroFifteenZero}
38
+ #
39
+ # ====== Terraform < 0.15.0
40
+ #
41
+ # {include:Kitchen::Terraform::Command::Init::PreZeroFifteenZero}
35
42
  #
36
43
  # ===== Selecting or Creating the Test Terraform Workspace
37
44
  #
@@ -56,7 +63,8 @@ module Kitchen
56
63
  # @raise [Kitchen::TransientFailure] if a command fails.
57
64
  # @return [self]
58
65
  def call
59
- verify_version.call command: version, options: options
66
+ read_client_version
67
+ verify_version.call version: client_version
60
68
  execute_workflow
61
69
 
62
70
  self
@@ -70,25 +78,24 @@ module Kitchen
70
78
  # @param workspace_name [String] the name of the Terraform workspace to select or to create.
71
79
  # @return [Kitchen::Terraform::Driver::Destroy]
72
80
  def initialize(config:, logger:, version_requirement:, workspace_name:)
73
- hash_config = config.to_hash.merge upgrade_during_init: false, workspace_name: workspace_name
81
+ self.complete_config = config.to_hash.merge upgrade_during_init: false, workspace_name: workspace_name
82
+ self.client_version = ::Gem::Version.new "0.0.0"
74
83
  self.command_executor = ::Kitchen::Terraform::CommandExecutor.new(
75
- client: config.fetch(:client),
84
+ client: complete_config.fetch(:client),
76
85
  logger: logger,
77
86
  )
78
87
  self.logger = logger
79
- self.options = { cwd: config.fetch(:root_module_directory), timeout: config.fetch(:command_timeout) }
88
+ define_options
80
89
  self.workspace_name = workspace_name
81
- self.destroy = ::Kitchen::Terraform::Command::Destroy.new config: config
82
- self.init = ::Kitchen::Terraform::Command::Init.new config: hash_config
83
- self.workspace_delete_test = ::Kitchen::Terraform::Command::WorkspaceDelete.new config: hash_config
84
- self.workspace_new_test = ::Kitchen::Terraform::Command::WorkspaceNew.new config: hash_config
85
- self.workspace_select_test = ::Kitchen::Terraform::Command::WorkspaceSelect.new config: hash_config
90
+ self.destroy = ::Kitchen::Terraform::Command::Destroy.new config: complete_config
91
+ self.workspace_delete_test = ::Kitchen::Terraform::Command::WorkspaceDelete.new config: complete_config
92
+ self.workspace_new_test = ::Kitchen::Terraform::Command::WorkspaceNew.new config: complete_config
93
+ self.workspace_select_test = ::Kitchen::Terraform::Command::WorkspaceSelect.new config: complete_config
86
94
  self.workspace_select_default = ::Kitchen::Terraform::Command::WorkspaceSelect.new(
87
- config: hash_config.merge(workspace_name: "default"),
95
+ config: complete_config.merge(workspace_name: "default"),
88
96
  )
89
97
  self.verify_version = ::Kitchen::Terraform::VerifyVersion.new(
90
- command_executor: command_executor,
91
- config: config,
98
+ config: complete_config,
92
99
  logger: logger,
93
100
  version_requirement: version_requirement,
94
101
  )
@@ -98,7 +105,10 @@ module Kitchen
98
105
  private
99
106
 
100
107
  attr_accessor(
108
+ :client_version,
101
109
  :command_executor,
110
+ :complete_config,
111
+ :destroy_options,
102
112
  :destroy,
103
113
  :init,
104
114
  :logger,
@@ -119,9 +129,19 @@ module Kitchen
119
129
  logger.warn "Finished creating the #{workspace_name} Terraform workspace."
120
130
  end
121
131
 
132
+ def define_options
133
+ self.options = {
134
+ cwd: complete_config.fetch(:root_module_directory),
135
+ timeout: complete_config.fetch(:command_timeout),
136
+ }
137
+ self.destroy_options = options.merge(
138
+ environment: { "LC_ALL" => nil, "TF_IN_AUTOMATION" => "true", "TF_WARN_OUTPUT_ERRORS" => "true" },
139
+ )
140
+ end
141
+
122
142
  def destroy_infrastructure
123
143
  logger.warn "Destroying the Terraform-managed infrastructure..."
124
- command_executor.run command: destroy, options: options do |standard_output:|
144
+ command_executor.run command: destroy, options: destroy_options do |standard_output:|
125
145
  end
126
146
  logger.warn "Finished destroying the Terraform-managed infrastructure."
127
147
  end
@@ -143,11 +163,23 @@ module Kitchen
143
163
 
144
164
  def initialize_directory
145
165
  logger.warn "Initializing the Terraform working directory..."
146
- command_executor.run command: init, options: options do |standard_output:|
166
+ command_executor.run(
167
+ command: ::Kitchen::Terraform::Command::InitFactory.new(version: client_version)
168
+ .build(config: complete_config),
169
+ options: options,
170
+ ) do |standard_output:|
147
171
  end
148
172
  logger.warn "Finished initializing the Terraform working directory."
149
173
  end
150
174
 
175
+ def read_client_version
176
+ logger.warn "Reading the Terraform client version..."
177
+ command_executor.run command: version, options: options do |standard_output:|
178
+ self.client_version = ::Gem::Version.new standard_output.slice /Terraform v(\d+\.\d+\.\d+)/, 1
179
+ end
180
+ logger.warn "Finished reading the Terraform client version."
181
+ end
182
+
151
183
  def select_default_workspace
152
184
  logger.warn "Selecting the default Terraform workspace..."
153
185
  command_executor.run command: workspace_select_default, options: options do |standard_output:|
@@ -60,7 +60,14 @@ module Kitchen
60
60
  self.host = options.fetch :host do
61
61
  ""
62
62
  end
63
+
64
+ ::Inspec::Plugin::V2::Loader.new.tap do |loader|
65
+ loader.load_all
66
+ loader.exit_on_load_error
67
+ end
68
+
63
69
  self.runner = ::Inspec::Runner.new options.merge logger: ::Inspec::Log.logger
70
+
64
71
  profile_locations.each do |profile_location|
65
72
  runner.add_target profile_location
66
73
  end
@@ -19,7 +19,7 @@ require "kitchen/terraform/command_executor"
19
19
  require "kitchen/terraform/command/apply"
20
20
  require "kitchen/terraform/command/get"
21
21
  require "kitchen/terraform/command/output"
22
- require "kitchen/terraform/command/validate"
22
+ require "kitchen/terraform/command/validate_factory"
23
23
  require "kitchen/terraform/command/workspace_select"
24
24
  require "kitchen/terraform/debug_logger"
25
25
  require "kitchen/terraform/outputs_manager"
@@ -28,6 +28,7 @@ require "kitchen/terraform/outputs_reader"
28
28
  require "kitchen/terraform/variables_manager"
29
29
  require "kitchen/terraform/verify_version"
30
30
  require "kitchen/terraform/version"
31
+ require "rubygems"
31
32
 
32
33
  module Kitchen
33
34
  module Terraform
@@ -44,7 +45,13 @@ module Kitchen
44
45
  #
45
46
  # ===== Validating the Terraform Root Module
46
47
  #
47
- # {include:Kitchen::Terraform::Command::Validate}
48
+ # ====== Terraform >= 0.15.0
49
+ #
50
+ # {include:Kitchen::Terraform::Command::Validate::PostZeroFifteenZero}
51
+ #
52
+ # ====== Terraform < 0.15.0
53
+ #
54
+ # {include:Kitchen::Terraform::Command::Validate::PreZeroFifteenZero}
48
55
  #
49
56
  # ===== Applying the Terraform State Changes
50
57
  #
@@ -60,7 +67,8 @@ module Kitchen
60
67
  # @raise [Kitchen::TransientFailure] if a command fails.
61
68
  # @return [self]
62
69
  def call(state:)
63
- verify_version.call command: version, options: options
70
+ read_client_version
71
+ verify_version.call version: client_version
64
72
  execute_workflow
65
73
  save_variables_and_outputs state: state
66
74
 
@@ -75,51 +83,50 @@ module Kitchen
75
83
  # @param workspace_name [String] the name of the Terraform workspace to select or to create.
76
84
  # @return [Kitchen::Terraform::Driver::Converge]
77
85
  def initialize(config:, logger:, version_requirement:, workspace_name:)
78
- client = config.fetch :client
79
- hash_config = config.to_hash.merge workspace_name: workspace_name
86
+ self.complete_config = config.to_hash.merge workspace_name: workspace_name
87
+ client = complete_config.fetch :client
88
+ self.client_version = ::Gem::Version.new "0.0.0"
80
89
  self.command_executor = ::Kitchen::Terraform::CommandExecutor.new(
81
90
  client: client,
82
91
  logger: logger,
83
92
  )
84
93
  self.logger = logger
85
- self.options = { cwd: config.fetch(:root_module_directory), timeout: config.fetch(:command_timeout) }
94
+ self.options = {
95
+ cwd: complete_config.fetch(:root_module_directory),
96
+ timeout: complete_config.fetch(:command_timeout),
97
+ }
86
98
  self.workspace_name = workspace_name
87
- self.apply = ::Kitchen::Terraform::Command::Apply.new config: config
88
- self.get = ::Kitchen::Terraform::Command::Get.new
89
- self.output = ::Kitchen::Terraform::Command::Output.new
99
+ initialize_commands
90
100
  initialize_outputs_handlers client: client, logger: logger
91
- self.validate = ::Kitchen::Terraform::Command::Validate.new config: config
92
- self.workspace_select = ::Kitchen::Terraform::Command::WorkspaceSelect.new config: hash_config
93
- self.variables = config.fetch :variables
101
+ self.variables = complete_config.fetch :variables
94
102
  self.variables_manager = ::Kitchen::Terraform::VariablesManager.new
95
103
  self.verify_version = ::Kitchen::Terraform::VerifyVersion.new(
96
- command_executor: command_executor,
97
- config: config,
104
+ config: complete_config,
98
105
  logger: logger,
99
106
  version_requirement: version_requirement,
100
107
  )
101
- self.version = ::Kitchen::Terraform::Command::Version.new
102
108
  end
103
109
 
104
110
  private
105
111
 
106
112
  attr_accessor(
107
- :command_executor,
108
113
  :apply,
114
+ :client_version,
115
+ :command_executor,
116
+ :complete_config,
109
117
  :get,
110
- :output,
111
- :validate,
112
- :workspace_select,
113
118
  :logger,
114
119
  :options,
120
+ :output,
115
121
  :outputs_manager,
116
122
  :outputs_parser,
117
123
  :outputs_reader,
118
- :variables,
119
124
  :variables_manager,
125
+ :variables,
120
126
  :verify_version,
121
127
  :version,
122
128
  :workspace_name,
129
+ :workspace_select,
123
130
  )
124
131
 
125
132
  def build_infrastructure
@@ -143,6 +150,14 @@ module Kitchen
143
150
  build_infrastructure
144
151
  end
145
152
 
153
+ def initialize_commands
154
+ self.apply = ::Kitchen::Terraform::Command::Apply.new config: complete_config
155
+ self.get = ::Kitchen::Terraform::Command::Get.new
156
+ self.output = ::Kitchen::Terraform::Command::Output.new
157
+ self.workspace_select = ::Kitchen::Terraform::Command::WorkspaceSelect.new config: complete_config
158
+ self.version = ::Kitchen::Terraform::Command::Version.new
159
+ end
160
+
146
161
  def initialize_outputs_handlers(client:, logger:)
147
162
  self.outputs_manager = ::Kitchen::Terraform::OutputsManager.new
148
163
  self.outputs_parser = ::Kitchen::Terraform::OutputsParser.new
@@ -172,10 +187,18 @@ module Kitchen
172
187
  end
173
188
  end
174
189
 
190
+ def read_client_version
191
+ logger.warn "Reading the Terraform client version..."
192
+ command_executor.run command: version, options: options do |standard_output:|
193
+ self.client_version = ::Gem::Version.new standard_output.slice /Terraform v(\d+\.\d+\.\d+)/, 1
194
+ end
195
+ logger.warn "Finished reading the Terraform client version."
196
+ end
197
+
175
198
  def save_outputs(parsed_outputs:, state:)
176
199
  logger.warn "Writing the output variables to the Kitchen instance state..."
177
200
  outputs_manager.save outputs: parsed_outputs, state: state
178
- logger.warn "Finished writing the output varibales to the Kitchen instance state."
201
+ logger.warn "Finished writing the output variables to the Kitchen instance state."
179
202
  end
180
203
 
181
204
  def save_variables_and_outputs(state:)
@@ -196,7 +219,11 @@ module Kitchen
196
219
 
197
220
  def validate_files
198
221
  logger.warn "Validating the Terraform configuration files..."
199
- command_executor.run command: validate, options: options do |standard_output:|
222
+ command_executor.run(
223
+ command: ::Kitchen::Terraform::Command::ValidateFactory.new(version: client_version)
224
+ .build(config: complete_config),
225
+ options: options,
226
+ ) do |standard_output:|
200
227
  end
201
228
  logger.warn "Finished validating the Terraform configuration files."
202
229
  end