upcloud_api 1.5.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6837056b4cff07402d608702a95d56d6be14ec38
4
- data.tar.gz: 82526825eb93b43aef2160e3fc1ebcb5a689114d
3
+ metadata.gz: 9b9499c81732dab73f760bcfc382a7c288d82960
4
+ data.tar.gz: 61ec8087b5b00f870495fdf4c9859e962c8675f4
5
5
  SHA512:
6
- metadata.gz: 3ff71fa95959d11049d3d55398fd17f8b84baf43600c6bf38990ae9ccf58ba5521b3b82d405a1691085257854caa0d90755421d2ec0a4d5734b72a06eb879819
7
- data.tar.gz: 7a158330aff7be804d5a9f386bf6b0baff416ef6b176eb4e3521d0de512384b9381051fb77338f0f35c083f1259adb4a052921a5c734e138157dfa0fd597d970
6
+ metadata.gz: 52496d8d4289e67cf15da5703500e497201d312553c78eaf58e548d1958796075177405ae5b5acc75dbfc1a594bc6b6fc7913cc8d789c7cce7cbdbb47b4c7e8e
7
+ data.tar.gz: edab2b929517934e42e082142ae19b5c186116b4a3cc248cf31df506974b415641dd4e46efbe07d175fa100c120b63f642bcd799ac06352ac35de4a0e66d3660
data/lib/upcloud_api.rb CHANGED
@@ -19,7 +19,7 @@ class UpcloudApi
19
19
  #
20
20
  # Calls GET /1.2/server
21
21
  #
22
- # Returns true in success, false if not
22
+ # Returns true in success, false if not.
23
23
  def login
24
24
  response = get "server"
25
25
  response.code == 200
@@ -27,7 +27,13 @@ class UpcloudApi
27
27
 
28
28
  # Returns available server configurations.
29
29
  #
30
- # Calls GET /1.2/server_size
30
+ # Calls GET /1.2/server_size.
31
+ #
32
+ # Returns array of server size hashes:
33
+ # {
34
+ # "core_number": "1",
35
+ # "memory_amount": "512"
36
+ # }
31
37
  def server_configurations
32
38
  response = get "server_size"
33
39
  response["server_sizes"]["server_size"]
@@ -36,17 +42,19 @@ class UpcloudApi
36
42
  # Returns available credits.
37
43
  #
38
44
  # Calls GET /1.2/acccount
45
+ #
46
+ # Returns available credits in the account as a string.
39
47
  def account_information
40
48
  response = get "account"
41
49
  data = JSON.parse response.body
42
50
  data["account"]["credits"]
43
51
  end
44
52
 
45
- # Lists servers associated with the account
53
+ # Lists servers associated with the account.
46
54
  #
47
- # Calls GET /1.2/server
55
+ # Calls GET /1.2/server.
48
56
  #
49
- # Returns array of servers with values
57
+ # Returns array of servers with following values or empty array if no servers found.
50
58
  # - zone
51
59
  # - core_number
52
60
  # - title
@@ -62,54 +70,121 @@ class UpcloudApi
62
70
 
63
71
  # Shows details of a server.
64
72
  #
65
- # Calls GET /1.2/server/#{uuid}
73
+ # Calls GET /1.2/server/_uuid_.
66
74
  #
67
75
  # @param uuid from UpcloudApi#servers
