cwa 0.2.2 → 0.3.1

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: c56ab01901ac99a76dbb7e5d30d9326af51975c589ca97187989f28226acd5d2
4
- data.tar.gz: 54a8f447849101f6c709294d5b303202e4611aba7912112404ce72fa32d6186a
3
+ metadata.gz: 18c398f2c91a08ef355f725b361937608fa500b37ab27b6a610b9668fc3ae92a
4
+ data.tar.gz: 90aa7ff8fa4231e29effd83e4093803e862afd4e23c535f31232a59448e5a415
5
5
  SHA512:
6
- metadata.gz: 4cc4befa70fc27e7f48cd2b9c829e7f94ddfaa45248e5b01f4868baa564bb3c005c7e0e81235b8ae992ba31256bb9f81520e3d5d69f698e3e32ded969d5ea620
7
- data.tar.gz: 2da1cf9b13550ee6e61cb1c507af9b34f2fd34f609d776485eac60eb09107e9099642a56078e4d863cea5c80b3cfa15eeef0749765cd714632a66313f2a13990
6
+ metadata.gz: b0400884cd212804b16584a58f982ce397b787ea469444f6e936da9a180b89b96b605e9049fe94d5e5c1dba3fcfcdc23de043ab8df9516f089415f054ea46543
7
+ data.tar.gz: 6447715b1e308ebd2801748447f7984c49046c526f4cd42244edc43cefb4d75cf2e6c6f1459d68bfe0645d7b9e81741b4016397e2505e0ce45fed45ce6ab9d02
@@ -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,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.2"
4
+ VERSION = "0.3.1"
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.2
4
+ version: 0.3.1
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-19 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