swa 0.7.1 → 0.7.3

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: 588a6a0445908b4b0c24eeab23190c51143c2b603f801c7c64fe8d296ac42180
4
- data.tar.gz: 21f835b69987db9c4011e4672c1e2bed5a5bf1b2f84112662fb72af1114499c7
3
+ metadata.gz: 3e104c27cd170b45f520bf23f8ea1c1b3f2c08dfb5b4fbce5c81778d8dac8812
4
+ data.tar.gz: 25be55194508205f3d044a22734e144a777972c8b091f99e7658742d3214175f
5
5
  SHA512:
6
- metadata.gz: c3633166d5fd54e8299875540f0415b36ee092a01d7e3f055d415910ce9268456f89afe5fe88c79df37597d7dd452a8ffd5c5f935bfb397a1afe0353302e6b1c
7
- data.tar.gz: 9189b5b62448a4430865f4965824c05513b334a0af372df722145a5d945996a15874bab4b3f7b9e7396308adde75633201c3e4c7f9264ac5535dd25ef97956ed
6
+ metadata.gz: e5e1856a85450d6b8194a253cbb7773dd81a8cfa2206ff24853ce1da0e55268b4c4ccf2448a91adc2c934da5ec6d43135b4d0c4d1ac7cb4d05cd97d044d49fc9
7
+ data.tar.gz: 700faafa6b9e53f0dd926b7fd63768c0cc9dedae487fb28e74a7bb5a63c9a5ab79015028f5e285666606fb0e3eb732ff0f4f500ea1242cc52e30c1721b15cc93
@@ -0,0 +1,24 @@
1
+ require "swa/record"
2
+
3
+ module Swa
4
+ module Athena
5
+
6
+ class Catalog < Record
7
+
8
+ def summary
9
+ [
10
+ pad(name, 48),
11
+ type
12
+ ].join(" ")
13
+ end
14
+
15
+ def name
16
+ aws_record.catalog_name
17
+ end
18
+
19
+ delegate :type
20
+
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,21 @@
1
+ require "swa/record"
2
+
3
+ module Swa
4
+ module Athena
5
+
6
+ class Database < Record
7
+
8
+ def summary
9
+ [
10
+ pad(name, 48),
11
+ description
12
+ ].join(" ")
13
+ end
14
+
15
+ delegate :name
16
+ delegate :description
17
+
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,28 @@
1
+ require "swa/record"
2
+
3
+ module Swa
4
+ module Athena
5
+
6
+ class QueryExecution < Record
7
+
8
+ def summary
9
+ [
10
+ pad(id, 48),
11
+ pad(state, 12)
12
+ ].join(" ")
13
+ end
14
+
15
+ delegate :id
16
+
17
+ def id
18
+ aws_record.query_execution_id
19
+ end
20
+
21
+ def state
22
+ aws_record.status.state
23
+ end
24
+
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,23 @@
1
+ require "swa/record"
2
+
3
+ module Swa
4
+ module Athena
5
+
6
+ class WorkGroup < Record
7
+
8
+ def summary
9
+ [
10
+ pad(name, 48),
11
+ pad(state, 12),
12
+ description
13
+ ].join(" ")
14
+ end
15
+
16
+ delegate :name
17
+ delegate :state
18
+ delegate :description
19
+
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,198 @@
1
+ require "aws-sdk-athena"
2
+ require "csv"
3
+ require "swa/cli/base_command"
4
+ require "swa/cli/collection_behaviour"
5
+ require "swa/cli/item_behaviour"
6
+ require "swa/athena/catalog"
7
+ require "swa/athena/database"
8
+ require "swa/athena/query_execution"
9
+ require "swa/athena/work_group"
10
+
11
+ module Swa
12
+ module CLI
13
+
14
+ class AthenaCommand < BaseCommand
15
+
16
+ option "--catalog", "NAME", "Data catalog name", default: "AwsDataCatalog"
17
+ option %w(--workgroup -W), "NAME", "Workgroup name"
18
+
19
+ subcommand "catalogs", "Show catalogs" do
20
+
21
+ include CollectionBehaviour
22
+
23
+ private
24
+
25
+ def collection
26
+ query_for(:list_data_catalogs, :data_catalogs_summary, Swa::Athena::Catalog)
27
+ end
28
+
29
+ end
30
+
31
+ subcommand "database", "Database" do
32
+
33
+ parameter "NAME", "database name", attribute_name: :database
34
+
35
+ include ItemBehaviour
36
+
37
+ private
38
+
39
+ def item
40
+ Swa::Athena::Database.new(
41
+ athena_client.get_database(catalog_name: catalog, database_name: database).database
42
+ )
43
+ end
44
+
45
+ end
46
+
47
+ subcommand ["databases", "dbs"], "Show databases" do
48
+
49
+ include CollectionBehaviour
50
+
51
+ private
52
+
53
+ def collection
54
+ query_for(:list_databases, :database_list, Swa::Athena::Database, catalog_name: catalog)
55
+ end
56
+
57
+ end
58
+
59
+ subcommand ["execution", "query-execution"], "Inspect query execution" do
60
+
61
+ parameter "ID", "execution ID", attribute_name: :execution_id
62
+
63
+ include ItemBehaviour
64
+
65
+ private
66
+
67
+ def item
68
+ Swa::Athena::QueryExecution.new(
69
+ athena_client.get_query_execution(query_execution_id: execution_id).query_execution
70
+ )
71
+ end
72
+
73
+ subcommand "results", "Show results" do
74
+
75
+ def execute
76
+ query_results = athena_client.get_query_results(query_execution_id: execution_id)
77
+ output_results_as_csv(query_results.result_set)
78
+ end
79
+
80
+ end
81
+
82
+ end
83
+
84
+ subcommand ["executions", "query-executions"], "List query executions" do
85
+
86
+ def execute
87
+ athena_client.list_query_executions(work_group: workgroup).query_execution_ids.each do |id|
88
+ puts id
89
+ end
90
+ end
91
+
92
+ end
93
+
94
+ subcommand ["executions", "query-executions"], "List query executions" do
95
+
96
+ include CollectionBehaviour
97
+
98
+ private
99
+
100
+ def collection
101
+ query_for(:list_query_executions, :query_execution_ids, Swa::Athena::QueryExecution, work_group: workgroup)
102
+ end
103
+
104
+ end
105
+
106
+ subcommand ["query", "q", "run"], "Run a query" do
107
+
108
+ parameter "QUERY", "SQL query"
109
+
110
+ def execute
111
+ start_query_response = athena_client.start_query_execution(query_string: query, work_group: workgroup)
112
+ wait_for_query(start_query_response.query_execution_id)
113
+ query_results = athena_client.get_query_results(query_execution_id: start_query_response.query_execution_id)
114
+ output_results_as_csv(query_results.result_set)
115
+ end
116
+
117
+ private
118
+
119
+ def wait_for_query(query_execution_id)
120
+ QueryCompletionWaiter.new(client: athena_client).wait(query_execution_id: query_execution_id)
121
+ end
122
+
123
+ end
124
+
125
+ subcommand ["workgroups", "wgs"], "Show work-groups" do
126
+
127
+ include CollectionBehaviour
128
+
129
+ private
130
+
131
+ def collection
132
+ query_for(:list_work_groups, :work_groups, Swa::Athena::WorkGroup)
133
+ end
134
+
135
+ end
136
+
137
+ protected
138
+
139
+ def athena_client
140
+ ::Aws::Athena::Client.new(aws_config)
141
+ end
142
+
143
+ def query_for(query_method, response_key, model, **query_args)
144
+ records = athena_client.public_send(query_method, **query_args).public_send(response_key)
145
+ model.list(records)
146
+ end
147
+
148
+ def output_results_as_csv(result_set)
149
+ CSV($stdout.dup) do |csv|
150
+ result_set.rows.each do |row|
151
+ csv << row.data.map(&:var_char_value)
152
+ end
153
+ end
154
+ end
155
+
156
+ class QueryCompletionWaiter
157
+
158
+ def initialize(options)
159
+ @client = options.fetch(:client)
160
+ @waiter = Aws::Waiters::Waiter.new({
161
+ max_attempts: 30,
162
+ delay: 5,
163
+ poller: Aws::Waiters::Poller.new(
164
+ operation_name: :get_query_execution,
165
+ acceptors: [
166
+ {
167
+ "matcher" => "path",
168
+ "argument" => "query_execution.status.state",
169
+ "expected" => "SUCCEEDED",
170
+ "state" => "success",
171
+ },
172
+ {
173
+ "matcher" => "path",
174
+ "argument" => "query_execution.status.state",
175
+ "expected" => "FAILED",
176
+ "state" => "failure",
177
+ },
178
+ {
179
+ "matcher" => "path",
180
+ "argument" => "query_execution.status.state",
181
+ "expected" => "CANCELLED",
182
+ "state" => "error",
183
+ }
184
+ ]
185
+ )
186
+ }.merge(options))
187
+ end
188
+
189
+ def wait(params = {})
190
+ @waiter.wait(client: @client, params: params)
191
+ end
192
+
193
+ end
194
+
195
+ end
196
+
197
+ end
198
+ end
@@ -137,10 +137,14 @@ module Swa
137
137
 
