swa 0.7.2 → 0.7.3

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: c40d983e15b51e98b660a8ad7646b60c55ecc5dbe2aa390d64d69f6f8576b4e0
4
- data.tar.gz: 947bccdf34d4dfd1fd5dde03cd790211a81d5eab5e1c35b688a19aaf4865a940
3
+ metadata.gz: 3e104c27cd170b45f520bf23f8ea1c1b3f2c08dfb5b4fbce5c81778d8dac8812
4
+ data.tar.gz: 25be55194508205f3d044a22734e144a777972c8b091f99e7658742d3214175f
5
5
  SHA512:
6
- metadata.gz: 47ed26af61683becc4e72eced8bbcf4b3414046d0f1ebebe379a69e1097f52bd6f1ff55c714046973c46f78b49acf64b3680943d5c66fb5c240c02266e25b430
7
- data.tar.gz: 55ced2ec8ae59f6d6305a7f697b0b38429b76c551e3e0d33354f7ea7d3461df239575078bd805357e6827cb30c5b4a2ad97a87a09f86dad7d5096035d2e9fa6d
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
@@ -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/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.2"
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.2
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-11-17 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