aws-dev-utils 1.4.2

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
+ SHA256:
3
+ metadata.gz: 4253a280398d65c04410f7d15180c33de1c632a8b9b4aa394d4703d9f54ab5cc
4
+ data.tar.gz: d18c4b0b5ce15d900e34fb2233f9c62ef2a33ffc21a5c6abcbb2f8fb9e5a1fbe
5
+ SHA512:
6
+ metadata.gz: 546edb0c812adbdb2fc2cba3c78894df536f2a74f61da97331659feeb428a3cf6f4dd98b04c43f801277a78a134535f1693ed5647986516d2c395c5b4d2e6aa0
7
+ data.tar.gz: 3a3855a3f34204e57369aa0912c1c54c00c8d9e5f35260ea04f0b03c81ad3e0aafa125e4c606b4d7435310a3de28644688b2dc17704c416c1f69f991df202674
@@ -0,0 +1,14 @@
1
+ module AwsDevUtils
2
+ autoload :Refinements, "aws-dev-utils/refinements"
3
+ autoload :NextTokenWrapper, "aws-dev-utils/next_token_wrapper"
4
+ autoload :ClientWrapper, "aws-dev-utils/client_wrapper"
5
+ autoload :RetryWrapper, "aws-dev-utils/retry_wrapper"
6
+ autoload :CacheWrapper, "aws-dev-utils/cache_wrapper"
7
+ autoload :MachineData, "aws-dev-utils/machine_data"
8
+ autoload :Cache, "aws-dev-utils/cache"
9
+ autoload :Utils, "aws-dev-utils/utils"
10
+ module Backend
11
+ autoload :Memory, 'aws-dev-utils/backends/memory'
12
+ autoload :Redis, 'aws-dev-utils/backends/redis'
13
+ end
14
+ end
@@ -0,0 +1,28 @@
1
+ module AwsDevUtils
2
+ module Backend
3
+ class Memory
4
+
5
+ def initialize
6
+ @hash = {}
7
+ end
8
+
9
+ def get key
10
+ clean_cache!
11
+ @hash[key][1]
12
+ end
13
+
14
+ def set key, value, exp
15
+ clean_cache!
16
+ @hash[key] = [Time.now + exp, value]
17
+ end
18
+
19
+ private
20
+
21
+ def clean_cache!
22
+ @hash = @hash.each_with_object({}) do |(k, (exp, v)), acc|
23
+ acc[k] = [exp, v] if Time.now < exp
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,20 @@
1
+ require 'redis'
2
+
3
+ module AwsDevUtils
4
+ module Backend
5
+ class Redis
6
+ # :nocov:
7
+ def initialize url='redis://localhost:6379'
8
+ @redis = ::Redis.new(url: url)
9
+ end
10
+
11
+ def get key
12
+ @redis.get key
13
+ end
14
+
15
+ def set key, value, exp
16
+ @redis.setex key, exp, value
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,43 @@
1
+ require 'singleton'
2
+ require 'forwardable'
3
+
4
+ module AwsDevUtils
5
+ class Cache
6
+ include Singleton
7
+
8
+ #for testing
9
+ attr_writer :backend
10
+
11
+ class << self
12
+ extend Forwardable
13
+ def_delegators :fetch
14
+ end
15
+
16
+ def fetch key, exp=60, &block
17
+ get(key) or block.().tap {|x| set(key, x, exp)}
18
+ end
19
+
20
+ def get key
21
+ deserialize backend.get key.to_s rescue nil
22
+ end
23
+
24
+ def set key, value, expiration
25
+ backend.set key.to_s, serialize(value), expiration rescue nil
26
+ end
27
+
28
+ private
29
+
30
+ def backend
31
+ @backend ||= AwsDevUtils::Backend::Memory.new
32
+ end
33
+
34
+ def serialize obj
35
+ Marshal.dump obj
36
+ end
37
+
38
+ def deserialize obj
39
+ Marshal.load obj
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,25 @@
1
+ require 'deepsort'
2
+
3
+ module AwsDevUtils
4
+ class CacheWrapper
5
+ include AwsDevUtils::Utils
6
+
7
+ def initialize client, exp=60
8
+ @client = client
9
+ @exp = exp
10
+ end
11
+
12
+ def method_missing m, *args, &block
13
+ do_call(m, args.first || {})
14
+ end
15
+
16
+ private
17
+
18
+ def do_call m, args
19
+ Cache.instance.fetch([m, args.deep_sort, @exp], @exp) {
20
+ nested_hash(@client.send(m, args))
21
+ }
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,45 @@
1
+ module AwsDevUtils
2
+ class ClientWrapper
3
+ include AwsDevUtils::Utils
4
+
5
+ def initialize client, options={}
6
+ @client = client
7
+ @options = options
8
+ end
9
+
10
+ def with_next_token max=100
11
+ self.class.new(@client, @options.merge(next_token: max))
12
+ end
13
+
14
+ def with_retry max=5
15
+ self.class.new(@client, @options.merge(retry: max))
16
+ end
17
+
18
+ def with_cache exp=60
19
+ self.class.new(@client, @options.merge(cache: exp))
20
+ end
21
+
22
+ def method_missing m, *args, &block
23
+ @client = RetryWrapper.new(@client, @options[:retry]) if retry?
24
+ @client = NextTokenWrapper.new(@client, @options[:next_token]) if next_token?
25
+ @client = CacheWrapper.new(@client, @options[:cache]) if cache?
26
+
27
+ nested_struct(@client.send(m, *args, &block))
28
+ end
29
+
30
+ private
31
+
32
+ def cache?
33
+ @options[:cache]
34
+ end
35
+
36
+ def retry?
37
+ @options[:retry]
38
+ end
39
+
40
+ def next_token?
41
+ @options[:next_token]
42
+ end
43
+
44
+ end
45
+ end
@@ -0,0 +1,42 @@
1
+ module AwsDevUtils
2
+ class NextTokenWrapper
3
+
4
+ def initialize client, max=100
5
+ @client = client
6
+ @max = max
7
+ end
8
+
9
+ def method_missing m, *args, &block
10
+ do_call(m,*args,&block)
11
+ end
12
+
13
+ private
14
+
15
+ def do_call m, *args, &block
16
+ @client.send(m , *args, &block).to_h.tap do |r|
17
+ i = 1
18
+ key, key2 = extract_keys r
19
+ while(key && r[key] && i < @max) do
20
+ i += 1
21
+ res = @client.send(m, (args[0]||{}).merge(key2 => r[key])).to_h
22
+ res.each { |k,v| r[k] = v.is_a?(Array) ? r[k].concat(v) : v }
23
+ r.delete_if {|k,v| !res.keys.include?(k) }
24
+ end
25
+ r.delete key
26
+ end
27
+ end
28
+
29
+ def extract_keys x
30
+ case
31
+ when x[:next_token]
32
+ [:next_token, :next_token]
33
+ when x[:next_marker]
34
+ [:next_marker, :marker]
35
+ when x[:next_continuation_token]
36
+ [:next_continuation_token, :continuation_token]
37
+ else nil
38
+ end
39
+ end
40
+
41
+ end
42
+ end
@@ -0,0 +1,15 @@
1
+ require 'aws-sdk-core'
2
+
3
+ module AwsDevUtils
4
+ module Refinements
5
+
6
+ refine Seahorse::Client::Base do
7
+ def with_next_token *args
8
+ AwsDevUtils::ClientWrapper.new(self).yield_self { |c| c.send(__callee__, *args) }
9
+ end
10
+ alias_method :with_retry, :with_next_token
11
+ alias_method :with_cache, :with_next_token
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,33 @@
1
+ module AwsDevUtils
2
+ class RetryWrapper
3
+
4
+ def initialize client, max_tries=5
5
+ @client = client
6
+ @max_tries = max_tries
7
+ end
8
+
9
+ def method_missing m, *args, &block
10
+ do_call(m,*args,&block)
11
+ end
12
+
13
+ private
14
+
15
+ def do_call m, *args, &block
16
+ r = e = nil
17
+ tries = 0
18
+ while (r.nil? && tries < @max_tries) do
19
+ begin
20
+ r = @client.send(m, *args, &block)
21
+ rescue Exception => e
22
+ tries += 1
23
+ sleep 2**tries + rand
24
+ end
25
+ end
26
+
27
+ raise e unless e.nil? || r
28
+
29
+ return r
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,42 @@
1
+ module AwsDevUtils
2
+ module Utils
3
+
4
+ module_function
5
+
6
+ def nested_struct obj
7
+ case obj
8
+ when Hash
9
+ obj.each_with_object(OpenStruct.new) do |(k,v), acc|
10
+ acc[k]= case v
11
+ when Hash then send(__method__,v)
12
+ when Array then v.map(&method(__method__))
13
+ else v
14
+ end
15
+ end.
16
+ tap{|x| x.define_singleton_method(:keys){ obj.to_h.keys }}.
17
+ tap{|x| x.define_singleton_method(:values){ AwsDevUtils::Utils.nested_struct obj.to_h.values }}
18
+ when Array
19
+ obj.map(&method(__method__))
20
+ else
21
+ obj
22
+ end
23
+ end
24
+
25
+ def nested_hash obj
26
+ if obj.kind_of? Array
27
+ obj.map(&method(__method__))
28
+ elsif obj.respond_to?(:to_h)
29
+ obj.to_h.each_with_object({}) do |(k,v), acc|
30
+ acc[k] = case v
31
+ when Hash,OpenStruct then send(__method__,v)
32
+ when Array then v.map(&method(__method__))
33
+ else v
34
+ end
35
+ end
36
+ else
37
+ obj
38
+ end
39
+ end
40
+
41
+ end
42
+ end
@@ -0,0 +1,3 @@
1
+ module AwsDevUtils
2
+ VERSION = '1.4.2'
3
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aws-dev-utils
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.4.2
5
+ platform: ruby
6
+ authors:
7
+ - Infastructure
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-07-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aws-sdk-core
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 2.0.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.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: deepsort
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.4.2
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.4.2
41
+ description: ''
42
+ email:
43
+ - infra@amobee.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - lib/aws-dev-utils.rb
49
+ - lib/aws-dev-utils/backends/memory.rb
50
+ - lib/aws-dev-utils/backends/redis.rb
51
+ - lib/aws-dev-utils/cache.rb
52
+ - lib/aws-dev-utils/cache_wrapper.rb
53
+ - lib/aws-dev-utils/client_wrapper.rb
54
+ - lib/aws-dev-utils/next_token_wrapper.rb
55
+ - lib/aws-dev-utils/refinements.rb
56
+ - lib/aws-dev-utils/retry_wrapper.rb
57
+ - lib/aws-dev-utils/utils.rb
58
+ - lib/aws-dev-utils/version.rb
59
+ homepage: https://github.com/kontera-technologies/aws-dev-utils
60
+ licenses:
61
+ - MIT
62
+ metadata: {}
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: 2.5.0
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 2.7.8
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: Ruby library gem that provides common AWS utilities
83
+ test_files: []