Class: Hcloud::Client

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

.connectionObject

Raises:

  • (ArgumentError)


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

def connection
  return @connection if @connection.is_a? Hcloud::Client

  raise ArgumentError, "client not correctly initialized, actually #{@client.inspect}"
end

Instance Attribute Details

#auto_paginationObject (readonly)

Returns the value of attribute auto_pagination.



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

def auto_pagination
  @auto_pagination
end

#hydraObject (readonly)

Returns the value of attribute hydra.



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

def hydra
  @hydra
end

#tokenObject (readonly)

Returns the value of attribute token.



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

def token
  @token
end

#user_agentObject (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

#actionsObject



61
62
63
# File 'lib/hcloud/client.rb', line 61

def actions
  ActionResource.new(client: self)
end

#authorized?Boolean

Returns:

  • (Boolean)


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

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

#concurrentObject



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

Returns:

  • (Boolean)


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

def concurrent?
  !@concurrent.nil?
end

#datacentersObject



73
74
75
# File 'lib/hcloud/client.rb', line 73

def datacenters
  DatacenterResource.new(client: self)
end

#floating_ipsObject



89
90
91
# File 'lib/hcloud/client.rb', line 89

def floating_ips
  FloatingIPResource.new(client: self)
end

#imagesObject



69
70
71
# File 'lib/hcloud/client.rb', line 69

def images
  ImageResource.new(client: self)
end

#isosObject



65
66
67
# File 'lib/hcloud/client.rb', line 65

def isos
  IsoResource.new(client: self)
end

#locationsObject



77
78
79
# File 'lib/hcloud/client.rb', line 77

def locations
  LocationResource.new(client: self)
end

#networksObject



93
94
95
# File 'lib/hcloud/client.rb', line 93

def networks
  NetworkResource.new(client: self)
end

#prepare_request(url, args = {}, &block) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/hcloud/client.rb', line 111

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

#request(path, options = {}) ⇒ Object

rubocop:disable Metrics/MethodLength



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/hcloud/client.rb', line 119

def request(path, options = {}) # rubocop:disable Metrics/MethodLength
  hcloud_attributes = TyphoeusExt.collect_attributes(options)
  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}",
        'User-Agent' => user_agent,
        'Content-Type' => 'application/json'
      }
    }.merge(options)
  )
  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_typesObject



81
82
83
# File 'lib/hcloud/client.rb', line 81

def server_types
  ServerTypeResource.new(client: self)
end

#serversObject



57
58
59
# File 'lib/hcloud/client.rb', line 57

def servers
  ServerResource.new(client: self)
end

#ssh_keysObject



85
86
87
# File 'lib/hcloud/client.rb', line 85

def ssh_keys
  SSHKeyResource.new(client: self)
end

#volumesObject



97
98
99
# File 'lib/hcloud/client.rb', line 97

def volumes
  VolumeResource.new(client: self)
end