Class: Kontena::Machine::Upcloud::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/kontena/machine/upcloud/client.rb

Constant Summary collapse

API_URL =
'https://api.upcloud.com'.freeze
ACCEPT =
'Accept'.freeze
CTYPE =
'Content-Type'.freeze
APP_JSON =
'application/json'.freeze
CTYPE_HEAD =
{ CTYPE => APP_JSON }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password) ⇒ Client

Returns a new instance of Client.



17
18
19
20
21
22
23
24
# File 'lib/kontena/machine/upcloud/client.rb', line 17

def initialize(username, password)
  @http_client = Excon.new(
    API_URL,
    omit_default_port: true,
    user: username,
    password: password,
  )
end

Instance Attribute Details

#http_clientObject (readonly)

Returns the value of attribute http_client.



15
16
17
# File 'lib/kontena/machine/upcloud/client.rb', line 15

def http_client
  @http_client
end

Instance Method Details

#api_access?Boolean

Returns:

  • (Boolean)


62
63
64
65
66
67
# File 'lib/kontena/machine/upcloud/client.rb', line 62

def api_access?
  response = get('account')
  response.kind_of?(Hash) && response.has_key?(:account)
rescue
  false
end

#delete(path) ⇒ Object



34
35
36
# File 'lib/kontena/machine/upcloud/client.rb', line 34

def delete(path)
  request(method: :delete, path: path)
end

#find_plan(name) ⇒ Object



42
43
44
# File 'lib/kontena/machine/upcloud/client.rb', line 42

def find_plan(name)
  list_plans.find{|s| s[:name].downcase.eql?(name.downcase)}
end

#find_template(name) ⇒ Object



38
39
40
# File 'lib/kontena/machine/upcloud/client.rb', line 38

def find_template(name)
  get('storage/template')[:storages][:storage].find{|s| s[:title].downcase.start_with?(name.downcase)}
end

#get(path) ⇒ Object



26
27
28
# File 'lib/kontena/machine/upcloud/client.rb', line 26

def get(path)
  request(method: :get, path: path)
end

#get_server(id) ⇒ Object



58
59
60
# File 'lib/kontena/machine/upcloud/client.rb', line 58

def get_server(id)
  get("server/#{id}").fetch(:server, nil)
end

#list_plansObject



46
47
48
# File 'lib/kontena/machine/upcloud/client.rb', line 46

def list_plans
  get('plan')[:plans][:plan]
end

#list_zonesObject



50
51
52
# File 'lib/kontena/machine/upcloud/client.rb', line 50

def list_zones
  get('zone')[:zones][:zone]
end

#post(path, body) ⇒ Object



30
31
32
# File 'lib/kontena/machine/upcloud/client.rb', line 30

def post(path, body)
  request(method: :post, path: path, body: body, headers: CTYPE_HEAD)
end

#zone_exist?(name) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/kontena/machine/upcloud/client.rb', line 54

def zone_exist?(name)
  list_zones.any? { |zone| zone[:id] == name }
end