Module: GeoCLI::StatusCommand

Included in:
GeoCLI
Defined in:
lib/geoengineer/cli/status_command.rb

Overview

Status Command for Geo

Instance Method Summary collapse

Instance Method Details

#calculate_status(type_stats) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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) ⇒ Object



3
4
5
6
7
8
9
10
11
# 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_typesObject



24
25
26
27
28
29
30
31
32
33
# 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) ⇒ Object



74
75
76
77
78
# 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) ⇒ Object



50
51
52
53
54
55
56
57
58
# 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) ⇒ Object



13
14
15
16
17
# 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_actionObject



80
81
82
83
84
85
86
87
88
# 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_cmdObject



90
91
92
93
94
95
96
97
98
# 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) ⇒ Object



19
20
21
22
# 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) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
# 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