swa 0.3.2 → 0.3.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
  SHA1:
3
- metadata.gz: 5aa99633a7562065e12b0eda658a5eb0cb500298
4
- data.tar.gz: bcf698098737d8e568952ed7db966abf9bc28b75
3
+ metadata.gz: f67b72cbe913a6388331d39a0712396b0860ed53
4
+ data.tar.gz: ba6e2d45513837f958df812a219a752fd4cc823b
5
5
  SHA512:
6
- metadata.gz: c364ab568630bd03aca0d497db0bf6d3e292f9558764e9f3242ea8b8cf044290e5c6be28abd1fd8b11ac991578073fcfd01e082ecb6192837417d89c17ee0044
7
- data.tar.gz: 04a8bf044e19cecbd4825a9c505eadd35c3b7d1076a9c7733ebf983d403504f56c154ef82f0dc5403eb5336c649590d0353d642f636b55d1bf8630d283c8f179
6
+ metadata.gz: 6c52a78603d824cbcfc1c2326b573d333e1feda685fa3059daf2f90a0463e1365886c8810255e82d111ce4957fe347574ea958cad2f21d4ea0e58fabaf7834c8
7
+ data.tar.gz: c229d344051e47fde3ba951a30eb0c0c638f7616d8bd4c0fe88fb1d32f6d2c22beb82bedbbdf5cfc7de20ee3646aedd3411013c0a7b5fc7cb150d790c3e9377f
@@ -1,3 +1,4 @@
1
+ require "chronic"
1
2
  require "clamp"
2
3
  require "console_logger"
3
4
  require "jmespath"
@@ -97,6 +98,12 @@ module Swa
97
98
  signal_error("invalid JMESPath expression")
98
99
  end
99
100
 
101
+ def parse_datetime(datetime_string)
102
+ result = Chronic.parse(datetime_string, :guess => false, :endian_precedence => :little)
103
+ raise ArgumentError, "unrecognised date/time #{datetime_string.inspect}" unless result
104
+ result
105
+ end
106
+
100
107
  end
101
108
 
102
109
  end
@@ -1,3 +1,5 @@
1
+ require "swa/cli/selector"
2
+
1
3
  module Swa
2
4
  module CLI
3
5
 
@@ -27,6 +29,14 @@ module Swa
27
29
 
28
30
  end
29
31
 
32
+ def selector
33
+ context[:selector] ||= Selector.new
34
+ end
35
+
36
+ def query_options
37
+ context[:query_options] ||= {}
38
+ end
39
+
30
40
  end
31
41
 
32
42
  end
@@ -3,9 +3,9 @@ require "swa/cli/base_command"
3
3
  require "swa/cli/collection_behaviour"
4
4
  require "swa/cli/item_behaviour"
5
5
  require "swa/cli/tag_filter_options"
6
- require "swa/ec2/key_pair"
7
6
  require "swa/ec2/image"
8
7
  require "swa/ec2/instance"
8
+ require "swa/ec2/key_pair"
9
9
  require "swa/ec2/security_group"
10
10
  require "swa/ec2/snapshot"
11
11
  require "swa/ec2/volume"
@@ -33,12 +33,16 @@ module Swa
33
33
 
34
34
  subcommand ["key-pairs", "kps"], "List key-pairs" do
35
35
 
36
+ self.description = <<-EOF
37
+ List key-pairs.
38
+ EOF
39
+
36
40
  include CollectionBehaviour
37
41
 
38
42
  private
39
43
 
40
44
  def key_pairs
41
- Swa::EC2::KeyPair.list(ec2.key_pairs)
45
+ query_for(:key_pairs, Swa::EC2::KeyPair)
42
46
  end
43
47
 
44
48
  alias_method :collection, :key_pairs
@@ -63,9 +67,19 @@ module Swa
63
67
 
64
68
  subcommand ["images", "amis"], "List images" do
65
69
 
