rspec-terraform 0.1.0.pre.45 → 0.1.0.pre.47
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +3 -3
- data/lib/rspec/terraform/helpers/actions/apply.rb +49 -0
- data/lib/rspec/terraform/helpers/actions/clean.rb +18 -0
- data/lib/rspec/terraform/helpers/actions/command_instantiation.rb +15 -0
- data/lib/rspec/terraform/helpers/actions/destroy.rb +49 -0
- data/lib/rspec/terraform/helpers/actions/execute_if_required.rb +19 -0
- data/lib/rspec/terraform/helpers/actions/init.rb +50 -0
- data/lib/rspec/terraform/helpers/actions/output.rb +51 -0
- data/lib/rspec/terraform/helpers/actions/plan.rb +53 -0
- data/lib/rspec/terraform/helpers/actions/remove.rb +19 -0
- data/lib/rspec/terraform/helpers/actions/show.rb +52 -0
- data/lib/rspec/terraform/helpers/actions/validate.rb +36 -0
- data/lib/rspec/terraform/helpers/actions.rb +12 -0
- data/lib/rspec/terraform/helpers/apply.rb +8 -66
- data/lib/rspec/terraform/helpers/base.rb +3 -39
- data/lib/rspec/terraform/helpers/destroy.rb +8 -64
- data/lib/rspec/terraform/helpers/output.rb +10 -58
- data/lib/rspec/terraform/helpers/parameters.rb +33 -0
- data/lib/rspec/terraform/helpers/plan.rb +9 -89
- data/lib/rspec/terraform/helpers/var.rb +1 -2
- data/lib/rspec/terraform/version.rb +1 -1
- data/lib/rspec/terraform.rb +0 -1
- metadata +15 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 81533be8b2db5deed030c7d321cf443d1703b7c8e71d7379c70463fab9a9d3f6
|
4
|
+
data.tar.gz: 77b1e6c0b70fc4fd4a258f0a3d6db4c8aa1e404e6059c2eed81ef0163b59509e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c53f5bfe578e18b842e0d509c582fca0c06cb106c10d8fcde12c20361fd2c53b546e3b62dd60109af3bbcb71116c39d4ffcac29703cfa6399b3187da26ab5b58
|
7
|
+
data.tar.gz: 294f50f7e73613ade97534b6706d5df40c6194d50746509826c92f7692484f7cab267e34741257bada91fb1ff92e5e3b5fc7699a506cd6c1870d200f6ba719fb
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
rspec-terraform (0.1.0.pre.
|
4
|
+
rspec-terraform (0.1.0.pre.47)
|
5
5
|
confidante (>= 0.27)
|
6
6
|
rspec (>= 3.0)
|
7
7
|
ruby-terraform (= 1.7.0.pre.17)
|
@@ -145,7 +145,7 @@ GEM
|
|
145
145
|
parser (>= 3.1.1.0)
|
146
146
|
rubocop-rake (0.6.0)
|
147
147
|
rubocop (~> 1.0)
|
148
|
-
rubocop-rspec (2.13.
|
148
|
+
rubocop-rspec (2.13.2)
|
149
149
|
rubocop (~> 1.33)
|
150
150
|
ruby-progressbar (1.11.0)
|
151
151
|
ruby-terraform (1.7.0.pre.17)
|
@@ -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.
|
181
|
+
unicode-display_width (2.3.0)
|
182
182
|
|
183
183
|
PLATFORMS
|
184
184
|
arm64-darwin-21
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'ruby_terraform'
|
4
|
+
|
5
|
+
require_relative './command_instantiation'
|
6
|
+
|
7
|
+
module RSpec
|
8
|
+
module Terraform
|
9
|
+
module Helpers
|
10
|
+
module Actions
|
11
|
+
module Apply
|
12
|
+
include CommandInstantiation
|
13
|
+
|
14
|
+
def apply(parameters)
|
15
|
+
apply_command.execute(apply_parameters(parameters))
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def apply_command
|
21
|
+
instantiate_command(RubyTerraform::Commands::Apply)
|
22
|
+
end
|
23
|
+
|
24
|
+
def apply_parameters(parameters)
|
25
|
+
with_apply_state_file_parameters(
|
26
|
+
with_apply_standard_parameters(parameters)
|
27
|
+
)
|
28
|
+
end
|
29
|
+
|
30
|
+
def with_apply_standard_parameters(parameters)
|
31
|
+
parameters.merge(
|
32
|
+
chdir: parameters[:configuration_directory],
|
33
|
+
input: false,
|
34
|
+
auto_approve: true
|
35
|
+
)
|
36
|
+
end
|
37
|
+
|
38
|
+
def with_apply_state_file_parameters(parameters)
|
39
|
+
if parameters[:state_file]
|
40
|
+
return parameters.merge(state: parameters[:state_file])
|
41
|
+
end
|
42
|
+
|
43
|
+
parameters
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RSpec
|
4
|
+
module Terraform
|
5
|
+
module Helpers
|
6
|
+
module Actions
|
7
|
+
module Clean
|
8
|
+
def clean(parameters)
|
9
|
+
return unless execution_mode == :isolated
|
10
|
+
|
11
|
+
FileUtils.rm_rf(parameters[:configuration_directory])
|
12
|
+
FileUtils.mkdir_p(parameters[:configuration_directory])
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RSpec
|
4
|
+
module Terraform
|
5
|
+
module Helpers
|
6
|
+
module Actions
|
7
|
+
module CommandInstantiation
|
8
|
+
def instantiate_command(klass, opts = {})
|
9
|
+
klass.new(command_options.merge(opts))
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'ruby_terraform'
|
4
|
+
|
5
|
+
require_relative './command_instantiation'
|
6
|
+
|
7
|
+
module RSpec
|
8
|
+
module Terraform
|
9
|
+
module Helpers
|
10
|
+
module Actions
|
11
|
+
module Destroy
|
12
|
+
include CommandInstantiation
|
13
|
+
|
14
|
+
def destroy(parameters)
|
15
|
+
destroy_command.execute(destroy_parameters(parameters))
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def destroy_command
|
21
|
+
instantiate_command(RubyTerraform::Commands::Destroy)
|
22
|
+
end
|
23
|
+
|
24
|
+
def destroy_parameters(parameters)
|
25
|
+
with_destroy_state_file_parameters(
|
26
|
+
with_destroy_standard_parameters(parameters)
|
27
|
+
)
|
28
|
+
end
|
29
|
+
|
30
|
+
def with_destroy_standard_parameters(parameters)
|
31
|
+
parameters.merge(
|
32
|
+
chdir: parameters[:configuration_directory],
|
33
|
+
input: false,
|
34
|
+
auto_approve: true
|
35
|
+
)
|
36
|
+
end
|
37
|
+
|
38
|
+
def with_destroy_state_file_parameters(parameters)
|
39
|
+
if parameters[:state_file]
|
40
|
+
return parameters.merge(state: parameters[:state_file])
|
41
|
+
end
|
42
|
+
|
43
|
+
parameters
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RSpec
|
4
|
+
module Terraform
|
5
|
+
module Helpers
|
6
|
+
module Actions
|
7
|
+
module ExecuteIfRequired
|
8
|
+
def execute_if_required(parameters, &block)
|
9
|
+
only_if = parameters[:only_if]
|
10
|
+
only_if_args = only_if ? [parameters].slice(0, only_if.arity) : []
|
11
|
+
should_execute = only_if ? only_if.call(*only_if_args) : true
|
12
|
+
|
13
|
+
block.call if should_execute
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'ruby_terraform'
|
4
|
+
|
5
|
+
require_relative './command_instantiation'
|
6
|
+
|
7
|
+
module RSpec
|
8
|
+
module Terraform
|
9
|
+
module Helpers
|
10
|
+
module Actions
|
11
|
+
module Init
|
12
|
+
include CommandInstantiation
|
13
|
+
|
14
|
+
def init(parameters)
|
15
|
+
init_command.execute(init_parameters(parameters))
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def init_command
|
21
|
+
instantiate_command(RubyTerraform::Commands::Init)
|
22
|
+
end
|
23
|
+
|
24
|
+
def init_parameters(parameters)
|
25
|
+
with_init_execution_mode_parameters(
|
26
|
+
with_init_standard_parameters(parameters)
|
27
|
+
)
|
28
|
+
end
|
29
|
+
|
30
|
+
def with_init_standard_parameters(parameters)
|
31
|
+
parameters.merge(
|
32
|
+
chdir: parameters[:configuration_directory],
|
33
|
+
input: false
|
34
|
+
)
|
35
|
+
end
|
36
|
+
|
37
|
+
def with_init_execution_mode_parameters(parameters)
|
38
|
+
if execution_mode == :isolated
|
39
|
+
return parameters.merge(
|
40
|
+
from_module: parameters[:source_directory]
|
41
|
+
)
|
42
|
+
end
|
43
|
+
|
44
|
+
parameters
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'ruby_terraform'
|
4
|
+
require 'stringio'
|
5
|
+
|
6
|
+
require_relative './command_instantiation'
|
7
|
+
|
8
|
+
module RSpec
|
9
|
+
module Terraform
|
10
|
+
module Helpers
|
11
|
+
module Actions
|
12
|
+
module Output
|
13
|
+
include CommandInstantiation
|
14
|
+
|
15
|
+
def output(parameters)
|
16
|
+
stdout = StringIO.new
|
17
|
+
output_command(stdout: stdout)
|
18
|
+
.execute(output_parameters(parameters))
|
19
|
+
stdout.string
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def output_command(opts = {})
|
25
|
+
instantiate_command(RubyTerraform::Commands::Output, opts)
|
26
|
+
end
|
27
|
+
|
28
|
+
def output_parameters(parameters)
|
29
|
+
with_output_state_file_parameters(
|
30
|
+
with_output_standard_parameters(parameters)
|
31
|
+
)
|
32
|
+
end
|
33
|
+
|
34
|
+
def with_output_standard_parameters(parameters)
|
35
|
+
parameters.merge(
|
36
|
+
chdir: parameters[:configuration_directory]
|
37
|
+
)
|
38
|
+
end
|
39
|
+
|
40
|
+
def with_output_state_file_parameters(parameters)
|
41
|
+
if parameters[:state_file]
|
42
|
+
return parameters.merge(state: parameters[:state_file])
|
43
|
+
end
|
44
|
+
|
45
|
+
parameters
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'ruby_terraform'
|
4
|
+
require 'securerandom'
|
5
|
+
|
6
|
+
require_relative './command_instantiation'
|
7
|
+
|
8
|
+
module RSpec
|
9
|
+
module Terraform
|
10
|
+
module Helpers
|
11
|
+
module Actions
|
12
|
+
module Plan
|
13
|
+
include CommandInstantiation
|
14
|
+
|
15
|
+
def plan(parameters)
|
16
|
+
plan_parameters = plan_parameters(parameters)
|
17
|
+
plan_command.execute(plan_parameters)
|
18
|
+
plan_parameters[:out]
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def plan_command
|
24
|
+
instantiate_command(RubyTerraform::Commands::Plan)
|
25
|
+
end
|
26
|
+
|
27
|
+
def plan_parameters(parameters)
|
28
|
+
with_plan_state_file_parameters(
|
29
|
+
with_plan_standard_parameters(parameters)
|
30
|
+
)
|
31
|
+
end
|
32
|
+
|
33
|
+
def with_plan_standard_parameters(parameters)
|
34
|
+
parameters.merge(
|
35
|
+
chdir: parameters[:configuration_directory],
|
36
|
+
out: parameters[:plan_file_name] ||
|
37
|
+
"#{SecureRandom.hex[0, 10]}.tfplan",
|
38
|
+
input: false
|
39
|
+
)
|
40
|
+
end
|
41
|
+
|
42
|
+
def with_plan_state_file_parameters(parameters)
|
43
|
+
if parameters[:state_file]
|
44
|
+
return parameters.merge(state: parameters[:state_file])
|
45
|
+
end
|
46
|
+
|
47
|
+
parameters
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'fileutils'
|
4
|
+
|
5
|
+
module RSpec
|
6
|
+
module Terraform
|
7
|
+
module Helpers
|
8
|
+
module Actions
|
9
|
+
module Remove
|
10
|
+
def remove(parameters, file)
|
11
|
+
FileUtils.rm_f(
|
12
|
+
File.join(parameters[:configuration_directory], file)
|
13
|
+
)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'ruby_terraform'
|
4
|
+
require 'stringio'
|
5
|
+
|
6
|
+
require_relative './command_instantiation'
|
7
|
+
|
8
|
+
module RSpec
|
9
|
+
module Terraform
|
10
|
+
module Helpers
|
11
|
+
module Actions
|
12
|
+
module Show
|
13
|
+
include CommandInstantiation
|
14
|
+
|
15
|
+
def show(parameters, plan_file)
|
16
|
+
stdout = StringIO.new
|
17
|
+
show_command(stdout: stdout)
|
18
|
+
.execute(show_parameters(parameters, plan_file))
|
19
|
+
stdout.string
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def show_command(opts = {})
|
25
|
+
instantiate_command(RubyTerraform::Commands::Show, opts)
|
26
|
+
end
|
27
|
+
|
28
|
+
def show_parameters(parameters, plan_file)
|
29
|
+
with_show_plan_file_parameters(
|
30
|
+
with_show_standard_parameters(parameters),
|
31
|
+
plan_file
|
32
|
+
)
|
33
|
+
end
|
34
|
+
|
35
|
+
def with_show_standard_parameters(parameters)
|
36
|
+
parameters.merge(
|
37
|
+
chdir: parameters[:configuration_directory],
|
38
|
+
no_color: true,
|
39
|
+
json: true
|
40
|
+
)
|
41
|
+
end
|
42
|
+
|
43
|
+
def with_show_plan_file_parameters(parameters, plan_file)
|
44
|
+
parameters.merge(
|
45
|
+
path: plan_file
|
46
|
+
)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RSpec
|
4
|
+
module Terraform
|
5
|
+
module Helpers
|
6
|
+
module Actions
|
7
|
+
module Validate
|
8
|
+
def validate(parameters)
|
9
|
+
missing_parameters =
|
10
|
+
required_parameters(execution_mode)
|
11
|
+
.filter { |parameter| parameters[parameter].nil? }
|
12
|
+
|
13
|
+
return if missing_parameters.empty?
|
14
|
+
|
15
|
+
raise_missing_parameters(missing_parameters)
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def raise_missing_parameters(parameters)
|
21
|
+
parameters = parameters.collect { |parameter| "`:#{parameter}`" }
|
22
|
+
if parameters.count == 1
|
23
|
+
raise StandardError,
|
24
|
+
"Required parameter: #{parameters[0]} missing."
|
25
|
+
else
|
26
|
+
parameters =
|
27
|
+
"#{parameters[..-2].join(', ')} and #{parameters[-1]}"
|
28
|
+
raise StandardError,
|
29
|
+
"Required parameters: #{parameters} missing."
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative './actions/clean'
|
4
|
+
require_relative './actions/validate'
|
5
|
+
require_relative './actions/init'
|
6
|
+
require_relative './actions/apply'
|
7
|
+
require_relative './actions/destroy'
|
8
|
+
require_relative './actions/plan'
|
9
|
+
require_relative './actions/output'
|
10
|
+
require_relative './actions/show'
|
11
|
+
require_relative './actions/remove'
|
12
|
+
require_relative './actions/execute_if_required'
|
@@ -3,16 +3,20 @@
|
|
3
3
|
require 'ruby_terraform'
|
4
4
|
|
5
5
|
require_relative './base'
|
6
|
-
require_relative '
|
6
|
+
require_relative './actions'
|
7
7
|
|
8
8
|
module RSpec
|
9
9
|
module Terraform
|
10
10
|
module Helpers
|
11
11
|
class Apply < Base
|
12
|
+
include Actions::ExecuteIfRequired
|
13
|
+
include Actions::Validate
|
14
|
+
include Actions::Clean
|
15
|
+
include Actions::Init
|
16
|
+
include Actions::Apply
|
17
|
+
|
12
18
|
def execute(overrides = {}, &block)
|
13
|
-
parameters =
|
14
|
-
parameters = with_resolved_vars(parameters, &block)
|
15
|
-
parameters = with_mandatory_parameters(parameters)
|
19
|
+
parameters = resolve_parameters(overrides, &block)
|
16
20
|
|
17
21
|
execute_if_required(parameters) do
|
18
22
|
validate(parameters)
|
@@ -24,14 +28,6 @@ module RSpec
|
|
24
28
|
|
25
29
|
private
|
26
30
|
|
27
|
-
def execute_if_required(parameters, &block)
|
28
|
-
only_if = parameters[:only_if]
|
29
|
-
only_if_args = only_if ? [parameters].slice(0, only_if.arity) : []
|
30
|
-
should_execute = only_if ? only_if.call(*only_if_args) : true
|
31
|
-
|
32
|
-
block.call if should_execute
|
33
|
-
end
|
34
|
-
|
35
31
|
def mandatory_parameters
|
36
32
|
{
|
37
33
|
input: false,
|
@@ -45,60 +41,6 @@ module RSpec
|
|
45
41
|
isolated: %i[source_directory configuration_directory]
|
46
42
|
}[execution_mode] || []
|
47
43
|
end
|
48
|
-
|
49
|
-
def clean(parameters)
|
50
|
-
return unless execution_mode == :isolated
|
51
|
-
|
52
|
-
FileUtils.rm_rf(parameters[:configuration_directory])
|
53
|
-
FileUtils.mkdir_p(parameters[:configuration_directory])
|
54
|
-
end
|
55
|
-
|
56
|
-
def init(parameters)
|
57
|
-
init_command.execute(init_parameters(parameters))
|
58
|
-
end
|
59
|
-
|
60
|
-
def apply(parameters)
|
61
|
-
apply_command.execute(apply_parameters(parameters))
|
62
|
-
end
|
63
|
-
|
64
|
-
def init_command
|
65
|
-
RubyTerraform::Commands::Init.new(command_options)
|
66
|
-
end
|
67
|
-
|
68
|
-
def apply_command
|
69
|
-
RubyTerraform::Commands::Apply.new(command_options)
|
70
|
-
end
|
71
|
-
|
72
|
-
def init_parameters(parameters)
|
73
|
-
init_parameters = parameters.merge(
|
74
|
-
chdir: parameters[:configuration_directory],
|
75
|
-
input: false
|
76
|
-
)
|
77
|
-
if execution_mode == :isolated
|
78
|
-
init_parameters =
|
79
|
-
init_parameters.merge(from_module: parameters[:source_directory])
|
80
|
-
end
|
81
|
-
|
82
|
-
init_parameters
|
83
|
-
end
|
84
|
-
|
85
|
-
# rubocop:disable Metrics/MethodLength
|
86
|
-
def apply_parameters(parameters)
|
87
|
-
apply_parameters =
|
88
|
-
parameters.merge(
|
89
|
-
chdir: parameters[:configuration_directory],
|
90
|
-
input: false,
|
91
|
-
auto_approve: true
|
92
|
-
)
|
93
|
-
|
94
|
-
if parameters[:state_file]
|
95
|
-
apply_parameters =
|
96
|
-
apply_parameters.merge(state: parameters[:state_file])
|
97
|
-
end
|
98
|
-
|
99
|
-
apply_parameters
|
100
|
-
end
|
101
|
-
# rubocop:enable Metrics/MethodLength
|
102
44
|
end
|
103
45
|
end
|
104
46
|
end
|
@@ -1,11 +1,13 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative '
|
3
|
+
require_relative './parameters'
|
4
4
|
|
5
5
|
module RSpec
|
6
6
|
module Terraform
|
7
7
|
module Helpers
|
8
8
|
class Base
|
9
|
+
include Parameters
|
10
|
+
|
9
11
|
attr_reader(
|
10
12
|
:configuration_provider,
|
11
13
|
:binary,
|
@@ -29,44 +31,6 @@ module RSpec
|
|
29
31
|
|
30
32
|
private
|
31
33
|
|
32
|
-
def with_configuration_provider_parameters(parameters)
|
33
|
-
configuration_provider.resolve(parameters)
|
34
|
-
end
|
35
|
-
|
36
|
-
def with_resolved_vars(parameters, &block)
|
37
|
-
return parameters unless block_given?
|
38
|
-
|
39
|
-
var_captor = Configuration::VarCaptor.new(parameters[:vars] || {})
|
40
|
-
block.call(var_captor)
|
41
|
-
parameters.merge(vars: var_captor.to_h)
|
42
|
-
end
|
43
|
-
|
44
|
-
def with_mandatory_parameters(parameters)
|
45
|
-
parameters.merge(mandatory_parameters)
|
46
|
-
end
|
47
|
-
|
48
|
-
def raise_missing_parameters(parameters)
|
49
|
-
parameters = parameters.collect { |parameter| "`:#{parameter}`" }
|
50
|
-
if parameters.count == 1
|
51
|
-
raise StandardError,
|
52
|
-
"Required parameter: #{parameters[0]} missing."
|
53
|
-
else
|
54
|
-
parameters = "#{parameters[..-2].join(', ')} and #{parameters[-1]}"
|
55
|
-
raise StandardError,
|
56
|
-
"Required parameters: #{parameters} missing."
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
def validate(parameters)
|
61
|
-
missing_parameters =
|
62
|
-
required_parameters(execution_mode)
|
63
|
-
.filter { |parameter| parameters[parameter].nil? }
|
64
|
-
|
65
|
-
return if missing_parameters.empty?
|
66
|
-
|
67
|
-
raise_missing_parameters(missing_parameters)
|
68
|
-
end
|
69
|
-
|
70
34
|
def mandatory_parameters
|
71
35
|
{}
|
72
36
|
end
|
@@ -3,14 +3,20 @@
|
|
3
3
|
require 'ruby_terraform'
|
4
4
|
|
5
5
|
require_relative './base'
|
6
|
+
require_relative './actions'
|
6
7
|
|
7
8
|
module RSpec
|
8
9
|
module Terraform
|
9
10
|
module Helpers
|
10
11
|
class Destroy < Base
|
12
|
+
include Actions::ExecuteIfRequired
|
13
|
+
include Actions::Validate
|
14
|
+
include Actions::Clean
|
15
|
+
include Actions::Init
|
16
|
+
include Actions::Destroy
|
17
|
+
|
11
18
|
def execute(overrides = {}, &block)
|
12
|
-
parameters =
|
13
|
-
parameters = with_resolved_vars(parameters, &block)
|
19
|
+
parameters = resolve_parameters(overrides, &block)
|
14
20
|
|
15
21
|
execute_if_required(parameters) do
|
16
22
|
validate(parameters)
|
@@ -22,74 +28,12 @@ module RSpec
|
|
22
28
|
|
23
29
|
private
|
24
30
|
|
25
|
-
def execute_if_required(parameters, &block)
|
26
|
-
only_if = parameters[:only_if]
|
27
|
-
only_if_args = only_if ? [parameters].slice(0, only_if.arity) : []
|
28
|
-
should_execute = only_if ? only_if.call(*only_if_args) : true
|
29
|
-
|
30
|
-
block.call if should_execute
|
31
|
-
end
|
32
|
-
|
33
31
|
def required_parameters(execution_mode)
|
34
32
|
{
|
35
33
|
in_place: [:configuration_directory],
|
36
34
|
isolated: %i[source_directory configuration_directory]
|
37
35
|
}[execution_mode] || []
|
38
36
|
end
|
39
|
-
|
40
|
-
def clean(parameters)
|
41
|
-
return unless execution_mode == :isolated
|
42
|
-
|
43
|
-
FileUtils.rm_rf(parameters[:configuration_directory])
|
44
|
-
FileUtils.mkdir_p(parameters[:configuration_directory])
|
45
|
-
end
|
46
|
-
|
47
|
-
def init(parameters)
|
48
|
-
init_command.execute(init_parameters(parameters))
|
49
|
-
end
|
50
|
-
|
51
|
-
def destroy(parameters)
|
52
|
-
destroy_command.execute(destroy_parameters(parameters))
|
53
|
-
end
|
54
|
-
|
55
|
-
def init_command
|
56
|
-
RubyTerraform::Commands::Init.new(command_options)
|
57
|
-
end
|
58
|
-
|
59
|
-
def destroy_command
|
60
|
-
RubyTerraform::Commands::Destroy.new(command_options)
|
61
|
-
end
|
62
|
-
|
63
|
-
def init_parameters(parameters)
|
64
|
-
init_parameters = parameters.merge(
|
65
|
-
chdir: parameters[:configuration_directory],
|
66
|
-
input: false
|
67
|
-
)
|
68
|
-
if execution_mode == :isolated
|
69
|
-
init_parameters =
|
70
|
-
init_parameters.merge(from_module: parameters[:source_directory])
|
71
|
-
end
|
72
|
-
|
73
|
-
init_parameters
|
74
|
-
end
|
75
|
-
|
76
|
-
# rubocop:disable Metrics/MethodLength
|
77
|
-
def destroy_parameters(parameters)
|
78
|
-
destroy_parameters =
|
79
|
-
parameters.merge(
|
80
|
-
chdir: parameters[:configuration_directory],
|
81
|
-
input: false,
|
82
|
-
auto_approve: true
|
83
|
-
)
|
84
|
-
|
85
|
-
if parameters[:state_file]
|
86
|
-
destroy_parameters =
|
87
|
-
destroy_parameters.merge(state: parameters[:state_file])
|
88
|
-
end
|
89
|
-
|
90
|
-
destroy_parameters
|
91
|
-
end
|
92
|
-
# rubocop:enable Metrics/MethodLength
|
93
37
|
end
|
94
38
|
end
|
95
39
|
end
|
@@ -3,29 +3,30 @@
|
|
3
3
|
require 'ruby_terraform'
|
4
4
|
|
5
5
|
require_relative './base'
|
6
|
+
require_relative './actions'
|
6
7
|
|
7
8
|
module RSpec
|
8
9
|
module Terraform
|
9
10
|
module Helpers
|
10
11
|
class Output < Base
|
12
|
+
include Actions::Validate
|
13
|
+
include Actions::Clean
|
14
|
+
include Actions::Init
|
15
|
+
include Actions::Output
|
16
|
+
|
11
17
|
def execute(overrides = {})
|
12
|
-
parameters =
|
13
|
-
parameters = with_mandatory_parameters(parameters)
|
18
|
+
parameters = resolve_parameters(overrides)
|
14
19
|
|
15
20
|
validate(parameters)
|
16
21
|
clean(parameters)
|
17
22
|
init(parameters)
|
18
23
|
output_value = output(parameters)
|
19
24
|
|
20
|
-
|
25
|
+
parse(output_value)
|
21
26
|
end
|
22
27
|
|
23
28
|
private
|
24
29
|
|
25
|
-
def with_mandatory_parameters(parameters)
|
26
|
-
parameters.merge(mandatory_parameters)
|
27
|
-
end
|
28
|
-
|
29
30
|
def mandatory_parameters
|
30
31
|
{
|
31
32
|
json: true
|
@@ -39,57 +40,8 @@ module RSpec
|
|
39
40
|
}[execution_mode] || []
|
40
41
|
end
|
41
42
|
|
42
|
-
def
|
43
|
-
|
44
|
-
|
45
|
-
FileUtils.rm_rf(parameters[:configuration_directory])
|
46
|
-
FileUtils.mkdir_p(parameters[:configuration_directory])
|
47
|
-
end
|
48
|
-
|
49
|
-
def init(parameters)
|
50
|
-
init_command.execute(init_parameters(parameters))
|
51
|
-
end
|
52
|
-
|
53
|
-
def output(parameters)
|
54
|
-
stdout = StringIO.new
|
55
|
-
output_command(stdout: stdout)
|
56
|
-
.execute(output_parameters(parameters))
|
57
|
-
stdout.string
|
58
|
-
end
|
59
|
-
|
60
|
-
def init_command
|
61
|
-
RubyTerraform::Commands::Init.new(command_options)
|
62
|
-
end
|
63
|
-
|
64
|
-
def output_command(opts = {})
|
65
|
-
RubyTerraform::Commands::Output.new(command_options.merge(opts))
|
66
|
-
end
|
67
|
-
|
68
|
-
def init_parameters(parameters)
|
69
|
-
init_parameters = parameters.merge(
|
70
|
-
chdir: parameters[:configuration_directory],
|
71
|
-
input: false
|
72
|
-
)
|
73
|
-
if execution_mode == :isolated
|
74
|
-
init_parameters =
|
75
|
-
init_parameters.merge(from_module: parameters[:source_directory])
|
76
|
-
end
|
77
|
-
|
78
|
-
init_parameters
|
79
|
-
end
|
80
|
-
|
81
|
-
def output_parameters(parameters)
|
82
|
-
output_parameters =
|
83
|
-
parameters.merge(
|
84
|
-
chdir: parameters[:configuration_directory]
|
85
|
-
)
|
86
|
-
|
87
|
-
if parameters[:state_file]
|
88
|
-
output_parameters =
|
89
|
-
output_parameters.merge(state: parameters[:state_file])
|
90
|
-
end
|
91
|
-
|
92
|
-
output_parameters
|
43
|
+
def parse(output_value)
|
44
|
+
JSON.parse(output_value, symbolize_names: true)
|
93
45
|
end
|
94
46
|
end
|
95
47
|
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
|
@@ -1,18 +1,21 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'ruby_terraform'
|
4
|
-
require 'securerandom'
|
5
|
-
require 'stringio'
|
6
|
-
|
7
3
|
require_relative './base'
|
4
|
+
require_relative './actions'
|
8
5
|
|
9
6
|
module RSpec
|
10
7
|
module Terraform
|
11
8
|
module Helpers
|
12
9
|
class Plan < Base
|
10
|
+
include Actions::Validate
|
11
|
+
include Actions::Clean
|
12
|
+
include Actions::Init
|
13
|
+
include Actions::Plan
|
14
|
+
include Actions::Show
|
15
|
+
include Actions::Remove
|
16
|
+
|
13
17
|
def execute(overrides = {}, &block)
|
14
|
-
parameters =
|
15
|
-
parameters = with_resolved_vars(parameters, &block)
|
18
|
+
parameters = resolve_parameters(overrides, &block)
|
16
19
|
|
17
20
|
validate(parameters)
|
18
21
|
clean(parameters)
|
@@ -32,94 +35,11 @@ module RSpec
|
|
32
35
|
}[execution_mode] || []
|
33
36
|
end
|
34
37
|
|
35
|
-
def clean(parameters)
|
36
|
-
return unless execution_mode == :isolated
|
37
|
-
|
38
|
-
FileUtils.rm_rf(parameters[:configuration_directory])
|
39
|
-
FileUtils.mkdir_p(parameters[:configuration_directory])
|
40
|
-
end
|
41
|
-
|
42
|
-
def init(parameters)
|
43
|
-
init_command.execute(init_parameters(parameters))
|
44
|
-
end
|
45
|
-
|
46
|
-
def plan(parameters)
|
47
|
-
plan_parameters = plan_parameters(parameters)
|
48
|
-
plan_command.execute(plan_parameters)
|
49
|
-
plan_parameters[:out]
|
50
|
-
end
|
51
|
-
|
52
|
-
def show(parameters, plan_file)
|
53
|
-
stdout = StringIO.new
|
54
|
-
show_command(stdout: stdout)
|
55
|
-
.execute(show_parameters(parameters, plan_file))
|
56
|
-
stdout.string
|
57
|
-
end
|
58
|
-
|
59
38
|
def parse(plan_contents)
|
60
39
|
RubyTerraform::Models::Plan.new(
|
61
40
|
JSON.parse(plan_contents, symbolize_names: true)
|
62
41
|
)
|
63
42
|
end
|
64
|
-
|
65
|
-
def remove(parameters, plan_file)
|
66
|
-
FileUtils.rm_f(
|
67
|
-
File.join(parameters[:configuration_directory], plan_file)
|
68
|
-
)
|
69
|
-
end
|
70
|
-
|
71
|
-
def init_command
|
72
|
-
RubyTerraform::Commands::Init.new(command_options)
|
73
|
-
end
|
74
|
-
|
75
|
-
def plan_command
|
76
|
-
RubyTerraform::Commands::Plan.new(command_options)
|
77
|
-
end
|
78
|
-
|
79
|
-
def show_command(opts = {})
|
80
|
-
RubyTerraform::Commands::Show.new(command_options.merge(opts))
|
81
|
-
end
|
82
|
-
|
83
|
-
def init_parameters(parameters)
|
84
|
-
init_parameters = parameters.merge(
|
85
|
-
chdir: parameters[:configuration_directory],
|
86
|
-
input: false
|
87
|
-
)
|
88
|
-
if execution_mode == :isolated
|
89
|
-
init_parameters =
|
90
|
-
init_parameters.merge(from_module: parameters[:source_directory])
|
91
|
-
end
|
92
|
-
|
93
|
-
init_parameters
|
94
|
-
end
|
95
|
-
|
96
|
-
# rubocop:disable Metrics/MethodLength
|
97
|
-
def plan_parameters(parameters)
|
98
|
-
plan_parameters =
|
99
|
-
parameters.merge(
|
100
|
-
chdir: parameters[:configuration_directory],
|
101
|
-
out: parameters[:plan_file_name] ||
|
102
|
-
"#{SecureRandom.hex[0, 10]}.tfplan",
|
103
|
-
input: false
|
104
|
-
)
|
105
|
-
|
106
|
-
if parameters[:state_file]
|
107
|
-
plan_parameters =
|
108
|
-
plan_parameters.merge(state: parameters[:state_file])
|
109
|
-
end
|
110
|
-
|
111
|
-
plan_parameters
|
112
|
-
end
|
113
|
-
# rubocop:enable Metrics/MethodLength
|
114
|
-
|
115
|
-
def show_parameters(parameters, plan_file)
|
116
|
-
parameters.merge(
|
117
|
-
chdir: parameters[:configuration_directory],
|
118
|
-
path: plan_file,
|
119
|
-
no_color: true,
|
120
|
-
json: true
|
121
|
-
)
|
122
|
-
end
|
123
43
|
end
|
124
44
|
end
|
125
45
|
end
|
@@ -8,8 +8,7 @@ module RSpec
|
|
8
8
|
module Helpers
|
9
9
|
class Var < Base
|
10
10
|
def execute(overrides = {}, &block)
|
11
|
-
parameters =
|
12
|
-
parameters = with_resolved_vars(parameters, &block)
|
11
|
+
parameters = resolve_parameters(overrides, &block)
|
13
12
|
|
14
13
|
parameters[:vars][parameters[:name].to_sym]
|
15
14
|
end
|
data/lib/rspec/terraform.rb
CHANGED
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.
|
4
|
+
version: 0.1.0.pre.47
|
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
|
+
date: 2022-09-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: confidante
|
@@ -304,10 +304,23 @@ 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
|
308
|
+
- lib/rspec/terraform/helpers/actions/apply.rb
|
309
|
+
- lib/rspec/terraform/helpers/actions/clean.rb
|
310
|
+
- lib/rspec/terraform/helpers/actions/command_instantiation.rb
|
311
|
+
- lib/rspec/terraform/helpers/actions/destroy.rb
|
312
|
+
- lib/rspec/terraform/helpers/actions/execute_if_required.rb
|
313
|
+
- lib/rspec/terraform/helpers/actions/init.rb
|
314
|
+
- lib/rspec/terraform/helpers/actions/output.rb
|
315
|
+
- lib/rspec/terraform/helpers/actions/plan.rb
|
316
|
+
- lib/rspec/terraform/helpers/actions/remove.rb
|
317
|
+
- lib/rspec/terraform/helpers/actions/show.rb
|
318
|
+
- lib/rspec/terraform/helpers/actions/validate.rb
|
307
319
|
- lib/rspec/terraform/helpers/apply.rb
|
308
320
|
- lib/rspec/terraform/helpers/base.rb
|
309
321
|
- lib/rspec/terraform/helpers/destroy.rb
|
310
322
|
- lib/rspec/terraform/helpers/output.rb
|
323
|
+
- lib/rspec/terraform/helpers/parameters.rb
|
311
324
|
- lib/rspec/terraform/helpers/plan.rb
|
312
325
|
- lib/rspec/terraform/helpers/var.rb
|
313
326
|
- lib/rspec/terraform/logging.rb
|