76
+ #
77
+ # Returns hash of server details or nil:
78
+ # {
79
+ # "boot_order" => "cdrom,disk",
80
+ # "core_number" => "3",
81
+ # "firewall" => "off",
82
+ # "hostname" => "dummy",
83
+ # "ip_addresses" => {
84
+ # "ip_address" => [
85
+ # {
86
+ # "access" => "private",
87
+ # "address" => "192.168.0.1",
88
+ # "family" => "IPv4"
89
+ # },
90
+ # {
91
+ # "access" => "public",
92
+ # "address" => "::1",
93
+ # "family" => "IPv6"
94
+ # },
95
+ # {
96
+ # "access" => "public",
97
+ # "address" => "198.51.100.1",
98
+ # "family" => "IPv4"
99
+ # }
100
+ # ]
101
+ # },
102
+ # "license" => 0,
103
+ # "memory_amount" => "3072",
104
+ # "nic_model" => "virtio",
105
+ # "plan" => "custom",
106
+ # "state" => "stopped",
107
+ # "storage_devices" => {
108
+ # "storage_device" => [
109
+ # {
110
+ # "address" => "virtio:1",
111
+ # "storage" => "storage_uuid",
112
+ # "storage_size" => 10,
113
+ # "storage_title" => "Disk name",
114
+ # "type" => "disk"
115
+ # }
116
+ # ]
117
+ # },
118
+ # "tags" => {"tag" => []},
119
+ # "timezone" => "UTC",
120
+ # "title" => "Server name",
121
+ # "uuid" => "uuid",
122
+ # "video_model" => "cirrus",
123
+ # "vnc" => "off",
124
+ # "vnc_password" => "1234",
125
+ # "zone" => "de-fra1"
126
+ # }
68
127
  def server_details(uuid)
69
128
  response = get "server/#{uuid}"
70
129
  data = JSON.parse response.body
71
- data
72
- end
73
130
 
74
- # Lists templates available from Upcloud
75
- #
76
- # Calls GET /1.2/storage/template
77
- def templates
78
- response = get "storage/template"
79
- data = JSON.parse response.body
80
- data
131
+ return nil if data["server"].nil?
132
+
133
+ data["server"]
81
134
  end
82
135
 
83
136
  # Creates new server from template.
84
137
  #
85
- # Calls POST /1.2/server
138
+ # Calls POST /1.2/server.
86
139
  #
87
140
  # Storage devices should be array of hashes containing following data:
88
141
  #
89
142
  # {
90
- # "action" => "clone" # Can be "create", "clone" or "attach"
91
- # "storage" => template_uuid, # Should be passed only for "clone" or "attach"
92
- # "title" => disk_name # Name of the storage,
93
- # "tier" => "maxiops" # No sense using HDD any more
143
+ # "action" => "clone" # Can be "create", "clone" or "attach"
144
+ # "storage" => template_uuid, # Should be passed only for "clone" or "attach"
145
+ # "title" => disk_name, # Name of the storage,
146
+ # "tier" => "maxiops", # No sense using HDD any more
147
+ # }
148
+ #
149
+ # @param plan [String] Preconfigured plan for the server. If nil, a custom plan will be created from input data, otherwise this overrides custom configuration.
150
+ # Predefined plans can be fetched with {#plans}.
151
+ #
152
+ # login_user should be following hash or nil:
153
+ # {
154
+ # "username": "upclouduser",
155
+ # "ssh_keys": {
156
+ # "ssh_key": [
157
+ # "ssh-rsa AAAAB3NzaC1yc2EAA[...]ptshi44x user@some.host",
158
+ # "ssh-dss AAAAB3NzaC1kc3MAA[...]VHRzAA== someuser@some.other.host"
159
+ # ]
160
+ # }
94
161
  # }
95
162
  #
96
163
  # ip_addresses should be an array containing :public, :private and/or :ipv6. It defaults to
97
164
  # :all, which means the server will get public IPv4, private IPv4 and public IPv6 addresses.
98
165
  #
99
- # Returns HTTParty response
166
+ # @param other [Hash] Other optional arguments create_server API call takes. See Upcloud’s documentation for possible values.
167
+ #
168
+ # Returns HTTParty response object.
100
169
  def create_server(zone: "fi-hel1", title:, hostname:, core_number: 1,
101
- memory_amount: 1024, storage_devices:, ip_addresses: :all)
170
+ memory_amount: 1024, storage_devices:, ip_addresses: :all,
171
+ plan: nil, login_user: nil, other: nil)
102
172
  data = {
103
173
  "server" => {
104
174
  "zone" => zone,
105
175
  "title" => title,
106
176
  "hostname" => hostname,
107
- "core_number" => core_number,
108
- "memory_amount" => memory_amount,
109
177
  "storage_devices" => { "storage_device" => storage_devices }
110
178
  }
111
179
  }
