Class: Hcloud::Client
- Inherits:
-
Object
- Object
- Hcloud::Client
- Defined in:
- lib/hcloud/client.rb
Defined Under Namespace
Classes: ResourceFuture
Constant Summary collapse
- MAX_ENTRIES_PER_PAGE =
50
Class Attribute Summary collapse
Instance Attribute Summary collapse
-
#auto_pagination ⇒ Object
readonly
Returns the value of attribute auto_pagination.
-
#hydra ⇒ Object
readonly
Returns the value of attribute hydra.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
-
#user_agent ⇒ Object
readonly
Returns the value of attribute user_agent.
Instance Method Summary collapse
- #actions ⇒ Object
- #authorized? ⇒ Boolean
- #certificates ⇒ Object
- #concurrent ⇒ Object
- #concurrent? ⇒ Boolean
- #datacenters ⇒ Object
- #firewalls ⇒ Object
- #floating_ips ⇒ Object
- #images ⇒ Object
-
#initialize(token:, auto_pagination: false, concurrency: 20, user_agent: nil) ⇒ Client
constructor
A new instance of Client.
- #isos ⇒ Object
- #load_balancer_types ⇒ Object
- #load_balancers ⇒ Object
- #locations ⇒ Object
- #networks ⇒ Object
- #placement_groups ⇒ Object
- #prepare_request(url, args = {}, &block) ⇒ Object
- #primary_ips ⇒ Object
-
#request(path, options = {}) ⇒ Object
rubocop:disable Metrics/MethodLength.
- #server_types ⇒ Object
- #servers ⇒ Object
- #ssh_keys ⇒ Object
- #volumes ⇒ Object
Constructor Details
#initialize(token:, auto_pagination: false, concurrency: 20, user_agent: nil) ⇒ Client
Returns a new instance of Client.
24 25 26 27 28 29 30 |
# File 'lib/hcloud/client.rb', line 24 def initialize(token:, auto_pagination: false, concurrency: 20, user_agent: nil) @token = token @user_agent = user_agent || "hcloud-ruby v#{VERSION}" @auto_pagination = auto_pagination @concurrency = concurrency @hydra = Typhoeus::Hydra.new(max_concurrency: concurrency) end |
Class Attribute Details
Instance Attribute Details
#auto_pagination ⇒ Object (readonly)
Returns the value of attribute auto_pagination.
22 23 24 |
# File 'lib/hcloud/client.rb', line 22 def auto_pagination @auto_pagination end |
#hydra ⇒ Object (readonly)
Returns the value of attribute hydra.
22 23 24 |
# File 'lib/hcloud/client.rb', line 22 def hydra @hydra end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
22 23 24 |
# File 'lib/hcloud/client.rb', line 22 def token @token end |
#user_agent ⇒ Object (readonly)
Returns the value of attribute user_agent.
22 23 24 |
# File 'lib/hcloud/client.rb', line 22 def user_agent @user_agent end |
Instance Method Details
#actions ⇒ Object
61 62 63 |
# File 'lib/hcloud/client.rb', line 61 def actions ActionResource.new(client: self) end |
#authorized? ⇒ Boolean
50 51 52 53 54 55 |
# File 'lib/hcloud/client.rb', line 50 def request('server_types').run true rescue Error::Unauthorized false end |
#certificates ⇒ Object
89 90 91 |
# File 'lib/hcloud/client.rb', line 89 def certificates CertificateResource.new(client: self) end |
#concurrent ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/hcloud/client.rb', line 32 def concurrent @concurrent = true ret = yield ret.each do |element| next unless element.is_a?(AbstractResource) element.run end hydra.run ret ensure @concurrent = nil end |
#concurrent? ⇒ Boolean
46 47 48 |
# File 'lib/hcloud/client.rb', line 46 def concurrent? !@concurrent.nil? end |
#datacenters ⇒ Object
73 74 75 |
# File 'lib/hcloud/client.rb', line 73 def datacenters DatacenterResource.new(client: self) end |
#firewalls ⇒ Object
105 106 107 |
# File 'lib/hcloud/client.rb', line 105 def firewalls FirewallResource.new(client: self) end |
#floating_ips ⇒ Object
93 94 95 |
# File 'lib/hcloud/client.rb', line 93 def floating_ips FloatingIPResource.new(client: self) end |
#images ⇒ Object
69 70 71 |
# File 'lib/hcloud/client.rb', line 69 def images ImageResource.new(client: self) end |
#isos ⇒ Object
65 66 67 |
# File 'lib/hcloud/client.rb', line 65 def isos IsoResource.new(client: self) end |
#load_balancer_types ⇒ Object
117 118 119 |
# File 'lib/hcloud/client.rb', line 117 def load_balancer_types LoadBalancerTypeResource.new(client: self) end |
#load_balancers ⇒ Object
121 122 123 |
# File 'lib/hcloud/client.rb', line 121 def load_balancers LoadBalancerResource.new(client: self) end |
#locations ⇒ Object
77 78 79 |
# File 'lib/hcloud/client.rb', line 77 def locations LocationResource.new(client: self) end |
#networks ⇒ Object
101 102 103 |
# File 'lib/hcloud/client.rb', line 101 def networks NetworkResource.new(client: self) end |
#placement_groups ⇒ Object
113 114 115 |
# File 'lib/hcloud/client.rb', line 113 def placement_groups PlacementGroupResource.new(client: self) end |
#prepare_request(url, args = {}, &block) ⇒ Object
135 136 137 138 139 140 141 |
# File 'lib/hcloud/client.rb', line 135 def prepare_request(url, args = {}, &block) req = request(url, **args.merge(block: block)) return req.run.resource unless concurrent? hydra.queue req ResourceFuture.new(req) end |
#primary_ips ⇒ Object
97 98 99 |
# File 'lib/hcloud/client.rb', line 97 def primary_ips PrimaryIPResource.new(client: self) end |
#request(path, options = {}) ⇒ Object
rubocop:disable Metrics/MethodLength
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/hcloud/client.rb', line 143 def request(path, = {}) # rubocop:disable Metrics/MethodLength hcloud_attributes = TyphoeusExt.collect_attributes() if x = .delete(:j) [:body] = Oj.dump(x, mode: :compat) [:method] ||= :post end q = [] q << .delete(:ep).to_s if x = .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}", 'User-Agent' => user_agent, 'Content-Type' => 'application/json' } }.merge() ) r.on_complete do |response| response.extend(TyphoeusExt) response.attributes = hcloud_attributes response.context.client = self response.check_for_error unless response.request.hydra end r end |
#server_types ⇒ Object
81 82 83 |
# File 'lib/hcloud/client.rb', line 81 def server_types ServerTypeResource.new(client: self) end |
#servers ⇒ Object
57 58 59 |
# File 'lib/hcloud/client.rb', line 57 def servers ServerResource.new(client: self) end |
#ssh_keys ⇒ Object
85 86 87 |
# File 'lib/hcloud/client.rb', line 85 def ssh_keys SSHKeyResource.new(client: self) end |
#volumes ⇒ Object
109 110 111 |
# File 'lib/hcloud/client.rb', line 109 def volumes VolumeResource.new(client: self) end |