cwa 0.2.3 → 0.2.4

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: bdd0039b73d4d3126c9a5c87c4a2d6f833831feb92faf4d14906d79bcc893dd0
4
+ data.tar.gz: fc54890732b65298266aa34537cf265a49a5e68560265d35a9391ce70ea23acf
5
5
  SHA512:
6
- metadata.gz: 789f2ccffae2409eedbc6cc1da3feb3b92d5f56c175ae208f16a85423957ed51dc572832fdab08e3245c14dc9b14579d50e9378188e2fcc95c9ce1b2f45d2e94
7
- data.tar.gz: 50de2f1713343c6c967286e2c927d8dcf2d7e497e041d0a16266e49178caa2f2aac08a863cf1ff90297b982e53c30778ff3ca513b685d16efb52860e1e68ff3a
6
+ metadata.gz: 218570c60a834aaa5fbde62a255dd566bc4efddb0e84137ba19059820aa9163b6019f6efc841c88b0a940a9facae88e149f8ea404f17bb458c8f4cccaf3ea486
7
+ data.tar.gz: 9bdf816d06aa4533af24a55336a7bbc0e7104fca4a582cd92aab6bce7698c8328ad984d9d1aaa70e564d97c3ebd57deb9343eba5c0eb5e58c41d59245eca4ce4
@@ -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
+ }
@@ -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
 
@@ -13,7 +19,7 @@ module CWA
13
19
  def get(opts = {})
14
20
  @aws_opts ||= {}
15
21
  @aws_opts[:profile] = opts[:profile] if opts[:profile]
16
- @aws_opts[:region] = opts[:region ] if opts[:region]
22
+ @aws_opts[:region] = opts[:region] if opts[:region]
17
23
 
18
24
  Client.new(@aws_opts)
19
25
  end
@@ -1,39 +1,60 @@
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
+ alms = @client.describe_alarms(opts)
49
+ next_token = alms.next_token
50
+ @alms << alms
51
+ end
52
+ end
53
+
54
+ @alms.map {|e| e.metric_alarms }.flatten
34
55
  end
35
56
 
36
- def _dimension?(alms, dimensions)
57
+ def dimension?(alms, dimensions)
37
58
  alms.select do |alm|
38
59
  alm.dimensions.any? do |dims|
39
60
  dimensions.keys.any?(dims.name) && dimensions.values.any?(dims.value)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'cwa'
2
4
  require 'thor'
3
5
  require 'terminal-table'
@@ -5,71 +7,69 @@ require 'colorize'
5
7
  require 'yaml'
6
8
  require 'fileutils'
7
9
 
8
- ASSUME_DIR = File.join(Dir.home, '.config', 'cwa')
9
- ASSUME_FILE = File.join(ASSUME_DIR, 'assume.yml')
10
+ ASSUME_DIR = File.join(Dir.home, '.config', 'cwa').freeze
11
+ ASSUME_FILE = File.join(ASSUME_DIR, 'assume.yml').freeze
10
12
 
11
- OUTPUT_KEYS = %i(
13
+ OUTPUT_KEYS = %i[
12
14
  namespace
13
15
  alarm_name
14
16
  actions_enabled
15
17
  dimensions
16
18
  alarm_arn
17
19
  alarm_description
18
- )
20
+ ].freeze
19
21
 
20
22
 
21
- AWS_OPTIONS = %i(
23
+ AWS_OPTIONS = %i[
22
24
  profile
23
25
  region
24
- )
26
+ ].freeze
25
27
 
26
- OPTIONS = "--name ALARMNAME --regexp ALARMNAME --namespae NAMESPACE --dimensions KEY:VALUE"
28
+ OPTIONS = '--name ALARMNAME --regexp ALARMNAME --namespae NAMESPACE --dimensions KEY:VALUE'.freeze
27
29
 
28
30
  module CWA
31
+ # cli class
29
32
  class Cli < Thor
33
+ begin
30
34
  class_option :verbose, type: :boolean
31
35
  class_option :profile, type: :string
32
36
  class_option :region, type: :string
33
37
  class_option :assume_role, type: :string
34
38
 
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"
39
+ desc "alarms #{OPTIONS}", 'show cloudwatch alms'
40
+ option :name, type: :string, aliases: 'n'
41
+ option :namespace, type: :string, aliases: 's'
42
+ option :regexp, type: :string, aliases: 'r'
43
+ option :dimensions, type: :hash, aliases: 'd'
40
44
  def alarms
41
- begin
42
- _enable_assume if options[:assume_role]
45
+ enable_assume if options[:assume_role]
43
46
 
44
- alms = _output_alms
45
- raise "not alarms" if alms.empty?
47
+ alms = output_alms
48
+ raise 'not alarms' if alms.empty?
46
49
 
47
- head = alms.first.keys
48
- rows = alms.map{|alm| alm.values }
49
- table = Terminal::Table.new :headings => head, :rows => rows
50
+ head = alms.first.keys
51
+ rows = alms.map{|alm| alm.values }
52
+ table = Terminal::Table.new :headings => head, :rows => rows
50
53
 
51
- puts table
52
- rescue => err
53
- puts "error => #{err}".colorize(:red)
54
- exit 1
55
- end
54
+ puts table
56
55
  end
57
56
 
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"
57
+ desc "enable #{OPTIONS}", 'enable cloudwatch alms'
58
+ option :name, type: :string, aliases: 'n'
59
+ option :namespace, type: :string, aliases: 's'
60
+ option :regexp, type: :string, aliases: 'r'
61
+ option :dimensions, type: :hash, aliases: 'd'
63
62
  def enable
64
63
  begin
65
- _enable_assume if options[:assume_role]
64
+ enable_assume if options[:assume_role]
66
65
 
67
66
  cwa = CWA.get(options)
68
67
  alms = cwa.alarms(options)
69
- alms = _check_alm(alms, :enable)
68
+ alms = check_alm(alms, :enable)
70
69
 
71
- raise "not alarms" if alms.empty?
72
- _confirm("cloudwatch alarm enable?")
70
+ raise 'not alarms' if alms.empty?
71
+
72
+ confirm('cloudwatch alarm enable?')
73
73
 
74
74
  alms.each do |alm|
75
75
  cwa.enable(alm)
@@ -77,27 +77,28 @@ module CWA
77
77
  end
78
78
  puts
79
79
  alarms
80
- rescue => err
81
- puts "error => #{err}".colorize(:red)
80
+ rescue StandardError => e
81
+ puts "error => #{e}".colorize(:red)
82
82
  exit 1
83
83
  end
84
84
  end
85
85
 
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"
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'
91
91
  def disable
92
92
  begin
93
93
  _enable_assume if options[:assume_role]
94
94
 
95
95
  cwa = CWA.get(options)
96
96
  alms = cwa.alarms(options)
97
- alms = _check_alm(alms, :disable)
97
+ alms = check_alm(alms, :disable)
98
+
99
+ raise 'not alarms' if alms.empty?
98
100
 
99
- raise "not alarms" if alms.empty?
100
- _confirm("cloudwatch alarm disable?")
101
+ confirm('cloudwatch alarm disable?')
101
102
 
102
103
  alms.each do |alm|
103
104
  cwa.disable(alm)
@@ -111,9 +112,9 @@ module CWA
111
112
  end
112
113
  end
113
114
 
114
- desc "configure", "create config files"
115
+ desc 'configure', 'create config files'
115
116
  def configure
116
- configs = %w(assume_role)
117
+ configs = %w[assume_role]
117
118
 
118
119
  puts configs
119
120
  print "create type? : "
@@ -134,9 +135,13 @@ module CWA
134
135
  puts "create => #{ASSUME_FILE.colorize(:yellow)}"
135
136
  end
136
137
  end
138
+ rescue StandardError => e
139
+ puts "error => #{e}".colorize(:red)
140
+ exit 1
141
+ end
137
142
 
138
143
  private
139
- def _output_alms
144
+ def output_alms
140
145
  cwa = CWA.get(options)
141
146
  alms = cwa.alarms(options)
142
147
 
@@ -149,39 +154,40 @@ module CWA
149
154
  end
150
155
  end
151
156
 
152
- def _enable_assume
157
+ def enable_assume
153
158
  raise 'not config file, pls "cwa configure"' unless File.exist?(ASSUME_FILE)
159
+
154
160
  assume = YAML.load_file(ASSUME_FILE)[options[:assume_role]]
155
161
  CWA.assume_role(assume)
156
162
  end
157
163
 
158
- def _check_alm(alms, mode)
164
+ def check_alm(alms, mode)
159
165
  check = false if mode == :enable
160
166
  check = true if mode == :disable
161
167
  alms.map do |alm|
162
- puts "-" * 50
163
- puts "namespace : #{alm[:namespace ]}"
164
- puts "alarm_name : #{alm[:alarm_name ]}"
165
- puts "dimensions : #{alm[:dimensions ]}"
168
+ puts '-' * 50
169
+ puts "namespace : #{alm[:namespace]}"
170
+ puts "alarm_name : #{alm[:alarm_name]}"
171
+ puts "dimensions : #{alm[:dimensions]}"
166
172
  puts "actions_enabled : #{alm[:actions_enabled]}"
167
173
  unless alm[:actions_enabled] == check
168
174
  puts "=> #{'skip'.colorize(:yellow)}"
169
- puts "-" * 50
175
+ puts '-' * 50
170
176
  next
171
177
  end
172
- puts "-" * 50
178
+ puts '-' * 50
173
179
  alm
174
180
  end.compact
175
181
  end
176
182
 
177
- def _confirm(check_word, **opt)
178
- true_word = ( opt[:true] || /yes|y/ )
179
- false_word = ( opt[:false] || /no/ )
183
+ def confirm(check_word, **_opt)
184
+ true_word = /yes|y/
185
+ false_word = /no/
180
186
 
181
187
  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
188
+ print "#{check_word} (#{true_word.inspect.delete('/')}/#{false_word.inspect.delete('/')}) : "
189
+ case $stdin.gets.strip
190
+ when true_word then return true
185
191
  when false_word then return false
186
192
  end
187
193
  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.3"
4
+ VERSION = "0.2.4"
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.2.4
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