Skip to content

Common Resource Group Based Service Methods

Daniel Berger edited this page Aug 3, 2018 · 9 revisions

The ResourceGroupBasedService class serves as an abstract base class from which most current and future service classes are subclassed. It provides a common set of methods, and handles all the REST url generation and method calling internally so that its subclasses don't have to.

The common set of methods are:

  • create
  • delete
  • delete_by_id
  • get
  • get_by_id
  • list
  • list_all
  • update

Create or Update

The create method takes a name, a resource group, and a hash of options and creates the resource.

# Example:

nis = Azure::Armrest::Network::NetworkInterfaceService.new(conf)

options = {
  :location => 'westus',
  :properties => {
    :ipConfigurations => [
      {:name => 'testIP', :properties => {:subnet => {:id => some_subnet_id}}}
    ]
  }
}

nis.create('test-nic', 'some_group', options)

The exact nature of the options hash is dependent on the type of resource you are trying to create. The documentation for which parameters are required and which are optional is far too extensive for us to document inline. Therefore, it requires that you look up the Azure REST documentation yourself to see which options you must pass, and in what configuration.

Note that the update method is really just an alias for create. The Azure API will simply update the existing resource if it already exists.

Delete

The delete method takes a name and a resource group name and deletes the resource.

# Example:

vms = Azure::Armrest::VirtualMachineService.new(conf)
vms.delete('some_vm', 'some_resource_group')

This is an asynchronous operation that may take a minute or so to complete, depending on the resource type. You will have to poll the resource to know exactly when it's actually been deleted, possibly using the get method.

Get

The get method takes a name and a resource group. It returns a corresponding model object for that type of service.

# Example:

nsgs = Azure::Armrest::Network::NetworkSecurityGroupService.new(conf)
nsg = nsgs.get('some_security_group', 'some_resource_group')

puts nsg.name
p nsg.properties.security_rules

Getting and Deleting by ID

The get_by_id and delete_by_id methods allow you to get or delete a resource by its resource ID value, respectively. The resource ID is typically a string in the format of e.g. "/subscription/xxx/resourceGroups/yyy/virtualMachines/foo".

This will work for most services and subservices.

This is handy when you have a nested resource ID and want to get detailed information for it. For example, to get information on a NIC associated with a VM you could do this:

Example:

vms = Azure::Armrest::VirtualMachineService.new(conf)
nis = Azure::Armrest::Network::NetworkInterfaceService.new(conf)

vm = vms.get('some_vm', 'some_group')
nic = nis.get_by_id(vm.properties.network_profile.network_interfaces.first.id)

List

The list method takes a resource group. It returns a list of model objects for all instances of that resource found within the given resource group.

# Example:

ipas = Azure::Armrest::Network::IpAddressService.new(conf)

ipas.list('some_group').each do |ip|
  p ip
end

List All

The list_all method is similar to the list method, but returns a list of model objects for all instances in all resource groups for the current subscription. You can also apply a filter for the results as an optional argument.

# Example:

vns = Azure::Armrest::Network::VirtualNetworkService.new(conf)

vns.list_all.each do |vn|
  p vn
end

# With a filter
vns.list_all(:location => "eastus"){ |vn| p vn }

Both the list and list_all methods will automatically use continuation tokens, if present, to gather all data.

Subservices

The ResourceGroupBasedSubservice class is a subclass of ResourceGroupBasedService class. It is also an abstract class meant to be subclassed by "nested" resource service classes, such as the Network::SubnetService class.

It provides the same list of methods that the ResourceGroupBasedService class does. The primary difference is that subservice classes require an additional argument for each of the provided methods.

# Example:
vns = Azure::Armrest::Network::VirtualNetworkService.new(conf)
sns = Azure::Armrest::Network::SubnetService.new(conf)

vn = vns.list_all.map(&:name).first
p sns.list(name, some_group)