Class: UpcloudApi
- Inherits:
-
Object
- Object
- UpcloudApi
- Defined in:
- lib/upcloud_api.rb,
lib/upcloud_api/version.rb
Constant Summary collapse
- VERSION =
"1.2.0"
Instance Method Summary collapse
-
#account_information ⇒ Object
Returns available credits.
-
#create_server(zone: "fi-hel1", title:, hostname:, core_number: 1, memory_amount: 1024, storage_devices:) ⇒ Object
Creates new server from template.
-
#delete_server(server_uuid) ⇒ Object
Deletes a server.
-
#initialize(user, password) ⇒ UpcloudApi
constructor
A new instance of UpcloudApi.
-
#login ⇒ Object
Tests that authentication to Upcloud works.
-
#modify_server(server_uuid, params) ⇒ Object
Modifies existing server.
-
#restart_server(server_uuid, type: :soft, timeout: nil, timeout_action: :ignore) ⇒ Object
Restarts down a server that is currently running.
-
#server_details(uuid) ⇒ Object
Shows details of a server.
-
#servers ⇒ Object
Lists servers associated with the account.
-
#start_server(server_uuid) ⇒ Object
Starts server that is shut down.
-
#stop_server(server_uuid, type: :soft, timeout: nil, asynchronous: false) ⇒ Object
Shuts down a server that is currently running.
-
#templates ⇒ Object
Lists templates available from Upcloud.
Constructor Details
#initialize(user, password) ⇒ UpcloudApi
Returns a new instance of UpcloudApi.
8 9 10 11 12 |
# File 'lib/upcloud_api.rb', line 8 def initialize user, password @user = user @password = password @auth = { username: @user, password: @password } end |
Instance Method Details
#account_information ⇒ Object
Returns available credits.
Calls GET /1.2/acccount
30 31 32 33 34 |
# File 'lib/upcloud_api.rb', line 30 def account_information response = get "account" data = JSON.parse response.body data["acccount"]["credits"] end |
#create_server(zone: "fi-hel1", title:, hostname:, core_number: 1, memory_amount: 1024, storage_devices:) ⇒ Object
Creates new server from template.
Calls POST /1.2/server
Storage devices should be array of hashes containing following data:
{
"action" => "clone" # Can be "create", "clone" or "attach"
"storage" => template_uuid, # Should be passed only for "clone" or "attach"
"title" => disk_name # Name of the storage,
"tier" => "maxiops" # No sense using HDD any more
}
Returns HTTParty response
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/upcloud_api.rb', line 88 def create_server zone: "fi-hel1", title:, hostname:, core_number: 1, memory_amount: 1024, storage_devices: data = { "server" => { "zone" => zone, "title" => title, "hostname" => hostname, "core_number" => core_number, "memory_amount" => memory_amount, "storage_devices" => { "storage_device" => storage_devices } } } json = JSON.generate data response = post "server", json response end |
#delete_server(server_uuid) ⇒ Object
Deletes a server.
In order to delete a server, the server must be stopped first.
Calls DELETE /1.2/server/#uuid
127 128 129 130 131 |
# File 'lib/upcloud_api.rb', line 127 def delete_server server_uuid response = delete "server/#{server_uuid}" response end |
#login ⇒ Object
Tests that authentication to Upcloud works.
This is not required to use, as authentication is used with HTTP basic auth with each request.
Calls GET /1.2/server
Returns true in success, false if not
22 23 24 25 |
# File 'lib/upcloud_api.rb', line 22 def login response = get "server" response.code == 200 end |
#modify_server(server_uuid, params) ⇒ Object
Modifies existing server.
In order to modify a server, the server must be stopped first.
Calls PUT /1.2/server/#uuid
114 115 116 117 118 119 120 |
# File 'lib/upcloud_api.rb', line 114 def modify_server server_uuid, params data = { "server" => params } json = JSON.generate data response = put "server/#{server_uuid}", json response end |
#restart_server(server_uuid, type: :soft, timeout: nil, timeout_action: :ignore) ⇒ Object
Restarts down a server that is currently running
Calls POST /1.2/server/#uuid/restart
Hard shutdown means practically same as taking the power cable off from the computer. Soft shutdown sends ACPI signal to the server, which should then automatically handle shutdown routines by itself. If timeout is given, server will be forcibly shut down after the timeout has expired.
server if timeout happens. Default is :ignore.
193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/upcloud_api.rb', line 193 def restart_server server_uuid, type: :soft, timeout: nil, timeout_action: :ignore data = { "stop_server" => { "stop_type" => type.to_s } } data["stop_server"]["timeout"] = timeout unless timeout.nil? json = JSON.generate data response = post "server/#{server_uuid}/restart", json response end |
#server_details(uuid) ⇒ Object
Shows details of a server.
Calls GET /1.2/server/#uuid
59 60 61 62 63 |
# File 'lib/upcloud_api.rb', line 59 def server_details uuid response = get "server/#{uuid}" data = JSON.parse response.body data end |
#servers ⇒ Object
Lists servers associated with the account
Calls GET /1.2/server
Returns array of servers with values
-
zone
-
core_number
-
title
-
hostname
-
memory_amount
-
uuid
-
state
48 49 50 51 52 |
# File 'lib/upcloud_api.rb', line 48 def servers response = get "server" data = JSON.parse response.body data["servers"]["server"] end |
#start_server(server_uuid) ⇒ Object
Starts server that is shut down.
Calls POST /1.2/server/#uuid/start
138 139 140 141 142 |
# File 'lib/upcloud_api.rb', line 138 def start_server server_uuid response = post "server/#{server_uuid}/start" response end |
#stop_server(server_uuid, type: :soft, timeout: nil, asynchronous: false) ⇒ Object
Shuts down a server that is currently running
Calls POST /1.2/server/#uuid/stop
Hard shutdown means practically same as taking the power cable off from the computer. Soft shutdown sends ACPI signal to the server, which should then automatically handle shutdown routines by itself. If timeout is given, server will be forcibly shut down after the timeout has expired.
Raises Timeout::Error in case server does not shut down in 300 seconds in non-asynchronous mode.
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/upcloud_api.rb', line 158 def stop_server server_uuid, type: :soft, timeout: nil, asynchronous: false data = { "stop_server" => { "stop_type" => type.to_s } } data["stop_server"]["timeout"] = timeout unless timeout.nil? json = JSON.generate data response = post "server/#{server_uuid}/stop", json return response if asynchronous Timeout 300 do loop do details = server_details server_uuid return response if details["server"]["state"] == "stopped" end end end |
#templates ⇒ Object
Lists templates available from Upcloud
Calls GET /1.2/storage/template
68 69 70 71 72 |
# File 'lib/upcloud_api.rb', line 68 def templates response = get "storage/template" data = JSON.parse response.body data end |