azure-armrest 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,7 +3,7 @@ module Azure
3
3
  # Armrest namespace
4
4
  module Armrest
5
5
  # Base class for managing virtual machines
6
- class VirtualMachineService < ArmrestService
6
+ class VirtualMachineService < ResourceGroupBasedService
7
7
 
8
8
  # The provider used in requests when gathering VM information.
9
9
  attr_reader :provider
@@ -19,7 +19,8 @@ module Azure
19
19
  def initialize(_armrest_configuration, options = {})
20
20
  super
21
21
  @provider = options[:provider] || 'Microsoft.Compute'
22
- set_service_api_version(options, 'virtualMachines')
22
+ @service_name = 'virtualMachines'
23
+ set_service_api_version(options, @service_name)
23
24
  end
24
25
 
25
26
  # Set a new provider to use the default for other methods. This may alter
@@ -51,93 +52,16 @@ module Azure
51
52
 
52
53
  alias sizes series
53
54
 
54
- # Returns a list of available virtual machines for the given subscription
55
- # for the provided +group+, or all resource groups if none is provided.
56
- #
57
- # Examples:
58
- #
59
- # # Get VM's for all resource groups
60
- # vmm.list
61
- #
62
- # # Get VM's only for a specific group
63
- # vmm.list('some_group')
64
- #--
65
- # The specific hashes we can grab are:
66
- # p JSON.parse(resp.body)["value"][0]["properties"]["instanceView"]
67
- # p JSON.parse(resp.body)["value"][0]["properties"]["hardwareProfile"]
68
- # p JSON.parse(resp.body)["value"][0]["properties"]["storageProfile"]
69
- #
70
- def list(group = armrest_configuration.resource_group)
71
- array = []
72
-
73
- if group
74
- url = build_url(group)
75
- array << JSON.parse(rest_get(url))['value']
76
- else
77
- threads = []
78
- mutex = Mutex.new
79
-
80
- resource_groups.each do |rg|
81
- threads << Thread.new(rg['name']) do |rgroup|
82
- response = rest_get(build_url(rgroup))
83
- result = JSON.parse(response)['value']
84
- if result
85
- mutex.synchronize{
86
- result.each{ |hash| hash['resourceGroup'] = rgroup }
87
- array << result
88
- }
89
- end
90
- end
91
- end
92
-
93
- threads.each(&:join)
94
-
95
- array = array.flatten
96
-
97
- if provider.downcase == 'microsoft.compute'
98
- add_network_profile(array)
99
- add_power_status(array)
100
- end
101
- end
102
-
103
- array
104
- end
105
-
106
- alias get_vms list
107
-
108
55
  # Captures the +vmname+ and associated disks into a reusable CSM template.
109
56
  # The 3rd argument is a hash of options that supports the following keys:
110
57
  #
111
- # * prefix - The prefix in the name of the blobs.
112
- # * container - The name of the container inside which the image will reside.
113
- # * overwrite - Boolean that indicates whether or not to overwrite any VHD's
114
- # with the same prefix. The default is false.
115
- #
116
- # The :prefix and :container options are mandatory. You may optionally
117
- # specify the :resource_group as an option, or as the 3rd argument, but
118
- # it is also mandatory if not already set in the constructor.
58
+ # * vhdPrefix - The prefix in the name of the blobs.
59
+ # * destinationContainerName - The name of the container inside which the image will reside.
60
+ # * overwriteVhds - Boolean that indicates whether or not to overwrite any VHD's
61
+ # with the same prefix. The default is false.
119
62
  #
120
- # Note that this is a long running operation. You are expected to
121
- # poll on the client side to check the status of the operation.
122
- #
123
- def capture(vmname, options = {}, group = nil)
124
- prefix = options.fetch(:prefix)
125
- container = options.fetch(:container)
126
- overwrite = options[:overwrite] || false
127
- group ||= options[:resource_group] || armrest_configuration.resource_group
128
-
129
- raise ArgumentError, "no resource group provided" unless group
130
-
131
- body = {
132
- :vhdPrefix => prefix,
133
- :destinationContainerName => container,
134
- :overwriteVhds => overwrite
135
- }.to_json
136
-
137
- url = build_url(group, vmname, 'capture')
138
-
139
- response = rest_post(url, body)
140
- response.return!
63
+ def capture(vmname, options, group = armrest_configuration.resource_group)
64
+ vm_operate('capture', vmname, group, options)
141
65
  end
142
66
 
143
67
  # Creates a new virtual machine (or updates an existing one). Pass a hash
@@ -228,34 +152,16 @@ module Azure
228
152
  #def create(options = {})
229
153
  #end
230
154
 
231
- #alias update create
232
-
233
155
  # Stop the VM +vmname+ in +group+ and deallocate the tenant in Fabric.
234
156
  #
235
157
  def deallocate(vmname, group = armrest_configuration.resource_group)
