Class: Terraspace::CLI::Summary

Inherits:
Object
  • Object
show all
Includes:
Util::Logging
Defined in:
lib/terraspace/cli/summary.rb

Instance Method Summary collapse

Methods included from Util::Logging

#logger

Constructor Details

#initialize(options = {}) ⇒ Summary

Returns a new instance of Summary.



9
10
11
# File 'lib/terraspace/cli/summary.rb', line 9

def initialize(options={})
  @options = options
end

Instance Method Details

#build_placeholderObject

Grab the last module and build that. Assume the backend key has the same prefix



24
25
26
# File 'lib/terraspace/cli/summary.rb', line 24

def build_placeholder
  Build::Placeholder.new(@options).build
end

#process(path) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/terraspace/cli/summary.rb', line 28

def process(path)
  ext = File.extname(path)
  code = IO.read(path)
  data = ext == ".tf" ? HclParser.load(code) : JSON.load(code)

  backend = data['terraform']['backend']
  name = backend.keys.first # backend name. IE: s3, gcs, azurerm

  info = backend.values.first # structure within the s3 or gcs key
  klass = summary_class(name)
  unless klass
    logger.info "Summary is unavailable for this backend: #{name}"
    exit
  end
  summary = klass.new(info, @options)
  summary.call
end

#runObject



13
14
15
16
17
18
19
20
# File 'lib/terraspace/cli/summary.rb', line 13

def run
  build_placeholder
  puts "Summary of resources based on backend storage statefiles"
  backend_expr = '.terraspace-cache/**/backend.*'
  # Currently summary assumes backend are within the same bucket and key prefix
  backend = Dir.glob(backend_expr).find { |p| p.include?("/#{Terraspace.env}/") }
  process(backend) if backend
end

#summary_class(name) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/terraspace/cli/summary.rb', line 46

def summary_class(name)
  return unless name
  # IE: TerraspacePluginAws::Interfaces::Summary
  klass_name = Terraspace::Plugin.klass("Summary", backend: name)
  klass_name.constantize if klass_name
rescue NameError => e
  logger.error "#{e.class}: #{e.message}"
  logger.error "ERROR: No summary class implementation provided by plugins installed for this backend: #{name}".color(:red)
  exit 1
end