Class: Kontena::Machine::Upcloud::Client
- Inherits:
-
Object
- Object
- Kontena::Machine::Upcloud::Client
- 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
-
#http_client ⇒ Object
readonly
Returns the value of attribute http_client.
Instance Method Summary collapse
- #api_access? ⇒ Boolean
- #delete(path) ⇒ Object
- #find_plan(name) ⇒ Object
- #find_template(name) ⇒ Object
- #get(path) ⇒ Object
- #get_server(id) ⇒ Object
-
#initialize(username, password) ⇒ Client
constructor
A new instance of Client.
- #list_plans ⇒ Object
- #list_zones ⇒ Object
- #post(path, body) ⇒ Object
- #zone_exist?(name) ⇒ Boolean
Constructor Details
Instance Attribute Details
#http_client ⇒ Object (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
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_plans ⇒ Object
46 47 48 |
# File 'lib/kontena/machine/upcloud/client.rb', line 46 def list_plans get('plan')[:plans][:plan] end |
#list_zones ⇒ Object
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
54 55 56 |
# File 'lib/kontena/machine/upcloud/client.rb', line 54 def zone_exist?(name) list_zones.any? { |zone| zone[:id] == name } end |