cwa 0.1.1 → 0.2.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: e21a07d7fe8b8c5f28bdc354c35a44065e0a755ce05ef34ad75eceeca73d53b8
4
- data.tar.gz: 7361cab2144cac541b90609f8321b5722d9cc33ed5ac9d3300382c096f033641
3
+ metadata.gz: 1519d445e5671c8a4bde54a85bfc3718352340250ef13c74b76412a10d2a87cc
4
+ data.tar.gz: 837f764c34d6249820bbf08a59dd72bb9a96e8fed12ac52d811c9d79ce3299a2
5
5
  SHA512:
6
- metadata.gz: e5abc10648836931a6bea3c21221459a6b0fbfac65c1beed03d83adb77de7296095133abee6fcf931697006bca2238f8c6ddcf3772e2bd23b937425b331cdad0
7
- data.tar.gz: 3de701a4724fc7afe878057c71c441f248a8c441915b93116621f6ee6db118f4c8da5f35a25414a4d63d0a93721649e473dcfa8c079c5a446ffe992595309ddc
6
+ metadata.gz: 13320f6632e46850cfc74c189cdccf3d38ffb4b647c77ae5435df089adcd229b397496898747d74c1febf47ca304ccff0418021b72e529744691db0b678cd7d6
7
+ data.tar.gz: 432f170c2c6f920315e7aaa84f961a4ca1709fd8e66d60fa8bf3cd52e07d12c28e89b26f56511a4b9c403c898faae2d91047bfa0aad0312d76b50a27b688f3cb
@@ -0,0 +1 @@
1
+ 2.6.5
@@ -32,6 +32,7 @@ Gem::Specification.new do |spec|
32
32
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
33
33
  spec.require_paths = ["lib"]
34
34
 
35
+ spec.add_runtime_dependency"aws-sdk-core", "~> 3"
35
36
  spec.add_runtime_dependency"aws-sdk-cloudwatch", "~> 1"
36
37
  spec.add_runtime_dependency"thor", "~> 1"
37
38
  spec.add_runtime_dependency"terminal-table", "~> 1.8"
@@ -0,0 +1,4 @@
1
+ ---
2
+ <name>:
3
+ :arn: <arn>
4
+ :session_name: <session_name>
data/lib/cwa.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'aws-sdk-core'
1
2
  require "cwa/client"
2
3
  require "cwa/version"
3
4
 
@@ -9,9 +10,21 @@ module CWA
9
10
  @aws_opts = opts
10
11
  end
11
12
 
12
- def get
13
- @aws_opts = {} unless @aws_opts
13
+ def get(opts = {})
14
+ @aws_opts ||= {}
15
+ @aws_opts[:profile] = opts.delete(:profile) if opts[:profile]
16
+ @aws_opts[:region] = opts.delete(:profile) if opts[:region]
17
+
14
18
  Client.new(@aws_opts)
15
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
28
+ end
16
29
  end
17
30
  end
@@ -2,30 +2,45 @@ require 'cwa'
2
2
  require 'thor'
3
3
  require 'terminal-table'
4
4
  require 'colorize'
5
+ require 'yaml'
6
+ require 'fileutils'
5
7
 
6
- OUTPUT_KEYS = [
7
- :namespace,
8
- :alarm_name,
9
- :actions_enabled,
10
- :dimensions,
11
- :alarm_arn,
12
- :alarm_description,
13
- ]
8
+ ASSUME_DIR = File.join(Dir.home, '.config', 'cwa')
9
+ ASSUME_FILE = File.join(ASSUME_DIR, 'assume.yml')
10
+
11
+ OUTPUT_KEYS = %i(
12
+ namespace
13
+ alarm_name
14
+ actions_enabled
15
+ dimensions
16
+ alarm_arn
17
+ alarm_description
18
+ )
19
+
20
+
21
+ AWS_OPTIONS = %i(
22
+ profile
23
+ region
24
+ )
14
25
 
15
26
  OPTIONS = "--name ALARMNAME --regexp ALARMNAME --namespae NAMESPACE --dimensions KEY:VALUE"
16
27
 
17
28
  module CWA
18
29
  class Cli < Thor
19
- class_option :verbose, :type => :boolean
30
+ class_option :verbose, type: :boolean
31
+ class_option :profile, type: :string
32
+ class_option :region, type: :string
33
+ class_option :assume_role, type: :string
20
34
 
21
35
  desc "alarms #{OPTIONS}", "show cloudwatch alms"
22
36
  option :name, type: :string, aliases: "n"
23
37
  option :namespace, type: :string, aliases: "s"
24
38
  option :regexp, type: :string, aliases: "r"
25
39
  option :dimensions, type: :hash, aliases: "d"
26
- #option ambiguous: :string
27
40
  def alarms
28
41
  begin
42
+ _enable_assume if options[:assume_role]
43
+
29
44
  alms = _output_alms
30
45
  raise "not alarms" if alms.empty?
31
46
 
@@ -47,7 +62,9 @@ module CWA
47
62
  option :dimensions, type: :hash, aliases: "d"
48
63
  def enable
49
64
  begin
50
- cwa = CWA.get
65
+ _enable_assume if options[:assume_role]
66
+
67
+ cwa = CWA.get(options)
51
68
  alms = cwa.alarms(options)
52
69
  alms = _check_alm(alms, :enable)
53
70
 
@@ -73,7 +90,9 @@ module CWA
73
90
  option :dimensions, type: :hash, aliases: "d"
74
91
  def disable
75
92
  begin
76
- cwa = CWA.get
93
+ _enable_assume if options[:assume_role]
94
+
95
+ cwa = CWA.get(options)
77
96
  alms = cwa.alarms(options)
78
97
  alms = _check_alm(alms, :disable)
79
98
 
@@ -92,9 +111,33 @@ module CWA
92
111
  end
93
112
  end
94
113
 
114
+ desc "configure", "create config files"
115
+ 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)}"
135
+ end
136
+ end
137
+
95
138
  private
96
139
  def _output_alms
97
- cwa = CWA.get
140
+ cwa = CWA.get(options)
98
141
  alms = cwa.alarms(options)
99
142
 
100
143
  alms.map do |alm|
@@ -106,6 +149,12 @@ module CWA
106
149
  end
107
150
  end
108
151
 
152
+ def _enable_assume
153
+ 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)
156
+ end
157
+
109
158
  def _check_alm(alms, mode)
110
159
  check = false if mode == :enable
111
160
  check = true if mode == :disable
@@ -1,3 +1,3 @@
1
1
  module CWA
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.1"
3
3
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cwa
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.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-03 00:00:00.000000000 Z
11
+ date: 2020-10-05 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aws-sdk-core
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: aws-sdk-cloudwatch
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -103,6 +117,7 @@ extensions: []
103
117
  extra_rdoc_files: []
104
118
  files:
105
119
  - ".gitignore"
120
+ - ".ruby-version"
106
121
  - Gemfile
107
122
  - README.md
108
123
  - Rakefile
@@ -110,6 +125,7 @@ files:
110
125
  - bin/setup
111
126
  - cwa.gemspec
112
127
  - exe/cwa
128
+ - exsample/assume.yml
113
129
  - lib/cwa.rb
114
130
  - lib/cwa/alarms.rb
115
131
  - lib/cwa/cli.rb
@@ -135,7 +151,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
135
151
  - !ruby/object:Gem::Version
136
152
  version: '0'
137
153
  requirements: []
138
- rubygems_version: 3.1.2
154
+ rubygems_version: 3.0.3
139
155
  signing_key:
140
156
  specification_version: 4
141
157
  summary: cloudwatch alarm client