236
- raise ArgumentError, "no resource group provided" unless group
237
- url = build_url(group, vmname, 'deallocate')
238
- response = rest_post(url)
239
- response.return!
240
- end
241
-
242
- # Deletes the +vmname+ in +group+ that you specify. Note that associated
243
- # disks are not deleted.
244
- #
245
- def delete(vmname, group = armrest_configuration.resource_group)
246
- raise ArgumentError, "no resource group provided" unless group
247
- url = build_url(group, vmname)
248
- response = rest_delete(url)
249
- response.return!
158
+ vm_operate('deallocate', vmname, group)
250
159
  end
251
160
 
252
161
  # Sets the OSState for the +vmname+ in +group+ to 'Generalized'.
253
162
  #
254
163
  def generalize(vmname, group = armrest_configuration.resource_group)
255
- raise ArgumentError, "no resource group provided" unless group
256
- url = build_url(group, vmname, 'generalize')
257
- response = rest_post(url)
258
- response.return!
164
+ vm_operate('generalize', vmname, group)
259
165
  end
260
166
 
261
167
  # Retrieves the settings of the VM named +vmname+ in resource group
@@ -266,22 +172,13 @@ module Azure
266
172
  # in the details of the information retrieved.
267
173
  #
268
174
  def get(vmname, group = armrest_configuration.resource_group, model_view = true)
269
- raise ArgumentError, "no resource group provided" unless group
270
-
271
- if model_view
272
- url = build_url(group, vmname)
273
- else
274
- url = build_url(group, vmname, 'instanceView')
275
- end
276
-
277
- JSON.parse(rest_get(url))
175
+ model_view ? super(vmname, group) : get_instance_view(vmname, group)
278
176
  end
279
177
 
280
178
  # Convenient wrapper around the get method that retrieves the model view
281
179
  # for +vmname+ in resource_group +group+.
282
180
  #
283
181
  def get_model_view(vmname, group = armrest_configuration.resource_group)
284
- raise ArgumentError, "no resource group provided" unless group
285
182
  get(vmname, group, true)
286
183
  end
287
184
 
@@ -289,8 +186,12 @@ module Azure
289
186
  # for +vmname+ in resource_group +group+.
290
187
  #
291
188
  def get_instance_view(vmname, group = armrest_configuration.resource_group)
292
- raise ArgumentError, "no resource group provided" unless group
293
- get(vmname, group, false)
189
+ raise ArgumentError, "must specify resource group" unless group
190
+ raise ArgumentError, "must specify name of the resource" unless vmname
191
+
192
+ url = build_url(group, vmname, 'instanceView')
193
+ response = rest_get(url)
194
+ VirtualMachineInstance.new(response)
294
195
  end
295
196
 
296
197
  # Restart the VM +vmname+ for the given +group+, which will default
@@ -300,10 +201,7 @@ module Azure
300
201
  # which you can inspect, such as response.code or response.headers.
301
202
  #
302
203
  def restart(vmname, group = armrest_configuration.resource_group)
303
- raise ArgumentError, "no resource group provided" unless group
304
- url = build_url(group, vmname, 'restart')
305
- response = rest_post(url)
306
- response.return!
204
+ vm_operate('restart', vmname, group)
307
205
  end
308
206
 
309
207
  # Start the VM +vmname+ for the given +group+, which will default
@@ -313,10 +211,7 @@ module Azure
313
211
  # which you can inspect, such as response.code or response.headers.
314
212
  #
315
213
  def start(vmname, group = armrest_configuration.resource_group)
316
- raise ArgumentError, "no resource group provided" unless group
317
- url = build_url(group, vmname, 'start')
318
- response = rest_post(url)
319
- response.return!
214
+ vm_operate('start', vmname, group)
320
215
  end
321
216
 
322
217
  # Stop the VM +vmname+ for the given +group+ gracefully. However,
@@ -326,74 +221,22 @@ module Azure
326
221
  # which you can inspect, such as response.code or response.headers.
327
222
  #
328
223
  def stop(vmname, group = armrest_configuration.resource_group)
329
- raise ArgumentError, "no resource group provided" unless group
330
- url = build_url(group, vmname, 'powerOff')
331
- response = rest_post(url)
332
- response.return!
333
- end
334
-
335
- private
336
-
337
- def add_network_profile(vms)
338
- vms.each { |vm|
339
- vm['properties']['networkProfile']['networkInterfaces'].each { |net|
340
- get_nic_profile(net)
341
- }
342
- }
224
+ vm_operate('powerOff', vmname, group)
343
225
  end
344
226
 
345
- def get_nic_profile(nic)
346
- url = File.join(
347
- Azure::Armrest::RESOURCE,
348
- nic['id'],
349
- "?api-version=#{@api_version}"
350
- )
351
-
352
- nic['properties'] = JSON.parse(rest_get(url))['properties']['ipConfigurations']
353
- nic['properties'].each do |n|
354
- next if n['properties']['publicIPAddress'].nil?
355
-
356
- # public IP is a URI so we need to make another rest call to get it.
357
- url = File.join(
358
- Azure::Armrest::RESOURCE,
359
- n['properties']['publicIPAddress']['id'],
360
- "?api-version=#{@api_version}"
361
- )
362
-
363
- public_ip = JSON.parse(rest_get(url))['properties']['ipAddress']
364
- n['properties']['publicIPAddress'] = public_ip
365
- end
227
+ def model_class
228
+ VirtualMachineModel
366
229
  end
367
230
 
368
- def add_power_status(vms)
369
- vms.each do |vm|
370
- i_view = get_instance_view(vm["name"], vm["resourceGroup"])
371
- powerstatus_hash = i_view["statuses"].find {|h| h["code"].include? "PowerState"}
372
- vm["powerStatus"] = powerstatus_hash['displayStatus'] unless powerstatus_hash.nil?
373
- end
374
- end
375
-
376
- # If no default subscription is set, then use the first one found.
377
- def set_default_subscription
378
- @subscription_id ||= subscriptions.first['subscriptionId']
379
- end
231
+ private
380
232
 
381
- # Builds a URL based on subscription_id an resource_group and any other
382
- # arguments provided, and appends it with the api_version.
383
- #
384
- def build_url(resource_group, *args)
385
- url = File.join(
386
- Azure::Armrest::COMMON_URI,
387
- armrest_configuration.subscription_id,
388
- 'resourceGroups',
389
- resource_group,
390
- 'providers',
391
- @provider,
392
- 'virtualMachines',
393
- )
233
+ def vm_operate(action, vmname, group, options = {})
234
+ raise ArgumentError, "must specify resource group" unless group
235
+ raise ArgumentError, "must specify name of the vm" unless vmname
394
236
 
395
- url = File.join(url, *args) unless args.empty?
396
- url << "?api-version=#{@api_version}"
237
+ url = build_url(group, vmname, action)
238
+ rest_post(url)
239
+ nil
397
240
  end
398
241
  end
399
242
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: azure-armrest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel J. Berger
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2015-10-13 00:00:00.000000000 Z
14
+ date: 2015-10-20 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: json
@@ -31,16 +31,16 @@ dependencies:
31
31
  name: rest-client
32
32
  requirement: !ruby/object:Gem::Requirement
33
33
  requirements:
34
- - - ">="
34
+ - - '='
35
35
  - !ruby/object:Gem::Version
36
- version: '0'
36
+ version: 2.0.0.rc1
37
37
  type: :runtime
38
38
  prerelease: false
39
39
  version_requirements: !ruby/object:Gem::Requirement
40
40
  requirements:
41
- - - ">="
41
+ - - '='
42
42
  - !ruby/object:Gem::Version
43
- version: '0'
43
+ version: 2.0.0.rc1
44
44
  - !ruby/object:Gem::Dependency
45
45
  name: cache_method
46
46
  requirement: !ruby/object:Gem::Requirement
@@ -55,6 +55,48 @@ dependencies:
55
55
  - - "~>"
56
56
  - !ruby/object:Gem::Version
57
57
  version: 0.2.7
58
+ - !ruby/object:Gem::Dependency
59
+ name: azure-signature
60
+ requirement: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - "~>"
63
+ - !ruby/object:Gem::Version
64
+ version: 0.2.0
65
+ type: :runtime
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - "~>"
70
+ - !ruby/object:Gem::Version
71
+ version: 0.2.0
72
+ - !ruby/object:Gem::Dependency
73
+ name: activesupport
74
+ requirement: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: 1.2.0
79
+ type: :runtime
80
+ prerelease: false
81
+ version_requirements: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: 1.2.0
86
+ - !ruby/object:Gem::Dependency
87
+ name: nokogiri
88
+ requirement: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - "~>"
91
+ - !ruby/object:Gem::Version
92
+ version: 1.6.0
93
+ type: :runtime
94
+ prerelease: false
95
+ version_requirements: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - "~>"
98
+ - !ruby/object:Gem::Version
99
+ version: 1.6.0
58
100
  - !ruby/object:Gem::Dependency
59
101
  name: bundler
60
102
  requirement: !ruby/object:Gem::Requirement
@@ -152,12 +194,15 @@ files:
152
194
  - lib/azure/armrest/armrest_service.rb
153
195
  - lib/azure/armrest/availability_set_service.rb
154
196
  - lib/azure/armrest/event_service.rb
197
+ - lib/azure/armrest/exception.rb
155
198
  - lib/azure/armrest/model/base_model.rb
199
+ - lib/azure/armrest/model/storage_account.rb
156
200
  - lib/azure/armrest/network/ip_address_service.rb
157
201
  - lib/azure/armrest/network/network_interface_service.rb
158
202
  - lib/azure/armrest/network/network_security_group_service.rb
159
203
  - lib/azure/armrest/network/subnet_service.rb
160
204
  - lib/azure/armrest/network/virtual_network_service.rb
205
+ - lib/azure/armrest/resource_group_based_service.rb
161
206
  - lib/azure/armrest/resource_group_service.rb
162
207
  - lib/azure/armrest/resource_provider_service.rb
163
208
  - lib/azure/armrest/resource_service.rb