rspec-terraform 0.1.0.pre.44 → 0.1.0.pre.46

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 84471e75598a3fc301a717d206fd91ba40583271430805b183cbaf58610a8331
4
- data.tar.gz: 6178c6b399ff218dffbb21c4f036c64233e3ac84e96ba86660254af7d6f2d4d0
3
+ metadata.gz: 2595a9e060198c8472a6a91148d1d0e8c11532efee15fef65d9c732605b17a8d
4
+ data.tar.gz: e63395a9eb9228904cf3641b27e8f98d383757e1f2caa32a4249db296de99162
5
5
  SHA512:
6
- metadata.gz: f3b96974345ca144b4314eb2c90237bb238223e706c214fe94b97f4e5462edf969f6b4da8b3cd83a4a81daed33771f18a0e9f415c361ec6f9cb3ebfe97be7b74
7
- data.tar.gz: 19651b182453d3de16dfca2b5b4fe205df0d2af4bbfdc80dc1aa4d71298cd359a0ce70655720e100dc21f4f1f19b4784f51bdd56b3adfd63c05c3177a825f21d
6
+ metadata.gz: a8cf7c02682147df95b9229c8fbf923b0cb1d35fc2b3b104452b560bbc5f0ee796d0fac397bc24ce188d7293ec7bc49bbed9782846d3e381aac20be9d2e89d94
7
+ data.tar.gz: 531a6420c69096768cb72f8593bea6e8e1d347ebe0182cc58e7a8c9b2ca76cfc67ec89b67c47bf1579712978a5d76a38bc850683cfe192adead3ca1e7d8e9368
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rspec-terraform (0.1.0.pre.44)
4
+ rspec-terraform (0.1.0.pre.46)
5
5
  confidante (>= 0.27)
6
6
  rspec (>= 3.0)
7
7
  ruby-terraform (= 1.7.0.pre.17)
@@ -145,8 +145,8 @@ GEM
145
145
  parser (>= 3.1.1.0)
146
146
  rubocop-rake (0.6.0)
147
147
  rubocop (~> 1.0)
148
- rubocop-rspec (2.12.1)
149
- rubocop (~> 1.31)
148
+ rubocop-rspec (2.13.2)
149
+ rubocop (~> 1.33)
150
150
  ruby-progressbar (1.11.0)
151
151
  ruby-terraform (1.7.0.pre.17)
152
152
  immutable-struct (~> 2.4)
@@ -178,7 +178,7 @@ GEM
178
178
  thor (1.2.1)
179
179
  tzinfo (2.0.5)
180
180
  concurrent-ruby (~> 1.0)
181
- unicode-display_width (2.2.0)
181
+ unicode-display_width (2.3.0)
182
182
 
183
183
  PLATFORMS
184
184
  arm64-darwin-21
