Skip to content
Daniel Berger edited this page Mar 26, 2017 · 12 revisions

Almost all Service classes return corresponding model objects for the returned results of their methods. For example, the various methods of the VirtualMachineService class return VirtualMachine objects. These are wrapper classes that convert the raw JSON into a bonafide Ruby object.

The JSON hash keys become available as methods, so you no longer need to use hash keys to reference the various properties, though both approaches are valid.

Example:

# Assume we got our configuration object earlier
vms = Azure::Armrest::VirtualMachineService.new(conf)

vm = vms.get('some_vm', 'some_resource_group')

puts vm.name
puts vm.properties.storage_profile.os_disk.vhd.uri

# Versus

puts vm['name']
puts vm['properties']['storage_profile']['os_disk']['vhd']['uri']

The properties/methods available on the object depend what the Azure API returns for that particular REST call. If you are unsure of the properties/methods available for the returned object, an easy way to inspect the object is to call either .to_json or .to_h on the object.

# Example:
require 'pp'
nis = Azure::Armrest::Network::NetworkInterfaceService.new(conf)
nic = nis.list(some_group).first

pp nic.to_json
pp nic.to_h

Note that StorageAccount model objects are special in that they have custom methods.

See StorageAccounts.

Pretty Printing Models

Your models may contain a lot of information that may be difficult to visualize. This library does implement pretty_print if you're happy with just using the 'pp' library. However, we recommend using the awesome_print gem with an indent of -2 because it gives nicer output.

# In your $HOME/.aprc file
AwesomePrint.defaults = {
  :indent => -2
}
# Get a single storage account and inspect it with awesome_print
require 'awesome_print'
acct = storage_account_service.get('your_storage_account', 'your_resource_group')
ap acct
# Sample results

{
  "id"         => "/subscriptions/xxx-yyy-zzz/resourceGroups/your_resource_group/providers/Microsoft.Storage/storageAccounts/someacct12345",
  "location"   => "East US",
  "name"       => "someacct12345",
  "properties" => {
    "accountType"       => "Standard_LRS",
    "creationTime"      => "2016-03-18T17:30:25.7028008Z",
    "primaryEndpoints"  => {
      "blob"  => "https://someacct12345.blob.core.windows.net/",
      "file"  => "https://someacct12345.file.core.windows.net/",
      "queue" => "https://someacct12345.queue.core.windows.net/",
      "table" => "https://someacct12345.table.core.windows.net/"
    },
    "primaryLocation"   => "East US",
    "provisioningState" => "Succeeded",
    "statusOfPrimary"   => "available"
  },
  "tags"       => {},
  "type"       => "Microsoft.Storage/storageAccounts"
}