cwa 0.2.6 → 0.3.1

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: 68fe5fa6a7a7028fe3a8aa3c6781ba92b95c77972bcb9b32f0ef87b7d3ffea5f
4
- data.tar.gz: d37958cce67757b099ab293a157060d91053c83f34a563fc9743254cbdbe4fe5
3
+ metadata.gz: 18c398f2c91a08ef355f725b361937608fa500b37ab27b6a610b9668fc3ae92a
4
+ data.tar.gz: 90aa7ff8fa4231e29effd83e4093803e862afd4e23c535f31232a59448e5a415
5
5
  SHA512:
6
- metadata.gz: b4380bddd08abd79d38d6a5dde6b99c5af8ad93d4ab0086783c258f50accebb0c4c6abb9f649d468b1e7578ecfd60aa56bdccfc13943ec8e43191a5ef999b525
7
- data.tar.gz: 6dc99a2dedb8a4ffabdd1a086cb5204a91c7e56c4afd20f47a5593fa220d742c52dd01b987c4027fdd6d89f4f26df36744d8edf6cd2b5e0b261160723eb21ebf
6
+ metadata.gz: b0400884cd212804b16584a58f982ce397b787ea469444f6e936da9a180b89b96b605e9049fe94d5e5c1dba3fcfcdc23de043ab8df9516f089415f054ea46543
7
+ data.tar.gz: 6447715b1e308ebd2801748447f7984c49046c526f4cd42244edc43cefb4d75cf2e6c6f1459d68bfe0645d7b9e81741b4016397e2505e0ce45fed45ce6ab9d02
data/lib/cwa.rb CHANGED
@@ -12,25 +12,8 @@ module CWA
12
12
  class Error < StandardError; end
13
13
 
14
14
  class << self
15
- def configure(**opts)
16
- @aws_opts = opts
17
- end
18
-
19
15
  def get(opts = {})
20
- @aws_opts ||= {}
21
- @aws_opts[:profile] = opts[:profile] if opts[:profile]
22
- @aws_opts[:region] = opts[:region] if opts[:region]
23
-
24
- Client.new(@aws_opts)
25
- end
26
-
27
- def assume_role(opts)
28
- role_credentials = Aws::AssumeRoleCredentials.new(
29
- client: Aws::STS::Client.new(opts),
30
- role_arn: opts[:arn],
31
- role_session_name: opts[:session_name]
32
- )
33
- @aws_opts[:credentials] = role_credentials
16
+ Client.new(opts)
34
17
  end
35
18
  end
36
19
  end
@@ -10,28 +10,28 @@ module CWA
10
10
  }.freeze
11
11
 
12
12
  def initialize(client, **opts)
13
- @opts = opts
14
- @client = client
13
+ @client = client
15
14
  end
16
15
 
17
16
  def filter(query)
18
- alms = alarms
17
+ @alms = alarms
19
18
 
20
19
  # querys
21
20
  name_query = ->(alm) { alm.alarm_name == query[:name] }
22
21
  regexp_query = ->(alm) { alm.alarm_name =~ /#{query[:regexp]}/ }
23
22
  namespace_query = ->(alm) { alm.namespace == query[:namespace] }
24
23
 
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]
29
- alms
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
30
30
  end
31
31
 
32
- def refresh
32
+ def refresh(query = nil)
33
33
  @alms = nil
34
- alarms
34
+ query ? filter(query) : alarms
35
35
  end
36
36
 
37
37
  private
@@ -12,24 +12,24 @@ ASSUME_DIR = File.join(Dir.home, '.config', 'cwa').freeze
12
12
  ASSUME_FILE = File.join(ASSUME_DIR, 'assume.yml').freeze
13
13
 
14
14
  OUTPUT_KEYS = %i[
15
- namespace
16
- alarm_name
17
- actions_enabled
15
+ namespace
16
+ alarm_name
17
+ actions_enabled
18
18
  ].freeze
19
19
 
20
20
  OUTPUT_KEYS_DETAIL = %i[
21
- namespace
22
- alarm_name
23
- actions_enabled
24
- dimensions
25
- alarm_arn
26
- alarm_description
21
+ namespace
22
+ alarm_name
23
+ actions_enabled
24
+ dimensions
25
+ alarm_arn
26
+ alarm_description
27
27
  ].freeze
28
28
 
29
29
 
30
30
  AWS_OPTIONS = %i[
31
- profile
32
- region
31
+ profile
32
+ region
33
33
  ].freeze
34
34
 
35
35
  OPTIONS = '--name ALARMNAME --regexp ALARMNAME --namespae NAMESPACE --dimensions KEY:VALUE'.freeze
@@ -37,7 +37,6 @@ OPTIONS = '--name ALARMNAME --regexp ALARMNAME --namespae NAMESPACE --dimensions
37
37
  module CWA
38
38
  # cli class
39
39
  class Cli < Thor
40
- begin
41
40
  class_option :verbose, type: :boolean
42
41
  class_option :profile, type: :string
43
42
  class_option :region, type: :string
@@ -50,22 +49,32 @@ module CWA
50
49
  option :regexp, type: :string, aliases: 'r'
51
50
  option :dimensions, type: :hash, aliases: 'd'
52
51
  def alarms
53
- enable_assume if options[:assume_role]
54
-
55
- alms = output_alms
56
- raise 'not alarms' if alms.empty?
57
-
58
-
59
- case options[:output]
60
- when 'json'
61
- puts JSON.dump(alms)
62
- when 'yaml'
63
- puts YAML.dump(alms)
64
- else
65
- head = alms.first.keys
66
- rows = alms.map{|alm| alm.values }
67
- table = Terminal::Table.new :headings => head, :rows => rows
68
- puts table
52
+ begin
53
+ cwa = CWA.get(aws_opts)
54
+ alms = cwa.alarms(options)
55
+ keys = OUTPUT_KEYS
56
+ keys = OUTPUT_KEYS_DETAIL if options[:verbose]
57
+
58
+ alms = alms.map do |alm|
59
+ keys.reduce({}) { |h, key| h.merge!(key => alm.method(key).call) }
60
+ end
61
+
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)
77
+ exit 1
69
78
  end
70
79
  end
71
80
 
@@ -76,9 +85,7 @@ module CWA
76
85
  option :dimensions, type: :hash, aliases: 'd'
77
86
  def enable
78
87
  begin
79
- enable_assume if options[:assume_role]
80
-
81
- cwa = CWA.get(options)
88
+ cwa = CWA.get(aws_opts)
82
89
  alms = cwa.alarms(options)
83
90
  alms = check_alm(alms, :enable)
84
91
 
@@ -105,9 +112,7 @@ module CWA
105
112
  option :dimensions, type: :hash, aliases: 'd'
106
113
  def disable
107
114
  begin
108
- _enable_assume if options[:assume_role]
109
-
110
- cwa = CWA.get(options)
115
+ cwa = CWA.get(aws_opts)
111
116
  alms = cwa.alarms(options)
112
117
  alms = check_alm(alms, :disable)
113
118
 
@@ -129,53 +134,51 @@ module CWA
129
134
 
130
135
  desc 'configure', 'create config files'
131
136
  def configure
132
- configs = %w[assume_role]
133
-
134
- puts configs
135
- print "create type? : "
136
- type = $stdin.gets.strip
137
- case type
138
- when 'assume_role'
139
- print "name? : "
140
- name = $stdin.gets.strip
141
- print "arn? : "
142
- arn = $stdin.gets.strip
143
- print "session_name? : "
144
- session = $stdin.gets.strip
145
-
146
- assume = {name => { arn: arn, session_name: session}}
147
- FileUtils.mkdir_p(ASSUME_DIR) unless Dir.exist?(ASSUME_DIR)
148
- file = open(ASSUME_FILE, "w")
149
- YAML.dump(assume, file)
150
- puts "create => #{ASSUME_FILE.colorize(:yellow)}"
151
- end
152
- end
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
153
161
  rescue StandardError => e
154
162
  puts "error => #{e}".colorize(:red)
155
163
  exit 1
156
164
  end
165
+ end
157
166
 
158
167
  private
159
- def output_alms
160
- cwa = CWA.get(options)
161
- alms = cwa.alarms(options)
162
- keys = OUTPUT_KEYS
163
- keys = OUTPUT_KEYS_DETAIL if options[:verbose]
168
+ def aws_opts
169
+ opts = {}
164
170
 
165
- alms.map do |alm|
166
- v = Hash.new
167
- keys.each do |key|
168
- v[key] = alm.method(key).call
169
- end
170
- v
171
- end
171
+ opts[:region] = options[:region]
172
+ opts[:profile] = options[:profile]
173
+ opts[:assume_role] = assume
174
+ opts.compact
172
175
  end
173
176
 
174
- def enable_assume
177
+ def assume
178
+ return nil unless options[:assume_role]
175
179
  raise 'not config file, pls "cwa configure"' unless File.exist?(ASSUME_FILE)
176
180
 
177
- assume = YAML.load_file(ASSUME_FILE)[options[:assume_role]]
178
- CWA.assume_role(assume)
181
+ YAML.load_file(ASSUME_FILE)[options[:assume_role]]
179
182
  end
180
183
 
181
184
  def check_alm(alms, mode)
@@ -9,6 +9,11 @@ module CWA
9
9
  class Error < StandardError; end
10
10
 
11
11
  def initialize(opts)
12
+ if opts[:assume_role]
13
+ role = opts.delete(:assume_role)
14
+ opts[:credentials] = assume_role(role)
15
+ end
16
+
12
17
  @client = Aws::CloudWatch::Client.new(opts)
13
18
  end
14
19
 
@@ -16,11 +21,17 @@ module CWA
16
21
  @alarms ||= Alarms.new(@client)
17
22
  alms = @alarms.filter(query)
18
23
  alms.each { |alm| yield alm } if block_given?
24
+
25
+ @query_cache = query
19
26
  alms
20
27
  end
21
28
 
22
- def refresh
23
- @alarms.refresh
29
+ def update(cache: true)
30
+ if cache
31
+ @alarms.refresh(@query_cache)
32
+ else
33
+ @alarms.refresh
34
+ end
24
35
  end
25
36
 
26
37
  def enable(alm)
@@ -32,5 +43,14 @@ module CWA
32
43
  alm = alm[:alarm_name]
33
44
  @client.disable_alarm_actions({ alarm_names: [alm] })
34
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
+ )
54
+ end
35
55
  end
36
56
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CWA
4
- VERSION = "0.2.6"
4
+ VERSION = "0.3.1"
5
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.6
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-10 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