hyperb 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 30372b492d04628c4baaa53f4694439eb684ffb9
4
- data.tar.gz: 6a8a68096e76d8d3c6becfceed11f5516b26f7dd
3
+ metadata.gz: c9f045c224b54d0ecc9ee1e45cc89fb352aa5160
4
+ data.tar.gz: c4a8bac272ea63b754689d6e8dcd2f680d117b8c
5
5
  SHA512:
6
- metadata.gz: 866e805b09e1f788c3034dfe459685e11097cf47ab36bdbe7b85f6586e3fec5c88ac8937a6aa2283afcf2e2eb8276c271c59e3a821e817dc538847123c06b1de
7
- data.tar.gz: 8211d6f878ccaec8df7a09d19139cd647b239f9c282d6571f5074a2484792ce78a04a6e04aa5ba3c3c9a8bc0fb0876b0b05d398367f3ca998829aa834cbf4094
6
+ metadata.gz: 1ff12b4b5343c8e441052f51ab771579d7d1355aa3e507d7e89124581c1ea727aaf95f760a571ce04f9735ae01ccadc72a3fb322158774342bd207a59834b06c
7
+ data.tar.gz: 2544f2cd88858f8db54f16689dfaf7cc498282a781f2774ac249d725c913197aac77fc5b71ec58e704f987b895f98e64edbae22ee32cdaf8d4dc0ebf5a45edd1
@@ -28,7 +28,7 @@ Metrics/BlockLength:
28
28
 
29
29
  Metrics/MethodLength:
30
30
  CountComments: false # count full line comments?
31
- Max: 15
31
+ Max: 16
32
32
 
33
33
  Metrics/ModuleLength:
34
34
  Exclude:
