hcloud 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +24 -0
- data/.rubocop_todo.yml +148 -0
- data/.travis.yml +3 -1
- data/CHANGELOG.md +40 -0
- data/Gemfile +4 -3
- data/Rakefile +2 -2
- data/bin/console +3 -3
- data/hcloud.gemspec +19 -19
- data/lib/hcloud.rb +1 -1
- data/lib/hcloud/abstract_resource.rb +18 -19
- data/lib/hcloud/action.rb +3 -3
- data/lib/hcloud/action_resource.rb +6 -7
- data/lib/hcloud/client.rb +18 -18
- data/lib/hcloud/datacenter.rb +2 -3
- data/lib/hcloud/datacenter_resource.rb +12 -14
- data/lib/hcloud/entry_loader.rb +6 -6
- data/lib/hcloud/floating_ip.rb +13 -14
- data/lib/hcloud/floating_ip_resource.rb +12 -12
- data/lib/hcloud/image.rb +5 -5
- data/lib/hcloud/image_resource.rb +9 -10
- data/lib/hcloud/iso.rb +2 -2
- data/lib/hcloud/iso_resource.rb +4 -5
- data/lib/hcloud/location.rb +2 -3
- data/lib/hcloud/location_resource.rb +10 -11
- data/lib/hcloud/multi_reply.rb +5 -6
- data/lib/hcloud/pagination.rb +3 -3
- data/lib/hcloud/server.rb +33 -34
- data/lib/hcloud/server_resource.rb +14 -14
- data/lib/hcloud/server_type.rb +2 -3
- data/lib/hcloud/server_type_resource.rb +10 -11
- data/lib/hcloud/ssh_key.rb +4 -5
- data/lib/hcloud/ssh_key_resource.rb +11 -12
- data/lib/hcloud/version.rb +1 -1
- metadata +20 -17
data/lib/hcloud/client.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
autoload :Typhoeus,
|
2
|
-
autoload :Oj,
|
1
|
+
autoload :Typhoeus, 'typhoeus'
|
2
|
+
autoload :Oj, 'oj'
|
3
3
|
|
4
4
|
module Hcloud
|
5
5
|
class Client
|
@@ -14,7 +14,7 @@ module Hcloud
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def authorized?
|
17
|
-
request(
|
17
|
+
request('server_types').run
|
18
18
|
true
|
19
19
|
rescue Error::Unauthorized
|
20
20
|
false
|
@@ -68,14 +68,14 @@ module Hcloud
|
|
68
68
|
q << x.to_param
|
69
69
|
end
|
70
70
|
path = path.dup
|
71
|
-
path <<
|
71
|
+
path << '?' + q.join('&')
|
72
72
|
r = Typhoeus::Request.new(
|
73
73
|
"https://api.hetzner.cloud/v1/#{path}",
|
74
74
|
{
|
75
|
-
headers: {
|
76
|
-
|
77
|
-
|
78
|
-
}
|
75
|
+
headers: {
|
76
|
+
'Authorization' => "Bearer #{token}",
|
77
|
+
'Content-Type' => 'application/json'
|
78
|
+
}
|
79
79
|
}.merge(options)
|
80
80
|
)
|
81
81
|
r.on_complete do |response|
|
@@ -88,20 +88,20 @@ module Hcloud
|
|
88
88
|
raise Error::ServerError, "Connection error: #{response.return_code}"
|
89
89
|
when 400...600
|
90
90
|
j = Oj.load(response.body)
|
91
|
-
code = j.dig(
|
91
|
+
code = j.dig('error', 'code')
|
92
92
|
error_class = case code
|
93
|
-
when
|
94
|
-
when
|
95
|
-
when
|
96
|
-
when
|
97
|
-
when
|
98
|
-
when
|
99
|
-
when
|
100
|
-
when
|
93
|
+
when 'invalid_input' then Error::InvalidInput
|
94
|
+
when 'forbidden' then Error::Forbidden
|
95
|
+
when 'locked' then Error::Locked
|
96
|
+
when 'not_found' then Error::NotFound
|
97
|
+
when 'rate_limit_exceeded' then Error::RateLimitExceeded
|
98
|
+
when 'resource_unavailable' then Error::ResourceUnavilable
|
99
|
+
when 'service_error' then Error::ServiceError
|
100
|
+
when 'uniqueness_error' then Error::UniquenessError
|
101
101
|
else
|
102
102
|
Error::ServerError
|
103
103
|
end
|
104
|
-
raise error_class, j.dig(
|
104
|
+
raise error_class, j.dig('error', 'message')
|
105
105
|
end
|
106
106
|
end
|
107
107
|
r
|
data/lib/hcloud/datacenter.rb
CHANGED
@@ -3,10 +3,10 @@ module Hcloud
|
|
3
3
|
include Enumerable
|
4
4
|
|
5
5
|
def all
|
6
|
-
j = Oj.load(request(
|
7
|
-
dc = j[
|
8
|
-
dc.reject{|x| x.id == j[
|
9
|
-
dc.find{|x| x.id == j[
|
6
|
+
j = Oj.load(request('datacenters').run.body)
|
7
|
+
dc = j['datacenters'].map { |x| Datacenter.new(x, self, client) }
|
8
|
+
dc.reject { |x| x.id == j['recommendation'] }.unshift(
|
9
|
+
dc.find { |x| x.id == j['recommendation'] }
|
10
10
|
)
|
11
11
|
end
|
12
12
|
|
@@ -16,32 +16,30 @@ module Hcloud
|
|
16
16
|
|
17
17
|
def find(id)
|
18
18
|
Datacenter.new(
|
19
|
-
Oj.load(request("datacenters/#{id}").run.body)[
|
19
|
+
Oj.load(request("datacenters/#{id}").run.body)['datacenter'],
|
20
20
|
self,
|
21
21
|
client
|
22
22
|
)
|
23
23
|
end
|
24
|
-
|
24
|
+
|
25
25
|
def find_by(name:)
|
26
|
-
x = Oj.load(request(
|
26
|
+
x = Oj.load(request('datacenters', q: { name: name }).run.body)['datacenters']
|
27
27
|
return nil if x.none?
|
28
28
|
x.each do |s|
|
29
29
|
return Datacenter.new(s, self, client)
|
30
30
|
end
|
31
31
|
end
|
32
|
-
|
32
|
+
|
33
33
|
def [](arg)
|
34
34
|
case arg
|
35
35
|
when Integer
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
36
|
+
begin
|
37
|
+
find(arg)
|
38
|
+
rescue Error::NotFound
|
39
|
+
end
|
40
40
|
when String
|
41
41
|
find_by(name: arg)
|
42
42
|
end
|
43
43
|
end
|
44
|
-
|
45
44
|
end
|
46
45
|
end
|
47
|
-
|
data/lib/hcloud/entry_loader.rb
CHANGED
@@ -5,7 +5,7 @@ module Hcloud
|
|
5
5
|
|
6
6
|
included do |klass|
|
7
7
|
klass.send(:attr_reader, :parent, :client)
|
8
|
-
klass.const_get(:Attributes).each do |attribute,
|
8
|
+
klass.const_get(:Attributes).each do |attribute, _value|
|
9
9
|
klass.send(:attr_accessor, attribute)
|
10
10
|
end
|
11
11
|
end
|
@@ -16,14 +16,14 @@ module Hcloud
|
|
16
16
|
self.class.const_get(:Attributes).each do |attribute, value|
|
17
17
|
case value
|
18
18
|
when nil
|
19
|
-
|
19
|
+
send("#{attribute}=", resource[attribute.to_s])
|
20
20
|
when :time
|
21
21
|
unless resource[attribute.to_s].nil?
|
22
|
-
|
22
|
+
send("#{attribute}=", Time.parse(resource[attribute.to_s]))
|
23
23
|
end
|
24
|
-
else
|
25
|
-
if value.is_a?(Class)
|
26
|
-
|
24
|
+
else
|
25
|
+
if value.is_a?(Class) && value.include?(EntryLoader)
|
26
|
+
send("#{attribute}=", value.new(resource[attribute.to_s], self, client))
|
27
27
|
end
|
28
28
|
end
|
29
29
|
end
|
data/lib/hcloud/floating_ip.rb
CHANGED
@@ -8,29 +8,29 @@ module Hcloud
|
|
8
8
|
dns_ptr: nil,
|
9
9
|
server: nil,
|
10
10
|
home_location: Location,
|
11
|
-
blocked: nil
|
12
|
-
}
|
11
|
+
blocked: nil
|
12
|
+
}.freeze
|
13
13
|
include EntryLoader
|
14
14
|
|
15
15
|
def update(description:)
|
16
|
-
j = Oj.load(request("floating_ips/#{id.to_i}",
|
17
|
-
j: {description: description},
|
16
|
+
j = Oj.load(request("floating_ips/#{id.to_i}",
|
17
|
+
j: { description: description },
|
18
18
|
method: :put).run.body)
|
19
|
-
FloatingIP.new(j[
|
19
|
+
FloatingIP.new(j['floating_ip'], self, client)
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
def assign(server:)
|
23
|
-
j = Oj.load(request("floating_ips/#{id.to_i}/actions/assign",
|
24
|
-
j: {server: server}).run.body)
|
25
|
-
Action.new(j[
|
23
|
+
j = Oj.load(request("floating_ips/#{id.to_i}/actions/assign",
|
24
|
+
j: { server: server }).run.body)
|
25
|
+
Action.new(j['action'], self, client)
|
26
26
|
end
|
27
|
-
|
27
|
+
|
28
28
|
def unassign
|
29
|
-
j = Oj.load(request("floating_ips/#{id.to_i}/actions/unassign",
|
29
|
+
j = Oj.load(request("floating_ips/#{id.to_i}/actions/unassign",
|
30
30
|
method: :post).run.body)
|
31
|
-
Action.new(j[
|
31
|
+
Action.new(j['action'], self, client)
|
32
32
|
end
|
33
|
-
|
33
|
+
|
34
34
|
def actions
|
35
35
|
ActionResource.new(client: client, parent: self, base_path: "floating_ips/#{id.to_i}")
|
36
36
|
end
|
@@ -39,6 +39,5 @@ module Hcloud
|
|
39
39
|
request("floating_ips/#{id}", method: :delete).run.body
|
40
40
|
true
|
41
41
|
end
|
42
|
-
|
43
42
|
end
|
44
43
|
end
|
@@ -3,25 +3,26 @@ module Hcloud
|
|
3
3
|
include Enumerable
|
4
4
|
|
5
5
|
def all
|
6
|
-
|
7
|
-
|
6
|
+
mj('floating_ips') do |j|
|
7
|
+
j.flat_map { |x| x['floating_ips'].map { |x| FloatingIP.new(x, self, client) } }
|
8
|
+
end
|
8
9
|
end
|
9
10
|
|
10
11
|
def create(type:, server: nil, home_location: nil, description: nil)
|
11
12
|
query = {}
|
12
|
-
method(:create).parameters.inject(query) do |r,x|
|
13
|
+
method(:create).parameters.inject(query) do |r, x|
|
13
14
|
(var = eval(x.last.to_s)).nil? ? r : r.merge!(x.last => var)
|
14
15
|
end
|
15
|
-
j = Oj.load(request(
|
16
|
+
j = Oj.load(request('floating_ips', j: query, code: 200).run.body)
|
16
17
|
[
|
17
|
-
j.key?(
|
18
|
-
FloatingIP.new(j[
|
18
|
+
j.key?('action') ? Action.new(j['action'], self, client) : nil,
|
19
|
+
FloatingIP.new(j['floating_ip'], self, client)
|
19
20
|
]
|
20
21
|
end
|
21
22
|
|
22
23
|
def find(id)
|
23
24
|
FloatingIP.new(
|
24
|
-
Oj.load(request("floating_ips/#{id}").run.body)[
|
25
|
+
Oj.load(request("floating_ips/#{id}").run.body)['floating_ip'],
|
25
26
|
self,
|
26
27
|
client
|
27
28
|
)
|
@@ -30,12 +31,11 @@ module Hcloud
|
|
30
31
|
def [](arg)
|
31
32
|
case arg
|
32
33
|
when Integer
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
34
|
+
begin
|
35
|
+
find(arg)
|
36
|
+
rescue Error::NotFound
|
37
|
+
end
|
37
38
|
end
|
38
39
|
end
|
39
|
-
|
40
40
|
end
|
41
41
|
end
|
data/lib/hcloud/image.rb
CHANGED
@@ -13,22 +13,22 @@ module Hcloud
|
|
13
13
|
bound_to: nil,
|
14
14
|
os_flavor: nil,
|
15
15
|
os_version: nil,
|
16
|
-
rapid_deploy: nil
|
17
|
-
}
|
16
|
+
rapid_deploy: nil
|
17
|
+
}.freeze
|
18
18
|
|
19
19
|
include EntryLoader
|
20
20
|
|
21
21
|
def to_snapshot
|
22
|
-
update(type:
|
22
|
+
update(type: 'snapshot')
|
23
23
|
end
|
24
24
|
|
25
25
|
def update(description: nil, type: nil)
|
26
26
|
query = {}
|
27
|
-
method(:update).parameters.inject(query) do |r,x|
|
27
|
+
method(:update).parameters.inject(query) do |r, x|
|
28
28
|
(var = eval(x.last.to_s)).nil? ? r : r.merge!(x.last => var)
|
29
29
|
end
|
30
30
|
Image.new(
|
31
|
-
Oj.load(request("images/#{id.to_i}", j: query, method: :put).run.body)[
|
31
|
+
Oj.load(request("images/#{id.to_i}", j: query, method: :put).run.body)['image'],
|
32
32
|
parent,
|
33
33
|
client
|
34
34
|
)
|
@@ -1,11 +1,11 @@
|
|
1
1
|
module Hcloud
|
2
2
|
class ImageResource < AbstractResource
|
3
3
|
def all
|
4
|
-
mj(
|
5
|
-
j.flat_map{|x| x[
|
4
|
+
mj('images') do |j|
|
5
|
+
j.flat_map { |x| x['images'].map { |x| Image.new(x, self, client) } }
|
6
6
|
end
|
7
7
|
end
|
8
|
-
|
8
|
+
|
9
9
|
def [](arg)
|
10
10
|
case arg
|
11
11
|
when Integer
|
@@ -20,29 +20,28 @@ module Hcloud
|
|
20
20
|
|
21
21
|
def find(id)
|
22
22
|
Image.new(
|
23
|
-
Oj.load(request("images/#{id.to_i}").run.body)[
|
23
|
+
Oj.load(request("images/#{id.to_i}").run.body)['image'],
|
24
24
|
self,
|
25
25
|
client
|
26
26
|
)
|
27
27
|
end
|
28
|
-
|
28
|
+
|
29
29
|
def where(sort: nil, type: nil, bound_to: nil, name: nil)
|
30
30
|
query = {}
|
31
|
-
method(:where).parameters.inject(query) do |r,x|
|
31
|
+
method(:where).parameters.inject(query) do |r, x|
|
32
32
|
(var = eval(x.last.to_s)).nil? ? r : r.merge!(x.last => var)
|
33
33
|
end
|
34
|
-
mj(
|
35
|
-
j.flat_map{|x| x[
|
34
|
+
mj('images', q: query) do |j|
|
35
|
+
j.flat_map { |x| x['images'].map { |x| Image.new(x, self, client) } }
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
39
39
|
def find_by(name:)
|
40
|
-
j = Oj.load(request(
|
40
|
+
j = Oj.load(request('images', q: { name: name }).run.body)['images']
|
41
41
|
return if j.none?
|
42
42
|
j.each do |x|
|
43
43
|
return Image.new(x, self, client)
|
44
44
|
end
|
45
45
|
end
|
46
|
-
|
47
46
|
end
|
48
47
|
end
|
data/lib/hcloud/iso.rb
CHANGED
data/lib/hcloud/iso_resource.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
module Hcloud
|
2
2
|
class IsoResource < AbstractResource
|
3
3
|
def all
|
4
|
-
mj(
|
5
|
-
j.flat_map{|x| x[
|
4
|
+
mj('isos') do |j|
|
5
|
+
j.flat_map { |x| x['isos'].map { |x| Iso.new(x, self, client) } }
|
6
6
|
end
|
7
7
|
end
|
8
8
|
|
@@ -20,19 +20,18 @@ module Hcloud
|
|
20
20
|
|
21
21
|
def find(id)
|
22
22
|
Iso.new(
|
23
|
-
Oj.load(request("isos/#{id.to_i}").run.body)[
|
23
|
+
Oj.load(request("isos/#{id.to_i}").run.body)['iso'],
|
24
24
|
self,
|
25
25
|
client
|
26
26
|
)
|
27
27
|
end
|
28
28
|
|
29
29
|
def find_by(name:)
|
30
|
-
j = Oj.load(request(
|
30
|
+
j = Oj.load(request('isos', q: { name: name }).run.body)['isos']
|
31
31
|
return if j.none?
|
32
32
|
j.each do |x|
|
33
33
|
return Iso.new(x, self, client)
|
34
34
|
end
|
35
35
|
end
|
36
|
-
|
37
36
|
end
|
38
37
|
end
|
data/lib/hcloud/location.rb
CHANGED
@@ -1,38 +1,37 @@
|
|
1
1
|
module Hcloud
|
2
2
|
class LocationResource < AbstractResource
|
3
3
|
def all
|
4
|
-
mj(
|
5
|
-
j.flat_map{|x| x[
|
4
|
+
mj('locations') do |j|
|
5
|
+
j.flat_map { |x| x['locations'].map { |x| Location.new(x, self, client) } }
|
6
6
|
end
|
7
7
|
end
|
8
8
|
|
9
9
|
def find(id)
|
10
10
|
Location.new(
|
11
|
-
Oj.load(request("locations/#{id}").run.body)[
|
11
|
+
Oj.load(request("locations/#{id}").run.body)['location'],
|
12
12
|
self,
|
13
13
|
client
|
14
14
|
)
|
15
15
|
end
|
16
|
-
|
16
|
+
|
17
17
|
def find_by(name:)
|
18
|
-
x = Oj.load(request(
|
18
|
+
x = Oj.load(request('locations', q: { name: name }).run.body)['locations']
|
19
19
|
return nil if x.none?
|
20
20
|
x.each do |s|
|
21
21
|
return Location.new(s, self, client)
|
22
22
|
end
|
23
23
|
end
|
24
|
-
|
24
|
+
|
25
25
|
def [](arg)
|
26
26
|
case arg
|
27
27
|
when Integer
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
28
|
+
begin
|
29
|
+
find(arg)
|
30
|
+
rescue Error::NotFound
|
31
|
+
end
|
32
32
|
when String
|
33
33
|
find_by(name: arg)
|
34
34
|
end
|
35
35
|
end
|
36
|
-
|
37
36
|
end
|
38
37
|
end
|