112
180
 
181
+ if plan.nil?
182
+ data["server"]["core_number"] = core_number
183
+ data["server"]["memory_amount"] = memory_amount
184
+ else
185
+ data["server"]["plan"] = plan
186
+ end
187
+
113
188
  if ip_addresses != :all
114
189
  ips = []
115
190
  ips << { "access" => "public", "family" => "IPv4" } if ip_addresses.include? :public
@@ -120,6 +195,14 @@ class UpcloudApi
120
195
  data["server"]["ip_addresses"]["ip_address"] = ips
121
196
  end
122
197
 
198
+ unless login_user.nil?
199
+ data["login_user"] = login_user
200
+ end
201
+
202
+ unless other.nil?
203
+ data.merge! other
204
+ end
205
+
123
206
  json = JSON.generate data
124
207
  response = post "server", json
125
208
  response
@@ -129,10 +212,12 @@ class UpcloudApi
129
212
  #
130
213
  # In order to modify a server, the server must be stopped first.
131
214
  #
132
- # Calls PUT /1.2/server/#{uuid}
215
+ # Calls PUT /1.2/server/_uuid_.
133
216
  #
134
217
  # @param server_uuid [String] UUID of the server that will be modified.
135
218
  # @param params [Hash] Hash of params that will be passed to be changed.
219
+ #
220
+ # Returns HTTParty response object.
136
221
  def modify_server(server_uuid, params)
137
222
  data = { "server" => params }
138
223
  json = JSON.generate data
@@ -145,7 +230,9 @@ class UpcloudApi
145
230
  #
146
231
  # In order to delete a server, the server must be stopped first.
147
232
  #
148
- # Calls DELETE /1.2/server/#{uuid}
233
+ # Calls DELETE /1.2/server/_uuid_.
234
+ #
235
+ # Returns HTTParty response object.
149
236
  def delete_server(server_uuid)
150
237
  response = delete "server/#{server_uuid}"
151
238
 
@@ -154,18 +241,20 @@ class UpcloudApi
154
241
 
155
242
  # Starts server that is shut down.
156
243
  #
157
- # Calls POST /1.2/server/#{uuid}/start
244
+ # Calls POST /1.2/server/_uuid_/start.
158
245
  #
159
- # @param server_uuid UUID of the server
246
+ # @param server_uuid UUID of the server.
247
+ #
248
+ # Returns HTTParty response object.
160
249
  def start_server(server_uuid)
161
250
  response = post "server/#{server_uuid}/start"
162
251
 
163
252
  response
164
253
  end
165
254
 
166
- # Shuts down a server that is currently running
255
+ # Shuts down a server that is currently running.
167
256
  #
168
- # Calls POST /1.2/server/#{uuid}/stop
257
+ # Calls POST /1.2/server/_uuid_/stop.
169
258
  #
170
259
  # Hard shutdown means practically same as taking the power cable
171
260
  # off from the computer. Soft shutdown sends ACPI signal to the server,
@@ -183,6 +272,8 @@ class UpcloudApi
183
272
  #
184
273
  # Raises Timeout::Error in case server does not shut down in 300
185
274
  # seconds in non-asynchronous mode.
275
+ #
276
+ # Returns HTTParty response object if server was removed successfully or request is asynchronous and nil otherwise
186
277
  def stop_server(server_uuid, type: :soft, timeout: nil, asynchronous: false)