data/README.md CHANGED
@@ -37,9 +37,21 @@ Or install it yourself as:
37
37
  Hyper.sh requires you to create credentials on their [dashboard](https://console.hyper.sh/account/credential), after that you can configure your client as following:
38
38
 
39
39
  ```ruby
40
- client = Hyperb::Client.new(access_key: 'ak', secret_key: 'sk')
40
+ client = Hyperb::Client.new(access_key: 'ak', secret_key: 'sk', region: 'us-west-1')
41
41
  ```
42
42
 
43
+ or
44
+
45
+ ```ruby
46
+ client = Hyperb::Client.new do |client|
47
+ client.secret_key = 'secret_key',
48
+ client.access_key = 'access_key',
49
+ client.region = 'eu-central-1'
50
+ end
51
+ ```
52
+
53
+ If `region` is not set, `us-west-1` is set by default.
54
+
43
55
  ## Usage Examples
44
56
 
45
57
  After configuring a `client`, you can run the following examples.
@@ -25,6 +25,7 @@ Gem::Specification.new do |spec|
25
25
  spec.add_development_dependency 'simplecov', '~> 0.9'
26
26
  spec.add_development_dependency 'rspec', '~> 3.0'
27
27
  spec.add_development_dependency 'pry', '~> 0.10.4'
28
+ spec.add_development_dependency 'pry-byebug'
28
29
 
29
30
  spec.required_ruby_version = '>= 2.2'
30
31
  end
@@ -1,19 +1,42 @@
1
1
  require 'hyperb/api'
2
+ require 'hyperb/error'
2
3
 
3
4
  module Hyperb
4
5
  # client class
5
6
  class Client
6
7
  include Hyperb::API
7
8
 
8
- attr_accessor :secret_key, :access_key
9
+ REGIONS = [
10
+ 'us-west-1',
11
+ 'eu-central-1'
12
+ ].freeze
13
+
14
+ attr_accessor :secret_key, :access_key, :region
9
15
 
10
16
  def initialize(options = {})
11
17
  options.each do |key, value|
12
18
  instance_variable_set("@#{key}", value)
13
19
  end
20
+ validate_and_set_region
14
21
  yield(self) if block_given?
15
22
  end
16
23
 
24
+ def validate_and_set_region
25
+ if @region.nil?
26
+ @region = default_region
27
+ else
28
+ raise Hyperb::Error::UnsupportedRegion, @region.to_s unless supported_region?(@region)
29
+ end
30
+ end
31
+
32
+ def default_region
33
+ REGIONS.first
34
+ end
35
+
36
+ def supported_region?(region)
37
+ REGIONS.include?(region.to_s)
38
+ end
39
+
17
40
  def credentials
18
41
  {
19
42
  secret_key: secret_key,
@@ -22,7 +22,7 @@ module Hyperb
22
22
  # @returns [Hash]
23
23
  def fmt
24
24
  formated = {}
25
- attrs.keys.each do |key|
25
+ attrs.each_key do |key|
26
26
  formated[camelize(key)] = attrs[key]
27
27
  end
28
28
  formated
@@ -3,6 +3,13 @@ module Hyperb
3
3
  class Error < StandardError
4
4
  attr_reader :code, :msg
5
5
 
6
+ # region not supported
7
+ class UnsupportedRegion < self
8
+ def initialize(region)
9
+ super("Unsupported region: #{region}", nil)
10
+ end
11
+ end
12
+
6
13
  # 4xx HTTP status code
7
14
  ClientError = Class.new(self)
8
15
 
@@ -36,7 +43,7 @@ module Hyperb
36
43
  500 => Hyperb::Error::InternalServerError
37
44
  }.freeze
38
45
 
39
- def initialize(msg, code)
46
+ def initialize(msg, code = nil)
40
47
  super(msg)
41
48
  @code = code
42
49
  end
@@ -11,12 +11,10 @@ module Hyperb
11
11
  class Request
12
12
  FMT = '%Y%m%dT%H%M%SZ'.freeze
13
13
  VERSION = 'v1.23'.freeze
14
- HOST = 'us-west-1.hyper.sh'.freeze
15
- REGION = 'us-west-1'.freeze
14
+
16
15
  SERVICE = 'hyper'.freeze
17
16
  ALGORITHM = 'HYPER-HMAC-SHA256'.freeze
18
17
  KEYPARTS_REQUEST = 'hyper_request'.freeze
19
- BASE_URL = "https://#{HOST}/".freeze
20
18
 
21
19
  attr_accessor :verb, :path, :client, :date, :headers, :signed
22
20
 
@@ -28,19 +26,27 @@ module Hyperb
28
26
  @hashed_body = hexdigest(@body)
29
27
  @verb = verb.upcase
30
28
  @date = Time.now.utc.strftime(FMT)
29
+
30
+ set_base_url
31
+
31
32
  @headers = {
32
33
  content_type: 'application/json',
33
34
  x_hyper_date: @date,
34
- host: HOST,
35
+ host: @host,
35
36
  x_hyper_content_sha256: @hashed_body
36
37
  }
37
38
  @headers.merge!(optional_headers) unless optional_headers.empty?
38
39
  @signed = false
39
40
  end
40
41
 
42
+ def set_base_url
43
+ @host = "#{client.region}.hyper.sh".freeze
44
+ @base_url = "https://#{@host}/".freeze
45
+ end
46
+
41
47
  def perform
42
48
  sign unless signed
43
- final = BASE_URL + @path + '?' + @query
49
+ final = @base_url + @path + '?' + @query
44
50
  options = {}
45
51
  options[:body] = @body unless @body.empty?
46
52
  response = HTTP.headers(@headers).public_send(@verb.downcase.to_sym, final, options)
@@ -84,7 +90,7 @@ module Hyperb
84
90
  # https://docs.hyper.sh/Reference/API/2016-04-04%20[Ver.%201.23]/index.html
85
91
  def signature
86
92
  k_date = hmac('HYPER' + @client.secret_key, @date[0, 8])
87
- k_region = hmac(k_date, REGION)
93
+ k_region = hmac(k_date, 'us-west-1')
88
94
  k_service = hmac(k_region, SERVICE)
89
95
  k_credentials = hmac(k_service, KEYPARTS_REQUEST)
90
96
  hexhmac(k_credentials, string_to_sign)
@@ -108,7 +114,7 @@ module Hyperb
108
114
  def credential_scope
109
115
  [
110
116
  @date[0, 8],
111
- REGION,
117
+ 'us-west-1',
112
118
  SERVICE,
113
119
  KEYPARTS_REQUEST
114
120
  ].join("/") # rubocop:disable StringLiterals
@@ -129,13 +135,13 @@ module Hyperb
129
135
 
130
136
  # func requests are very simple, they do not require signing
131
137
  class FuncCallRequest
132
- REGION = 'us-west-1'.freeze
133
- URL = "https://#{REGION}.hyperfunc.io/".freeze
134
-
135
- attr_accessor :path, :query, :verb, :body, :headers
138
+ attr_accessor :client, :path, :query, :verb, :body, :headers
136
139
 
137
140
  def initialize(client, path, query = {}, verb = 'GET', body = '')
138
141
  @client = client
142
+
143
+ set_base_url
144
+
139
145
  @path = path
140
146
  @verb = verb
141
147
  @query = URI.encode_www_form(query)
@@ -143,8 +149,13 @@ module Hyperb
143
149
  @headers = { content_type: 'application/json' }
144
150
  end
145
151
 
152
+ def set_base_url
153
+ @host = "#{client.region}.hyperfunc.io".freeze
154
+ @base_url = "https://#{@host}/".freeze
155
+ end
156
+
146
157
  def perform
147
- final_url = URL + @path + '?' + @query
158
+ final_url = @base_url + @path + '?' + @query
148
159
  options = {}
149
160
  options[:body] = @body unless @body.empty?
150
161
  response = HTTP.headers(@headers).public_send(@verb.downcase.to_sym, final_url, options)
@@ -1,3 +1,3 @@
1
1
  module Hyperb
2
- VERSION = '0.6.0'.freeze
2
+ VERSION = '0.7.0'.freeze
3
3
  end
@@ -22,6 +22,32 @@ RSpec.describe Hyperb::Client do
22
22
  client = Hyperb::Client.new(secret_key: 'SK')
23
23
  expect(client.credentials?).to be false
24
24
  end
25
+ end
26
+
27
+ describe '#region' do
28
+
29
+ it 'should set region' do
30
+ client = Hyperb::Client.new do |c|
31
+ c.access_key = 's'
32
+ c.secret_key = 'key'
33
+ c.region = 'eu-central-1'
34
+ end
35
+ expect(client.region).to eql('eu-central-1')
36
+ end
25
37
 
38
+ it 'should raise UnsupportedRegion' do
39
+ expect { Hyperb::Client.new(access_key: 's', secret_key: 'key', region: 'eu-unsup')}.to raise_error do |e|
40
+ expect(e).to be_a Hyperb::Error::UnsupportedRegion
41
+ expect(e.message).to eql('Unsupported region: eu-unsup')
42
+ end
43
+ end
44
+
45
+ it 'should set default region' do
46
+ client = Hyperb::Client.new do |c|
47
+ c.access_key = 's'
48
+ c.secret_key = 'key'
49
+ end
50
+ expect(client.region).to eql(client.default_region)
51
+ end
26
52
  end
27
53
  end
@@ -5,7 +5,7 @@ RSpec.describe Hyperb::Compose do
5
5
 
6
6
  before do
7
7
  @client = Hyperb::Client.new(access_key: 'key', secret_key: '123')
8
- @base_path = Hyperb::Request::BASE_URL + Hyperb::Request::VERSION + '/compose/'
8
+ @base_path = "#{base_url(@client)}/compose/"
9
9
  end
10
10
 
11
11
  describe '#compose_down' do
@@ -22,4 +22,4 @@ RSpec.describe Hyperb::Container do
22
22
  end
23
23
  end
24
24
 
25
- end
25
+ end
@@ -6,7 +6,8 @@ RSpec.describe Hyperb::Containers do
6
6
  before do
7
7
  @client = Hyperb::Client.new(access_key: 'key', secret_key: '123')
8
8
 
9
- @containers_base_path = Hyperb::Request::BASE_URL + Hyperb::Request::VERSION + '/containers/'
9
+ @containers_base_path = "#{base_url(@client)}/containers/"
10
+
10
11
  @containers_path = @containers_base_path + 'json'
11
12
  @remove_container_path = @containers_base_path
12
13
  @create_container_path = @containers_base_path + 'create'
@@ -404,7 +405,6 @@ RSpec.describe Hyperb::Containers do
404
405
  expect(a_request(:post, path)
405
406
  .with(body: "")).to have_been_made
406
407
  end
407
-
408
408
  end
409
409
 
410
410
  describe '#container_logs' do
@@ -4,8 +4,8 @@ RSpec.describe Hyperb::Error do
4
4
 
5
5
  before do
6
6
  # random url for request mocking
7
- @path = Hyperb::Request::BASE_URL + Hyperb::Request::VERSION + '/version'
8
7
  @client = Hyperb::Client.new secret_key: '123', access_key: 'ak'
8
+ @path = "#{base_url(@client)}/version"
9
9
  end
10
10
 
11
11
  Hyperb::Error::ERRORS.each do |code, exception|
@@ -4,8 +4,8 @@ RSpec.describe Hyperb::Funcs do
4
4
 
5
5
  before do
6
6
  @client = Hyperb::Client.new(access_key: 'key', secret_key: '123')
7
- @funcs_path = Hyperb::Request::BASE_URL + Hyperb::Request::VERSION + '/funcs'
8
- @call_path = Hyperb::FuncCallRequest::URL
7
+ @funcs_path = "#{base_url(@client)}/funcs"
8
+ @call_path = funcs_base_url(@client)
9
9
  end
10
10
 
11
11
  describe '#funcs' do
@@ -16,6 +16,7 @@ require 'webmock/rspec'
16
16
  WebMock.disable_net_connect!(allow: 'coveralls.io')
17
17
 
18
18
  RSpec.configure do |config|
19
+
19
20
  # Enable flags like --only-failures and --next-failure
20
21
  config.example_status_persistence_file_path = ".rspec_status"
21
22
 
@@ -25,6 +26,9 @@ RSpec.configure do |config|
25
26
  config.expect_with :rspec do |c|
26
27
  c.syntax = :expect
27
28
  end
29
+
30
+ config.run_all_when_everything_filtered = true
31
+ config.filter_run focus: true
28
32
  end
29
33
 
30
34
  def fixture_path
@@ -33,4 +37,13 @@ end
33
37
 
34
38
  def fixture(file)
35
39
  File.new(fixture_path + '/' + file)
36
- end
40
+ end
41
+
42
+ def base_url(client)
43
+ host = "#{client.region}.hyper.sh".freeze
44
+ "https://#{host}/#{Hyperb::Request::VERSION}".freeze
45
+ end
46
+
47
+ def funcs_base_url(client)
48
+ "https://#{client.region}.hyperfunc.io/".freeze
49
+ end
@@ -5,10 +5,10 @@ RSpec.describe Hyperb::Images do
5
5
 
6
6
  before do
7
7
  @client = Hyperb::Client.new(access_key: 'key', secret_key: '123')
8
- @images_path = Hyperb::Request::BASE_URL + Hyperb::Request::VERSION + '/images/json?all=true'
9
- @create_image_path = Hyperb::Request::BASE_URL + Hyperb::Request::VERSION + '/images/create?fromImage=busybox'
10
- @remove_image_path = Hyperb::Request::BASE_URL + Hyperb::Request::VERSION + '/images/busybox'
11
- @inspect_image_path = Hyperb::Request::BASE_URL + Hyperb::Request::VERSION + '/images/busybox/json'
8
+ @images_path = "#{base_url(@client)}/images/json?all=true"
9
+ @create_image_path = "#{base_url(@client)}/images/create?fromImage=busybox"
10
+ @remove_image_path = "#{base_url(@client)}/images/busybox"
11
+ @inspect_image_path = "#{base_url(@client)}/images/busybox/json"
12
12
  end
13
13
 
14
14
  describe '#images' do
@@ -43,7 +43,6 @@ RSpec.describe Hyperb::Images do
43
43
  expect(img.created).to be_a Fixnum
44
44
  end
45
45
  end
46
-
47
46
  end
48
47
 
49
48
  describe '#create_image' do
@@ -68,7 +67,7 @@ RSpec.describe Hyperb::Images do
68
67
  end
69
68
 
70
69
  it 'create image with auth_object' do
71
- p = Hyperb::Request::BASE_URL + Hyperb::Request::VERSION + '/images/create?fromImage=gcr.io/private/custom_busybox'
70
+ p = "#{base_url(@client)}/images/create?fromImage=gcr.io/private/custom_busybox"
72
71
  stub_request(:post, p)
73
72
  .to_return(body: fixture('create_image.json'))
74
73
 
@@ -4,7 +4,7 @@ RSpec.describe Hyperb::Misc do
4
4
 
5
5
  before do
6
6
  @client = Hyperb::Client.new(access_key: 'key', secret_key: '123')
7
- @base_path = Hyperb::Request::BASE_URL + Hyperb::Request::VERSION + '/info'
7
+ @base_path = "#{base_url(@client)}/info"
8
8
  end
9
9
 
10
10
  describe '#info' do
@@ -4,7 +4,7 @@ RSpec.describe Hyperb::Network do
4
4
 
5
5
  before do
6
6
  @client = Hyperb::Client.new(access_key: 'key', secret_key: '123')
7
- @base_path = Hyperb::Request::BASE_URL + Hyperb::Request::VERSION + '/fips'
7
+ @base_path = "#{base_url(@client)}/fips"
8
8
  end
9
9
 
10
10
  describe '#fip_allocate' do
@@ -5,7 +5,7 @@ RSpec.describe Hyperb::Services do
5
5
 
6
6
  before do
7
7
  @client = Hyperb::Client.new(access_key: 'key', secret_key: '123')
8
- @base_path = Hyperb::Request::BASE_URL + Hyperb::Request::VERSION + '/services'
8
+ @base_path = "#{base_url(@client)}/services"
9
9
  end
10
10
 
11
11
  describe '#inspect_service' do
@@ -5,7 +5,7 @@ RSpec.describe Hyperb::Snapshots do
5
5
 
6
6
  before do
7
7
  @client = Hyperb::Client.new(access_key: 'key', secret_key: '123')
8
- @base_path = Hyperb::Request::BASE_URL + Hyperb::Request::VERSION + '/snapshots'
8
+ @base_path = "#{base_url(@client)}/snapshots"
9
9
  end
10
10
 
11
11
  describe '#create_snapshot' do
@@ -5,7 +5,7 @@ RSpec.describe Hyperb::Volumes do
5
5
 
6
6
  before do
7
7
  @client = Hyperb::Client.new(access_key: 'key', secret_key: '123')
8
- @base_path = Hyperb::Request::BASE_URL + Hyperb::Request::VERSION + '/volumes'
8
+ @base_path = "#{base_url(@client)}/volumes"
9
9
  end
10
10
 
11
11
  describe '#remove_volume' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hyperb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - drish
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-09-10 00:00:00.000000000 Z
11
+ date: 2017-09-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: http
@@ -114,6 +114,20 @@ dependencies:
114
114
  - - ~>
115
115
  - !ruby/object:Gem::Version
116
116
  version: 0.10.4
117
+ - !ruby/object:Gem::Dependency
118
+ name: pry-byebug
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - '>='
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ type: :development
125
+ prerelease: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - '>='
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
117
131
  description: The Hyper.sh Ruby Gem
118
132
  email:
119
133
  - carlosderich@gmail.com