Class: Hcloud::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/hcloud/client.rb

Constant Summary collapse

MAX_ENTRIES_PER_PAGE =
50

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token:, auto_pagination: false, concurrency: 20) ⇒ Client

Returns a new instance of Client.



9
10
11
12
13
14
# File 'lib/hcloud/client.rb', line 9

def initialize(token:, auto_pagination: false, concurrency: 20)
  @token = token
  @auto_pagination = auto_pagination
  @concurrency = concurrency
  @hydra = Typhoeus::Hydra.new(max_concurrency: concurrency)
end

Instance Attribute Details

#auto_paginationObject (readonly)

Returns the value of attribute auto_pagination.



8
9
10
# File 'lib/hcloud/client.rb', line 8

def auto_pagination
  @auto_pagination
end

#hydraObject (readonly)

Returns the value of attribute hydra.



8
9
10
# File 'lib/hcloud/client.rb', line 8

def hydra
  @hydra
end

#tokenObject (readonly)

Returns the value of attribute token.



8
9
10
# File 'lib/hcloud/client.rb', line 8

def token
  @token
end

Instance Method Details

#actionsObject



27
28
29
# File 'lib/hcloud/client.rb', line 27

def actions
  ActionResource.new(client: self)
end

#authorized?Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
# File 'lib/hcloud/client.rb', line 16

def authorized?
  request("server_types").run
  true
rescue Error::Unauthorized
  false
end

#datacentersObject



39
40
41
# File 'lib/hcloud/client.rb', line 39

def datacenters
  DatacenterResource.new(client: self)
end

#floating_ipsObject



55
56
57
# File 'lib/hcloud/client.rb', line 55

def floating_ips
  FloatingIPResource.new(client: self)
end

#imagesObject



35
36
37
# File 'lib/hcloud/client.rb', line 35

def images
  ImageResource.new(client: self)
end

#isosObject



31
32
33
# File 'lib/hcloud/client.rb', line 31

def isos
  IsoResource.new(client: self)
end

#locationsObject



43
44
45
# File 'lib/hcloud/client.rb', line 43

def locations
  LocationResource.new(client: self)
end

#request(path, **options) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/hcloud/client.rb', line 59

def request(path, **options)
  code = options.delete(:code)
  if x = options.delete(:j)
    options[:body] = Oj.dump(x, mode: :compat)
    options[:method] ||= :post
  end
  q = []
  q << options.delete(:ep).to_s
  if x = options.delete(:q)
    q << x.to_param
  end
  path = path.dup
  path << "?"+q.join("&")
  r = Typhoeus::Request.new(
    "https://api.hetzner.cloud/v1/#{path}",
    {
      headers: { 
        "Authorization" => "Bearer #{token}",
        "Content-Type" => "application/json",
      },
    }.merge(options)
  )
  r.on_complete do |response|
    case response.code
    when code
      raise Error::UnexpectedError, response.body
    when 401
      raise Error::Unauthorized
    when 0
      raise Error::ServerError, "Connection error: #{response.return_code}"
    when 400...600
      j = Oj.load(response.body)
      code = j.dig("error", "code")
      error_class = case code
                    when "invalid_input" then Error::InvalidInput
                    when "forbidden" then Error::Forbidden
                    when "locked" then Error::Locked
                    when "not_found" then Error::NotFound
                    when "rate_limit_exceeded" then Error::RateLimitExceeded
                    when "resource_unavailable" then Error::ResourceUnavilable
                    when "service_error" then Error::ServiceError
                    when "uniqueness_error" then Error::UniquenessError
                    else
                      Error::ServerError
                    end
      raise error_class, j.dig("error", "message")
    end
  end
  r
end

#server_typesObject



47
48
49
# File 'lib/hcloud/client.rb', line 47

def server_types
  ServerTypeResource.new(client: self)
end

#serversObject



23
24
25
# File 'lib/hcloud/client.rb', line 23

def servers
  ServerResource.new(client: self)
end

#ssh_keysObject



51
52
53
# File 'lib/hcloud/client.rb', line 51

def ssh_keys
  SSHKeyResource.new(client: self)
end