70
+ self.description = <<-EOF
71
+ List images (AMIs).
72
+
73
+ By default only AMIs owned by the current account are listed;
74
+ use `--owned-by` to select a different scope.
75
+ EOF
76
+
66
77
  option "--owned-by", "OWNER", "with specified owner", :default => "self"
67
78
  option "--named", "PATTERN", "with matching name"
68
79
 
80
+ option ["--created-after", "--after"], "WHEN", "earliest creation-date"
81
+ option ["--created-before", "--before"], "WHEN", "latest creation-date"
82
+
69
83
  include TagFilterOptions
70
84
  include CollectionBehaviour
71
85
 
@@ -75,12 +89,23 @@ module Swa
75
89
  add_filter("name", name_pattern)
76
90
  end
77
91
 
92
+ def created_after=(datetime_string)
93
+ min_creation_date = parse_datetime(datetime_string).max
94
+ selector.add do |image|
95
+ Time.parse(image.creation_date) > min_creation_date
96
+ end
97
+ end
98
+
99
+ def created_before=(datetime_string)
100
+ max_creation_date = parse_datetime(datetime_string).min
101
+ selector.add do |image|
102
+ Time.parse(image.creation_date) < max_creation_date
103
+ end
104
+ end
105
+
78
106
  def images
79
- options = {
80
- :owners => [owned_by]
81
- }
82
- options[:filters] = filters unless filters.empty?
83
- Swa::EC2::Image.list(ec2.images(options))
107
+ query_options[:owners] = [owned_by]
108
+ query_for(:images, Swa::EC2::Image)
84
109
  end
85
110
 
86
111
  alias_method :collection, :images
@@ -123,6 +148,13 @@ module Swa
123
148
 
124
149
  subcommand ["instances", "is"], "List instances" do
125
150
 
151
+ self.description = <<-EOF
152
+ List images (AMIs).
153
+
154
+ By default only runnning instances are listed;
155
+ use `--state` to override ('*' for all states).
156
+ EOF
157
+
126
158
  option ["--state"], "STATE", "with specified status",
127
159
  :default => "running"
128
160
 
@@ -142,6 +174,9 @@ module Swa
142
174
  end
143
175
  end
144
176
 
177
+ option ["--launched-after", "--after"], "WHEN", "earliest launch-time"
178
+ option ["--launched-before", "--before"], "WHEN", "latest launch-time"
179
+
145
180
  include TagFilterOptions
146
181
  include CollectionBehaviour
147
182
 
@@ -157,12 +192,25 @@ module Swa
157
192
  RUBY
158
193
  end
159
194
 
195
+ def launched_after=(datetime_string)
196
+ min_launch_time = parse_datetime(datetime_string).max
197
+ selector.add do |instance|
198
+ instance.launch_time > min_launch_time
199
+ end
200
+ end
201
+
202
+ def launched_before=(datetime_string)
203
+ max_launch_time = parse_datetime(datetime_string).min
204
+ selector.add do |instance|
205
+ instance.launch_time < max_launch_time
206
+ end
207
+ end
208
+
160
209
  private
161
210
 
162
211
  def instances
163
212
  add_filter("instance-state-name", state)
164
- options = {:filters => filters}
165
- Swa::EC2::Instance.list(ec2.instances(options))
213
+ query_for(:instances, Swa::EC2::Instance)
166
214
  end
167
215
 
168
216
  alias_method :collection, :instances
@@ -193,9 +241,7 @@ module Swa
193
241
  private
194
242
 
195
243
  def security_groups
196
- options = {}
197
- options[:filters] = filters unless filters.empty?
198
- Swa::EC2::SecurityGroup.list(ec2.security_groups(options))
244
+ query_for(:security_groups, Swa::EC2::SecurityGroup)
199
245
  end
200
246
 
201
247
  alias_method :collection, :security_groups
@@ -220,6 +266,13 @@ module Swa
220
266
 
221
267
  subcommand ["snapshots", "snaps"], "List snapshots" do
222
268
 
