Class: Azure::Armrest::ResourceGroupBasedService

Inherits:
ArmrestService show all
Defined in:
lib/azure/armrest/resource_group_based_service.rb

Overview

Base class for services that need to run in a resource group

Constant Summary collapse

SERVICE_NAME_MAP =

Used to map service name strings to internal classes

{
  'availabilitysets'      => Azure::Armrest::AvailabilitySet,
  'loadbalancers'         => Azure::Armrest::Network::LoadBalancer,
  'networkinterfaces'     => Azure::Armrest::Network::NetworkInterface,
  'networksecuritygroups' => Azure::Armrest::Network::NetworkSecurityGroup,
  'publicipaddresses'     => Azure::Armrest::Network::IpAddress,
  'storageaccounts'       => Azure::Armrest::StorageAccount,
  'virtualnetworks'       => Azure::Armrest::Network::VirtualNetwork,
  'subnets'               => Azure::Armrest::Network::Subnet,
  'inboundnatrules'       => Azure::Armrest::Network::InboundNat,
  'securityrules'         => Azure::Armrest::Network::NetworkSecurityRule,
  'routes'                => Azure::Armrest::Network::Route,
  'databases'             => Azure::Armrest::Sql::SqlDatabase,
  'extensions'            => Azure::Armrest::VirtualMachineExtension,
  'disks'                 => Azure::Armrest::Storage::Disk,
  'snapshots'             => Azure::Armrest::Storage::Snapshot,
  'images'                => Azure::Armrest::Storage::Image,
  'deployments'           => Azure::Armrest::TemplateDeployment,
  'operations'            => Azure::Armrest::TemplateDeploymentOperation
}.freeze

Instance Attribute Summary

Attributes inherited from ArmrestService

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

Instance Method Summary collapse

Methods inherited from ArmrestService

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

Constructor Details

This class inherits a constructor from Azure::Armrest::ArmrestService

Instance Method Details

#create(name, rgroup = configuration.resource_group, options = {}) ⇒ Object Also known as: update

Create a resource name within the resource group rgroup, or the resource group that was specified in the configuration, along with a hash of appropriate options.

Returns an instance of the object that was created if possible, otherwise nil is returned.

Note that this is an asynchronous operation. You can check the current status of the resource by inspecting the :response_headers instance and polling either the :azure_asyncoperation or :location URL.

The options hash keys are automatically converted to camelCase for flexibility, so :createOption and :create_option will both work when creating a virtual machine, for example.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/azure/armrest/resource_group_based_service.rb', line 44

def create(name, rgroup = configuration.resource_group, options = {})
  validate_resource_group(rgroup)
  validate_resource(name)

  url = build_url(rgroup, name)
  url = yield(url) || url if block_given?

  body = transform_create_options(options).to_json

  response = rest_put(url, body)

  headers = Azure::Armrest::ResponseHeaders.new(response.headers)
  headers.response_code = response.code

  if response.body.empty?
    obj = get(name, rgroup)
  else
    obj = model_class.new(response.body)
  end

  obj.response_headers = headers
  obj.response_code = headers.response_code

  obj
end

#delete(name, rgroup = configuration.resource_group) ⇒ Object

Delete the resource with the given name for the provided resource_group, or the resource group specified in your original configuration object. If successful, returns a ResponseHeaders object.

If the delete operation returns a 204 (no body), which is what the Azure REST API typically returns if the resource is not found, it is treated as an error and a ResourceNotFoundException is raised.



182
183
184
185
186
187
188
189
190
# File 'lib/azure/armrest/resource_group_based_service.rb', line 182

def delete(name, rgroup = configuration.resource_group)
  validate_resource_group(rgroup)
  validate_resource(name)

  url = build_url(rgroup, name)
  url = yield(url) || url if block_given?

  delete_by_url(url, "#{rgroup}/#{name}")
end

#delete_by_id(id_string) ⇒ Object



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

def delete_by_id(id_string)
  url = convert_id_string_to_url(id_string)
  delete_by_url(url, id_string)
end

#get(name, rgroup = configuration.resource_group) ⇒ Object

Get information about a single resource name within resource group rgroup, or the resource group that was set in the configuration.



159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/azure/armrest/resource_group_based_service.rb', line 159

def get(name, rgroup = configuration.resource_group)
  validate_resource_group(rgroup)
  validate_resource(name)

  url = build_url(rgroup, name)
  url = yield(url) || url if block_given?
  response = rest_get(url)

  obj = model_class.new(response.body)
  obj.response_headers = Azure::Armrest::ResponseHeaders.new(response.headers)
  obj.response_code = response.code

  obj
end

#get_by_id(id_string) ⇒ Object Also known as: get_associated_resource

This method returns a model object based on an ID string for a resource.

Example:

vms = Azure::Armrest::VirtualMachineService.new(conf)

vm = vms.get('your_vm', 'your_group')
nic_id = vm.properties.network_profile.network_interfaces[0].id
nic = vm.get_by_id(nic_id)


136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/azure/armrest/resource_group_based_service.rb', line 136

def get_by_id(id_string)
  info = parse_id_string(id_string)
  url = convert_id_string_to_url(id_string, info)

  service_name = info['subservice_name'] || info['service_name'] || 'resourceGroups'

  model_class = SERVICE_NAME_MAP.fetch(service_name.downcase) do
    raise ArgumentError, "unable to map service name #{service_name} to model"
  end

  model_class.new(rest_get(url))
end

#list(rgroup = configuration.resource_group, skip_accessors_definition = false) ⇒ Object

List all resources within the resource group rgroup, or the resource group that was specified in the configuration.

Returns an ArmrestCollection, with the response headers set for the operation as a whole.



78
79
80
81
82
83
84
85
86
# File 'lib/azure/armrest/resource_group_based_service.rb', line 78

def list(rgroup = configuration.resource_group, skip_accessors_definition = false)
  validate_resource_group(rgroup)

  url = build_url(rgroup)
  url = yield(url) || url if block_given?
  response = rest_get(url)

  get_all_results(response, skip_accessors_definition)
end

#list_all(filter = {}) ⇒ Object

Use a single call to get all resources for the service. You may optionally provide a filter on various properties to limit the result set.

Example:

vms = Azure::Armrest::VirtualMachineService.new(conf)
vms.list_all(:location => "eastus", :resource_group => "rg1")

Note that comparisons against string values are caseless.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/azure/armrest/resource_group_based_service.rb', line 99

def list_all(filter = {})
  url = build_url
  url = yield(url) || url if block_given?

  skip_accessors_definition = filter.delete(:skip_accessors_definition) || false
  response = rest_get(url)
  results  = get_all_results(response, skip_accessors_definition)

  if filter.empty?
    results
  else
    results.select do |obj|
      filter.all? do |method_name, value|
        if value.kind_of?(String)
          if skip_accessors_definition
            obj[method_name.to_s].casecmp(value).zero?
          else
            obj.public_send(method_name).casecmp(value).zero?
          end
        else
          obj.public_send(method_name) == value
        end
      end
    end
  end
end