class Kontena::Machine::Upcloud::Client
Constants
- ACCEPT
- API_URL
- APP_JSON
- CTYPE
- CTYPE_HEAD
Attributes
http_client[R]
Public Class Methods
new(username, password)
click to toggle source
# 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
Public Instance Methods
api_access?()
click to toggle source
# 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)
click to toggle source
# File lib/kontena/machine/upcloud/client.rb, line 34 def delete(path) request(method: :delete, path: path) end
find_plan(name)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/kontena/machine/upcloud/client.rb, line 26 def get(path) request(method: :get, path: path) end
get_server(id)
click to toggle source
# File lib/kontena/machine/upcloud/client.rb, line 58 def get_server(id) get("server/#{id}").fetch(:server, nil) end
list_plans()
click to toggle source
# File lib/kontena/machine/upcloud/client.rb, line 46 def list_plans get('plan')[:plans][:plan] end
list_zones()
click to toggle source
# File lib/kontena/machine/upcloud/client.rb, line 50 def list_zones get('zone')[:zones][:zone] end
post(path, body)
click to toggle source
# 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)
click to toggle source
# File lib/kontena/machine/upcloud/client.rb, line 54 def zone_exist?(name) list_zones.any? { |zone| zone[:id] == name } end
Private Instance Methods
request(opts)
click to toggle source
# File lib/kontena/machine/upcloud/client.rb, line 71 def request(opts) response = http_client.request( opts.merge( path: "/1.2/#{opts[:path]}", headers: (opts[:headers] || {}).merge(ACCEPT => APP_JSON), ) ) if (200..299).cover?(response.status) if response.body && response.body.start_with?('{'.freeze) JSON.parse(response.body, symbolize_names: true) else { success: true } end else raise "Request to Upcloud failed: #{response.status} #{response.body}" end end