aws_ro 1.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 31b6a50bebc67276ec85b42ec3991e11117e52ae
4
+ data.tar.gz: a8dfcba129535e7adbc70cad7a62dc9686010cfe
5
+ SHA512:
6
+ metadata.gz: 889aeff1eddacfdace7ec0f7e8bbd507ff0aa95135bd6c7276bc6092d171b0b823c96611ebc4586e9ef485881d8dc2e4c7b9b27711545b97e04fd56634b83265
7
+ data.tar.gz: a6830c209c7d002f8495fea16bc441db7a4291b5dd87e1bcdb00aebd33694a1edf3d0d539f51f89f8e62ef0136ebf033a1ac86318632684d8f932e3f9fa0dada
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,178 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2016-02-04 14:19:06 +0900 using RuboCop version 0.36.0.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
8
+
9
+ AllCops:
10
+ Exclude:
11
+ - 'aws_ro.gemspec'
12
+
13
+ # Offense count: 2
14
+ #Lint/UnderscorePrefixedVariableName:
15
+ # Exclude:
16
+ # - 'lib/aws_ro/ec2/repository.rb'
17
+
18
+ # Offense count: 1
19
+ #Lint/UselessAssignment:
20
+ # Exclude:
21
+ # - 'lib/aws_ro/ec2/repository.rb'
22
+
23
+ # Offense count: 12
24
+ # Configuration parameters: AllowHeredoc, AllowURI, URISchemes.
25
+ # URISchemes: http, https
26
+ Metrics/LineLength:
27
+ Max: 120
28
+ # Max: 122
29
+
30
+ # Offense count: 6
31
+ # Cop supports --auto-correct.
32
+ # Configuration parameters: EnforcedStyle, SupportedStyles.
33
+ # SupportedStyles: prefer_alias, prefer_alias_method
34
+ Style/Alias:
35
+ Exclude:
36
+ - 'lib/aws_ro/ec2/instance.rb'
37
+ - 'lib/aws_ro/ec2/repository.rb'
38
+
39
+ # Offense count: 3
40
+ # Cop supports --auto-correct.
41
+ # Configuration parameters: EnforcedStyle, SupportedStyles, ProceduralMethods, FunctionalMethods, IgnoredMethods.
42
+ # SupportedStyles: line_count_based, semantic, braces_for_chaining
43
+ # ProceduralMethods: benchmark, bm, bmbm, create, each_with_object, measure, new, realtime, tap, with_object
44
+ # FunctionalMethods: let, let!, subject, watch
45
+ # IgnoredMethods: lambda, proc, it
46
+ Style/BlockDelimiters:
47
+ AutoCorrect: false
48
+ Exclude:
49
+ - 'spec/**/*'
50
+
51
+ # Offense count: 2
52
+ # Cop supports --auto-correct.
53
+ #Style/CommentIndentation:
54
+ # Exclude:
55
+ # - 'aws_ro.gemspec'
56
+
57
+ # Offense count: 7
58
+ Style/Documentation:
59
+ Enabled: false
60
+
61
+ # Offense count: 4
62
+ # Cop supports --auto-correct.
63
+ # Configuration parameters: EnforcedStyle, SupportedStyles.
64
+ # SupportedStyles: leading, trailing
65
+ Style/DotPosition:
66
+ EnforcedStyle: trailing
67
+
68
+ # Offense count: 2
69
+ # Cop supports --auto-correct.
70
+ #Style/EmptyLinesAroundAccessModifier:
71
+ # Exclude:
72
+ # - 'lib/aws_ro/ec2/instance.rb'
73
+ # - 'lib/aws_ro/ec2/repository.rb'
74
+
75
+ # Offense count: 1
76
+ # Cop supports --auto-correct.
77
+ # Configuration parameters: EnforcedStyle, SupportedStyles.
78
+ # SupportedStyles: empty_lines, no_empty_lines
79
+ #Style/EmptyLinesAroundClassBody:
80
+ # Exclude:
81
+ # - 'lib/aws_ro/ec2/instance.rb'
82
+
83
+ # Offense count: 1
84
+ # Cop supports --auto-correct.
85
+ # Configuration parameters: AllowForAlignment, ForceEqualSignAlignment.
86
+ Style/ExtraSpacing:
87
+ Exclude:
88
+ - 'spec/**/*'
89
+
90
+ # Offense count: 1
91
+ # Cop supports --auto-correct.
92
+ # Configuration parameters: EnforcedStyle, SupportedStyles, IndentationWidth.
93
+ # SupportedStyles: special_inside_parentheses, consistent, align_brackets
94
+ #Style/IndentArray:
95
+ # Enabled: false
96
+
97
+ # Offense count: 1
98
+ # Cop supports --auto-correct.
99
+ Style/MultilineBlockLayout:
100
+ Exclude:
101
+ - 'spec/aws_ro/ec2/key_value_accessible_spec.rb'
102
+
103
+ # Offense count: 1
104
+ # Cop supports --auto-correct.
105
+ Style/MutableConstant:
106
+ Enabled: false
107
+
108
+ # Offense count: 1
109
+ # Cop supports --auto-correct.
110
+ Style/Not:
111
+ Exclude:
112
+ - 'lib/aws_ro/ec2/key_value_accessible.rb'
113
+
114
+ # Offense count: 2
115
+ # Cop supports --auto-correct.
116
+ # Configuration parameters: PreferredDelimiters.
117
+ Style/PercentLiteralDelimiters:
118
+ Exclude:
119
+ - 'aws_ro.gemspec'
120
+
121
+ # Offense count: 3
122
+ # Cop supports --auto-correct.
123
+ #Style/SpaceAfterComma:
124
+ # Exclude:
125
+ # - 'lib/aws_ro/ec2/key_value_accessible.rb'
126
+
127
+ # Offense count: 1
128
+ # Cop supports --auto-correct.
129
+ #Style/SpaceBeforeComma:
130
+ # Exclude:
131
+ # - 'spec/aws_ro/ec2/key_value_accessible_spec.rb'
132
+
133
+ # Offense count: 2
134
+ # Cop supports --auto-correct.
135
+ #Style/SpaceInsideBrackets:
136
+ # Exclude:
137
+ # - 'spec/aws_ro/ec2/instance_spec.rb'
138
+
139
+ # Offense count: 3
140
+ # Cop supports --auto-correct.
141
+ # Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces, SupportedStyles.
142
+ # SupportedStyles: space, no_space
143
+ # Style/SpaceInsideHashLiteralBraces:
144
+ # Enabled: false
145
+
146
+ # Offense count: 39
147
+ # Cop supports --auto-correct.
148
+ # Configuration parameters: EnforcedStyle, SupportedStyles, ConsistentQuotesInMultiline.
149
+ # SupportedStyles: single_quotes, double_quotes
150
+ Style/StringLiterals:
151
+ Enabled: false
152
+
153
+ # Offense count: 1
154
+ # Cop supports --auto-correct.
155
+ # Configuration parameters: EnforcedStyleForMultiline, SupportedStyles.
156
+ # SupportedStyles: comma, consistent_comma, no_comma
157
+ Style/TrailingCommaInLiteral:
158
+ EnforcedStyleForMultiline: comma
159
+
160
+
161
+ # Offense count: 1
162
+ # Cop supports --auto-correct.
163
+ Style/TrailingWhitespace:
164
+ Exclude:
165
+ - 'spec/aws_ro/ec2/instance_spec.rb'
166
+
167
+ # Offense count: 2
168
+ # Cop supports --auto-correct.
169
+ Style/UnneededPercentQ:
170
+ Exclude:
171
+ - 'aws_ro.gemspec'
172
+
173
+ # Offense count: 1
174
+ # Cop supports --auto-correct.
175
+ # Configuration parameters: SupportedStyles, MinSize, WordRegex.
176
+ # SupportedStyles: percent, brackets
177
+ Style/WordArray:
178
+ EnforcedStyle: brackets
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in aws_ro.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,42 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ ## Uncomment and set this to only include directories you want to watch
5
+ # directories %w(app lib config test spec features) \
6
+ # .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}
7
+
8
+ ## Note: if you are using the `directories` clause above and you are not
9
+ ## watching the project directory ('.'), then you will want to move
10
+ ## the Guardfile to a watched dir and symlink it back, e.g.
11
+ #
12
+ # $ mkdir config
13
+ # $ mv Guardfile config/
14
+ # $ ln -s config/Guardfile .
15
+ #
16
+ # and, you'll have to watch "config/Guardfile" instead of "Guardfile"
17
+
18
+ # Note: The cmd option is now required due to the increasing number of ways
19
+ # rspec may be run, below are examples of the most common uses.
20
+ # * bundler: 'bundle exec rspec'
21
+ # * bundler binstubs: 'bin/rspec'
22
+ # * spring: 'bin/rspec' (This will use spring if running and you have
23
+ # installed the spring binstubs per the docs)
24
+ # * zeus: 'zeus rspec' (requires the server to be started separately)
25
+ # * 'just' rspec: 'rspec'
26
+
27
+ guard :rspec, cmd: "bundle exec rspec" do
28
+ require "guard/rspec/dsl"
29
+ dsl = Guard::RSpec::Dsl.new(self)
30
+
31
+ # Feel free to open issues for suggestions and improvements
32
+
33
+ # RSpec files
34
+ rspec = dsl.rspec
35
+ watch(rspec.spec_helper) { rspec.spec_dir }
36
+ watch(rspec.spec_support) { rspec.spec_dir }
37
+ watch(rspec.spec_files)
38
+
39
+ # Ruby files
40
+ ruby = dsl.ruby
41
+ dsl.watch_spec_files_for(ruby.lib_files)
42
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,180 @@
1
+ # AwsRo
2
+
3
+ Wrapper class of AWS Resource objects to enable to access properties more easily, more ruby-likely.
4
+
5
+ The targets of this library are small and medium scale AWS systems.
6
+
7
+ Now supported only `Aws::EC2` and `Aws::ElasticLoadBalancing`.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'aws_ro', '~> 1.1'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ <!-- Or install it yourself as: -->
22
+
23
+ <!-- $ gem install aws_ro -->
24
+
25
+ ## Purpose of this gem
26
+
27
+ Easy access to AWS Resources.
28
+
29
+ For example, I'd like to print values of 'MyTag' tag of instances which also contains 'MyEnv'=='develop' tag.
30
+
31
+ By using plain AWS SDK v2:
32
+
33
+ ```ruby
34
+ client = Aws::EC2::Client.new(some_options)
35
+ res = clinet.describe_instances(filters: [{name: 'tag:MyEnv', values: ['develop']}, {name: 'tag:MyTag', values: ['*']}])
36
+ instances = res.reservations.flat_map(&:instances)
37
+ instances.each do |i|
38
+ v = i.tags.find { |t| t.key == 'MyTag' }.value
39
+ puts "ID: #{i.instance_id}, MyTag value: #{v}"
40
+ end
41
+ ```
42
+
43
+ This is not difficult, but is a little boring.
44
+
45
+ On the other hand, by using the `aws_ro` gem:
46
+
47
+ ```ruby
48
+ repo = AwsRo::EC2::Repository.new(some_options)
49
+ repo.tags({'MyEnv' => 'develop', 'MyTag' => '*'}).each |i|
50
+ puts "ID: #{i.instance_id}, MyTag value: #{i.my_tag}"
51
+ end
52
+ ```
53
+
54
+ This is simplified and keeping readability.
55
+
56
+
57
+ ## Usage
58
+ ### Basic Usage
59
+
60
+ ```ruby
61
+ ec2_options = { region: 'ap-northeast-1' }
62
+ repo = AwsRo::EC2::Repository.new(ec2_options)
63
+ instances = repo.running.tags({'MyAttrName' => 'MyValue'})
64
+ instances.each do |i|
65
+ puts "#{i.name} #{i.public_ip_address}, #{i.my_attr_name}"
66
+ end
67
+ ```
68
+
69
+ ### Classes
70
+ #### class : `AwsRo::EC2::Repository`
71
+
72
+ Repository is wrapper of EC2 client. It supports chainable queries to describe instances.
73
+
74
+
75
+ ##### Initialize
76
+ ```ruby
77
+ # initialize with Aws::EC2::Client
78
+ client.class
79
+ # => Aws::EC2::Client
80
+ repo = AwsRo::EC2::Repository.new(client)
81
+
82
+ # initialize with option hash
83
+ repo = AwsRo::EC2::Repository.new({ region: 'ap-northeast-1' })
84
+ ```
85
+
86
+ ##### accessor
87
+
88
+ ```ruby
89
+ # get raw client
90
+ repo.client
91
+ # => Aws::EC2::Client
92
+ ```
93
+
94
+ ##### query methods
95
+
96
+ ```ruby
97
+ # all running instance
98
+ repo.running.all? do |i|
99
+ i.class
100
+ # => AwsRo::EC2::Instance
101
+ i.state.name == 'running'
102
+ end
103
+ # => true
104
+
105
+ # All query methods are chainable and return an `Array` like object.
106
+ ## list security group of running-public-instances
107
+ repo.running.filters([{name: 'ip-address', values: ['*'] }]).each do |i|
108
+ puts i.ec2.security_groups.map(&:group_name)}
109
+ end
110
+
111
+ ## list my 'InUse' tag instances
112
+ repo.not_terminated.tags('InUse' => '*').map(&:instance_id)
113
+ # => ["i-xxxxxxxx","i-yyyyyyyy","i-zzzzzzzz", ...]
114
+ ```
115
+
116
+ #### class : `AwsRo::EC2::Instance`
117
+
118
+ Instance is wrapper of EC2 instace object.
119
+
120
+ ```ruby
121
+ ins = repo.all.first
122
+ ```
123
+
124
+ ##### static accessors
125
+
126
+ ```ruby
127
+ # get raw ec2 object
128
+ ins.ec2
129
+ # => Aws::EC2::Instance
130
+
131
+ # some delegated methods and aliases for accessibility
132
+ [
133
+ ins.instance_id == ins.ec2.instance_id,
134
+ ins.public_ip_address == ins.ec2.public_ip_address && ins.public_ip_address == ins.public_ip,
135
+ ins.private_ip_address == ins.ec2.private_ip_address && ins.private_ip == ins.private_ip,
136
+ ins.key_name == ins.ec2.key_name,
137
+ ins.state == ins.ec2.state
138
+ ].all?
139
+ # => true
140
+ ```
141
+
142
+ ##### dynamic tag accessors
143
+
144
+ ```ruby
145
+ # Instance#tags returns `Struct` of tags.
146
+ ins.tags.name
147
+ # => 'Value of Name tag'
148
+ ins.tags.roles
149
+ # => 'staging, somerole'
150
+ ins.tags.xyz_enabled
151
+ # => 'True'
152
+
153
+ ## and dynamically-defined reader methods are available as snake_case-ed tag name.
154
+ ## their values are stripped and formatted as `Array` if the value include whitespaces,
155
+ ## as boolean if the value like boolean and when use `?`-terminated method.
156
+ ins.name
157
+ # => 'Value of Name tag'
158
+ ins.roles
159
+ # => ['staging', 'somerole']
160
+ ins.xyz_enabled?
161
+ # => true
162
+ ```
163
+
164
+ ## Development
165
+
166
+ After checking out the repo`, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
167
+
168
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
169
+
170
+ ## Contributing
171
+
172
+ 1. Fork it ( https://github.com/gree/aws_ro/fork )
173
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
174
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
175
+ 4. Push to the branch (`git push origin my-new-feature`)
176
+ 5. Create a new Pull Request
177
+
178
+ ## License
179
+
180
+ This library is distributed under the Apache License, version 2.0
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/aws_ro.gemspec ADDED
@@ -0,0 +1,41 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'aws_ro/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "aws_ro"
8
+ spec.version = AwsRo::VERSION
9
+ spec.authors = ["takuto.komazaki"]
10
+ spec.email = ["takuto.komazaki@gree.net"]
11
+
12
+ spec.summary = %q{Wrpper library of AWS SDK objects.}
13
+ spec.description = <<-EOS
14
+ Wrpper library of AWS SDK objects to enable to access properties
15
+ more easily, more ruby-likely.
16
+ EOS
17
+ spec.homepage = "https://github.com/gree/aws_ro"
18
+
19
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
20
+ # delete this section to allow pushing this gem to any host.
21
+ # if spec.respond_to?(:metadata)
22
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
23
+ # else
24
+ # raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
25
+ # end
26
+
27
+ spec.required_ruby_version = '>= 1.9.3'
28
+
29
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
30
+ spec.bindir = "exe"
31
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ["lib"]
33
+
34
+ spec.add_dependency "aws-sdk", "~> 2.0"
35
+ spec.add_development_dependency "bundler", "~> 1.9"
36
+ spec.add_development_dependency "rake", "~> 10.0"
37
+ spec.add_development_dependency "rspec", "~> 3.0"
38
+ spec.add_development_dependency "yard"
39
+ spec.add_development_dependency "guard-rspec"
40
+ spec.add_development_dependency "pry"
41
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "aws_ro"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/lib/aws_ro.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'aws_ro/ec2'
2
+ require 'aws_ro/elastic_load_balancing'
3
+
4
+ module AwsRo
5
+ end
data/lib/aws_ro/ec2.rb ADDED
@@ -0,0 +1,8 @@
1
+ require "aws_ro/ec2/instance"
2
+ require "aws_ro/ec2/repository"
3
+
4
+ module AwsRo
5
+ module EC2
6
+ # Your code goes here...
7
+ end
8
+ end
@@ -0,0 +1,38 @@
1
+ require 'aws_ro/ec2/key_value_accessible'
2
+
3
+ module AwsRo
4
+ module EC2
5
+ class Instance
6
+ extend Forwardable
7
+ def_delegators :@ec2_instance, :instance_id, :private_ip_address, :public_ip_address, :key_name, :state
8
+
9
+ attr_reader :ec2_instance, :tags
10
+ include KeyValueAccessible
11
+ alias :ec2 :ec2_instance
12
+ alias :id :instance_id
13
+ alias :private_ip :private_ip_address
14
+ alias :public_ip :public_ip_address
15
+
16
+ def initialize(ec2_instance)
17
+ @ec2_instance = ec2_instance
18
+ if @ec2_instance.respond_to?(:tags)
19
+ tags = format_ec2_tags(@ec2_instance.tags)
20
+ define_custom_accessors_unless_conflict(tags)
21
+ @tags = Struct.new(*tags.keys)[*tags.values] unless tags.empty?
22
+ end
23
+ end
24
+
25
+ def running?
26
+ state.name == 'running'
27
+ end
28
+
29
+ private
30
+
31
+ def format_ec2_tags(ec2_tags)
32
+ ec2_tags.inject({}) do |hash, tag|
33
+ hash.tap { |h| h[tag.key.to_sym] = tag.value }
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,67 @@
1
+ module AwsRo
2
+ module EC2
3
+ module KeyValueAccessible
4
+ private
5
+
6
+ def define_custom_accessors_unless_conflict(keys_values)
7
+ keys_values.each do |k, v|
8
+ key = underscore(k.to_s).to_sym
9
+ unless instance_method_conflict?(key)
10
+ define_reader_method(key, to_array_if_include_separator(v))
11
+ end
12
+
13
+ if like_a_boolean_value?(v) && (not instance_method_conflict?("#{key}?"))
14
+ define_reader_method("#{key}?", to_boolean(v))
15
+ end
16
+ end
17
+ end
18
+
19
+ def instance_method_conflict?(sym)
20
+ self.class.instance_methods(false).include?(sym.to_sym)
21
+ end
22
+
23
+ def like_a_boolean_value?(value)
24
+ val = value.is_a?(String) ? value.strip : value
25
+ ['True', 'False', 'true', 'false', true, false].include? val
26
+ end
27
+
28
+ def define_reader_method(sym, value)
29
+ define_singleton_method(sym) { value }
30
+ end
31
+
32
+ def to_boolean(val)
33
+ case val
34
+ when /[Ff]alse/; then false
35
+ else true
36
+ end
37
+ end
38
+
39
+ def to_array_if_include_separator(val)
40
+ return val unless val.is_a? String
41
+ if val.include?(KeyValueAccessible.tag_separator)
42
+ val.split(KeyValueAccessible.tag_separator).map(&:strip)
43
+ else
44
+ val.strip
45
+ end
46
+ end
47
+
48
+ def underscore(str)
49
+ str.gsub(/::/, '/').
50
+ gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
51
+ gsub(/([a-z\d])([A-Z])/, '\1_\2').
52
+ tr("-", "_").
53
+ downcase
54
+ end
55
+
56
+ class << self
57
+ def tag_separator=(char)
58
+ @separator = char.to_s
59
+ end
60
+
61
+ def tag_separator
62
+ @separator ||= ' '
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,119 @@
1
+ require 'aws-sdk'
2
+
3
+ module AwsRo
4
+ module EC2
5
+ class Repository
6
+ # Return `Aws::EC2::Client` instance.
7
+ # @return [Aws::EC2::Client]
8
+ attr_reader :client
9
+
10
+ # @param [Hash|Aws::EC2::Client] client_or_options
11
+ def initialize(client_or_options = {})
12
+ @client = if client_or_options.is_a? Aws::EC2::Client
13
+ client_or_options
14
+ else
15
+ Aws::EC2::Client.new(client_or_options)
16
+ end
17
+ end
18
+
19
+ # @return [Array(AwsRo::EC2::Instance)]
20
+ def all
21
+ filters([]).to_a
22
+ end
23
+
24
+ # Wrapper query of `filters: [{ name: "tag:HASH_KEY", values: [HASH_VALUES] }]`
25
+ # @param [Hash] filter_hash
26
+ # @example
27
+ # repo.tags({'Name' => 'my instance'})
28
+ # @return [AwsRo::EC2::Repository::Relation]
29
+ def tags(filter_hash = {})
30
+ Relation.new(self).tags(filter_hash)
31
+ end
32
+
33
+ # @return [AwsRo::EC2::Repository::Relation]
34
+ def running
35
+ Relation.new(self).running
36
+ end
37
+
38
+ # @return [AwsRo::EC2::Repository::Relation]
39
+ def not_terminated
40
+ Relation.new(self).not_terminated
41
+ end
42
+
43
+ # @return [AwsRo::EC2::Repository::Relation]
44
+ def filters(filters)
45
+ Relation.new(self).filters(filters)
46
+ end
47
+
48
+ # @return [AwsRo::EC2::Repository::Relation]
49
+ def instance_ids(ids)
50
+ Relation.new(self).instance_ids(ids)
51
+ end
52
+
53
+ class Relation
54
+ extend Forwardable
55
+ include Enumerable
56
+ array_methods =
57
+ (Array.instance_methods - Enumerable.instance_methods - Object.instance_methods)
58
+ def_delegators :to_a, *array_methods
59
+
60
+ def initialize(klass)
61
+ @klass = klass
62
+ @filters = []
63
+ @instance_ids = []
64
+ end
65
+
66
+ def instance_ids(ids)
67
+ @instance_ids += ids
68
+ self
69
+ end
70
+
71
+ def tags(filter_hash = {})
72
+ @filters += hash_to_tags_array(filter_hash)
73
+ self
74
+ end
75
+
76
+ def filters(filters)
77
+ @filters += filters
78
+ self
79
+ end
80
+
81
+ def running
82
+ @filters << { name: 'instance-state-name', values: ['running'] }
83
+ self
84
+ end
85
+
86
+ def not_terminated
87
+ @filters << { name: 'instance-state-name',
88
+ values: ['pending', 'running', 'shutting-down', 'stopping', 'stopped'] }
89
+ self
90
+ end
91
+
92
+ def to_a
93
+ fetch
94
+ end
95
+ alias :force :to_a
96
+ alias :inspect :to_a
97
+
98
+ private
99
+
100
+ def hash_to_tags_array(hash)
101
+ hash.map do |k, v|
102
+ { name: "tag:#{k}", values: Array(v) }
103
+ end
104
+ end
105
+
106
+ def fetch
107
+ filter_param = @filters.empty? ? nil : @filters.uniq
108
+ id_param = @instance_ids.empty? ? nil : @instance_ids.uniq
109
+ @instances = @klass.client.describe_instances(instance_ids: id_param,
110
+ filters: filter_param).inject([]) do |all, page|
111
+ all + page.reservations.map(&:instances).flatten.map do |ec2_instance|
112
+ AwsRo::EC2::Instance.new(ec2_instance)
113
+ end
114
+ end
115
+ end
116
+ end
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,7 @@
1
+ require 'aws_ro/elastic_load_balancing/load_balancer'
2
+ require 'aws_ro/elastic_load_balancing/repository'
3
+
4
+ module AwsRo
5
+ module ElasticLoadBalancing
6
+ end
7
+ end
@@ -0,0 +1,41 @@
1
+ require 'aws_ro/ec2'
2
+
3
+ module AwsRo
4
+ module ElasticLoadBalancing
5
+ class LoadBalancer
6
+ extend Forwardable
7
+ def_delegators :@load_balancer, :load_balancer_name, :vpc_id, :health_check
8
+ attr_reader :load_balancer, :instance_states
9
+ attr_accessor :ec2_repository
10
+ alias elb load_balancer
11
+ alias name load_balancer_name
12
+
13
+ def initialize(load_balancer, ec2_repository = nil)
14
+ @load_balancer = load_balancer
15
+ @ec2_repository = ec2_repository
16
+ end
17
+
18
+ def instances
19
+ unless ec2_repository.respond_to?(:instance_ids)
20
+ fail "Cannot use AwsRo::EC2::Repository"
21
+ end
22
+ @instances ||= elb.instances.empty? ? [] : ec2_repository.instance_ids(instance_ids)
23
+ end
24
+
25
+ def instance_ids
26
+ elb.instances.map(&:instance_id)
27
+ end
28
+
29
+ def health(instance_id)
30
+ fail "Empty instance_states." if instance_states.nil?
31
+ instance_states[instance_id.to_s]
32
+ end
33
+
34
+ def store_instance_states(instance_states)
35
+ @instance_states = instance_states.each_with_object({}) do |state, hash|
36
+ hash[state.instance_id] = state
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,40 @@
1
+ require 'aws-sdk'
2
+ require 'aws_ro/elastic_load_balancing/load_balancer'
3
+ require 'aws_ro/ec2/repository'
4
+
5
+ module AwsRo
6
+ module ElasticLoadBalancing
7
+ class Repository
8
+ attr_reader :client
9
+
10
+ def initialize(client_or_options)
11
+ @client = if client_or_options.is_a? Aws::ElasticLoadBalancing::Client
12
+ client_or_options
13
+ else
14
+ Aws::ElasticLoadBalancing::Client.new(client_or_options)
15
+ end
16
+ end
17
+
18
+ def ec2_repository
19
+ @ec2_repository ||= AwsRo::EC2::Repository.new(
20
+ region: client.config.region,
21
+ credentials: client.config.credentials
22
+ )
23
+ end
24
+
25
+ def all
26
+ client.describe_load_balancers.each_with_object([]) do |page, arr|
27
+ page.load_balancer_descriptions.each do |elb|
28
+ lb = LoadBalancer.new(elb, ec2_repository)
29
+ lb.store_instance_states(client.describe_instance_health(load_balancer_name: lb.name).instance_states)
30
+ arr << lb
31
+ end
32
+ end
33
+ end
34
+
35
+ def find_by_name(name)
36
+ client.describe_load_balancers(load_balancer_names: [name]).first
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,3 @@
1
+ module AwsRo
2
+ VERSION = "1.1.3"
3
+ end
metadata ADDED
@@ -0,0 +1,164 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aws_ro
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.3
5
+ platform: ruby
6
+ authors:
7
+ - takuto.komazaki
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-03-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aws-sdk
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.9'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.9'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: yard
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: guard-rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: |
112
+ Wrpper library of AWS SDK objects to enable to access properties
113
+ more easily, more ruby-likely.
114
+ email:
115
+ - takuto.komazaki@gree.net
116
+ executables: []
117
+ extensions: []
118
+ extra_rdoc_files: []
119
+ files:
120
+ - ".gitignore"
121
+ - ".rspec"
122
+ - ".rubocop.yml"
123
+ - Gemfile
124
+ - Guardfile
125
+ - LICENSE.txt
126
+ - README.md
127
+ - Rakefile
128
+ - aws_ro.gemspec
129
+ - bin/console
130
+ - bin/setup
131
+ - lib/aws_ro.rb
132
+ - lib/aws_ro/ec2.rb
133
+ - lib/aws_ro/ec2/instance.rb
134
+ - lib/aws_ro/ec2/key_value_accessible.rb
135
+ - lib/aws_ro/ec2/repository.rb
136
+ - lib/aws_ro/elastic_load_balancing.rb
137
+ - lib/aws_ro/elastic_load_balancing/load_balancer.rb
138
+ - lib/aws_ro/elastic_load_balancing/repository.rb
139
+ - lib/aws_ro/version.rb
140
+ homepage: https://github.com/gree/aws_ro
141
+ licenses: []
142
+ metadata: {}
143
+ post_install_message:
144
+ rdoc_options: []
145
+ require_paths:
146
+ - lib
147
+ required_ruby_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: 1.9.3
152
+ required_rubygems_version: !ruby/object:Gem::Requirement
153
+ requirements:
154
+ - - ">="
155
+ - !ruby/object:Gem::Version
156
+ version: '0'
157
+ requirements: []
158
+ rubyforge_project:
159
+ rubygems_version: 2.5.1
160
+ signing_key:
161
+ specification_version: 4
162
+ summary: Wrpper library of AWS SDK objects.
163
+ test_files: []
164
+ has_rdoc: