upcloud_api 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 00ada3fa455f28b43b44810cf220f82d45cfad1f
4
+ data.tar.gz: beb7aab61179f05542b475ad4ff38ca61e4d678e
5
+ SHA512:
6
+ metadata.gz: d79da75d918016abc1c2a7ef085df7d1152bffbe254646e7926dfedbc572130545531e215cc9c7e35564cd63514d8cbf49a8bae2480785c011390a4f0b8748c6
7
+ data.tar.gz: e0252fd9d2106ca4a7c723984eebecd54d1f83e059b3740f0fddbe598d6902e49999dc12660d30b24f64151e5021680520ebee889604b467693e42a8fb804ebc
@@ -0,0 +1,212 @@
1
+
2
+ require "httparty"
3
+
4
+ class UpcloudApi
5
+
6
+ # @param user [String] Upcloud API account
7
+ # @param password [String] Upcloud API password
8
+ def initialize user, password
9
+ @user = user
10
+ @password = password
11
+ @auth = { username: @user, password: @password }
12
+ end
13
+
14
+ # Tests that authentication to Upcloud works.
15
+ #
16
+ # This is not required to use, as authentication is used
17
+ # with HTTP basic auth with each request.
18
+ #
19
+ # Calls GET /1.2/server
20
+ #
21
+ # Returns true in success, false if not
22
+ def login
23
+ response = get "server"
24
+ response.code == 200
25
+ end
26
+
27
+ # Returns available credits.
28
+ #
29
+ # Calls GET /1.2/acccount
30
+ def account_information
31
+ response = get "account"
32
+ data = JSON.parse response.body
33
+ data["acccount"]["credits"]
34
+ end
35
+
36
+ # Lists servers associated with the account
37
+ #
38
+ # Calls GET /1.2/server
39
+ #
40
+ # Returns array of servers with values
41
+ # - zone
42
+ # - core_number
43
+ # - title
44
+ # - hostname
45
+ # - memory_amount
46
+ # - uuid
47
+ # - state
48
+ def servers
49
+ response = get "server"
50
+ data = JSON.parse response.body
51
+ data["servers"]["server"]
52
+ end
53
+
54
+ # Shows details of a server.
55
+ #
56
+ # Calls GET /1.2/server/#{uuid}
57
+ #
58
+ # @param uuid from UpcloudApi#servers
59
+ def server_details uuid
60
+ response = get "server/#{uuid}"
61
+ data = JSON.parse response.body
62
+ data
63
+ end
64
+
65
+ # Lists templates available from Upcloud
66
+ #
67
+ # Calls GET /1.2/storage/template
68
+ def templates
69
+ response = get "storage/template"
70
+ data = JSON.parse response.body
71
+ data
72
+ end
73
+
74
+ # Creates new server from template.
75
+ #
76
+ # Calls POST /1.2/server
77
+ #
78
+ # @param template_uuid Uuid from UpcloudApi#templates (a disk’s UUID)
79
+ #
80
+ # Returns HTTParty response bodys
81
+ def create_server template_uuid, disk_name
82
+ data = {
83
+ server = {
84
+ "zone" => "",
85
+ "title" => "",
86
+ "hostname" => "",
87
+ "core_number" => "",
88
+ "memory_amount" => "",
89
+ "storage_devices" => [
90
+ "action" => "clone",
91
+ "storage" => template_uuid,
92
+ "title" => disk_name
93
+ ]
94
+ }
95
+ }
96
+
97
+ json = JSON.encode data
98
+ response = post "server", json
99
+
100
+ response
101
+ end
102
+
103
+ # Modifies existing server.
104
+ #
105
+ # In order to modify a server, the server must be stopped first.
106
+ #
107
+ # Calls PUT /1.2/server/#{uuid}
108
+ #
109
+ # @param server_uuid [String] UUID of the server that will be modified.
110
+ # @param params [Hash] Hash of params that will be passed to be changed.
111
+ def modify_server server_uuid, params
112
+ data = { "server" => params }
113
+ json = JSON.encode data
114
+ response = put "server/#{server_uuid}", json
115
+
116
+ response
117
+ end
118
+
119
+ # Deletes a server.
120
+ #
121
+ # In order to delete a server, the server must be stopped first.
122
+ #
123
+ # Calls DELETE /1.2/server/#{uuid}
124
+ def delete_server server_uuid
125
+ response = delete "server/#{server_uuid}"
126
+
127
+ response
128
+ end
129
+
130
+ # Starts server that is shut down.
131
+ #
132
+ # Calls POST /1.2/server/#{uuid}/start
133
+ #
134
+ # @param server_uuid UUID of the server
135
+ def start_server server_uuid
136
+ response = post "server/#{server_uuid}/start"
137
+
138
+ response
139
+ end
140
+
141
+ # Shuts down a server that is currently running
142
+ #
143
+ # Calls POST /1.2/server/#{uuid}/stop
144
+ #
145
+ # Hard shutdown means practically same as taking the power cable off from the computer.
146
+ # Soft shutdown sends ACPI signal to the server, which should then automatically handle shutdown routines by itself.
147
+ # If timeout is given, server will be forcibly shut down after the timeout has expired.
148
+ #
149
+ # @param server_uuid UUID of the server
150
+ # @param type Type of the shutdown. Available types are :hard and :soft. Defaults to :soft.
151
+ # @param timeout Time after server will be hard stopped if it didn’t close cleanly. Only affects :soft type.
152
+ def start_server server_uuid, type: :soft, timeout: nil
153
+ data = {
154
+ "stop_server" => {
155
+ "stop_type" => type.to_s
156
+ }
157
+ }
158
+ data["stop_server"]["timeout"] = timeout unless timeout.nil?
159
+
160
+ json = JSON.encode data
161
+
162
+ response = post "server/#{server_uuid}/stop", json
163
+
164
+ response
165
+ end
166
+
167
+ # Restarts down a server that is currently running
168
+ #
169
+ # Calls POST /1.2/server/#{uuid}/restart
170
+ #
171
+ # Hard shutdown means practically same as taking the power cable off from the computer.
172
+ # Soft shutdown sends ACPI signal to the server, which should then automatically handle shutdown routines by itself.
173
+ # If timeout is given, server will be forcibly shut down after the timeout has expired.
174
+ #
175
+ # @param server_uuid UUID of the server
176
+ # @param type Type of the shutdown. Available types are :hard and :soft. Defaults to :soft.
177
+ # @param timeout Time after server will be hard stopped if it didn’t close cleanly. Only affects :soft type.
178
+ # @param timeout_action What will happen when timeout happens. :destroy hard stops the server and :ignore makes
179
+ # server if timeout happens. Default is :ignore.
180
+ def start_server server_uuid, type: :soft, timeout: nil, timeout_action: :ignore
181
+ data = {
182
+ "stop_server" => {
183
+ "stop_type" => type.to_s
184
+ }
185
+ }
186
+ data["stop_server"]["timeout"] = timeout unless timeout.nil?
187
+
188
+ json = JSON.encode data
189
+
190
+ response = post "server/#{server_uuid}/restart", json
191
+
192
+ response
193
+ end
194
+
195
+ private
196
+
197
+ def get action
198
+ HTTParty.get "https://api.upcloud.com/1.2/#{action}", basic_auth: @auth
199
+ end
200
+
201
+ def post action, body = ""
202
+ HTTParty.post "https://api.upcloud.com/1.2/#{action}", basic_auth: @auth, body: body
203
+ end
204
+
205
+ def put action, body = ""
206
+ HTTParty.put "https://api.upcloud.com/1.2/#{action}", basic_auth: @auth, body: body
207
+ end
208
+
209
+ def delete action, body = ""
210
+ HTTParty.delete "https://api.upcloud.com/1.2/#{action}", basic_auth: @auth
211
+ end
212
+ end
@@ -0,0 +1,3 @@
1
+ class UpcloudApi
2
+ VERSION = "1.0.0"
3
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: upcloud_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Samu Voutilainen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.13'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.13'
27
+ description: Ruby implementation of Upcloud API, meant for programmable maintenance
28
+ of virtual private servers in Upcloud’s system.
29
+ email: smar@smar.fi
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - lib/upcloud_api.rb
35
+ - lib/upcloud_api/version.rb
36
+ homepage: https://github.com/Smarre/upcloud_api
37
+ licenses:
38
+ - MIT
39
+ metadata: {}
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubyforge_project:
56
+ rubygems_version: 2.4.6
57
+ signing_key:
58
+ specification_version: 4
59
+ summary: Implementation of Upcloud API for VPS management
60
+ test_files: []
61
+ has_rdoc: