cloudstack_client 0.2.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +3 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +21 -0
  5. data/README.md +26 -0
  6. data/cloudstack_client.gemspec +24 -0
  7. data/lib/cloudstack_client.rb +2 -0
  8. data/lib/cloudstack_client/client.rb +136 -0
  9. data/lib/cloudstack_client/commands/account.rb +22 -0
  10. data/lib/cloudstack_client/commands/capacity.rb +19 -0
  11. data/lib/cloudstack_client/commands/cluster.rb +19 -0
  12. data/lib/cloudstack_client/commands/disk_offering.rb +49 -0
  13. data/lib/cloudstack_client/commands/domain.rb +22 -0
  14. data/lib/cloudstack_client/commands/host.rb +28 -0
  15. data/lib/cloudstack_client/commands/ip_address.rb +82 -0
  16. data/lib/cloudstack_client/commands/iso.rb +64 -0
  17. data/lib/cloudstack_client/commands/job.rb +29 -0
  18. data/lib/cloudstack_client/commands/load_balancer_rule.rb +61 -0
  19. data/lib/cloudstack_client/commands/network.rb +128 -0
  20. data/lib/cloudstack_client/commands/pod.rb +19 -0
  21. data/lib/cloudstack_client/commands/port_forwarding_rule.rb +49 -0
  22. data/lib/cloudstack_client/commands/project.rb +32 -0
  23. data/lib/cloudstack_client/commands/router.rb +89 -0
  24. data/lib/cloudstack_client/commands/server.rb +311 -0
  25. data/lib/cloudstack_client/commands/service_offering.rb +98 -0
  26. data/lib/cloudstack_client/commands/snapshot.rb +40 -0
  27. data/lib/cloudstack_client/commands/ssh_key_pair.rb +106 -0
  28. data/lib/cloudstack_client/commands/template.rb +60 -0
  29. data/lib/cloudstack_client/commands/user.rb +32 -0
  30. data/lib/cloudstack_client/commands/volume.rb +20 -0
  31. data/lib/cloudstack_client/commands/zone.rb +57 -0
  32. data/lib/cloudstack_client/version.rb +3 -0
  33. data/lib/connection_helper.rb +12 -0
  34. metadata +106 -0