@@ -0,0 +1,209 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../configuration/var_captor'
4
+
5
+ module RSpec
6
+ module Terraform
7
+ module Helpers
8
+ # rubocop:disable Metrics/ModuleLength
9
+ module Actions
10
+ def execute_if_required(parameters, &block)
11
+ only_if = parameters[:only_if]
12
+ only_if_args = only_if ? [parameters].slice(0, only_if.arity) : []
13
+ should_execute = only_if ? only_if.call(*only_if_args) : true
14
+
15
+ block.call if should_execute
16
+ end
17
+
18
+ def clean(parameters)
19
+ return unless execution_mode == :isolated
20
+
21
+ FileUtils.rm_rf(parameters[:configuration_directory])
22
+ FileUtils.mkdir_p(parameters[:configuration_directory])
23
+ end
24
+
25
+ def remove(parameters, file)
26
+ FileUtils.rm_f(
27
+ File.join(parameters[:configuration_directory], file)
28
+ )
29
+ end
30
+
31
+ def validate(parameters)
32
+ missing_parameters =
33
+ required_parameters(execution_mode)
34
+ .filter { |parameter| parameters[parameter].nil? }
35
+
36
+ return if missing_parameters.empty?
37
+
38
+ raise_missing_parameters(missing_parameters)
39
+ end
40
+
41
+ def init(parameters)
42
+ init_command.execute(init_parameters(parameters))
43
+ end
44
+
45
+ def apply(parameters)
46
+ apply_command.execute(apply_parameters(parameters))
47
+ end
48
+
49
+ def destroy(parameters)
50
+ destroy_command.execute(destroy_parameters(parameters))
51
+ end
52
+
53
+ def plan(parameters)
54
+ plan_parameters = plan_parameters(parameters)
55
+ plan_command.execute(plan_parameters)
56
+ plan_parameters[:out]
57
+ end
58
+
59
+ def show(parameters, plan_file)
60
+ stdout = StringIO.new
61
+ show_command(stdout: stdout)
62
+ .execute(show_parameters(parameters, plan_file))
63
+ stdout.string
64
+ end
65
+
66
+ def output(parameters)
67
+ stdout = StringIO.new
68
+ output_command(stdout: stdout)
69
+ .execute(output_parameters(parameters))
70
+ stdout.string
71
+ end
72
+
73
+ private
74
+
75
+ def raise_missing_parameters(parameters)
76
+ parameters = parameters.collect { |parameter| "`:#{parameter}`" }
77
+ if parameters.count == 1
78
+ raise StandardError,
79
+ "Required parameter: #{parameters[0]} missing."
80
+ else
81
+ parameters = "#{parameters[..-2].join(', ')} and #{parameters[-1]}"
82
+ raise StandardError,
83
+ "Required parameters: #{parameters} missing."
84
+ end
85
+ end
86
+
87
+ def instantiate_command(klass, opts = {})
88
+ klass.new(command_options.merge(opts))
89
+ end
90
+
91
+ def init_command
92
+ instantiate_command(RubyTerraform::Commands::Init)
93
+ end
94
+
95
+ def apply_command
96
+ instantiate_command(RubyTerraform::Commands::Apply)
97
+ end
98
+
99
+ def destroy_command
100
+ instantiate_command(RubyTerraform::Commands::Destroy)
101
+ end
102
+
103
+ def plan_command
104
+ instantiate_command(RubyTerraform::Commands::Plan)
105
+ end
106
+
107
+ def show_command(opts = {})
108
+ instantiate_command(RubyTerraform::Commands::Show, opts)
109
+ end
110
+
111
+ def output_command(opts = {})
112
+ instantiate_command(RubyTerraform::Commands::Output, opts)
113
+ end
114
+
115
+ def init_parameters(parameters)
116
+ init_parameters = parameters.merge(
117
+ chdir: parameters[:configuration_directory],
118
+ input: false
119
+ )
120
+ if execution_mode == :isolated
121
+ init_parameters =
122
+ init_parameters.merge(from_module: parameters[:source_directory])
123
+ end
124
+
125
+ init_parameters
126
+ end
127
+
128
+ # rubocop:disable Metrics/MethodLength
129
+ def apply_parameters(parameters)
130
+ apply_parameters =
131
+ parameters.merge(
132
+ chdir: parameters[:configuration_directory],
133
+ input: false,
134
+ auto_approve: true
135
+ )
136
+
137
+ if parameters[:state_file]
138
+ apply_parameters =
139
+ apply_parameters.merge(state: parameters[:state_file])
140
+ end
141
+
142
+ apply_parameters
143
+ end
144
+ # rubocop:enable Metrics/MethodLength
145
+
146
+ # rubocop:disable Metrics/MethodLength
147
+ def destroy_parameters(parameters)
148
+ destroy_parameters =
149
+ parameters.merge(
150
+ chdir: parameters[:configuration_directory],
151
+ input: false,
152
+ auto_approve: true
153
+ )
154
+
155
+ if parameters[:state_file]
156
+ destroy_parameters =
157
+ destroy_parameters.merge(state: parameters[:state_file])
158
+ end
159
+
160
+ destroy_parameters
161
+ end
162
+ # rubocop:enable Metrics/MethodLength
163
+
164
+ # rubocop:disable Metrics/MethodLength
165
+ def plan_parameters(parameters)
166
+ plan_parameters =
167
+ parameters.merge(
168
+ chdir: parameters[:configuration_directory],
169
+ out: parameters[:plan_file_name] ||
170
+ "#{SecureRandom.hex[0, 10]}.tfplan",
171
+ input: false
172
+ )
173
+
174
+ if parameters[:state_file]
175
+ plan_parameters =
176
+ plan_parameters.merge(state: parameters[:state_file])
177
+ end
178
+
179
+ plan_parameters
180
+ end
181
+ # rubocop:enable Metrics/MethodLength
182
+
183
+ def show_parameters(parameters, plan_file)
184
+ parameters.merge(
185
+ chdir: parameters[:configuration_directory],
186
+ path: plan_file,
187
+ no_color: true,
188
+ json: true
189
+ )
190
+ end
191
+
192
+ def output_parameters(parameters)
193
+ output_parameters =
194
+ parameters.merge(
195
+ chdir: parameters[:configuration_directory]
196
+ )
197
+
198
+ if parameters[:state_file]
199
+ output_parameters =
200
+ output_parameters.merge(state: parameters[:state_file])
201
+ end
202
+
203
+ output_parameters
204
+ end
205
+ end
206
+ # rubocop:enable Metrics/ModuleLength
207
+ end
208
+ end
209
+ end
@@ -2,38 +2,14 @@
2
2
 
