cwa 0.2.1 → 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1519d445e5671c8a4bde54a85bfc3718352340250ef13c74b76412a10d2a87cc
4
- data.tar.gz: 837f764c34d6249820bbf08a59dd72bb9a96e8fed12ac52d811c9d79ce3299a2
3
+ metadata.gz: 68fe5fa6a7a7028fe3a8aa3c6781ba92b95c77972bcb9b32f0ef87b7d3ffea5f
4
+ data.tar.gz: d37958cce67757b099ab293a157060d91053c83f34a563fc9743254cbdbe4fe5
5
5
  SHA512:
6
- metadata.gz: 13320f6632e46850cfc74c189cdccf3d38ffb4b647c77ae5435df089adcd229b397496898747d74c1febf47ca304ccff0418021b72e529744691db0b678cd7d6
7
- data.tar.gz: 432f170c2c6f920315e7aaa84f961a4ca1709fd8e66d60fa8bf3cd52e07d12c28e89b26f56511a4b9c403c898faae2d91047bfa0aad0312d76b50a27b688f3cb
6
+ metadata.gz: b4380bddd08abd79d38d6a5dde6b99c5af8ad93d4ab0086783c258f50accebb0c4c6abb9f649d468b1e7578ecfd60aa56bdccfc13943ec8e43191a5ef999b525
7
+ data.tar.gz: 6dc99a2dedb8a4ffabdd1a086cb5204a91c7e56c4afd20f47a5593fa220d742c52dd01b987c4027fdd6d89f4f26df36744d8edf6cd2b5e0b261160723eb21ebf
@@ -0,0 +1,16 @@
1
+ ---
2
+ include:
3
+ - "**/*.rb"
4
+ exclude:
5
+ - spec/**/*
6
+ - test/**/*
7
+ - vendor/**/*
8
+ - ".bundle/**/*"
9
+ require: []
10
+ domains: []
11
+ reporters:
12
+ - rubocop
13
+ - require_not_found
14
+ require_paths: []
15
+ plugins: []
16
+ max_files: 5000
@@ -0,0 +1,5 @@
1
+ {
2
+ "solargraph": {
3
+ "cmd": ["bundle", "exec", "solargraph", "stdio"]
4
+ }
5
+ }
data/README.md CHANGED
@@ -19,12 +19,18 @@ Or install it yourself as:
19
19
 
20
20
  ## Usage
21
21
  ```
22
+ $ >> cwa help
22
23
  Commands:
23
24
  cwa alarms --name ALARMNAME --regexp ALARMNAME --namespae NAMESPACE --dimensions KEY:VALUE # show cloudwatch alms
25
+ cwa configure # create config files
24
26
  cwa disable --name ALARMNAME --regexp ALARMNAME --namespae NAMESPACE --dimensions KEY:VALUE # disable cloudwatch alms
25
27
  cwa enable --name ALARMNAME --regexp ALARMNAME --namespae NAMESPACE --dimensions KEY:VALUE # enable cloudwatch alms
26
28
  cwa help [COMMAND] # Describe available commands or one specific command
27
29
 
28
30
  Options:
29
31
  [--verbose], [--no-verbose]
32
+ [--profile=PROFILE]
33
+ [--region=REGION]
34
+ [--assume-role=ASSUME_ROLE]
35
+ [--output=OUTPUT]
30
36
  ```
@@ -40,4 +40,5 @@ Gem::Specification.new do |spec|
40
40
 
41
41
  spec.add_development_dependency "bundler"
42
42
  spec.add_development_dependency "rake"
43
+ spec.add_development_dependency "solargraph"
43
44
  end
data/lib/cwa.rb CHANGED
@@ -1,7 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'aws-sdk-core'
2
- require "cwa/client"
3
- require "cwa/version"
4
+ require 'cwa/client'
5
+ require 'cwa/version'
4
6
 
7
+ #--
8
+ # Copyright (c) 2021 Ito Toshifumi
9
+ # cloudwatch alarm cli
10
+ #++
5
11
  module CWA
6
12
  class Error < StandardError; end
7
13
 
@@ -12,8 +18,8 @@ module CWA
12
18
 
13
19
  def get(opts = {})
14
20
  @aws_opts ||= {}
15
- @aws_opts[:profile] = opts.delete(:profile) if opts[:profile]
16
- @aws_opts[:region] = opts.delete(:profile) if opts[:region]
21
+ @aws_opts[:profile] = opts[:profile] if opts[:profile]
22
+ @aws_opts[:region] = opts[:region] if opts[:region]
17
23
 
18
24
  Client.new(@aws_opts)
19
25
  end
@@ -1,39 +1,62 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module CWA
2
4
  class Alarms
3
5
  class Error < StandardError; end
4
6
 
7
+ DEFAULT_OPTS = {
8
+ alarm_name_prefix: '*',
9
+ max_records: 100
10
+ }.freeze
11
+
5
12
  def initialize(client, **opts)
6
13
  @opts = opts
7
14
  @client = client
8
15
  end
9
16
 
10
17
  def filter(query)
11
- alms = _alarms.metric_alarms
18
+ alms = alarms
12
19
 
13
20
  # querys
14
- name_query = ->(alm) { alm.alarm_name == query[:name] }
15
- regexp_query = ->(alm) { alm.alarm_name =~ /#{query[:regexp]}/ }
16
- namespace_query = ->(alm) { alm.namespace == query[:namespace] }
17
-
18
- alms = alms.select(&name_query) if query[:name ]
19
- alms = alms.select(&regexp_query) if query[:regexp ]
20
- alms = alms.select(&namespace_query) if query[:namespace ]
21
- alms = _dimension?(alms, query[:dimensions]) if query[:dimensions]
21
+ name_query = ->(alm) { alm.alarm_name == query[:name] }
22
+ regexp_query = ->(alm) { alm.alarm_name =~ /#{query[:regexp]}/ }
23
+ namespace_query = ->(alm) { alm.namespace == query[:namespace] }
24
+
25
+ alms = alms.select(&name_query) if query[:name ]
26
+ alms = alms.select(&regexp_query) if query[:regexp ]
27
+ alms = alms.select(&namespace_query) if query[:namespace ]
28
+ alms = dimension?(alms, query[:dimensions]) if query[:dimensions]
22
29
  alms
23
30
  end
24
31
 
25
32
  def refresh
26
33
  @alms = nil
27
- _alarms
34
+ alarms
28
35
  end
29
36
 
30
37
  private
31
- def _alarms(**opts)
32
- opts[:alarm_name_prefix] = "*" unless opts
33
- @alms ||= @client.describe_alarms(opts)
38
+ def alarms(**opts)
39
+ opts = DEFAULT_OPTS unless opts
40
+
41
+ unless @alms
42
+ @alms = Array.new
43
+ alms = @client.describe_alarms(opts)
44
+ next_token = alms.next_token
45
+ @alms << alms
46
+
47
+ while next_token
48
+ opts[:next_token] = next_token
49
+ alms = @client.describe_alarms(opts)
50
+ next_token = alms.next_token
51
+
52
+ @alms << alms
53
+ end
54
+ end
55
+
56
+ @alms.map {|e| e.metric_alarms }.flatten
34
57
  end
35
58
 
36
- def _dimension?(alms, dimensions)
59
+ def dimension?(alms, dimensions)
37
60
  alms.select do |alm|
38
61
  alm.dimensions.any? do |dims|
39
62
  dimensions.keys.any?(dims.name) && dimensions.values.any?(dims.value)
@@ -1,75 +1,90 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'cwa'
2
4
  require 'thor'
3
5
  require 'terminal-table'
4
6
  require 'colorize'
5
7
  require 'yaml'
8
+ require 'json'
6
9
  require 'fileutils'
7
10
 
8
- ASSUME_DIR = File.join(Dir.home, '.config', 'cwa')
9
- ASSUME_FILE = File.join(ASSUME_DIR, 'assume.yml')
11
+ ASSUME_DIR = File.join(Dir.home, '.config', 'cwa').freeze
12
+ ASSUME_FILE = File.join(ASSUME_DIR, 'assume.yml').freeze
13
+
14
+ OUTPUT_KEYS = %i[
15
+ namespace
16
+ alarm_name
17
+ actions_enabled
18
+ ].freeze
10
19
 
11
- OUTPUT_KEYS = %i(
20
+ OUTPUT_KEYS_DETAIL = %i[
12
21
  namespace
13
22
  alarm_name
14
23
  actions_enabled
15
24
  dimensions
16
25
  alarm_arn
17
26
  alarm_description
18
- )
27
+ ].freeze
19
28
 
20
29
 
21
- AWS_OPTIONS = %i(
30
+ AWS_OPTIONS = %i[
22
31
  profile
23
32
  region
24
- )
33
+ ].freeze
25
34
 
26
- OPTIONS = "--name ALARMNAME --regexp ALARMNAME --namespae NAMESPACE --dimensions KEY:VALUE"
35
+ OPTIONS = '--name ALARMNAME --regexp ALARMNAME --namespae NAMESPACE --dimensions KEY:VALUE'.freeze
27
36
 
28
37
  module CWA
38
+ # cli class
29
39
  class Cli < Thor
40
+ begin
30
41
  class_option :verbose, type: :boolean
31
42
  class_option :profile, type: :string
32
43
  class_option :region, type: :string
33
44
  class_option :assume_role, type: :string
45
+ class_option :output, type: :string
34
46
 
35
- desc "alarms #{OPTIONS}", "show cloudwatch alms"
36
- option :name, type: :string, aliases: "n"
37
- option :namespace, type: :string, aliases: "s"
38
- option :regexp, type: :string, aliases: "r"
39
- option :dimensions, type: :hash, aliases: "d"
47
+ desc "alarms #{OPTIONS}", 'show cloudwatch alms'
48
+ option :name, type: :string, aliases: 'n'
49
+ option :namespace, type: :string, aliases: 's'
50
+ option :regexp, type: :string, aliases: 'r'
51
+ option :dimensions, type: :hash, aliases: 'd'
40
52
  def alarms
41
- begin
42
- _enable_assume if options[:assume_role]
53
+ enable_assume if options[:assume_role]
54
+
55
+ alms = output_alms
56
+ raise 'not alarms' if alms.empty?
43
57
 
44
- alms = _output_alms
45
- raise "not alarms" if alms.empty?
46
58
 
59
+ case options[:output]
60
+ when 'json'
61
+ puts JSON.dump(alms)
62
+ when 'yaml'
63
+ puts YAML.dump(alms)
64
+ else
47
65
  head = alms.first.keys
48
66
  rows = alms.map{|alm| alm.values }
49
67
  table = Terminal::Table.new :headings => head, :rows => rows
50
-
51
68
  puts table
52
- rescue => err
53
- puts "error => #{err}".colorize(:red)
54
- exit 1
55
69
  end
56
70
  end
57
71
 
58
- desc "enable #{OPTIONS}", "enable cloudwatch alms"
59
- option :name, type: :string, aliases: "n"
60
- option :namespace, type: :string, aliases: "s"
61
- option :regexp, type: :string, aliases: "r"
62
- option :dimensions, type: :hash, aliases: "d"
72
+ desc "enable #{OPTIONS}", 'enable cloudwatch alms'
73
+ option :name, type: :string, aliases: 'n'
74
+ option :namespace, type: :string, aliases: 's'
75
+ option :regexp, type: :string, aliases: 'r'
76
+ option :dimensions, type: :hash, aliases: 'd'
63
77
  def enable
64
78
  begin
65
- _enable_assume if options[:assume_role]
79
+ enable_assume if options[:assume_role]
66
80
 
67
81
  cwa = CWA.get(options)
68
82
  alms = cwa.alarms(options)
69
- alms = _check_alm(alms, :enable)
83
+ alms = check_alm(alms, :enable)
70
84
 
71
- raise "not alarms" if alms.empty?
72
- _confirm("cloudwatch alarm enable?")
85
+ raise 'not alarms' if alms.empty?
86
+
87
+ confirm('cloudwatch alarm enable?')
73
88
 
74
89
  alms.each do |alm|
75
90
  cwa.enable(alm)
@@ -77,27 +92,28 @@ module CWA
77
92
  end
78
93
  puts
79
94
  alarms
80
- rescue => err
81
- puts "error => #{err}".colorize(:red)
95
+ rescue StandardError => e
96
+ puts "error => #{e}".colorize(:red)
82
97
  exit 1
83
98
  end
84
99
  end
85
100
 
86
- desc "disable #{OPTIONS}", "disable cloudwatch alms"
87
- option :name, type: :string, aliases: "n"
88
- option :namespace, type: :string, aliases: "s"
89
- option :regexp, type: :string, aliases: "r"
90
- option :dimensions, type: :hash, aliases: "d"
101
+ desc "disable #{OPTIONS}", 'disable cloudwatch alms'
102
+ option :name, type: :string, aliases: 'n'
103
+ option :namespace, type: :string, aliases: 's'
104
+ option :regexp, type: :string, aliases: 'r'
105
+ option :dimensions, type: :hash, aliases: 'd'
91
106
  def disable
92
107
  begin
93
108
  _enable_assume if options[:assume_role]
94
109
 
95
110
  cwa = CWA.get(options)
96
111
  alms = cwa.alarms(options)
97
- alms = _check_alm(alms, :disable)
112
+ alms = check_alm(alms, :disable)
113
+
114
+ raise 'not alarms' if alms.empty?
98
115
 
99
- raise "not alarms" if alms.empty?
100
- _confirm("cloudwatch alarm disable?")
116
+ confirm('cloudwatch alarm disable?')
101
117
 
102
118
  alms.each do |alm|
103
119
  cwa.disable(alm)
@@ -111,9 +127,9 @@ module CWA
111
127
  end
112
128
  end
113
129
 
114
- desc "configure", "create config files"
130
+ desc 'configure', 'create config files'
115
131
  def configure
116
- configs = %w(assume_role)
132
+ configs = %w[assume_role]
117
133
 
118
134
  puts configs
119
135
  print "create type? : "
@@ -134,54 +150,61 @@ module CWA
134
150
  puts "create => #{ASSUME_FILE.colorize(:yellow)}"
135
151
  end
136
152
  end
153
+ rescue StandardError => e
154
+ puts "error => #{e}".colorize(:red)
155
+ exit 1
156
+ end
137
157
 
138
158
  private
139
- def _output_alms
159
+ def output_alms
140
160
  cwa = CWA.get(options)
141
161
  alms = cwa.alarms(options)
162
+ keys = OUTPUT_KEYS
163
+ keys = OUTPUT_KEYS_DETAIL if options[:verbose]
142
164
 
143
165
  alms.map do |alm|
144
166
  v = Hash.new
145
- OUTPUT_KEYS.each do |key|
167
+ keys.each do |key|
146
168
  v[key] = alm.method(key).call
147
169
  end
148
170
  v
149
171
  end
150
172
  end
151
173
 
152
- def _enable_assume
174
+ def enable_assume
153
175
  raise 'not config file, pls "cwa configure"' unless File.exist?(ASSUME_FILE)
176
+
154
177
  assume = YAML.load_file(ASSUME_FILE)[options[:assume_role]]
155
178
  CWA.assume_role(assume)
156
179
  end
157
180
 
158
- def _check_alm(alms, mode)
181
+ def check_alm(alms, mode)
159
182
  check = false if mode == :enable
160
183
  check = true if mode == :disable
161
184
  alms.map do |alm|
162
- puts "-" * 50
163
- puts "namespace : #{alm[:namespace ]}"
164
- puts "alarm_name : #{alm[:alarm_name ]}"
165
- puts "dimensions : #{alm[:dimensions ]}"
185
+ puts '-' * 50
186
+ puts "namespace : #{alm[:namespace]}"
187
+ puts "alarm_name : #{alm[:alarm_name]}"
188
+ puts "dimensions : #{alm[:dimensions]}"
166
189
  puts "actions_enabled : #{alm[:actions_enabled]}"
167
190
  unless alm[:actions_enabled] == check
168
191
  puts "=> #{'skip'.colorize(:yellow)}"
169
- puts "-" * 50
192
+ puts '-' * 50
170
193
  next
171
194
  end
172
- puts "-" * 50
195
+ puts '-' * 50
173
196
  alm
174
197
  end.compact
175
198
  end
176
199
 
177
- def _confirm(check_word, **opt)
178
- true_word = ( opt[:true] || /yes|y/ )
179
- false_word = ( opt[:false] || /no/ )
200
+ def confirm(check_word, **_opt)
201
+ true_word = /yes|y/
202
+ false_word = /no/
180
203
 
181
204
  while true
182
- print "#{check_word} (#{true_word.inspect.delete("/")}/#{false_word.inspect.delete("/")}) : "
183
- case anser = $stdin.gets.strip
184
- when true_word then return true
205
+ print "#{check_word} (#{true_word.inspect.delete('/')}/#{false_word.inspect.delete('/')}) : "
206
+ case $stdin.gets.strip
207
+ when true_word then return true
185
208
  when false_word then return false
186
209
  end
187
210
  end
@@ -1,7 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'aws-sdk-cloudwatch'
2
- require "cwa/alarms"
4
+ require 'cwa/alarms'
3
5
 
4
6
  module CWA
7
+ # AWS client class
5
8
  class Client
6
9
  class Error < StandardError; end
7
10
 
@@ -12,9 +15,7 @@ module CWA
12
15
  def alarms(query)
13
16
  @alarms ||= Alarms.new(@client)
14
17
  alms = @alarms.filter(query)
15
- if block_given?
16
- alms.each{|alm| yield alm }
17
- end
18
+ alms.each { |alm| yield alm } if block_given?
18
19
  alms
19
20
  end
20
21
 
@@ -24,12 +25,12 @@ module CWA
24
25
 
25
26
  def enable(alm)
26
27
  alm = alm[:alarm_name]
27
- @client.enable_alarm_actions({alarm_names: [alm] })
28
+ @client.enable_alarm_actions({ alarm_names: [alm] })
28
29
  end
29
30
 
30
31
  def disable(alm)
31
32
  alm = alm[:alarm_name]
32
- @client.disable_alarm_actions({alarm_names: [alm] })
33
+ @client.disable_alarm_actions({ alarm_names: [alm] })
33
34
  end
34
35
  end
35
36
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module CWA
2
- VERSION = "0.2.1"
4
+ VERSION = "0.2.6"
3
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cwa
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - gen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-10-05 00:00:00.000000000 Z
11
+ date: 2020-10-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk-core
@@ -108,6 +108,20 @@ dependencies:
108
108
  - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: solargraph
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
111
125
  description: cloudwatch alarm client
112
126
  email:
113
127
  - ymdtshm@gmail.com
@@ -118,6 +132,8 @@ extra_rdoc_files: []
118
132
  files:
119
133
  - ".gitignore"
120
134
  - ".ruby-version"
135
+ - ".solargraph.yml"
136
+ - ".vim-lsp-settings/settings.json"
121
137
  - Gemfile
122
138
  - README.md
123
139
  - Rakefile