aws-dev-utils 1.4.4 → 1.4.9
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 +4 -4
- data/LICENSE +21 -0
- data/README.md +166 -0
- data/lib/aws-dev-utils/backends/memory.rb +5 -0
- data/lib/aws-dev-utils/backends/redis.rb +3 -3
- data/lib/aws-dev-utils/cache.rb +12 -1
- data/lib/aws-dev-utils/cache_wrapper.rb +12 -8
- data/lib/aws-dev-utils/client_wrapper.rb +11 -1
- data/lib/aws-dev-utils/next_token_wrapper.rb +4 -4
- data/lib/aws-dev-utils/retry_wrapper.rb +3 -0
- data/lib/aws-dev-utils/utils.rb +4 -0
- data/lib/aws-dev-utils/version.rb +1 -1
- metadata +9 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4687e761d49ec6e0b5c4c49e8a0eb2bbca0505940b309c5b62eb2b82413645d6
|
4
|
+
data.tar.gz: 6916a3900eba9cef3409f86a0ec7eb0984eb0b631c18a1b18f617ad49070c6f9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 472cbb329917024f484fc736d7226b5e4e6207ec3d9223e4df1d9c5a619a882f7dfcbc612a26e87a689ad3b781a75bfb36bc47410bf88a84b4381be9317a0a7d
|
7
|
+
data.tar.gz: d2403e51af30fb2c4205305fbe8c258b03bbcb1cd080ffa726f20ffa6006ac9b0ff731af0f3a991f0cf8c5da93f3d95c5e9c402db25051d4a7e075f240edc4d5
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2019 Amobee BI
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,166 @@
|
|
1
|
+
# AWS Dev Utilities [](https://travis-ci.org/kontera-technologies/aws-dev-utils) [](https://codecov.io/gh/kontera-technologies/aws-dev-utils)
|
2
|
+
|
3
|
+
This library provides common ruby utilities to working with AWS SDK. It simplifies the work by reducing common boilerplates such as "next_token" pagination and "retry".
|
4
|
+
It provides a general API Wrapper with Redis based caching, paging and retry functionalities.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
AWS Dev Utilities is available from RubyGems.
|
8
|
+
|
9
|
+
### Option 1: Via Bundler
|
10
|
+
Add the following to your application's Gemfile:
|
11
|
+
```ruby
|
12
|
+
gem 'aws-dev-utils', '~> 1.4'
|
13
|
+
```
|
14
|
+
And then execute:
|
15
|
+
```
|
16
|
+
$ bundle install
|
17
|
+
```
|
18
|
+
### Option 2: Via `gem install`
|
19
|
+
Execute the following inside your application directory:
|
20
|
+
```
|
21
|
+
$ gem install aws-dev-utils -v '~> 1.4'
|
22
|
+
```
|
23
|
+
## Usage
|
24
|
+
Add the following at the top of each Ruby script, or in each module/class you want to use the `with_next_token`, `with_cache` or `with_retry` functions:
|
25
|
+
```ruby
|
26
|
+
require 'aws-dev-utils'
|
27
|
+
using AwsDevUtils::Refinements
|
28
|
+
```
|
29
|
+
|
30
|
+
### with_next_token
|
31
|
+
Many AWS operations limit the number of results returned with each response. To make it easy to get the next page of results, every AWS response object is enumerable.
|
32
|
+
This functionality is rarely needed and causes a lot of boilerplate code.
|
33
|
+
Using the `client.with_next_token` the paged results will be collected for you.
|
34
|
+
The AWS client function and its results will be concatenated until either no more results are available or the max number of requests is reached.
|
35
|
+
_`with_next_token` works with APIs that use `next_token`, `next_marker` or `next_continuation_token`._
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
require 'aws-dev-utils'
|
39
|
+
using AwsDevUtils::Refinements
|
40
|
+
|
41
|
+
ec2_client = Aws::EC2::Client.new
|
42
|
+
# To get the full set of results:
|
43
|
+
ec2_client.
|
44
|
+
with_next_token.
|
45
|
+
describe_instances(filters:[{ name: "vpc-id", values: ["vpc-foo"]}])
|
46
|
+
|
47
|
+
# To get up to 5 pages of results:
|
48
|
+
ec2_client.
|
49
|
+
with_next_token(5).
|
50
|
+
describe_instances(filters:[{ name: "vpc-id", values: ["vpc-foo"]}])
|
51
|
+
```
|
52
|
+
|
53
|
+
### with_cache
|
54
|
+
In many cases a cache is required due to many processes or different machines querying the AWS api for data.
|
55
|
+
By using the `client.with_cache`, the client functions results will be stored in cache.
|
56
|
+
The default cache is a simple in memory *time based* store - `AwsDevUtils::Backend::Memory`.
|
57
|
+
Incase in-memory cache is not a good solution then there is also `Redis` based caching (`AwsDevUtils::Backend::Redis`).
|
58
|
+
To use it, start a Redis instance and set it as the global cache backend:
|
59
|
+
```ruby
|
60
|
+
AwsDevUtils::Cache.instance.backend = AwsDevUtils::Backend::Redis.new("redis-url")
|
61
|
+
```
|
62
|
+
|
63
|
+
_Note: Data will be restored from the cache only if it is the same request to AWS client, the same filters and the same expiration time ._
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
# Memory Caching.
|
67
|
+
require 'aws-dev-utils'
|
68
|
+
using AwsDevUtils::Refinements
|
69
|
+
|
70
|
+
ec2_client = Aws::EC2::Client.new
|
71
|
+
ec2_client.
|
72
|
+
with_cache.
|
73
|
+
describe_instances(filters:[{ name: "vpc-id", values: ["vpc-foo"]}])
|
74
|
+
```
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
# Redis based caching with default experiation time (60 seconds).
|
78
|
+
require 'aws-dev-utils'
|
79
|
+
using AwsDevUtils::Refinements
|
80
|
+
|
81
|
+
AwsDevUtils::Cache.instance.backend = AwsDevUtils::Backend::Redis.new("redis-url")
|
82
|
+
ec2_client.
|
83
|
+
with_cache.
|
84
|
+
describe_instances(filters:[{ name: "vpc-id", values: ["vpc-foo"]}])
|
85
|
+
```
|
86
|
+
|
87
|
+
```ruby
|
88
|
+
# Redis based caching with custom expiration time (10 minutes).
|
89
|
+
require 'aws-dev-utils'
|
90
|
+
using AwsDevUtils::Refinements
|
91
|
+
|
92
|
+
AwsDevUtils::Cache.instance.backend = AwsDevUtils::Backend::Redis.new("redis-url")
|
93
|
+
ec2_client.
|
94
|
+
with_cache(600).
|
95
|
+
describe_instances(filters:[{ name: "vpc-id", values: ["vpc-foo"]}])
|
96
|
+
```
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
# Example of different cache keys from requests.
|
100
|
+
require 'aws-dev-utils'
|
101
|
+
using AwsDevUtils::Refinements
|
102
|
+
|
103
|
+
AwsDevUtils::Cache.instance.backend = AwsDevUtils::Backend::Redis.new("redis-url")
|
104
|
+
|
105
|
+
# No cache data so request goes to AWS servers.
|
106
|
+
foo_1_instances = ec2_client.
|
107
|
+
with_cache(600).
|
108
|
+
describe_instances(filters:[{ name: "vpc-id", values: ["vpc-foo"]}])
|
109
|
+
|
110
|
+
# Different filters than foo_1_instances so request goes to AWS servers.
|
111
|
+
bar_1_instances = ec2_client.
|
112
|
+
with_cache(600).
|
113
|
+
describe_instances(filters:[{ name: "vpc-id", values: ["vpc-bar"]}])
|
114
|
+
|
115
|
+
# Same filters as bar_1_instances, so result is fetched from cache.
|
116
|
+
bar_2_instances = ec2_client.
|
117
|
+
with_cache(600).
|
118
|
+
describe_instances(filters:[{ name: "vpc-id", values: ["vpc-bar"]}])
|
119
|
+
```
|
120
|
+
|
121
|
+
### with_retry
|
122
|
+
By using the `client.with_retry`, the client functions will be retried in case of an exception (until the max number of retries is reached - the default is 5).
|
123
|
+
Before each retry the client will sleep for an increasing number of seconds (implements exponential backoff)
|
124
|
+
```ruby
|
125
|
+
require 'aws-dev-utils'
|
126
|
+
using AwsDevUtils::Refinements
|
127
|
+
|
128
|
+
ec2_client = Aws::EC2::Client.new
|
129
|
+
# Retry 5 times (default):
|
130
|
+
ec2_client.
|
131
|
+
with_retry.
|
132
|
+
describe_instances(filters:[{ name: "vpc-id", values: ["vpc-foo"]}])
|
133
|
+
|
134
|
+
# Custom retries:
|
135
|
+
ec2_client.
|
136
|
+
with_retry(3).
|
137
|
+
describe_instances(filters:[{ name: "vpc-id", values: ["vpc-foo"]}])
|
138
|
+
```
|
139
|
+
|
140
|
+
### Combinations of wrappers
|
141
|
+
It is possible to combine between each of the wrappers:
|
142
|
+
```ruby
|
143
|
+
require 'aws-dev-utils'
|
144
|
+
using AwsDevUtils::Refinements
|
145
|
+
|
146
|
+
ec2_client = Aws::EC2::Client.new
|
147
|
+
#To retry 5 times:
|
148
|
+
ec2_client.
|
149
|
+
with_retry.
|
150
|
+
with_next_token.
|
151
|
+
with_cache(600)
|
152
|
+
describe_instances(filters:[{ name: "vpc-id", values: ["vpc-foo"]}])
|
153
|
+
```
|
154
|
+
|
155
|
+
## Contributing
|
156
|
+
|
157
|
+
### Releasing a new version
|
158
|
+
The `./release-new-version.sh` script will bump the version number. By default it will
|
159
|
+
increase by 0.0.1, but you can pass `minor` or `major` to increase it to the next
|
160
|
+
minor or major version accordingly.
|
161
|
+
|
162
|
+
The script will also create a git commit for the version bump, push it to this
|
163
|
+
GitHub repository and upload it to [rubygems.org](https://rubygems.org).
|
164
|
+
|
165
|
+
### Bugs and PRs
|
166
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/kontera-technologies/aws-dev-utils
|
@@ -6,11 +6,16 @@ module AwsDevUtils
|
|
6
6
|
@hash = {}
|
7
7
|
end
|
8
8
|
|
9
|
+
# Get the value of key, if not found, returns nil.
|
9
10
|
def get key
|
10
11
|
clean_cache!
|
11
12
|
@hash[key][1]
|
12
13
|
end
|
13
14
|
|
15
|
+
# Set key to hold the value and set key to timeout after the a given expiration time(in seconds).
|
16
|
+
# @param key [Object]
|
17
|
+
# @param value [Object]
|
18
|
+
# @param exp [Integer] - the key-value timeout
|
14
19
|
def set key, value, exp
|
15
20
|
clean_cache!
|
16
21
|
@hash[key] = [Time.now + exp, value]
|
@@ -4,13 +4,13 @@ module AwsDevUtils
|
|
4
4
|
module Backend
|
5
5
|
class Redis
|
6
6
|
# :nocov:
|
7
|
-
# Initialize a new redis client
|
8
|
-
# @
|
7
|
+
# Initialize a new redis client.
|
8
|
+
# @params url [String] - specify redis url connection
|
9
9
|
def initialize url='redis://localhost:6379'
|
10
10
|
@redis = ::Redis.new(url: url)
|
11
11
|
end
|
12
12
|
|
13
|
-
# Get the value of key
|
13
|
+
# Get the value of key, if not found, returns nil.
|
14
14
|
def get key
|
15
15
|
@redis.get key
|
16
16
|
end
|
data/lib/aws-dev-utils/cache.rb
CHANGED
@@ -5,7 +5,7 @@ module AwsDevUtils
|
|
5
5
|
class Cache
|
6
6
|
include Singleton
|
7
7
|
|
8
|
-
#
|
8
|
+
# Injectable backend.
|
9
9
|
attr_writer :backend
|
10
10
|
|
11
11
|
class << self
|
@@ -13,14 +13,25 @@ module AwsDevUtils
|
|
13
13
|
def_delegators :fetch
|
14
14
|
end
|
15
15
|
|
16
|
+
# Returns a value from the cache for the given key.
|
17
|
+
# If the key can't be found, the block will be run and its result returned and be set in the cache.
|
18
|
+
# @param key [Object]
|
19
|
+
# @param exp [Integer]
|
20
|
+
# @param block [block] - called with no arguments, and returns the new value for the cache (if no cached data is found).
|
21
|
+
# @return [Object] the value from the cache or the result of the block
|
16
22
|
def fetch key, exp=60, &block
|
17
23
|
get(key) or block.().tap {|x| set(key, x, exp)}
|
18
24
|
end
|
19
25
|
|
26
|
+
# Get the value of key. If not found, returns nil
|
20
27
|
def get key
|
21
28
|
deserialize backend.get key.to_s rescue nil
|
22
29
|
end
|
23
30
|
|
31
|
+
# Set key to hold the value and set key to timeout after the a given expiration time(in seconds).
|
32
|
+
# @param key [Object]
|
33
|
+
# @param value [Object]
|
34
|
+
# @param expiration [Time,Integer] - the key-value timeout
|
24
35
|
def set key, value, expiration
|
25
36
|
backend.set key.to_s, serialize(value), expiration rescue nil
|
26
37
|
end
|
@@ -4,13 +4,13 @@ module AwsDevUtils
|
|
4
4
|
class CacheWrapper
|
5
5
|
include AwsDevUtils::Utils
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
def initialize client, exp=60
|
7
|
+
# Initialize a new CacheWrapper, internal use only.
|
8
|
+
# @param client [Aws client, NextTokenWrapper, RetryWrapper]
|
9
|
+
# @param exp [Integer] - the key-value timeout
|
10
|
+
def initialize client, exp=60, client_name: nil
|
12
11
|
@client = client
|
13
12
|
@exp = exp
|
13
|
+
@client_name = client_name || client.class.name
|
14
14
|
end
|
15
15
|
|
16
16
|
def method_missing m, *args, &block
|
@@ -19,9 +19,13 @@ module AwsDevUtils
|
|
19
19
|
|
20
20
|
private
|
21
21
|
|
22
|
-
def
|
23
|
-
|
24
|
-
|
22
|
+
def cache_key m, params
|
23
|
+
[@client_name, m, params.deep_sort, @exp]
|
24
|
+
end
|
25
|
+
|
26
|
+
def do_call m, params
|
27
|
+
Cache.instance.fetch(cache_key(m, params), @exp) {
|
28
|
+
nested_hash(@client.send(m, params))
|
25
29
|
}
|
26
30
|
end
|
27
31
|
|
@@ -2,27 +2,37 @@ module AwsDevUtils
|
|
2
2
|
class ClientWrapper
|
3
3
|
include AwsDevUtils::Utils
|
4
4
|
|
5
|
+
# Initialize a new ClientWrapper, internal use only
|
6
|
+
# @params [Seahorse::Client::Base] client
|
7
|
+
# @param [Hash] options
|
8
|
+
# @option options [String] next_token max number of requests
|
9
|
+
# @option options [String] retry max number of retries
|
10
|
+
# @option options [String] cache the key-value timeout
|
5
11
|
def initialize client, options={}
|
6
12
|
@client = client
|
7
13
|
@options = options
|
8
14
|
end
|
9
15
|
|
16
|
+
# @return ClientWrapper with next_token option
|
10
17
|
def with_next_token max=100
|
11
18
|
self.class.new(@client, @options.merge(next_token: max))
|
12
19
|
end
|
13
20
|
|
21
|
+
# @return ClientWrapper with retry option
|
14
22
|
def with_retry max=5
|
15
23
|
self.class.new(@client, @options.merge(retry: max))
|
16
24
|
end
|
17
25
|
|
26
|
+
# @return ClientWrapper with cache option
|
18
27
|
def with_cache exp=60
|
19
28
|
self.class.new(@client, @options.merge(cache: exp))
|
20
29
|
end
|
21
30
|
|
22
31
|
def method_missing m, *args, &block
|
32
|
+
client_name = @client.class.name
|
23
33
|
@client = RetryWrapper.new(@client, @options[:retry]) if retry?
|
24
34
|
@client = NextTokenWrapper.new(@client, @options[:next_token]) if next_token?
|
25
|
-
@client = CacheWrapper.new(@client, @options[:cache]) if cache?
|
35
|
+
@client = CacheWrapper.new(@client, @options[:cache], client_name: client_name) if cache?
|
26
36
|
|
27
37
|
nested_struct(@client.send(m, *args, &block))
|
28
38
|
end
|
@@ -1,10 +1,10 @@
|
|
1
1
|
module AwsDevUtils
|
2
2
|
class NextTokenWrapper
|
3
3
|
|
4
|
-
# Initialize a new NextTokenWrapper
|
5
|
-
# @params client
|
6
|
-
# @param [Integer]
|
7
|
-
def initialize client, max=100
|
4
|
+
# Initialize a new NextTokenWrapper, internal use only
|
5
|
+
# @params client [Aws client / NextTokenWrapper / RetryWrapper]
|
6
|
+
# @param max [Integer] - max number of requests
|
7
|
+
def initialize client, max=100
|
8
8
|
@client = client
|
9
9
|
@max = max
|
10
10
|
end
|
@@ -1,6 +1,9 @@
|
|
1
1
|
module AwsDevUtils
|
2
2
|
class RetryWrapper
|
3
3
|
|
4
|
+
# Initialize a new RetryWrapper, internal use only
|
5
|
+
# @params client [Aws client, NextTokenWrapper, RetryWrapper]
|
6
|
+
# @param max_tries [Integer] - max number of retries
|
4
7
|
def initialize client, max_tries=5
|
5
8
|
@client = client
|
6
9
|
@max_tries = max_tries
|
data/lib/aws-dev-utils/utils.rb
CHANGED
@@ -3,6 +3,8 @@ module AwsDevUtils
|
|
3
3
|
|
4
4
|
module_function
|
5
5
|
|
6
|
+
# Transforms an object to a nested struct.
|
7
|
+
# @return [OpenStruct]
|
6
8
|
def nested_struct obj
|
7
9
|
case obj
|
8
10
|
when Hash
|
@@ -22,6 +24,8 @@ module AwsDevUtils
|
|
22
24
|
end
|
23
25
|
end
|
24
26
|
|
27
|
+
# Transforms an object to a nested hash.
|
28
|
+
# @return [Hash]
|
25
29
|
def nested_hash obj
|
26
30
|
if obj.kind_of? Array
|
27
31
|
obj.map(&method(__method__))
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: aws-dev-utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.4.
|
4
|
+
version: 1.4.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
-
|
8
|
-
autorequire:
|
7
|
+
- Amobee BI Infrastructure
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-06-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: aws-sdk-core
|
@@ -45,6 +45,8 @@ executables: []
|
|
45
45
|
extensions: []
|
46
46
|
extra_rdoc_files: []
|
47
47
|
files:
|
48
|
+
- LICENSE
|
49
|
+
- README.md
|
48
50
|
- lib/aws-dev-utils.rb
|
49
51
|
- lib/aws-dev-utils/backends/memory.rb
|
50
52
|
- lib/aws-dev-utils/backends/redis.rb
|
@@ -60,7 +62,7 @@ homepage: https://github.com/kontera-technologies/aws-dev-utils
|
|
60
62
|
licenses:
|
61
63
|
- MIT
|
62
64
|
metadata: {}
|
63
|
-
post_install_message:
|
65
|
+
post_install_message:
|
64
66
|
rdoc_options: []
|
65
67
|
require_paths:
|
66
68
|
- lib
|
@@ -75,9 +77,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
75
77
|
- !ruby/object:Gem::Version
|
76
78
|
version: '0'
|
77
79
|
requirements: []
|
78
|
-
|
79
|
-
|
80
|
-
signing_key:
|
80
|
+
rubygems_version: 3.0.8
|
81
|
+
signing_key:
|
81
82
|
specification_version: 4
|
82
83
|
summary: Ruby library gem that provides common AWS utilities
|
83
84
|
test_files: []
|