Class: Azure::Armrest::VirtualMachineService

Inherits:
ResourceGroupBasedService show all
Defined in:
lib/azure/armrest/virtual_machine_service.rb

Overview

Base class for managing virtual machines

Direct Known Subclasses

VirtualMachineExtensionService

Constant Summary

Constants inherited from ResourceGroupBasedService

ResourceGroupBasedService::SERVICE_NAME_MAP

Instance Attribute Summary

Attributes inherited from ArmrestService

#api_version, #armrest_configuration, #base_url, #provider, #service_name

Instance Method Summary collapse

Methods inherited from ResourceGroupBasedService

#create, #delete, #delete_by_id, #get_by_id, #list, #list_all

Methods inherited from ArmrestService

configure, #get_provider, #get_subscription, #list_locations, #list_resource_groups, #list_resources, #list_subscriptions, #locations, #poll, #tags, #tenants, #wait

Constructor Details

#initialize(configuration, options = {}) ⇒ VirtualMachineService

Create and return a new VirtualMachineService instance. Most methods for a VirtualMachineService instance will return one or more VirtualMachine instances.

This subclass accepts the additional :provider option as well. The default is 'Microsoft.Compute'. You may need to set this to 'Microsoft.ClassicCompute' for your purposes.


15
16
17
# File 'lib/azure/armrest/virtual_machine_service.rb', line 15

def initialize(configuration, options = {})
  super(configuration, 'virtualMachines', 'Microsoft.Compute', options)
end

Instance Method Details

#capture(vmname, options, group = configuration.resource_group) ⇒ Object

Captures the vmname and associated disks into a reusable CSM template. The 3rd argument is a hash of options that supports the following keys:

  • vhdPrefix - The prefix in the name of the blobs.

  • destinationContainerName - The name of the container inside which the image will reside.

  • overwriteVhds - Boolean that indicates whether or not to overwrite any VHD's

    with the same prefix. The default is false.
    

55
56
57
# File 'lib/azure/armrest/virtual_machine_service.rb', line 55

def capture(vmname, options, group = configuration.resource_group)
  vm_operate('capture', vmname, group, options)
end

#deallocate(vmname, group = configuration.resource_group) ⇒ Object

Stop the VM vmname in group and deallocate the tenant in Fabric.


61
62
63
# File 'lib/azure/armrest/virtual_machine_service.rb', line 61

def deallocate(vmname, group = configuration.resource_group)
  vm_operate('deallocate', vmname, group)
end

#delete_associated_resources(vmname, vmgroup, options = {}) ⇒ Object

Delete the VM and associated resources. By default, this will delete the VM, its NIC, the associated IP address, and the image files (.vhd and .status) for the VM.

If you want to delete other associated resources, such as any attached disks, the VM's underlying storage account, or associated network security groups you must explicitly specify them as an option.

An attempt to delete a resource that cannot be deleted because it's still associated with some other resource will be logged and skipped.

If the :verbose option is set to true, then additional messages are sent to your configuration log, or stdout if no log was specified.

Note that if all of your related resources are in a self-contained resource group, you do not necessarily need this method. You could just delete the resource group itself, which would automatically delete all of its resources.


175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/azure/armrest/virtual_machine_service.rb', line 175

def delete_associated_resources(vmname, vmgroup, options = {})
  options = {
    :network_interfaces      => true,
    :ip_addresses            => true,
    :os_disk                 => true,
    :data_disks              => false,
    :network_security_groups => false,
    :storage_account         => false,
    :verbose                 => false
  }.merge(options)

  Azure::Armrest::Configuration.log ||= STDOUT if options[:verbose]

  vm = get(vmname, vmgroup)

  delete_and_wait(self, vmname, vmgroup, options)

  # Must delete network interfaces first if you want to delete
  # IP addresses or network security groups.
  if options[:network_interfaces] || options[:ip_addresses] || options[:network_security_groups]
    delete_associated_nics(vm, options)
  end

  if options[:os_disk] || options[:storage_account]
    delete_associated_disk(vm, options)
  end

  if options[:data_disks]
    delete_associated_data_disks(vm, options)
  end
end

#generalize(vmname, group = configuration.resource_group) ⇒ Object

Sets the OSState for the vmname in group to 'Generalized'.


67
68
69
# File 'lib/azure/armrest/virtual_machine_service.rb', line 67

def generalize(vmname, group = configuration.resource_group)
  vm_operate('generalize', vmname, group)
end

#get(vmname, group = configuration.resource_group, options = {}) ⇒ Object