3
3
  require 'ruby_terraform'
4
4
 
5
- require_relative '../configuration/var_captor'
5
+ require_relative './base'
6
6
 
7
7
  module RSpec
8
8
  module Terraform
9
9
  module Helpers
10
- # rubocop:disable Metrics/ClassLength
11
- class Apply
12
- attr_reader(
13
- :configuration_provider,
14
- :binary,
15
- :logger,
16
- :stdin,
17
- :stdout,
18
- :stderr,
19
- :execution_mode
20
- )
21
-
22
- def initialize(opts = {})
23
- @configuration_provider =
24
- opts[:configuration_provider] || Configuration.identity_provider
25
- @binary = opts[:binary] || 'terraform'
26
- @logger = opts[:logger]
27
- @stdin = opts[:stdin]
28
- @stdout = opts[:stdout]
29
- @stderr = opts[:stderr]
30
- @execution_mode = opts[:execution_mode] || :in_place
31
- end
32
-
10
+ class Apply < Base
33
11
  def execute(overrides = {}, &block)
34
- parameters = with_configuration_provider_parameters(overrides)
35
- parameters = with_resolved_vars(parameters, &block)
36
- parameters = with_mandatory_parameters(parameters)
12
+ parameters = resolve_parameters(overrides, &block)
37
13
 
38
14
  execute_if_required(parameters) do
39
15
  validate(parameters)
@@ -45,31 +21,11 @@ module RSpec
45
21
 
46
22
  private
47
23
 
48
- def execute_if_required(parameters, &block)
49
- only_if = parameters[:only_if]
50
- only_if_args = only_if ? [parameters].slice(0, only_if.arity) : []
51
- should_execute = only_if ? only_if.call(*only_if_args) : true
52
-
53
- block.call if should_execute
54
- end
55
-
56
- def with_configuration_provider_parameters(parameters)
57
- configuration_provider.resolve(parameters)
58
- end
59
-
60
- def with_resolved_vars(parameters, &block)
61
- return parameters unless block_given?
62
-
63
- var_captor = Configuration::VarCaptor.new(parameters[:vars] || {})
64
- block.call(var_captor)
65
- parameters.merge(vars: var_captor.to_h)
66
- end
67
-
68
- def with_mandatory_parameters(parameters)
69
- parameters.merge(
24
+ def mandatory_parameters
25
+ {
70
26
  input: false,
71
27
  auto_approve: true
72
- )
28
+ }
73
29
  end
74
30
 
75
31
  def required_parameters(execution_mode)
@@ -78,94 +34,7 @@ module RSpec
78
34
  isolated: %i[source_directory configuration_directory]
79
35
  }[execution_mode] || []
80
36
  end
81
-
82
- def raise_missing_parameters(parameters)
83
- parameters = parameters.collect { |parameter| "`:#{parameter}`" }
84
- if parameters.count == 1
85
- raise StandardError,
86
- "Required parameter: #{parameters[0]} missing."
87
- else
88
- parameters = "#{parameters[..-2].join(', ')} and #{parameters[-1]}"
89
- raise StandardError,
90
- "Required parameters: #{parameters} missing."
91
- end
92
- end
93
-
94
- def validate(parameters)
95
- missing_parameters =
96
- required_parameters(execution_mode)
97
- .filter { |parameter| parameters[parameter].nil? }
98
-
99
- return if missing_parameters.empty?
100
-
101
- raise_missing_parameters(missing_parameters)
102
- end
103
-
104
- def clean(parameters)
105
- return unless execution_mode == :isolated
106
-
107
- FileUtils.rm_rf(parameters[:configuration_directory])
108
- FileUtils.mkdir_p(parameters[:configuration_directory])
109
- end
110
-
111
- def init(parameters)
112
- init_command.execute(init_parameters(parameters))
113
- end
114
-
115
- def apply(parameters)
116
- apply_command.execute(apply_parameters(parameters))
117
- end
118
-
119
- def init_command
120
- RubyTerraform::Commands::Init.new(command_options)
121
- end
122
-
123
- def apply_command
124
- RubyTerraform::Commands::Apply.new(command_options)
125
- end
126
-
127
- def init_parameters(parameters)
128
- init_parameters = parameters.merge(
129
- chdir: parameters[:configuration_directory],
130
- input: false
131
- )
132
- if execution_mode == :isolated
133
- init_parameters =
134
- init_parameters.merge(from_module: parameters[:source_directory])
135
- end
136
-
137
- init_parameters
138
- end
139
-
140
- # rubocop:disable Metrics/MethodLength
141
- def apply_parameters(parameters)
142
- apply_parameters =
143
- parameters.merge(
144
- chdir: parameters[:configuration_directory],
145
- input: false,
146
- auto_approve: true
147
- )
148
-
149
- if parameters[:state_file]
150
- apply_parameters =
151
- apply_parameters.merge(state: parameters[:state_file])
152
- end
153
-
154
- apply_parameters
155
- end
156
- # rubocop:enable Metrics/MethodLength
157
-
158
- def command_options
159
- {
160
- binary: binary,
161
- logger: logger,
162
- stdin: stdin,
163
- stdout: stdout,
164
- stderr: stderr
165
- }
166
- end
167
37
  end
168
- # rubocop:enable Metrics/ClassLength
169
38
  end
170
39
  end
171
40
  end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './actions'
4
+ require_relative './parameters'
5
+
6
+ module RSpec
7
+ module Terraform
8
+ module Helpers
9
+ class Base
10
+ include Parameters
11
+ include Actions
12
+
13
+ attr_reader(
14
+ :configuration_provider,
15
+ :binary,
16
+ :logger,
17
+ :stdin,
18
+ :stdout,
19
+ :stderr,
20
+ :execution_mode
21
+ )
22
+
23
+ def initialize(opts = {})
24
+ @configuration_provider =
25
+ opts[:configuration_provider] || Configuration.identity_provider
26
+ @binary = opts[:binary] || 'terraform'
27
+ @logger = opts[:logger]
28
+ @stdin = opts[:stdin]
29
+ @stdout = opts[:stdout]
30
+ @stderr = opts[:stderr]
31
+ @execution_mode = opts[:execution_mode] || :in_place
32
+ end
33
+
34
+ private
35
+
36
+ def mandatory_parameters
37
+ {}
38
+ end
39
+
40
+ def required_parameters(_)
41
+ []
42
+ end
43
+
44
+ def command_options
45
+ {
46
+ binary: binary,
47
+ logger: logger,
48
+ stdin: stdin,
49
+ stdout: stdout,
50
+ stderr: stderr
51
+ }
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -2,37 +2,14 @@
2
2
 
3
3
  require 'ruby_terraform'
4
4
 
5
- require_relative '../configuration/var_captor'
5
+ require_relative './base'
6
6
 
7
7
  module RSpec
8
8
  module Terraform
9
9
  module Helpers
10
- # rubocop:disable Metrics/ClassLength
11
- class Destroy
12
- attr_reader(
13
- :configuration_provider,
14
- :binary,
15
- :logger,
16
- :stdin,
17
- :stdout,
18
- :stderr,
19
- :execution_mode
20
- )
21
-
22
- def initialize(opts = {})
23
- @configuration_provider =
24
- opts[:configuration_provider] || Configuration.identity_provider
25
- @binary = opts[:binary] || 'terraform'
26
- @logger = opts[:logger]
27
- @stdin = opts[:stdin]
28
- @stdout = opts[:stdout]
29
- @stderr = opts[:stderr]
30
- @execution_mode = opts[:execution_mode] || :in_place
31
- end
32
-
10
+ class Destroy < Base
33
11
  def execute(overrides = {}, &block)
34
- parameters = with_configuration_provider_parameters(overrides)
35
- parameters = with_resolved_vars(parameters, &block)
12
+ parameters = resolve_parameters(overrides, &block)
36
13
 
37
14
  execute_if_required(parameters) do
38
15
  validate(parameters)
@@ -44,120 +21,13 @@ module RSpec
44
21
 
45
22
  private
46
23
 
47
- def execute_if_required(parameters, &block)
48
- only_if = parameters[:only_if]
49
- only_if_args = only_if ? [parameters].slice(0, only_if.arity) : []
50
- should_execute = only_if ? only_if.call(*only_if_args) : true
51
-
52
- block.call if should_execute
53
- end
54
-
55
- def with_configuration_provider_parameters(parameters)
56
- configuration_provider.resolve(parameters)
57
- end
58
-
59
- def with_resolved_vars(parameters, &block)
60
- return parameters unless block_given?
61
-
62
- var_captor = Configuration::VarCaptor.new(parameters[:vars] || {})
63
- block.call(var_captor)
64
- parameters.merge(vars: var_captor.to_h)
65
- end
66
-
67
24
  def required_parameters(execution_mode)
68
25
  {
69
26
  in_place: [:configuration_directory],
70
27
  isolated: %i[source_directory configuration_directory]
71
28
  }[execution_mode] || []
72
29
  end
73
-
74
- def raise_missing_parameters(parameters)
75
- parameters = parameters.collect { |parameter| "`:#{parameter}`" }
76
- if parameters.count == 1
77
- raise StandardError,
78
- "Required parameter: #{parameters[0]} missing."
79
- else
80
- parameters = "#{parameters[..-2].join(', ')} and #{parameters[-1]}"
81
- raise StandardError,
82
- "Required parameters: #{parameters} missing."
83
- end
84
- end
85
-
86
- def validate(parameters)
87
- missing_parameters =
88
- required_parameters(execution_mode)
89
- .filter { |parameter| parameters[parameter].nil? }
90
-
91
- return if missing_parameters.empty?
92
-
93
- raise_missing_parameters(missing_parameters)
94
- end
95
-
96
- def clean(parameters)
97
- return unless execution_mode == :isolated
98
-
99
- FileUtils.rm_rf(parameters[:configuration_directory])
100
- FileUtils.mkdir_p(parameters[:configuration_directory])
101
- end
102
-
103
- def init(parameters)
104
- init_command.execute(init_parameters(parameters))
105
- end
106
-
107
- def destroy(parameters)
108
- destroy_command.execute(destroy_parameters(parameters))
109
- end
110
-
111
- def init_command
112
- RubyTerraform::Commands::Init.new(command_options)
113
- end
114
-
115
- def destroy_command
116
- RubyTerraform::Commands::Destroy.new(command_options)
117
- end
118
-
119
- def init_parameters(parameters)
120
- init_parameters = parameters.merge(
121
- chdir: parameters[:configuration_directory],
122
- input: false
123
- )
124
- if execution_mode == :isolated
125
- init_parameters =
126
- init_parameters.merge(from_module: parameters[:source_directory])
127
- end
128
-
129
- init_parameters
130
- end
131
-
132
- # rubocop:disable Metrics/MethodLength
133
- def destroy_parameters(parameters)
134
- destroy_parameters =
135
- parameters.merge(
136
- chdir: parameters[:configuration_directory],
137
- input: false,
138
- auto_approve: true
139
- )
140
-
141
- if parameters[:state_file]
142
- destroy_parameters =
143
- destroy_parameters.merge(state: parameters[:state_file])
144
- end
145
-
146
- destroy_parameters
147
- end
148
- # rubocop:enable Metrics/MethodLength
149
-
150
- def command_options
151
- {
152
- binary: binary,
153
- logger: logger,
154
- stdin: stdin,
155
- stdout: stdout,
156
- stderr: stderr
157
- }
158
- end
159
30
  end
160
- # rubocop:enable Metrics/ClassLength
161
31
  end
162
32
  end
163
33
  end
@@ -2,55 +2,29 @@
2
2
 
3
3
  require 'ruby_terraform'
4
4
 
5
+ require_relative './base'
6
+
5
7
  module RSpec
6
8
  module Terraform
7
9
  module Helpers
8
- # rubocop:disable Metrics/ClassLength
9
- class Output
10
- attr_reader(
11
- :configuration_provider,
12
- :binary,
13
- :logger,
14
- :stdin,
15
- :stdout,
16
- :stderr,
17
- :execution_mode
18
- )
19
-
20
- def initialize(opts = {})
21
- @configuration_provider =
22
- opts[:configuration_provider] || Configuration.identity_provider
23
- @binary = opts[:binary] || 'terraform'
24
- @logger = opts[:logger]
25
- @stdin = opts[:stdin]
26
- @stdout = opts[:stdout]
27
- @stderr = opts[:stderr]
28
- @execution_mode = opts[:execution_mode] || :in_place
29
- end
30
-
10
+ class Output < Base
31
11
  def execute(overrides = {})
32
- parameters = with_configuration_provider_parameters(overrides)
33
- parameters = with_mandatory_parameters(parameters)
34
-
35
- ensure_required_parameters(parameters)
12
+ parameters = resolve_parameters(overrides)
36
13
 
14
+ validate(parameters)
37
15
  clean(parameters)
38
16
  init(parameters)
39
17
  output_value = output(parameters)
40
18
 
41
- JSON.parse(output_value, symbolize_names: true)
19
+ parse(output_value)
42
20
  end
43
21
 
44
22
  private
45
23
 
46
- def with_configuration_provider_parameters(parameters)
47
- configuration_provider.resolve(parameters)
48
- end
49
-
50
- def with_mandatory_parameters(parameters)
51
- parameters.merge(
24
+ def mandatory_parameters
25
+ {
52
26
  json: true
53
- )
27
+ }
54
28
  end
55
29
 
56
30
  def required_parameters(execution_mode)
@@ -60,92 +34,10 @@ module RSpec
60
34
  }[execution_mode] || []
61
35
  end
62
36
 
63
- def ensure_required_parameters(parameters)
64
- missing_parameters =
65
- required_parameters(execution_mode)
66
- .filter { |parameter| parameters[parameter].nil? }
67
-
68
- return if missing_parameters.empty?
69
-
70
- raise_missing_parameters(missing_parameters)
71
- end
72
-
73
- def raise_missing_parameters(parameters)
74
- parameters = parameters.collect { |parameter| "`:#{parameter}`" }
75
- if parameters.count == 1
76
- raise StandardError,
77
- "Required parameter: #{parameters[0]} missing."
78
- else
79
- parameters = "#{parameters[..-2].join(', ')} and #{parameters[-1]}"
80
- raise StandardError,
81
- "Required parameters: #{parameters} missing."
82
- end
83
- end
84
-
85
- def clean(parameters)
86
- return unless execution_mode == :isolated
87
-
88
- FileUtils.rm_rf(parameters[:configuration_directory])
89
- FileUtils.mkdir_p(parameters[:configuration_directory])
90
- end
91
-
92
- def init(parameters)
93
- init_command.execute(init_parameters(parameters))
94
- end
95
-
96
- def output(parameters)
97
- stdout = StringIO.new
98
- output_command(stdout: stdout)
99
- .execute(output_parameters(parameters))
100
- stdout.string
101
- end
102
-
103
- def init_command
104
- RubyTerraform::Commands::Init.new(command_options)
105
- end
106
-
107
- def output_command(opts = {})
108
- RubyTerraform::Commands::Output.new(command_options.merge(opts))
109
- end
110
-
111
- def init_parameters(parameters)
112
- init_parameters = parameters.merge(
113
- chdir: parameters[:configuration_directory],
114
- input: false
115
- )
116
- if execution_mode == :isolated
117
- init_parameters =
118
- init_parameters.merge(from_module: parameters[:source_directory])
119
- end
120
-
121
- init_parameters
122
- end
123
-
124
- def output_parameters(parameters)
125
- output_parameters =
126
- parameters.merge(
127
- chdir: parameters[:configuration_directory]
128
- )
129
-
130
- if parameters[:state_file]
131
- output_parameters =
132
- output_parameters.merge(state: parameters[:state_file])
133
- end
134
-
135
- output_parameters
136
- end
137
-
138
- def command_options
139
- {
140
- binary: binary,
141
- logger: logger,
142
- stdin: stdin,
143
- stdout: stdout,
144
- stderr: stderr
145
- }
37
+ def parse(output_value)
38
+ JSON.parse(output_value, symbolize_names: true)
146
39
  end
147
40
  end
148
41
  end
149
- # rubocop:enable Metrics/ClassLength
150
42
  end
151
43
  end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../configuration/var_captor'
4
+
5
+ module RSpec
6
+ module Terraform
7
+ module Helpers
8
+ module Parameters
9
+ def resolve_parameters(overrides, &block)
10
+ parameters = with_configuration_provider_parameters(overrides)
11
+ parameters = with_resolved_vars(parameters, &block)
12
+ with_mandatory_parameters(parameters)
13
+ end
14
+
15
+ def with_configuration_provider_parameters(parameters)
16
+ configuration_provider.resolve(parameters)
17
+ end
18
+
19
+ def with_resolved_vars(parameters, &block)
20
+ return parameters unless block_given?
21
+
22
+ var_captor = Configuration::VarCaptor.new(parameters[:vars] || {})
23
+ block.call(var_captor)
24
+ parameters.merge(vars: var_captor.to_h)
25
+ end
26
+
27
+ def with_mandatory_parameters(parameters)
28
+ parameters.merge(mandatory_parameters)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -4,40 +4,16 @@ require 'ruby_terraform'
4
4
  require 'securerandom'
