lib/rspec/terraform/helpers/destroy.rb in rspec-terraform-0.1.0.pre.44 vs lib/rspec/terraform/helpers/destroy.rb in rspec-terraform-0.1.0.pre.45
- old
+ new
@@ -1,37 +1,15 @@
# frozen_string_literal: true
require 'ruby_terraform'
-require_relative '../configuration/var_captor'
+require_relative './base'
module RSpec
module Terraform
module Helpers
- # rubocop:disable Metrics/ClassLength
- class Destroy
- attr_reader(
- :configuration_provider,
- :binary,
- :logger,
- :stdin,
- :stdout,
- :stderr,
- :execution_mode
- )
-
- def initialize(opts = {})
- @configuration_provider =
- opts[:configuration_provider] || Configuration.identity_provider
- @binary = opts[:binary] || 'terraform'
- @logger = opts[:logger]
- @stdin = opts[:stdin]
- @stdout = opts[:stdout]
- @stderr = opts[:stderr]
- @execution_mode = opts[:execution_mode] || :in_place
- end
-
+ class Destroy < Base
def execute(overrides = {}, &block)
parameters = with_configuration_provider_parameters(overrides)
parameters = with_resolved_vars(parameters, &block)
execute_if_required(parameters) do
@@ -50,51 +28,17 @@
should_execute = only_if ? only_if.call(*only_if_args) : true
block.call if should_execute
end
- 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 required_parameters(execution_mode)
{
in_place: [:configuration_directory],
isolated: %i[source_directory configuration_directory]
}[execution_mode] || []
end
- def raise_missing_parameters(parameters)
- parameters = parameters.collect { |parameter| "`:#{parameter}`" }
- if parameters.count == 1
- raise StandardError,
- "Required parameter: #{parameters[0]} missing."
- else
- parameters = "#{parameters[..-2].join(', ')} and #{parameters[-1]}"
- raise StandardError,
- "Required parameters: #{parameters} missing."
- end
- end
-
- def validate(parameters)
- missing_parameters =
- required_parameters(execution_mode)
- .filter { |parameter| parameters[parameter].nil? }
-
- return if missing_parameters.empty?
-
- raise_missing_parameters(missing_parameters)
- end
-
def clean(parameters)
return unless execution_mode == :isolated
FileUtils.rm_rf(parameters[:configuration_directory])
FileUtils.mkdir_p(parameters[:configuration_directory])
@@ -144,20 +88,9 @@
end
destroy_parameters
end
# rubocop:enable Metrics/MethodLength
-
- def command_options
- {
- binary: binary,
- logger: logger,
- stdin: stdin,
- stdout: stdout,
- stderr: stderr
- }
- end
end
- # rubocop:enable Metrics/ClassLength
end
end
end