module GeoCLI::StatusCommand

Status Command for Geo

Public Instance Methods

calculate_status(type_stats) click to toggle source
# File lib/geoengineer/cli/status_command.rb, line 35
def calculate_status(type_stats)
  totals = {
    codified: 0,
    uncodified: 0,
    total: 0
  }
  type_stats.each do |type, stats|
    totals[:codified] += stats[:stats][:codified]
    totals[:uncodified] += stats[:stats][:uncodified]
    totals[:total] += stats[:stats][:total]
  end
  totals[:percent] = (100.0 * totals[:codified]) / totals[:total]
  totals
end
calculate_type_status(codified, uncodified) click to toggle source
# File lib/geoengineer/cli/status_command.rb, line 3
def calculate_type_status(codified, uncodified)
  total = codified.count + uncodified.count
  {
    codified: codified.count,
    uncodified: uncodified.count,
    total: total,
    percent: (100.0 * codified.count) / total
  }
end
default_status_types() click to toggle source
# File lib/geoengineer/cli/status_command.rb, line 24
def default_status_types
  [
    "aws_security_group",
    "aws_elb",
    "aws_db_instance",
    "aws_elasticache_cluster",
    "aws_s3_bucket",
    "aws_sqs_queue"
  ]
end
only_codified(status) click to toggle source
# File lib/geoengineer/cli/status_command.rb, line 74
def only_codified(status)
  status[:resources]
    .select { |t, r| r[:uncodified].any? }
    .each { |t, r| r.delete(:codified) }
end
report_json(type_stats, status) click to toggle source
# File lib/geoengineer/cli/status_command.rb, line 50
def report_json(type_stats, status)
  status[:resources] = {}
  type_stats.each do |type, resources|
    status[:resources][type] = {}
    status[:resources][type][:uncodified] = resource_id_array(resources[:uncodified])
    status[:resources][type][:codified] = resource_id_array(resources[:codified])
  end
  status
end
resource_id_array(resources) click to toggle source
# File lib/geoengineer/cli/status_command.rb, line 13
def resource_id_array(resources)
  resources
    .select { |r| !r.attributes.empty? }
    .map { |r| r._geo_id || r._terraform_id }
end
status_action() click to toggle source
# File lib/geoengineer/cli/status_command.rb, line 80
def status_action
  lambda do |args, options|
    type_stats = type_stats(options)
    status = calculate_status(type_stats)
    status = report_json(type_stats, status)
    status[:resources] = only_codified(status) unless @verbose
    puts JSON.pretty_generate(status)
  end
end
status_cmd() click to toggle source
# File lib/geoengineer/cli/status_command.rb, line 90
def status_cmd
  command :status do |c|
    c.syntax = 'geo status [<geo_files>]'
    c.description = 'Displays the the new, managed and unmanaged resources'
    c.option '--resources COMMA SEPERATED STRING', String, 'select resources for statuses'
    action = status_action
    c.action init_action(:status, &action)
  end
end
status_types(options) click to toggle source
# File lib/geoengineer/cli/status_command.rb, line 19
def status_types(options)
  return options.resources.split(',') if options.resources
  environment.status_types ? environment.status_types : default_status_types
end
type_stats(options) click to toggle source
# File lib/geoengineer/cli/status_command.rb, line 60
def type_stats(options)
  type_stats = {}
  status_types(options).each do |type|
    type_stats[type] = {}
    type_stats[type][:codified] = @environment.codified_resources(type)
    type_stats[type][:uncodified] = @environment.uncodified_resources(type)
    type_stats[type][:stats] = calculate_type_status(
      type_stats[type][:codified],
      type_stats[type][:uncodified]
    )
  end
  type_stats
end