5
5
  require 'stringio'
6
6
 
7
- require_relative '../configuration/var_captor'
7
+ require_relative './base'
8
8
 
9
9
  module RSpec
10
10
  module Terraform
11
11
  module Helpers
12
- # rubocop:disable Metrics/ClassLength
13
- class Plan
14
- attr_reader(
15
- :configuration_provider,
16
- :binary,
17
- :logger,
18
- :stdin,
19
- :stdout,
20
- :stderr,
21
- :execution_mode
22
- )
23
-
24
- def initialize(opts = {})
25
- @configuration_provider =
26
- opts[:configuration_provider] || Configuration.identity_provider
27
- @binary = opts[:binary] || 'terraform'
28
- @logger = opts[:logger]
29
- @stdin = opts[:stdin]
30
- @stdout = opts[:stdout]
31
- @stderr = opts[:stderr]
32
- @execution_mode = opts[:execution_mode] || :in_place
33
- end
34
-
12
+ class Plan < Base
35
13
  def execute(overrides = {}, &block)
36
- parameters = with_configuration_provider_parameters(overrides)
37
- parameters = with_resolved_vars(parameters, &block)
38
-
39
- ensure_required_parameters(parameters)
14
+ parameters = resolve_parameters(overrides, &block)
40
15
 
16
+ validate(parameters)
41
17
  clean(parameters)
42
18
  init(parameters)
43
19
  plan_file = plan(parameters)
@@ -48,18 +24,6 @@ module RSpec
48
24
 
49
25
  private
50
26
 
51
- def with_configuration_provider_parameters(parameters)
52
- configuration_provider.resolve(parameters)
53
- end
54
-
55
- def with_resolved_vars(parameters, &block)
56
- return parameters unless block_given?
57
-
58
- var_captor = Configuration::VarCaptor.new(parameters[:vars] || {})
59
- block.call(var_captor)
60
- parameters.merge(vars: var_captor.to_h)
61
- end
62
-
63
27
  def required_parameters(execution_mode)
64
28
  {
65
29
  in_place: [:configuration_directory],
@@ -67,128 +31,12 @@ module RSpec
67
31
  }[execution_mode] || []
68
32
  end
69
33
 
70
- def ensure_required_parameters(parameters)
71
- missing_parameters =
72
- required_parameters(execution_mode)
73
- .filter { |parameter| parameters[parameter].nil? }
74
-
75
- return if missing_parameters.empty?
76
-
77
- raise_missing_parameters(missing_parameters)
78
- end
79
-
80
- def raise_missing_parameters(parameters)
81
- parameters = parameters.collect { |parameter| "`:#{parameter}`" }
82
- if parameters.count == 1
83
- raise StandardError,
84
- "Required parameter: #{parameters[0]} missing."
85
- else
86
- parameters = "#{parameters[..-2].join(', ')} and #{parameters[-1]}"
87
- raise StandardError,
88
- "Required parameters: #{parameters} missing."
89
- end
90
- end
91
-
92
- def clean(parameters)
93
- return unless execution_mode == :isolated
94
-
95
- FileUtils.rm_rf(parameters[:configuration_directory])
96
- FileUtils.mkdir_p(parameters[:configuration_directory])
97
- end
98
-
99
- def init(parameters)
100
- init_command.execute(init_parameters(parameters))
101
- end
102
-
103
- def plan(parameters)
104
- plan_parameters = plan_parameters(parameters)
105
- plan_command.execute(plan_parameters)
106
- plan_parameters[:out]
107
- end
108
-
109
- def show(parameters, plan_file)
110
- stdout = StringIO.new
111
- show_command(stdout: stdout)
112
- .execute(show_parameters(parameters, plan_file))
113
- stdout.string
114
- end
115
-
116
34
  def parse(plan_contents)
117
35
  RubyTerraform::Models::Plan.new(
118
36
  JSON.parse(plan_contents, symbolize_names: true)
119
37
  )
120
38
  end
