cwa 0.2.3 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c78a561097938293f123e52178ed3c7268e62353ba3653c932a54ac874632693
4
- data.tar.gz: 45ef4753e2e7d92e9a021a15c2f0e712f8e9fe9d512a69cfd248403cce2f83b5
3
+ metadata.gz: ed03242af15ac021b94d8c3ba2d0eeb32d97769f6eba09ab8e71dc112efdb824
4
+ data.tar.gz: 9604ba10ae8fbee2d90b184e219dcc8b8878cf4ce8496f2fc1df632a2ca4430d
5
5
  SHA512:
6
- metadata.gz: 789f2ccffae2409eedbc6cc1da3feb3b92d5f56c175ae208f16a85423957ed51dc572832fdab08e3245c14dc9b14579d50e9378188e2fcc95c9ce1b2f45d2e94
7
- data.tar.gz: 50de2f1713343c6c967286e2c927d8dcf2d7e497e041d0a16266e49178caa2f2aac08a863cf1ff90297b982e53c30778ff3ca513b685d16efb52860e1e68ff3a
6
+ metadata.gz: '08d9a3e96588461c2c757d8fc038cfa246e9073fd63c8a595a2010a82bd25cbbad60c6aa1b733131bbcc91951bad081a3f3c4f07afe09b28dba1feebd513d1a1'
7
+ data.tar.gz: 72ea2ff7740f58c409fb961230b0d0493079f8a6ae13cb80dbf6f06e4a9fbc5349d71376f4ff28bf299a316ce3340aeb2f8fad3b5723b142c7117bd6fe117a22
@@ -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
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,30 +1,19 @@
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
 
8
14
  class << self
9
- def configure(**opts)
10
- @aws_opts = opts
11
- end
12
-
13
15
  def get(opts = {})
14
- @aws_opts ||= {}
15
- @aws_opts[:profile] = opts[:profile] if opts[:profile]
16
- @aws_opts[:region] = opts[:region ] if opts[:region]
17
-
18
- Client.new(@aws_opts)
19
- end
20
-
21
- def assume_role(opts)
22
- role_credentials = Aws::AssumeRoleCredentials.new(
23
- client: Aws::STS::Client.new(opts),
24
- role_arn: opts[:arn],
25
- role_session_name: opts[:session_name]
26
- )
27
- @aws_opts[:credentials] = role_credentials
16
+ Client.new(opts)
28
17
  end
29
18
  end
30
19
  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
- @opts = opts
7
- @client = client
13
+ @client = client
8
14
  end
9
15
 
10
16
  def filter(query)
11
- alms = _alarms.metric_alarms
17
+ @alms = alarms
12
18
 
13
19
  # 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]
22
- alms
20
+ name_query = ->(alm) { alm.alarm_name == query[:name] }
21
+ regexp_query = ->(alm) { alm.alarm_name =~ /#{query[:regexp]}/ }
22
+ namespace_query = ->(alm) { alm.namespace == query[:namespace] }
23
+
24
+ @alms = @alms.select(&name_query) if query[:name ]
25
+ @alms = @alms.select(&regexp_query) if query[:regexp ]
26
+ @alms = @alms.select(&namespace_query) if query[:namespace ]
27
+ @alms = dimension?(@alms, query[:dimensions]) if query[:dimensions]
28
+
29
+ @alms
23
30
  end
24
31
 
25
- def refresh
32
+ def refresh(query = nil)
26
33
  @alms = nil
27
- _alarms
34
+ query ? filter(query) : 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,97 @@
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(
12
- namespace
13
- alarm_name
14
- actions_enabled
15
- dimensions
16
- alarm_arn
17
- alarm_description
18
- )
20
+ OUTPUT_KEYS_DETAIL = %i[
21
+ namespace
22
+ alarm_name
23
+ actions_enabled
24
+ dimensions
25
+ alarm_arn
26
+ alarm_description
27
+ ].freeze
19
28
 
20
29
 
21
- AWS_OPTIONS = %i(
22
- profile
23
- region
24
- )
30
+ AWS_OPTIONS = %i[
31
+ profile
32
+ region
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
30
40
  class_option :verbose, type: :boolean
31
41
  class_option :profile, type: :string
32
42
  class_option :region, type: :string
33
43
  class_option :assume_role, type: :string
44
+ class_option :output, type: :string
34
45
 
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"
46
+ desc "alarms #{OPTIONS}", 'show cloudwatch alms'
47
+ option :name, type: :string, aliases: 'n'
48
+ option :namespace, type: :string, aliases: 's'
49
+ option :regexp, type: :string, aliases: 'r'
50
+ option :dimensions, type: :hash, aliases: 'd'
40
51
  def alarms
41
52
  begin
42
- _enable_assume if options[:assume_role]
43
-
44
- alms = _output_alms
45
- raise "not alarms" if alms.empty?
53
+ cwa = CWA.get(aws_opts)
54
+ alms = cwa.alarms(options)
55
+ keys = OUTPUT_KEYS
56
+ keys = OUTPUT_KEYS_DETAIL if options[:verbose]
46
57
 
47
- head = alms.first.keys
48
- rows = alms.map{|alm| alm.values }
49
- table = Terminal::Table.new :headings => head, :rows => rows
58
+ alms = alms.map do |alm|
59
+ keys.reduce({}) { |h, key| h.merge!(key => alm.method(key).call) }
60
+ end
50
61
 
51
- puts table
52
- rescue => err
53
- puts "error => #{err}".colorize(:red)
62
+ raise 'not alarms' if alms.empty?
63
+
64
+ case options[:output]
65
+ when 'json'
66
+ puts JSON.dump(alms)
67
+ when 'yaml'
68
+ puts YAML.dump(alms)
69
+ else
70
+ head = alms.first.keys
71
+ rows = alms.map{|alm| alm.values }
72
+ table = Terminal::Table.new :headings => head, :rows => rows
73
+ puts table
74
+ end
75
+ rescue StandardError => e
76
+ puts "error => #{e}".colorize(:red)
54
77
  exit 1
55
78
  end
56
79
  end
57
80
 
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"
81
+ desc "enable #{OPTIONS}", 'enable cloudwatch alms'
82
+ option :name, type: :string, aliases: 'n'
83
+ option :namespace, type: :string, aliases: 's'
84
+ option :regexp, type: :string, aliases: 'r'
85
+ option :dimensions, type: :hash, aliases: 'd'
63
86
  def enable
64
87
  begin
65
- _enable_assume if options[:assume_role]
66
-
67
- cwa = CWA.get(options)
88
+ cwa = CWA.get(aws_opts)
68
89
  alms = cwa.alarms(options)
69
- alms = _check_alm(alms, :enable)
90
+ alms = check_alm(alms, :enable)
70
91
 
71
- raise "not alarms" if alms.empty?
72
- _confirm("cloudwatch alarm enable?")
92
+ raise 'not alarms' if alms.empty?
93
+
94
+ confirm('cloudwatch alarm enable?')
73
95
 
74
96
  alms.each do |alm|
75
97
  cwa.enable(alm)
@@ -77,27 +99,26 @@ module CWA
77
99
  end
78
100
  puts
79
101
  alarms
80
- rescue => err
81
- puts "error => #{err}".colorize(:red)
102
+ rescue StandardError => e
103
+ puts "error => #{e}".colorize(:red)
82
104
  exit 1
83
105
  end
84
106
  end
85
107
 
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"
108
+ desc "disable #{OPTIONS}", 'disable cloudwatch alms'
109
+ option :name, type: :string, aliases: 'n'
110
+ option :namespace, type: :string, aliases: 's'
111
+ option :regexp, type: :string, aliases: 'r'
112
+ option :dimensions, type: :hash, aliases: 'd'
91
113
  def disable
92
114
  begin
93
- _enable_assume if options[:assume_role]
94
-
95
- cwa = CWA.get(options)
115
+ cwa = CWA.get(aws_opts)
96
116
  alms = cwa.alarms(options)
97
- alms = _check_alm(alms, :disable)
117
+ alms = check_alm(alms, :disable)
118
+
119
+ raise 'not alarms' if alms.empty?
98
120
 
99
- raise "not alarms" if alms.empty?
100
- _confirm("cloudwatch alarm disable?")
121
+ confirm('cloudwatch alarm disable?')
101
122
 
102
123
  alms.each do |alm|
103
124
  cwa.disable(alm)
@@ -111,77 +132,82 @@ module CWA
111
132
  end
112
133
  end
113
134
 
114
- desc "configure", "create config files"
135
+ desc 'configure', 'create config files'
115
136
  def configure
116
- configs = %w(assume_role)
117
-
118
- puts configs
119
- print "create type? : "
120
- type = $stdin.gets.strip
121
- case type
122
- when 'assume_role'
123
- print "name? : "
124
- name = $stdin.gets.strip
125
- print "arn? : "
126
- arn = $stdin.gets.strip
127
- print "session_name? : "
128
- session = $stdin.gets.strip
129
-
130
- assume = {name => { arn: arn, session_name: session}}
131
- FileUtils.mkdir_p(ASSUME_DIR) unless Dir.exist?(ASSUME_DIR)
132
- file = open(ASSUME_FILE, "w")
133
- YAML.dump(assume, file)
134
- puts "create => #{ASSUME_FILE.colorize(:yellow)}"
137
+ begin
138
+ configs = %w[assume_role]
139
+
140
+ puts configs
141
+ print "create type? : "
142
+ type = $stdin.gets.strip
143
+ case type
144
+ when 'assume_role'
145
+ print "name? : "
146
+ name = $stdin.gets.strip
147
+ print "arn? : "
148
+ arn = $stdin.gets.strip
149
+ print "session_name? : "
150
+ session = $stdin.gets.strip
151
+
152
+ assume = {name => { arn: arn, session_name: session}}
153
+
154
+ FileUtils.mkdir_p(ASSUME_DIR) unless Dir.exist?(ASSUME_DIR)
155
+ assume.merge!(YAML.load_file(ASSUME_FILE)) if File.exist?(ASSUME_FILE)
156
+ file = open(ASSUME_FILE, "w")
157
+ YAML.dump(assume, file)
158
+
159
+ puts "create => #{ASSUME_FILE.colorize(:yellow)}"
160
+ end
161
+ rescue StandardError => e
162
+ puts "error => #{e}".colorize(:red)
163
+ exit 1
135
164
  end
136
165
  end
137
166
 
138
167
  private
139
- def _output_alms
140
- cwa = CWA.get(options)
141
- alms = cwa.alarms(options)
168
+ def aws_opts
169
+ opts = {}
142
170
 
143
- alms.map do |alm|
144
- v = Hash.new
145
- OUTPUT_KEYS.each do |key|
146
- v[key] = alm.method(key).call
147
- end
148
- v
149
- end
171
+ opts[:region] = options[:region]
172
+ opts[:profile] = options[:profile]
173
+ opts[:assume_role] = assume
174
+ opts.compact
150
175
  end
151
176
 
152
- def _enable_assume
177
+ def assume
178
+ return nil unless options[:assume_role]
153
179
  raise 'not config file, pls "cwa configure"' unless File.exist?(ASSUME_FILE)
154
- assume = YAML.load_file(ASSUME_FILE)[options[:assume_role]]
155
- CWA.assume_role(assume)
180
+
181
+ YAML.load_file(ASSUME_FILE)[options[:assume_role]]
156
182
  end
157
183
 
158
- def _check_alm(alms, mode)
184
+ def check_alm(alms, mode)
159
185
  check = false if mode == :enable
160
186
  check = true if mode == :disable
161
187
  alms.map do |alm|
162
- puts "-" * 50
163
- puts "namespace : #{alm[:namespace ]}"
164
- puts "alarm_name : #{alm[:alarm_name ]}"
165
- puts "dimensions : #{alm[:dimensions ]}"
188
+ puts '-' * 50
189
+ puts "namespace : #{alm[:namespace]}"
190
+ puts "alarm_name : #{alm[:alarm_name]}"
191
+ puts "dimensions : #{alm[:dimensions]}"
166
192
  puts "actions_enabled : #{alm[:actions_enabled]}"
167
193
  unless alm[:actions_enabled] == check
168
194
  puts "=> #{'skip'.colorize(:yellow)}"
169
- puts "-" * 50
195
+ puts '-' * 50
170
196
  next
171
197
  end
172
- puts "-" * 50
198
+ puts '-' * 50
173
199
  alm
174
200
  end.compact
175
201
  end
176
202
 
177
- def _confirm(check_word, **opt)
178
- true_word = ( opt[:true] || /yes|y/ )
179
- false_word = ( opt[:false] || /no/ )
203
+ def confirm(check_word, **_opt)
204
+ true_word = /yes|y/
205
+ false_word = /no/
180
206
 
181
207
  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
208
+ print "#{check_word} (#{true_word.inspect.delete('/')}/#{false_word.inspect.delete('/')}) : "
209
+ case $stdin.gets.strip
210
+ when true_word then return true
185
211
  when false_word then return false
186
212
  end
187
213
  end
@@ -1,35 +1,56 @@
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
 
8
11
  def initialize(opts)
12
+ if opts[:assume_role]
13
+ role = opts.delete(:assume_role)
14
+ opts[:credentials] = assume_role(role)
15
+ end
16
+
9
17
  @client = Aws::CloudWatch::Client.new(opts)
10
18
  end
11
19
 
12
20
  def alarms(query)
13
21
  @alarms ||= Alarms.new(@client)
14
22
  alms = @alarms.filter(query)
15
- if block_given?
16
- alms.each{|alm| yield alm }
17
- end
23
+ alms.each { |alm| yield alm } if block_given?
24
+
25
+ @query_cache = query
18
26
  alms
19
27
  end
20
28
 
21
- def refresh
22
- @alarms.refresh
29
+ def update(cache: true)
30
+ if cache
31
+ @alarms.refresh(@query_cache)
32
+ else
33
+ @alarms.refresh
34
+ end
23
35
  end
24
36
 
25
37
  def enable(alm)
26
38
  alm = alm[:alarm_name]
27
- @client.enable_alarm_actions({alarm_names: [alm] })
39
+ @client.enable_alarm_actions({ alarm_names: [alm] })
28
40
  end
29
41
 
30
42
  def disable(alm)
31
43
  alm = alm[:alarm_name]
32
- @client.disable_alarm_actions({alarm_names: [alm] })
44
+ @client.disable_alarm_actions({ alarm_names: [alm] })
45
+ end
46
+
47
+ private
48
+ def assume_role(opts)
49
+ Aws::AssumeRoleCredentials.new(
50
+ client: Aws::STS::Client.new,
51
+ role_arn: opts[:arn],
52
+ role_session_name: opts[:session_name]
53
+ )
33
54
  end
34
55
  end
35
56
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module CWA
2
- VERSION = "0.2.3"
4
+ VERSION = "0.3.2"
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.3
4
+ version: 0.3.2
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-27 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
@@ -117,7 +131,7 @@ extensions: []
117
131
  extra_rdoc_files: []
118
132
  files:
119
133
  - ".gitignore"
120
- - ".ruby-version"
134
+ - ".solargraph.yml"
121
135
  - Gemfile
122
136
  - README.md
123
137
  - Rakefile
@@ -1 +0,0 @@
1
- 2.6.5