lib/rspec/terraform/helpers/apply.rb in rspec-terraform-0.1.0.pre.23 vs lib/rspec/terraform/helpers/apply.rb in rspec-terraform-0.1.0.pre.24
- old
+ new
@@ -1,9 +1,11 @@
# frozen_string_literal: true
require 'ruby_terraform'
+require_relative '../configuration/var_captor'
+
module RSpec
module Terraform
module Helpers
class Apply
attr_reader(:overrides, :configuration_provider)
@@ -12,28 +14,75 @@
@overrides = overrides
@configuration_provider =
configuration_provider || Configuration.identity_provider
end
- def execute
- parameters = configuration_provider.resolve(overrides)
- parameters = parameters.merge(
+ def execute(&block)
+ parameters = with_configuration_provider_parameters(overrides)
+ parameters = with_resolved_vars(parameters, &block)
+ parameters = with_mandatory_parameters(parameters)
+
+ ensure_required_parameters(parameters)
+
+ init(parameters)
+ apply(parameters)
+ end
+
+ private
+
+ def with_configuration_provider_parameters(parameters)
+ configuration_provider.resolve(parameters)
+ end
+
+ def with_resolved_vars(parameters, &block)
+ return parameters unless block_given?
+
+ var_captor = Configuration::VarCaptor.new(parameters[:vars] || {})
+ block.call(var_captor)
+ parameters.merge(vars: var_captor.to_h)
+ end
+
+ def with_mandatory_parameters(parameters)
+ parameters.merge(
input: false,
auto_approve: true
)
+ end
- do_apply(parameters)
+ def ensure_required_parameters(parameters)
+ return if parameters[:configuration_directory]
+
+ throw StandardError.new(
+ 'No Terraform configuration directory specified.'
+ )
end
- private
+ def init(parameters)
+ init_command.execute(
+ chdir: parameters[:configuration_directory],
+ input: parameters[:input]
+ )
+ end
- def do_apply(parameters)
- RubyTerraform.apply(
+ def apply(parameters)
+ apply_command.execute(
chdir: parameters[:configuration_directory],
state: parameters[:state_file],
vars: parameters[:vars],
input: parameters[:input],
auto_approve: parameters[:auto_approve]
+ )
+ end
+
+ def init_command
+ RubyTerraform::Commands::Init.new(
+ binary: RSpec.configuration.terraform_binary
+ )
+ end
+
+ def apply_command
+ RubyTerraform::Commands::Apply.new(
+ binary: RSpec.configuration.terraform_binary
)
end
end
end
end