121
-
122
- def remove(parameters, plan_file)
123
- FileUtils.rm_f(
124
- File.join(parameters[:configuration_directory], plan_file)
125
- )
126
- end
127
-
128
- def init_command
129
- RubyTerraform::Commands::Init.new(command_options)
130
- end
131
-
132
- def plan_command
133
- RubyTerraform::Commands::Plan.new(command_options)
134
- end
135
-
136
- def show_command(opts = {})
137
- RubyTerraform::Commands::Show.new(command_options.merge(opts))
138
- end
139
-
140
- def init_parameters(parameters)
141
- init_parameters = parameters.merge(
142
- chdir: parameters[:configuration_directory],
143
- input: false
144
- )
145
- if execution_mode == :isolated
146
- init_parameters =
147
- init_parameters.merge(from_module: parameters[:source_directory])
148
- end
149
-
150
- init_parameters
151
- end
152
-
153
- # rubocop:disable Metrics/MethodLength
154
- def plan_parameters(parameters)
155
- plan_parameters =
156
- parameters.merge(
157
- chdir: parameters[:configuration_directory],
158
- out: parameters[:plan_file_name] ||
159
- "#{SecureRandom.hex[0, 10]}.tfplan",
160
- input: false
161
- )
162
-
163
- if parameters[:state_file]
164
- plan_parameters =
165
- plan_parameters.merge(state: parameters[:state_file])
166
- end
167
-
168
- plan_parameters
169
- end
170
- # rubocop:enable Metrics/MethodLength
171
-
172
- def show_parameters(parameters, plan_file)
173
- parameters.merge(
174
- chdir: parameters[:configuration_directory],
175
- path: plan_file,
176
- no_color: true,
177
- json: true
178
- )
179
- end
180
-
181
- def command_options
182
- {
183
- binary: binary,
184
- logger: logger,
185
- stdin: stdin,
186
- stdout: stdout,
187
- stderr: stderr
188
- }
189
- end
190
39
  end
191
- # rubocop:enable Metrics/ClassLength
192
40
  end
193
41
  end
194
42
  end
@@ -1,38 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative './base'
3
4
  require_relative '../configuration/var_captor'
4
5
 
5
6
  module RSpec
6
7
  module Terraform
7
8
  module Helpers
8
- class Var
9
- attr_reader(:configuration_provider)
10
-
11
- def initialize(opts = {})
12
- @configuration_provider =
13
- opts[:configuration_provider] || Configuration.identity_provider
14
- end
15
-
9
+ class Var < Base
16
10
  def execute(overrides = {}, &block)
17
- parameters = with_configuration_provider_parameters(overrides)
18
- parameters = with_resolved_vars(parameters, &block)
11
+ parameters = resolve_parameters(overrides, &block)
19
12
 
20
13
  parameters[:vars][parameters[:name].to_sym]
21
14
  end
22
-
23
- private
24
-
25
- def with_configuration_provider_parameters(parameters)
26
- configuration_provider.resolve(parameters)
27
- end
28
-
29
- def with_resolved_vars(parameters, &block)
30
- return parameters unless block_given?
31
-
32
- var_captor = Configuration::VarCaptor.new(parameters[:vars] || {})
33
- block.call(var_captor)
34
- parameters.merge(vars: var_captor.to_h)
35
- end
36
15
  end
37
16
  end
38
17
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RSpec
4
4
  module Terraform
5
- VERSION = '0.1.0.pre.44'
5
+ VERSION = '0.1.0.pre.46'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-terraform
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0.pre.44
4
+ version: 0.1.0.pre.46
5
5
  platform: ruby
6
6
  authors:
7
7
  - InfraBlocks Maintainers
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-09-11 00:00:00.000000000 Z
11
+ date: 2022-09-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: confidante
@@ -304,9 +304,12 @@ files:
304
304
  - lib/rspec/terraform/configuration/providers/seed.rb
305
305
  - lib/rspec/terraform/configuration/var_captor.rb
306
306
  - lib/rspec/terraform/helpers.rb
307
+ - lib/rspec/terraform/helpers/actions.rb
307
308
  - lib/rspec/terraform/helpers/apply.rb
309
+ - lib/rspec/terraform/helpers/base.rb
308
310
  - lib/rspec/terraform/helpers/destroy.rb
309
311
  - lib/rspec/terraform/helpers/output.rb
312
+ - lib/rspec/terraform/helpers/parameters.rb
310
313
  - lib/rspec/terraform/helpers/plan.rb
311
314
  - lib/rspec/terraform/helpers/var.rb
312
315
  - lib/rspec/terraform/logging.rb