187
278
  data = {
188
279
  "stop_server" => {
@@ -200,15 +291,17 @@ class UpcloudApi
200
291
  Timeout.timeout 300 do
201
292
  loop do
202
293
  details = server_details server_uuid
203
- return response if details["server"].nil?
204
- return response if details["server"]["state"] == "stopped"
294
+ return response if details.nil?
295
+ return response if details["state"] == "stopped"
205
296
  end
206
297
  end
298
+
299
+ nil
207
300
  end
208
301
 
209
- # Restarts a server that is currently running
302
+ # Restarts a server that is currently running.
210
303
  #
211
- # Calls POST /1.2/server/#{uuid}/restart
304
+ # Calls POST /1.2/server/_uuid_/restart.
212
305
  #
213
306
  # Hard shutdown means practically same as taking the power cable
214
307
  # off from the computer. Soft shutdown sends ACPI signal to the server,
@@ -224,8 +317,9 @@ class UpcloudApi
224
317
  # @param timeout_action What will happen when timeout happens.
225
318
  # :destroy hard stops the server and :ignore stops the operation
226
319
  # if timeout happens. Default is :ignore.
227
- def restart_server(server_uuid, type: :soft, timeout: nil,
228
- timeout_action: :ignore)
320
+ #
321
+ # Returns HTTParty response object.
322
+ def restart_server(server_uuid, type: :soft, timeout: nil, timeout_action: :ignore)
229
323
  data = {
230
324
  "restart_server" => {
231
325
  "stop_type" => type.to_s,
@@ -243,7 +337,7 @@ class UpcloudApi
243
337
 
244
338
  # Lists all storages or storages matching to given type.
245
339
  #
246
- # Calls GET /1.2/storage or /1.2/storage/#{type}
340
+ # Calls GET /1.2/storage or /1.2/storage/_type_.
247
341
  #
248
342
  # Available types:
249
343
  # - public
@@ -254,27 +348,46 @@ class UpcloudApi
254
348
  # - template
255
349
  # - favorite
256
350
  #
257
- # @param type Type of the storages to be returned on nil
351
+ # @param type Type of the storages to be returned on nil.
352
+ #
353
+ # Returns array of storages, inside "storage" key in the API or empty array if none found.
258
354
  def storages(type: nil)
259
355
  response = get(type && "storage/#{type}" || "storage")
260
356
  data = JSON.parse response.body
261
- data
357
+ data["storages"]["storage"]
262
358
  end
263
359
 
264
360
  # Shows detailed information of single storage.
265
361
  #
266
- # Calls GET /1.2/storage/#{uuid}
362
+ # Calls GET /1.2/storage/_uuid_.
363
+ #
364
+ # @param storage_uuid UUID of the storage.
267
365
  #
268
- # @param storage_uuid UUID of the storage
366
+ # Returns hash of following storage details or nil:
367
+ # {
368
+ # "access" => "public",
369
+ # "license" => 0,
370
+ # "servers" => {
371
+ # "server"=> []
372
+ # },
373
+ # "size" => 1,
374
+ # "state" => "online",
375
+ # "title" => "Windows Server 2003 R2 Standard (CD 1)",
376
+ # "type" => "cdrom",
377
+ # "uuid" => "01000000-0000-4000-8000-000010010101"
378
+ # }
269
379
  def storage_details(storage_uuid)
270
380
  response = get "storage/#{storage_uuid}"
271
381
  data = JSON.parse response.body
272
- data
382
+
383
+ return nil if data["storage"].nil?
384
+
385
+ data["storage"]
273
386
  end
274
387
 
275
388
  # Creates new storage.
276
389
  #
277
- # Calls POST /1.2/storage
390
+ # Calls POST /1.2/storage.
278
391
  #
279
392
  # backup_rule should be hash with following attributes:
280
393
  # - interval # allowed values: daily / mon / tue / wed / thu / fri / sat / sun
@@ -289,6 +402,8 @@ class UpcloudApi
289
402
  # with the server
290
403
  # @param backup_rule Hash of backup information. If not given, no
291
404
  # backups will be automatically created.
405
+ #
406
+ # Returns HTTParty response object.
292
407
  def create_storage(size:, tier: "maxiops", title:, zone: "fi-hel1",
293
408
  backup_rule: nil)
294
409
  data = {
@@ -309,7 +424,7 @@ class UpcloudApi
309
424
 
310
425
  # Modifies existing storage.
311
426
  #
312
- # Calls PUT /1.2/storage/#{uuid}
427
+ # Calls PUT /1.2/storage/_uuid_.
313
428
  #
314
429
  # backup_rule should be hash with following attributes:
315
430
  # - interval # allowed values: daily / mon / tue / wed / thu / fri / sat / sun
@@ -321,6 +436,8 @@ class UpcloudApi
321
436
  # @param title Name of the disk
322
437
  # @param backup_rule Hash of backup information. If not given, no
323
438
  # backups will be automatically created.
439
+ #
440
+ # Returns HTTParty response object.
324
441
  def modify_storage(storage_uuid, size:, title:, backup_rule: nil)
325
442
  data = {
326
443
  "storage" => {
@@ -341,7 +458,7 @@ class UpcloudApi
341
458
  #
342
459
  # This operation is asynchronous.
343
460
  #
344
- # Calls POST /1.2/storage/#{uuid}/clone
461
+ # Calls POST /1.2/storage/_uuid_/clone.
345
462
  #
346
463
  # @param storage_uuid UUID of the storage that will be modified
347
464
  # @param tier Type of the disk. maxiops is SSD powered disk, other
@@ -349,6 +466,8 @@ class UpcloudApi
349
466
  # @param title Name of the disk
350
467
  # @param zone Where the disk will reside. Needs to be within same zone
351
468
  # with the server
469
+ #
470
+ # Returns HTTParty response object.
352
471
  def clone_storage(storage_uuid, zone: "fi-hel1", title:, tier: "maxiops")
353
472
  data = {
354
473
  "storage" => {
@@ -369,10 +488,12 @@ class UpcloudApi
369
488
  #
370
489
  # This operation is asynchronous.
371
490
  #
372
- # Calls POST /1.2/storage/#{uuid}/templatize
491
+ # Calls POST /1.2/storage/_uuid_/templatize.
373
492
  #
374
493
  # @param storage_uuid UUID of the storage that will be templatized
375
494
  # @param title Name of the template storage
495
+ #
496
+ # Returns HTTParty response object.
376
497
  def templatize_storage(storage_uuid, title:)
377
498
  data = {
378
499
  "storage" => {
@@ -390,7 +511,7 @@ class UpcloudApi
390
511
  # Attaches a storage to a server. Server must be stopped before the
391
512
  # storage can be attached.
392
513
  #
393
- # Calls POST /1.2/server/#{server_uuid}/storage/attach
514
+ # Calls POST /1.2/server/_server_uuid_/storage/attach.
394
515
  #
395
516
  # Valid values for address are: ide[01]:[01] / scsi:0:[0-7] / virtio:[0-7]
396
517
  #
@@ -399,6 +520,8 @@ class UpcloudApi
399
520
  # @param type Type of the disk. Valid values are "disk" and "cdrom".
400
521
  # @param address Address where the disk will be attached to. Defaults
401
522
  # to next available address.
523
+ #
524
+ # Returns HTTParty response object.
402
525
  def attach_storage(server_uuid, storage_uuid:, type: "disk", address: nil)
403
526
  data = {
404
527
  "storage_device" => {
@@ -418,10 +541,12 @@ class UpcloudApi
418
541
  # Detaches storage from a server. Server must be stopped before the
419
542
  # storage can be detached.
420
543
  #
421
- # Calls POST /1.2/server/#{server_uuid}/storage/detach
544
+ # Calls POST /1.2/server/_server_uuid_/storage/detach.
422
545
  #
423
546
  # @param server_uuid UUID of the server from which to detach the storage.
424
547
  # @param address Address where the storage that will be detached resides.
548
+ #
549
+ # Returns HTTParty response object.
425
550
  def detach_storage(server_uuid, address:)
426
551
  data = {
427
552
  "storage_device" => {
@@ -440,10 +565,12 @@ class UpcloudApi
440
565
  #
441
566
  # This operation is asynchronous.
442
567
  #
443
- # Calls /1.2/storage/#{uuid}/backup
568
+ # Calls /1.2/storage/_uuid_/backup
444
569
  #
445
570
  # @param storage_uuid UUID of the storage to be backed-up
446
571
  # @param title Name of the backup
572
+ #
573
+ # Returns HTTParty response object.
447
574
  def create_backup(storage_uuid, title:)
448
575
  data = {
449
576
  "storage" => {
@@ -462,31 +589,37 @@ class UpcloudApi
462
589
  #
463
590
  # If the storage is attached to server, the server must first be stopped.
464
591
  #
465
- # Calls /1.2/storage/#{backup_uuid}/restore.
592
+ # Calls /1.2/storage/_backup_uuid_/restore.
466
593
  #
467
594
  # @param backup_uuid UUID of the backup
595
+ #
596
+ # Returns HTTParty response object.
468
597
  def restore_backup(backup_uuid)
469
598
  response = post "storage/#{backup_uuid}/restore"
470
599
 
471
600
  response
472
601
  end
473
602
 
474
- # Adds storage to favorites
603
+ # Adds storage to favorites.
475
604
  #
476
- # Calls POST /1.2/storage/#{storage_uuid}/favorite.
605
+ # Calls POST /1.2/storage/_storage_uuid_/favorite.
477
606
  #
478
607
  # @param storage_uuid UUID of the storage to be included in favorites
608
+ #
609
+ # Returns HTTParty response object.
479
610
  def favorite_storage(storage_uuid)
480
611
  response = post "storage/#{storage_uuid}/favorite"
481
612
 
482
613
  response
483
614
  end
484
615
 
485
- # Removes storage to favorites
616
+ # Removes storage to favorites.
486
617
  #
487
- # Calls POST /1.2/storage/#{storage_uuid}/favorite.
618
+ # Calls POST /1.2/storage/_storage_uuid_/favorite.
488
619
  #
489
620
  # @param storage_uuid UUID of the storage to be removed from favorites
621
+ #
622
+ # Returns HTTParty response object.
490
623
  def defavorite_storage(storage_uuid)
491
624
  response = delete "storage/#{storage_uuid}/favorite"
492
625
 
@@ -500,12 +633,34 @@ class UpcloudApi
500
633
  # Backups will not be deleted.
501
634
  #
502
635
  # @param storage_uuid UUID of the storage that will be deleted.
636
+ #
637
+ # Returns HTTParty response object.
503
638
  def delete_storage(storage_uuid)
504
639
  response = delete "storage/#{storage_uuid}"
505
640
 
506
641
  response
507
642
  end
508
643
 
644
+ # Lists available predefined plans that can be used to create a server.
645
+ #
646
+ # Returns Array of plan hashes:
647
+ # [
648
+ # {
649
+ # "core_number" : 1,
650
+ # "memory_amount" : 1024,
651
+ # "name" : "1xCPU-1GB",
652
+ # "public_traffic_out" : 2048,
653
+ # "storage_size" : 30,
654
+ # "storage_tier" : "maxiops"
655
+ # }
656
+ # ]
657
+ def plans
658
+ response = get "plan"
659
+
660
+ data = JSON.parse response.body
661
+ data["plans"]["plan"]
662
+ end
663
+
509
664
  private
510
665
 
511
666
  def get(action)
@@ -1,3 +1,3 @@
1
1
  class UpcloudApi
2
- VERSION = "1.5.0".freeze
2
+ VERSION = "2.0.0".freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: upcloud_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samu Voutilainen
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-04-22 00:00:00.000000000 Z
12
+ date: 2017-01-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: httparty
@@ -54,9 +54,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
54
54
  version: '0'
55
55
  requirements: []
56
56
  rubyforge_project:
57
- rubygems_version: 2.4.8
57
+ rubygems_version: 2.5.1
58
58
  signing_key:
59
59
  specification_version: 4
60
60
  summary: Implementation of Upcloud API for VPS management
61
61
  test_files: []
62
- has_rdoc: