Class: CWA::Cli

Inherits:
Thor
  • Object
show all
Defined in:
lib/cwa/cli.rb

Overview

cli class

Instance Method Summary collapse

Instance Method Details

#alarmsObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/cwa/cli.rb', line 51

def alarms
  begin
    cwa  = CWA.get(aws_opts)
    alms = cwa.alarms(options)
    keys = OUTPUT_KEYS
    keys = OUTPUT_KEYS_DETAIL if options[:verbose]

    alms = alms.map do |alm|
      keys.reduce({}) { |h, key| h.merge!(key => alm.method(key).call) }
    end

    raise 'not alarms' if alms.empty?

    case options[:output]
    when 'json'
      puts JSON.dump(alms)
    when 'yaml'
      puts YAML.dump(alms)
    else
      head  = alms.first.keys
      rows  = alms.map{|alm| alm.values }
      table = Terminal::Table.new :headings => head, :rows => rows
      puts table
    end
  rescue StandardError => e
    puts "error => #{e}".colorize(:red)
    exit 1
  end
end

#configureObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/cwa/cli.rb', line 136

def configure
  begin
    configs     = %w[assume_role]

    puts configs
    print "create type? : "
    type = $stdin.gets.strip
    case type
    when 'assume_role'
      print "name?         : "
      name    = $stdin.gets.strip
      print "arn?          : "
      arn     = $stdin.gets.strip
      print "session_name? : "
      session = $stdin.gets.strip

      assume = {name => { arn: arn, session_name: session}}

      FileUtils.mkdir_p(ASSUME_DIR)              unless Dir.exist?(ASSUME_DIR)
      assume.merge!(YAML.load_file(ASSUME_FILE)) if     File.exist?(ASSUME_FILE)
      file = open(ASSUME_FILE, "w")
      YAML.dump(assume, file)

      puts "create => #{ASSUME_FILE.colorize(:yellow)}"
    end
  rescue StandardError => e
    puts "error => #{e}".colorize(:red)
    exit 1
  end
end

#disableObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/cwa/cli.rb', line 113

def disable
  begin
    cwa  = CWA.get(aws_opts)
    alms = cwa.alarms(options)
    alms = check_alm(alms, :disable)

    raise 'not alarms' if alms.empty?

    confirm('cloudwatch alarm disable?')

    alms.each do |alm|
      cwa.disable(alm)
      puts "#{'done'.colorize(:green)} => #{alm[:alarm_name]}"
    end
    puts
    alarms
  rescue => err
    puts "error => #{err}".colorize(:red)
    exit 1
  end
end

#enableObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/cwa/cli.rb', line 86

def enable
  begin
    cwa  = CWA.get(aws_opts)
    alms = cwa.alarms(options)
    alms = check_alm(alms, :enable)

    raise 'not alarms' if alms.empty?

    confirm('cloudwatch alarm enable?')

    alms.each do |alm|
      cwa.enable(alm)
      puts "#{'done'.colorize(:green)} => #{alm[:alarm_name]}"
    end
    puts
    alarms
  rescue StandardError => e
    puts "error => #{e}".colorize(:red)
    exit 1
  end
end