138
138
  private
139
139
 
140
- def item
140
+ def role
141
141
  Swa::IAM::Role.new(iam.role(File.basename(name)))
142
142
  end
143
143
 
144
+ def item
145
+ role
146
+ end
147
+
144
148
  subcommand "assume", "Assume the role" do
145
149
 
146
150
  option "--session-name", "NAME", "STS session-name",
@@ -176,6 +180,40 @@ module Swa
176
180
 
177
181
  end
178
182
 
183
+ subcommand ["policies"], "Show role policies." do
184
+
185
+ include CollectionBehaviour
186
+
187
+ private
188
+
189
+ def collection
190
+ role.policies
191
+ end
192
+
193
+ end
194
+
195
+ subcommand ["policy"], "Show named role policy" do
196
+
197
+ parameter "NAME", "policy name", attribute_name: :policy_name
198
+
199
+ include ItemBehaviour
200
+
201
+ subcommand "document", "print policy document" do
202
+
203
+ def execute
204
+ puts item.document
205
+ end
206
+
207
+ end
208
+
209
+ private
210
+
211
+ def item
212
+ role.policy(policy_name)
213
+ end
214
+
215
+ end
216
+
179
217
  end
180
218
 
181
219
  subcommand ["roles"], "Show roles" do
@@ -1,4 +1,5 @@
1
1
  require "swa/cli/base_command"
