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
}.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_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.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/azure/armrest/resource_group_based_service.rb', line 33

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?
  response = rest_put(url, options.to_json)

  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.



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

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?
  response = rest_delete(url)

  if response.code == 204
    msg = "#{self.class} resource #{rgroup}/#{name} not found"
    raise Azure::Armrest::ResourceNotFoundException.new(response.code, msg, response)
  end

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

  headers
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.



131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/azure/armrest/resource_group_based_service.rb', line 131

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_associated_resource(id_string) ⇒ Object Also known as: get_by_id

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

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_associated_resource(nic_id)


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 103

def get_associated_resource(id_string)
  info = parse_id_string(id_string)

  if info['subservice_name']
    full_service_name = info['service_name'] + '/' + info['subservice_name']
    api_version = configuration.provider_default_api_version(info['provider'], full_service_name)
    api_version ||= configuration.provider_default_api_version(info['provider'], info['service_name'])
  else
    api_version = configuration.provider_default_api_version(info['provider'], info['service_name'])
  end

  api_version ||= configuration.api_version
  service_name = info['subservice_name'] || info['service_name']

  url = File.join(Azure::Armrest::RESOURCE, id_string) + "?api-version=#{api_version}"

  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) ⇒ 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.



64
65
66
67
68
69
70
71
72
# File 'lib/azure/armrest/resource_group_based_service.rb', line 64

def list(rgroup = configuration.resource_group)
  validate_resource_group(rgroup)

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

  get_all_results(response)
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")


83
84
85
86
87
88
89
90
91
# File 'lib/azure/armrest/resource_group_based_service.rb', line 83

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

  response = rest_get(url)
  results  = get_all_results(response)

  filter.empty? ? results : results.select { |obj| filter.all? { |k, v| obj.public_send(k) == v } }
end