@@ -0,0 +1,311 @@
1
+ module CloudstackClient
2
+
3
+ module Server
4
+
5
+ ##
6
+ # Finds the server with the specified name.
7
+
8
+ def get_server(name, project_id=nil)
9
+ params = {
10
+ 'command' => 'listVirtualMachines',
11
+ 'name' => name
12
+ }
13
+ params['projectid'] = project_id if project_id
14
+ json = send_request(params)
15
+ machines = json['virtualmachine']
16
+
17
+ if !machines || machines.empty? then
18
+ return nil
19
+ end
20
+
21
+ machines.select {|m| m['name'] == name }.first
22
+ end
23
+
24
+ def get_server_state(id)
25
+ params = {
26
+ 'command' => 'listVirtualMachines',
27
+ 'id' => id
28
+ }
29
+ json = send_request(params)
30
+ machine_state = json['virtualmachine'][0]['state']
31
+
32
+ if !machine_state || machine_state.empty?
33
+ return nil
34
+ end
35
+
36
+ machine_state
37
+ end
38
+
39
+ def wait_for_server_state(id, state)
40
+ while get_server_state(id) != state
41
+ print '..'
42
+ sleep 5
43
+ end
44
+ state
45
+ end
46
+
47
+ ##
48
+ # Finds the public ip for a server
49
+
50
+ def get_server_public_ip(server, cached_rules=nil)
51
+ return nil unless server
52
+
53
+ # find the public ip
54
+ nic = get_server_default_nic(server) || {}
55
+ if nic['type'] == 'Virtual'
56
+ ssh_rule = get_ssh_port_forwarding_rule(server, cached_rules)
57
+ ssh_rule ? ssh_rule['ipaddress'] : nil
58
+ else
59
+ nic['ipaddress']
60
+ end
61
+ end
62
+
63
+ ##
64
+ # Returns the fully qualified domain name for a server.
65
+
66
+ def get_server_fqdn(server)
67
+ return nil unless server
68
+
69
+ nic = get_server_default_nic(server) || {}
70
+ networks = list_networks(project_id: server['projectid']) || {}
71
+
72
+ id = nic['networkid']
73
+ network = networks.select { |net|
74
+ net['id'] == id
75
+ }.first
76
+ return nil unless network
77
+
78
+ "#{server['name']}.#{network['networkdomain']}"
79
+ end
80
+
81
+ def get_server_default_nic(server)
82
+ server['nic'].each do |nic|
83
+ return nic if nic['isdefault']
84
+ end
85
+ end
86
+
87
+ ##
88
+ # Lists all the servers in your account.
89
+
90
+ def list_servers(options = {})
91
+ params = {
92
+ 'command' => 'listVirtualMachines',
93
+ 'listAll' => true
94
+ }
95
+ params['projectid'] = options[:project_id] if options[:project_id]
96
+
97
+ if options[:zone]
98
+ zone = get_zone(options[:zone])
99
+ unless zone
100
+ puts "Error: Zone #{options[:zone]} not found"
101
+ exit 1
102
+ end
103
+ params['zoneid'] = zone['id']
104
+ end
105
+
106
+ if options[:account]
107
+ if account = list_accounts({name: options[:account]}).first
108
+ params['domainid'] = account["domainid"]
109
+ params['account'] = options[:account]
110
+ end
111
+ end
112
+
113
+ json = send_request(params)
114
+ json['virtualmachine'] || []
115
+ end
116
+
117
+ ##
118
+ # Deploys a new server using the specified parameters.
119
+
120
+ def create_server(args = {})
121
+ if args[:name]
122
+ if get_server(args[:name])
123
+ puts "Error: Server '#{args[:name]}' already exists."
124
+ exit 1
125
+ end
126
+ end
127
+
128
+ service = get_service_offering(args[:offering])
129
+ if !service
130
+ puts "Error: Service offering '#{args[:offering]}' is invalid"
131
+ exit 1
132
+ end
133
+
134
+ if args[:template]
135
+ template = get_template(args[:template])
136
+ if !template
137
+ puts "Error: Template '#{args[:template]}' is invalid"
138
+ exit 1
139
+ end
140
+ end
141
+
142
+ if args[:disk_offering]
143
+ disk_offering = get_disk_offering(args[:disk_offering])
144
+ unless disk_offering
145
+ msg = "Disk offering '#{args[:disk_offering]}' is invalid"
146
+ puts "Error: #{msg}"
147
+ exit 1
148
+ end
149
+ end
150
+
151
+ if args[:iso]
152
+ iso = get_iso(args[:iso])
153
+ unless iso
154
+ puts "Error: Iso '#{args[:iso]}' is invalid"
155
+ exit 1
156
+ end
157
+ unless disk_offering
158
+ puts "Error: a disk offering is required when using iso"
159
+ exit 1
160
+ end
161
+ end
162
+
163
+ if !template && !iso
164
+ puts "Error: Iso or Template is required"
165
+ exit 1
166
+ end
167
+
168
+ zone = args[:zone] ? get_zone(args[:zone]) : get_default_zone
169
+ if !zone
170
+ msg = args[:zone] ? "Zone '#{args[:zone]}' is invalid" : "No default zone found"
171
+ puts "Error: #{msg}"
172
+ exit 1
173
+ end
174
+
175
+ if args[:project]
176
+ project = get_project(args[:project])
177
+ if !project
178
+ msg = "Project '#{args[:project]}' is invalid"
179
+ puts "Error: #{msg}"
180
+ exit 1
181
+ end
182
+ end
183
+
184
+ networks = []
185
+ if args[:networks]
186
+ args[:networks].each do |name|
187
+ network = project ? get_network(name, project['id']) : get_network(name)
188
+ if !network
189
+ puts "Error: Network '#{name}' not found"
190
+ exit 1
191
+ end
192
+ networks << network
193
+ end
194
+ end
195
+ if networks.empty?
196
+ networks << get_default_network
197
+ end
198
+ if networks.empty?
199
+ puts "No default network found"
200
+ exit 1
201
+ end
202
+ network_ids = networks.map { |network|
203
+ network['id']
204
+ }
205
+
206
+ params = {
207
+ 'command' => 'deployVirtualMachine',
208
+ 'serviceOfferingId' => service['id'],
209
+ 'templateId' => template ? template['id'] : iso['id'],
210
+ 'zoneId' => zone['id'],
211
+ 'networkids' => network_ids.join(',')
212
+ }
213
+ params['name'] = args[:name] if args[:name]
214
+ params['projectid'] = project['id'] if project
215
+ params['diskofferingid'] = disk_offering['id'] if disk_offering
216
+ params['hypervisor'] = (args[:hypervisor] || 'vmware') if iso
217
+ params['keypair'] = args[:keypair] if args[:keypair]
218
+ params['size'] = args[:disk_size] if args[:disk_size]
219
+ params['group'] = args[:group] if args[:group]
220
+ params['displayname'] = args[:displayname] if args[:displayname]
221
+
222
+ if args[:account]
223
+ account = list_accounts({name: args[:account]}).first
224
+ unless account
225
+ puts "Error: Account #{args[:account]} not found."
226
+ exit 1
227
+ end
228
+ params['domainid'] = account["domainid"]
229
+ params['account'] = args[:account]
230
+ end
231
+
232
+ args[:sync] ? send_request(params) : send_async_request(params)['virtualmachine']
233
+ end
234
+
235
+ ##
236
+ # Stops the server with the specified name.
237
+ #
238
+
239
+ def stop_server(name, forced=nil)
240
+ server = get_server(name)
241
+ if !server || !server['id']
242
+ puts "Error: Virtual machine '#{name}' does not exist"
243
+ exit 1
244
+ end
245
+
246
+ params = {
247
+ 'command' => 'stopVirtualMachine',
248
+ 'id' => server['id']
249
+ }
250
+ params['forced'] = true if forced
251
+
252
+ json = send_async_request(params)
253
+ json['virtualmachine']
254
+ end
255
+
256
+ ##
257
+ # Start the server with the specified name.
258
+ #
259
+
260
+ def start_server(name)
261
+ server = get_server(name)
262
+ if !server || !server['id']
263
+ puts "Error: Virtual machine '#{name}' does not exist"
264
+ exit 1
265
+ end
266
+
267
+ params = {
268
+ 'command' => 'startVirtualMachine',
269
+ 'id' => server['id']
270
+ }
271
+
272
+ json = send_async_request(params)
273
+ json['virtualmachine']
274
+ end
275
+
276
+ ##
277
+ # Reboot the server with the specified name.
278
+ #
279
+
280
+ def reboot_server(name)
281
+ server = get_server(name)
282
+ if !server || !server['id']
283
+ puts "Error: Virtual machine '#{name}' does not exist"
284
+ exit 1
285
+ end
286
+
287
+ params = {
288
+ 'command' => 'rebootVirtualMachine',
289
+ 'id' => server['id']
290
+ }
291
+
292
+ json = send_async_request(params)
293
+ json['virtualmachine']
294
+ end
295
+
296
+ ##
297
+ # Destroy the server with the specified name.
298
+ #
299
+
300
+ def destroy_server(id, async = true)
301
+ params = {
302
+ 'command' => 'destroyVirtualMachine',
303
+ 'id' => id
304
+ }
305
+
306
+ async ? send_async_request(params)['virtualmachine'] : send_request(params)
307
+ end
308
+
309
+ end
310
+
311
+ end
@@ -0,0 +1,98 @@
1
+ module CloudstackClient
2
+
3
+ module ServiceOffering
4
+
5
+ ##
6
+ # Finds the service offering with the specified name.
7
+
8
+ def get_service_offering(name)
9
+
10
+ # TODO: use name parameter
11
+ # listServiceOfferings in CloudStack 2.2 doesn't seem to work
12
+ # when the name parameter is specified. When this is fixed,
13
+ # the name parameter should be added to the request.
14
+ params = {
15
+ 'command' => 'listServiceOfferings'
16
+ }
17
+ json = send_request(params)
18
+
19
+ services = json['serviceoffering']
20
+ return nil unless services
21
+
22
+ services.each { |s|
23
+ if s['name'] == name then
24
+ return s
25
+ end
26
+ }
27
+ nil
28
+ end
29
+
30
+ ##
31
+ # Lists all available service offerings.
32
+
33
+ def list_service_offerings(domain = nil)
34
+ params = {
35
+ 'command' => 'listServiceOfferings'
36
+ }
37
+
38
+ if domain
39
+ params['domainid'] = list_domains(domain).first["id"]
40
+ end
41
+
42
+ json = send_request(params)
43
+ json['serviceoffering'] || []
44
+ end
45
+
46
+ ##
47
+ # Create a service offering.
48
+
49
+ def create_offering(args)
50
+ params = {
51
+ 'command' => 'createServiceOffering',
52
+ 'name' => args[:name],
53
+ 'cpunumber' => args[:cpunumber],
54
+ 'cpuspeed' => args[:cpuspeed],
55
+ 'displaytext' => args[:displaytext],
56
+ 'memory' => args[:memory]
57
+ }
58
+
59
+ if args['domain']
60
+ params['domainid'] = list_domains(args['domain']).first["id"]
61
+ end
62
+
63
+ params['tags'] = args[:tags] if args[:tags]
64
+ params['offerha'] = 'true' if args[:ha]
65
+
66
+ json = send_request(params)
67
+ json['serviceoffering'].first
68
+ end
69
+
70
+ ##
71
+ # Delete a service offering.
72
+
73
+ def delete_offering(id)
74
+ params = {
75
+ 'command' => 'deleteServiceOffering',
76
+ 'id' => id
77
+ }
78
+
79
+ json = send_request(params)
80
+ json['success']
81
+ end
82
+
83
+ def update_offering(args)
84
+ params = {
85
+ 'command' => 'updateServiceOffering',
86
+ 'id' => args['id']
87
+ }
88
+ params['name'] = args['name'] if args['name']
89
+ params['displaytext'] = args['displaytext'] if args['displaytext']
90
+ params['sortkey'] = args['sortkey'] if args['sortkey']
91
+
92
+ json = send_request(params)
93
+ json['serviceoffering']
94
+ end
95
+
96
+ end
97
+
98
+ end
@@ -0,0 +1,40 @@
1
+ module CloudstackClient
2
+
3
+ module Snapshot
4
+
5
+ ##
6
+ # Lists snapshots.
7
+
8
+ def list_snapshots(args = {})
9
+ params = {
10
+ 'command' => 'listSnapshots',
11
+ 'isrecursive' => 'true'
12
+ }
13
+ params['name'] = args[:name] if args[:name]
14
+
15
+ if args[:project]
16
+ project = get_project(args[:project])
17
+ unless project
18
+ puts "Error: project #{args[:project]} not found."
19
+ exit 1
20
+ end
21
+ params['projectid'] = project['id']
22
+ end
23
+ if args[:account]
24
+ account = list_accounts({name: args[:account]}).first
25
+ unless account
26
+ puts "Error: Account #{args[:account]} not found."
27
+ exit 1
28
+ end
29
+ params['domainid'] = account["domainid"]
30
+ params['account'] = args[:account]
31
+ end
32
+ params['listall'] = args[:listall] if args[:listall]
33
+
34
+ json = send_request(params)
35
+ json['snapshot'] || []
36
+ end
37
+
38
+ end
39
+
40
+ end