2
+ require "swa/cli/athena_command"
2
3
  require "swa/cli/cloud_formation_command"
3
4
  require "swa/cli/ec2_command"
4
5
  require "swa/cli/elb_command"
@@ -12,6 +13,7 @@ module Swa
12
13
 
13
14
  class MainCommand < BaseCommand
14
15
 
16
+ subcommand "athena", "Athena stuff", AthenaCommand
15
17
  subcommand ["cf", "cloudformation"], "CloudFormation stuff", CloudFormationCommand
16
18
  subcommand "ec2", "EC2 stuff", Ec2Command
17
19
  subcommand "elb", "elb stuff", ElbCommand
@@ -42,21 +42,18 @@ module Swa
42
42
  pad(value, width)
43
43
  end
44
44
 
45
- def camelize_keys(data)
45
+
46
+ def stringify_keys(data)
46
47
  case data
47
48
  when Hash
48
- data.map { |k,v| [camelize(k), camelize_keys(v)] }.to_h
49
+ data.map { |k,v| [k.to_s, stringify_keys(v)] }.to_h
49
50
  when Array
50
- data.map { |v| camelize_keys(v) }
51
+ data.map { |v| stringify_keys(v) }
51
52
  else
52
53
  data
53
54
  end
54
55
  end
55
56
 
56
- def camelize(symbol)
57
- symbol.to_s.split("_").map(&:capitalize).join("")
58
- end
59
-
60
57
  end
61
58
 
62
59
  end
data/lib/swa/iam/role.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require "swa/resource"
2
2
  require "swa/iam/credentials"
3
+ require "swa/iam/role_policy"
3
4
 
4
5
  module Swa
5
6
  module IAM
@@ -18,6 +19,14 @@ module Swa
18
19
  role.name
19
20
  end
20
21
 
22
+ def policies
23
+ RolePolicy.list(role.policies)
24
+ end
25
+
26
+ def policy(name)
27
+ RolePolicy.new(role.policy(name))
28
+ end
29
+
21
30
  private
22
31
 
23
32
  alias_method :role, :aws_resource
@@ -0,0 +1,28 @@
1
+ require "cgi"
2
+ require "swa/resource"
3
+
4
+ module Swa
5
+ module IAM
6
+
7
+ class RolePolicy < Resource
8
+
9
+ def name
10
+ role_policy.name
11
+ end
12
+
13
+ def summary
14
+ name
15
+ end
16
+
17
+ def document
18
+ CGI.unescape(role_policy.policy_document)
19
+ end
20
+
21
+ private
22
+
23
+ alias_method :role_policy, :aws_resource
24
+
25
+ end
26
+
27
+ end
28
+ end
data/lib/swa/record.rb CHANGED
@@ -16,7 +16,7 @@ module Swa
16
16
  include DataPresentation
17
17
 
18
18
  def data
19
- camelize_keys(aws_record.to_h)
19
+ stringify_keys(aws_record.to_h)
20
20
  end
21
21
 
22
22
  extend Forwardable
data/lib/swa/resource.rb CHANGED
@@ -19,7 +19,7 @@ module Swa
19
19
  include DataPresentation
20
20
 
21
21
  def data
22
- camelize_keys(_resource_.data.to_h)
22
+ stringify_keys(_resource_.data.to_h)
23
23
  end
24
24
 
25
25
  extend Forwardable
data/lib/swa/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Swa
2
- VERSION = "0.7.1"
2
+ VERSION = "0.7.3"
3
3
  end
data/swa.gemspec CHANGED
@@ -23,6 +23,7 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency "bundler", "~> 2.1"
24
24
  spec.add_development_dependency "rake", "~> 12.0"
25
25
 
26
+ spec.add_runtime_dependency "aws-sdk-athena", "~> 1"
26
27
  spec.add_runtime_dependency "aws-sdk-cloudformation", "~> 1"
27
28
  spec.add_runtime_dependency "aws-sdk-ec2", "~> 1"
28
29
  spec.add_runtime_dependency "aws-sdk-elasticloadbalancing", "~> 1"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: swa
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.1
4
+ version: 0.7.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Williams
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-10-24 00:00:00.000000000 Z
11
+ date: 2023-11-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '12.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: aws-sdk-athena
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: aws-sdk-cloudformation
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -252,6 +266,11 @@ files:
252
266
  - bin/setup
253
267
  - exe/swa
254
268
  - lib/swa.rb
269
+ - lib/swa/athena/catalog.rb
270
+ - lib/swa/athena/database.rb
271
+ - lib/swa/athena/query_execution.rb
272
+ - lib/swa/athena/work_group.rb
273
+ - lib/swa/cli/athena_command.rb
255
274
  - lib/swa/cli/base_command.rb
256
275
  - lib/swa/cli/cloud_formation_command.rb
257
276
  - lib/swa/cli/collection_behaviour.rb
@@ -292,6 +311,7 @@ files:
292
311
  - lib/swa/iam/instance_profile.rb
293
312
  - lib/swa/iam/policy.rb
294
313
  - lib/swa/iam/role.rb
314
+ - lib/swa/iam/role_policy.rb
295
315
  - lib/swa/iam/user.rb
296
316
  - lib/swa/kms/alias.rb
297
317
  - lib/swa/kms/key.rb