module Terraspace::Plugin::Summary::Interface

Public Class Methods

new(info, options={}) click to toggle source
# File lib/terraspace/plugin/summary/interface.rb, line 11
def initialize(info, options={})
  @info, @options = info, options
end

Public Instance Methods

bucket_field() click to toggle source

default because aws and google uses it. azure uses storage_account_name and other attributes interface method

# File lib/terraspace/plugin/summary/interface.rb, line 40
def bucket_field
  'bucket'
end
call() click to toggle source
  1. download state files to temp area

  2. show resources for each

# File lib/terraspace/plugin/summary/interface.rb, line 17
def call
  # Note: will not change any of these instance variables unless we note breaking changes
  @bucket = @info[bucket_field]
  @key = @info[key_field] # key_field is INTERFACE METHOD IE: aws: key , google: prefix
  @folder = folder(@key)  # useful as prefix for performance when listing objects in buckets
  @dest = dest(@bucket)
  # May change any of these instance variables that follow
  @dest_folder = "#{@dest}/#{@folder}"

  download_statefiles
  show_resources
end
delete_empty_statefile(key) click to toggle source

interface method

# File lib/terraspace/plugin/summary/interface.rb, line 93
def delete_empty_statefile(key)
end
download_statefiles() click to toggle source
# File lib/terraspace/plugin/summary/interface.rb, line 49
def download_statefiles
  return unless download?
  FileUtils.rm_rf(@dest_folder)
  logger.info("Downloading statefiles to #{@dest_folder}")
  download # INTERFACE METHOD
end
key_field() click to toggle source

default because aws and azurerm uses it. google uses prefix interface method

# File lib/terraspace/plugin/summary/interface.rb, line 33
def key_field
  'key'
end
remove_statefile(path) click to toggle source

Clean up empty statefiles because over time the extra network calls to download them slow down the summary command

# File lib/terraspace/plugin/summary/interface.rb, line 87
def remove_statefile(path)
  key = path.sub("#{statefiles_root}/#{@bucket}/",'')
  delete_empty_statefile(key)
end
show_each(path) click to toggle source
# File lib/terraspace/plugin/summary/interface.rb, line 65
def show_each(path)
  data = JSON.load(IO.read(path))
  return unless data # edge case: blank file
  resources = data['resources']
  return unless resources
  remove_statefile(path) if Terraspace.config.summary.prune && resources && resources.size == 0
  return unless resources && resources.size > 0

  pretty_path = path.sub(Regexp.new(".*#{@bucket}/#{@folder}"), '')
  logger.info pretty_path.color(:green)
  resources.each do |r|
    @has_shown_resources = true # flag to note some resources there were shown
    identifier = r['instances'].map do |i|
      i['attributes']['name'] || i['attributes']['id']
    end.join(',')
    return unless @options[:details]
    logger.info "    #{r['type']} #{r['name']}: #{identifier}"
  end
end
show_resources() click to toggle source
# File lib/terraspace/plugin/summary/interface.rb, line 56
def show_resources
  Dir.glob(statefile_expr).sort.each do |path|
    next unless File.file?(path)
    next if path.include?(".tflock")
    show_each(path)
  end
  logger.info("No resources found in statefiles") unless @has_shown_resources
end
statefile_expr() click to toggle source

Allow override by plugin implementation class. Generally, all files in these folders are tfstate files.

# File lib/terraspace/plugin/summary/interface.rb, line 45
def statefile_expr
  "#{@dest_folder}**/*"
end

Private Instance Methods

dest(bucket) click to toggle source
# File lib/terraspace/plugin/summary/interface.rb, line 116
def dest(bucket)
  "#{statefiles_root}/#{bucket}"
end
download?() click to toggle source
# File lib/terraspace/plugin/summary/interface.rb, line 128
def download?
  ENV['TS_SUMMARY_DOWNLOAD'] != '0'
end
folder(path) click to toggle source
# File lib/terraspace/plugin/summary/interface.rb, line 97
def folder(path)
  index = locate_env_index(path)
  path[0..index] # Example folder: us-central1/dev/
end
locate_env_index(path) click to toggle source

Assume that the state files are within a env folder. IE: us-central1/dev/stacks/vm

# File lib/terraspace/plugin/summary/interface.rb, line 104
def locate_env_index(path)
  regexp = Regexp.new("/#{Terraspace.env}/")
  index = path.index(regexp)
  unless index
    logger.error "ERROR: Unable to find the #{Terraspace.env} position in the prefix"
    logger.error "path used: #{path}"
    exit 1
  end
  env_chars = Terraspace.env.size + 1
  index + env_chars
end
logger() click to toggle source
# File lib/terraspace/plugin/summary/interface.rb, line 124
def logger
  Terraspace.logger
end
statefiles_root() click to toggle source
# File lib/terraspace/plugin/summary/interface.rb, line 120
def statefiles_root
  "#{Terraspace.tmp_root}/statefiles"
end