269
+ self.description = <<-EOF
270
+ List EBS snapshots.
271
+
272
+ By default only snapshots owned by the current account are listed;
273
+ use `--owned-by` to select a different scope.
274
+ EOF
275
+
223
276
  option "--owned-by", "OWNER", "with specified owner", :default => "self"
224
277
 
225
278
  include TagFilterOptions
@@ -228,11 +281,8 @@ module Swa
228
281
  private
229
282
 
230
283
  def snapshots
231
- options = {
232
- :owner_ids => [owned_by]
233
- }
234
- options[:filters] = filters unless filters.empty?
235
- Swa::EC2::Snapshot.list(ec2.snapshots(options))
284
+ query_options[:owner_ids] = [owned_by]
285
+ query_for(:snapshots, Swa::EC2::Snapshot)
236
286
  end
237
287
 
238
288
  alias_method :collection, :snapshots
@@ -257,15 +307,17 @@ module Swa
257
307
 
258
308
  subcommand ["volumes", "vols"], "List volumes" do
259
309
 
310
+ self.description = <<-EOF
311
+ List EBS volumes.
312
+ EOF
313
+
260
314
  include TagFilterOptions
261
315
  include CollectionBehaviour
262
316
 
263
317
  private
264
318
 
265
319
  def volumes
266
- options = {}
267
- options[:filters] = filters unless filters.empty?
268
- Swa::EC2::Volume.list(ec2.volumes(options))
320
+ query_for(:volumes, Swa::EC2::Volume)
269
321
  end
270
322
 
271
323
  alias_method :collection, :volumes
@@ -278,6 +330,12 @@ module Swa
278
330
  ::Aws::EC2::Resource.new(aws_config)
279
331
  end
280
332
 
333
+ def query_for(query_method, resource_model)
334
+ aws_resources = ec2.public_send(query_method, query_options)
335
+ wrapped_resources = resource_model.list(aws_resources)
336
+ selector.apply(wrapped_resources)
337
+ end
338
+
281
339
  end
282
340
 
283
341
  end
@@ -13,7 +13,7 @@ module Swa
13
13
  protected
14
14
 
15
15
  def filters
16
- @filters ||= []
16
+ query_options[:filters] ||= []
17
17
  end
18
18
 
19
19
  def add_filter(name, *values)
@@ -0,0 +1,27 @@
1
+ module Swa
2
+ module CLI
3
+
4
+ class Selector
5
+
6
+ def initialize
7
+ @predicates = []
8
+ end
9
+
10
+ def add(&block)
11
+ @predicates << block
12
+ end
13
+
14
+ def call(subject)
15
+ @predicates.all? do |predicate|
16
+ predicate.call(subject)
17
+ end
18
+ end
19
+
20
+ def apply(collection)
21
+ collection.lazy.select(&method(:call))
22
+ end
23
+
24
+ end
25
+
26
+ end
27
+ end
@@ -11,6 +11,9 @@ module Swa
11
11
 
12
12
  option "--tagged", "KEY[=VALUE]", "with matching tag",
13
13
  :multivalued => true, :attribute_name => :tag_list
14
+ option "--not-tagged", "KEY[=VALUE]", "WITHOUT matching tag",
15
+ :multivalued => true, :attribute_name => :tag_blacklist
16
+
14
17
  option "--stack", "NAME", "from the named CloudFormation stack"
15
18
 
16
19
  protected
@@ -20,6 +23,20 @@ module Swa
20
23
  add_tag_filter(key, value_pattern)
21
24
  end
22
25
 
26
+ def append_to_tag_blacklist(arg)
27
+ key, value_pattern = arg.split("=", 2)
28
+ if value_pattern
29
+ selector.add do |resource|
30
+ value = resource.tags[key]
31
+ value.nil? || !File.fnmatch(value_pattern, value)
32
+ end
33
+ else
34
+ selector.add do |resource|
35
+ resource.tags[key].nil?
36
+ end
37
+ end
38
+ end
39
+
23
40
  def stack=(name)
24
41
  add_tag_filter("aws:cloudformation:stack-name", name)
25
42
  end
@@ -20,6 +20,8 @@ module Swa
20
20
  ami.name
21
21
  end
22
22
 
23
+ delegate :creation_date
24
+
23
25
  private
24
26
 
25
27
  alias_method :ami, :aws_resource
@@ -1,4 +1,4 @@
1
- require "forwardable"
1
+ require "base64"
2
2
  require "swa/resource"
3
3
  require "swa/ec2/tagged_resource"
4
4
 
@@ -26,12 +26,11 @@ module Swa
26
26
  end
27
27
 
28
28
  def console_output
29
- i.console_output.output
29
+ Base64.decode64(i.console_output.output)
30
30
  end
31
31
 
32
- extend Forwardable
33
-
34
- def_delegators :i, :stop, :start, :reboot, :terminate
32
+ delegate :launch_time
33
+ delegate :stop, :start, :reboot, :terminate
35
34
 
36
35
  private
37
36
 
@@ -7,7 +7,7 @@ module Swa
7
7
 
8
8
  def summary
9
9
  [
10
- pad(name, 4),
10
+ pad(name, 44),
11
11
  aws_resource.key_fingerprint
12
12
  ].join(" ")
13
13
  end
@@ -1,4 +1,3 @@
1
- require "forwardable"
2
1
  require "swa/resource"
3
2
  require "swa/ec2/tagged_resource"
4
3
 
@@ -20,9 +19,7 @@ module Swa
20
19
  ].join(" ")
21
20
  end
22
21
 
23
- extend Forwardable
24
-
25
- def_delegators :s, :delete
22
+ delegate :delete
26
23
 
27
24
  private
28
25
 
@@ -1,4 +1,3 @@
1
- require "forwardable"
2
1
  require "swa/resource"
3
2
  require "swa/ec2/tagged_resource"
4
3
 
@@ -29,9 +28,7 @@ module Swa
29
28
  v.attachments.first || OpenStruct.new
30
29
  end
31
30
 
32
- extend Forwardable
33
-
34
- def_delegators :v, :delete
31
+ delegate :delete
35
32
 
36
33
  private
37
34
 
@@ -1,3 +1,4 @@
1
+ require "forwardable"
1
2
  require "swa/data_presentation"
2
3
 
3
4
  module Swa
@@ -21,6 +22,12 @@ module Swa
21
22
  camelize_keys(_resource_.data.to_h)
22
23
  end
23
24
 
25
+ extend Forwardable
26
+
27
+ def self.delegate(*methods)
28
+ def_delegators :aws_resource, *methods
29
+ end
30
+
24
31
  end
25
32
 
26
33
  end
@@ -1,3 +1,3 @@
1
1
  module Swa
2
- VERSION = "0.3.2"
2
+ VERSION = "0.3.3"
3
3
  end
@@ -25,6 +25,7 @@ Gem::Specification.new do |spec|
25
25
  spec.add_development_dependency "rspec", "~> 3.0"
26
26
 
27
27
  spec.add_runtime_dependency "aws-sdk-resources"
28
+ spec.add_runtime_dependency "chronic"
28
29
  spec.add_runtime_dependency "clamp"
29
30
  spec.add_runtime_dependency "console_logger"
30
31
  spec.add_runtime_dependency "multi_json"
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.3.2
4
+ version: 0.3.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: 2016-06-28 00:00:00.000000000 Z
11
+ date: 2016-06-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: chronic
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: clamp
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -133,6 +147,7 @@ files:
133
147
  - lib/swa/cli/filter_options.rb
134
148
  - lib/swa/cli/item_behaviour.rb
135
149
  - lib/swa/cli/main_command.rb
150
+ - lib/swa/cli/selector.rb
136
151
  - lib/swa/cli/tag_filter_options.rb
137
152
  - lib/swa/data_presentation.rb
138
153
  - lib/swa/ec2/image.rb