Retrieves the settings of the VM named vmname in resource group group, which will default to the same as the name of the VM.

You can also specify any query options. At this time only the :expand => 'instanceView' option is supported, but others could be added over time.

For backwards compatibility, the third argument may also be a boolean which will retrieve the model view by default. Set to false if you only want the instance view.

Examples:

vms = VirtualMachineService.new(credentials)

# Standard call, get just the model view
vms.get('some_name', 'some_group')
vms.get('some_name', 'some_group', true) # same

# Get the instance view only
vms.get('some_name', 'some_group', false)

# Get the instance view merged with the model view
vms.get('some_name', 'some_group', :expand => 'instanceView')

96
97
98
99
100
101
102
103
104
# File 'lib/azure/armrest/virtual_machine_service.rb', line 96

def get(vmname, group = configuration.resource_group, options = {})
  if options.kind_of?(Hash)
    url = build_url(group, vmname, options)
    response = rest_get(url)
    VirtualMachineInstance.new(response)
  else
    options ? super(vmname, group) : get_instance_view(vmname, group)
  end
end

#get_instance_view(vmname, group = configuration.resource_group) ⇒ Object

Convenient wrapper around the get method that retrieves only the instance view for vmname in resource_group group.

Raises:

  • (ArgumentError)

117
118
119
120
121
122
123
124
# File 'lib/azure/armrest/virtual_machine_service.rb', line 117

def get_instance_view(vmname, group = configuration.resource_group)
  raise ArgumentError, "must specify resource group" unless group
  raise ArgumentError, "must specify name of the resource" unless vmname

  url = build_url(group, vmname, 'instanceView')
  response = rest_get(url)
  VirtualMachineInstance.new(response)
end

#get_model_view(vmname, group = configuration.resource_group) ⇒ Object

Convenient wrapper around the get method that retrieves the model view for vmname in resource_group group without the instance view information.


110
111
112
# File 'lib/azure/armrest/virtual_machine_service.rb', line 110

def get_model_view(vmname, group = configuration.resource_group)
  get(vmname, group)
end

#list_by_location(location, options = {}) ⇒ Object

Return a list of virtual machines for the given location.


21
22
23
24
25
# File 'lib/azure/armrest/virtual_machine_service.rb', line 21

def list_by_location(location, options = {})
  url = url_with_api_version(api_version, base_url, 'providers', provider, 'locations', location, service_name)
  response = rest_get(url)
  get_all_results(response, options[:skip_accessors_definition])
end

#model_classObject


207
208
209
# File 'lib/azure/armrest/virtual_machine_service.rb', line 207

def model_class
  VirtualMachineModel
end

#restart(vmname, group = configuration.resource_group) ⇒ Object

Restart the VM vmname for the given group, which will default to the same as the vmname.

This is an asynchronous operation that returns a response object which you can inspect, such as response.code or response.headers.


132
133
134
# File 'lib/azure/armrest/virtual_machine_service.rb', line 132

def restart(vmname, group = configuration.resource_group)
  vm_operate('restart', vmname, group)
end

#series(location) ⇒ Object Also known as: sizes

Return a list of available VM series (aka sizes, flavors, etc), such as “Basic_A1”, though other information is included as well.


30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/azure/armrest/virtual_machine_service.rb', line 30

def series(location)
  namespace = 'microsoft.compute'
  version = configuration.provider_default_api_version(namespace, 'locations/vmsizes')

  unless version
    raise ArgumentError, "Unable to find resources for #{namespace}"
  end

  url = url_with_api_version(
    version, base_url, 'providers', provider, 'locations', location, 'vmSizes'
  )

  JSON.parse(rest_get(url))['value'].map{ |hash| VirtualMachineSize.new(hash) }
end

#start(vmname, group = configuration.resource_group) ⇒ Object

Start the VM vmname for the given group, which will default to the same as the vmname.

This is an asynchronous operation that returns a response object which you can inspect, such as response.code or response.headers.


142
143
144
# File 'lib/azure/armrest/virtual_machine_service.rb', line 142

def start(vmname, group = configuration.resource_group)
  vm_operate('start', vmname, group)
end

#stop(vmname, group = configuration.resource_group) ⇒ Object

Stop the VM vmname for the given group gracefully. However, a forced shutdown will occur after 15 minutes.

This is an asynchronous operation that returns a response object which you can inspect, such as response.code or response.headers.


152
153
154
# File 'lib/azure/armrest/virtual_machine_service.rb', line 152

def stop(vmname, group = configuration.resource_group)
  vm_operate('powerOff', vmname, group)
end