Module: Terraspace::Plugin::Summary::Interface

Defined in:
lib/terraspace/plugin/summary/interface.rb

Instance Method Summary collapse

Instance Method Details

#bucket_fieldObject

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



40
41
42
# File 'lib/terraspace/plugin/summary/interface.rb', line 40

def bucket_field
  'bucket'
end

#callObject

  1. download state files to temp area

  2. show resources for each



17
18
19
20
21
22
23
24
25
26
27
28
# 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) ⇒ Object

interface method



93
94
# File 'lib/terraspace/plugin/summary/interface.rb', line 93

def delete_empty_statefile(key)
end

#download_statefilesObject



49
50
51
52
53
54
# File 'lib/terraspace/plugin/summary/interface.rb', line 49

def download_statefiles
  return unless download?
  FileUtils.rm_rf(@dest_folder)
  logger.debug("Downloading statefiles to #{@dest_folder}")
  download # INTERFACE METHOD
end

#initialize(info, options = {}) ⇒ Object



11
12
13
# File 'lib/terraspace/plugin/summary/interface.rb', line 11

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

#key_fieldObject

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



33
34
35
# File 'lib/terraspace/plugin/summary/interface.rb', line 33

def key_field
  'key'
end

#remove_statefile(path) ⇒ Object

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



87
88
89
90
# 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) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# 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_resourcesObject



56
57
58
59
60
61
62
63
# 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_exprObject

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



45
46
47
# File 'lib/terraspace/plugin/summary/interface.rb', line 45

def statefile_expr
  "#{@dest_folder}**/*"
end