Class: Azure::Armrest::ArmrestService

Inherits:
Object
  • Object
show all
Extended by:
Gem::Deprecate
Defined in:
lib/azure/armrest/armrest_service.rb

Overview

Abstract base class for the other service classes.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(armrest_configuration, service_name, default_provider, options) ⇒ ArmrestService

Do not instantiate directly. This is an abstract base class from which all other service classes should subclass, and call super within their own constructors.



39
40
41
42
43
44
45
46
47
48
# File 'lib/azure/armrest/armrest_service.rb', line 39

def initialize(armrest_configuration, service_name, default_provider, options)
  @armrest_configuration = armrest_configuration
  @service_name = service_name
  @provider = options[:provider] || default_provider

  # Base URL used for REST calls. Modify within method calls as needed.
  @base_url = Azure::Armrest::RESOURCE

  set_service_api_version(options, service_name)
end

Instance Attribute Details

#api_versionObject

The api-version string for this particular service



25
26
27
# File 'lib/azure/armrest/armrest_service.rb', line 25

def api_version
  @api_version
end

#armrest_configurationObject Also known as: configuration

Configuration to access azure APIs



11
12
13
# File 'lib/azure/armrest/armrest_service.rb', line 11

def armrest_configuration
  @armrest_configuration
end

#base_urlObject

Base url used for REST calls.



16
17
18
# File 'lib/azure/armrest/armrest_service.rb', line 16

def base_url
  @base_url
end

#providerObject

Provider for service specific API calls



19
20
21
# File 'lib/azure/armrest/armrest_service.rb', line 19

def provider
  @provider
end

#service_nameObject

The service name for the Service class



22
23
24
# File 'lib/azure/armrest/armrest_service.rb', line 22

def service_name
  @service_name
end

Class Method Details

.configure(options) ⇒ Object

Returns a new Armrest::Configuration object.

This method is deprecated, but is provided for backwards compatibility.



31
32
33
# File 'lib/azure/armrest/armrest_service.rb', line 31

def self.configure(options)
  Azure::Armrest::Configuration.new(options)
end

Instance Method Details

#get_provider(provider) ⇒ Object Also known as: geo_locations, provider_info

Returns information about the specific provider namespace.



64
65
66
# File 'lib/azure/armrest/armrest_service.rb', line 64

def get_provider(provider)
  configuration.providers.find { |rp| rp.namespace.casecmp(provider) == 0 }
end

#get_subscription(subscription_id = configuration.subscription_id) ⇒ Object Also known as: subscription_info

Return information for the specified subscription ID, or the subscription ID that was provided in the constructor if none is specified.



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/azure/armrest/armrest_service.rb', line 102

def get_subscription(subscription_id = configuration.subscription_id)
  url = url_with_api_version(
    configuration.api_version,
    @base_url,
    'subscriptions',
    subscription_id
  )

  response = rest_get(url)
  Azure::Armrest::Subscription.new(response.body)
end

#list_resource_groupsObject Also known as: resource_groups

Returns an array of ResourceGroup objects for the current subscription.



134
135
136
# File 'lib/azure/armrest/armrest_service.rb', line 134

def list_resource_groups
  Azure::Armrest::ResourceGroupService.new(configuration).list
end

#list_resources(resource_group = nil) ⇒ Object Also known as: resources

Returns an array of Resource objects for the current subscription. If a resource_group is provided, only list resources for that resource group.



121
122
123
124
125
126
127
# File 'lib/azure/armrest/armrest_service.rb', line 121

def list_resources(resource_group = nil)
  if resource_group
    Azure::Armrest::ResourceService.new(configuration).list(resource_group)
  else
    Azure::Armrest::ResourceService.new(configuration).list_all
  end
end

#list_subscriptionsObject Also known as: subscriptions

Returns a list of Subscription objects for the tenant.



89
90
91
92
93
# File 'lib/azure/armrest/armrest_service.rb', line 89

def list_subscriptions
  url = url_with_api_version(configuration.api_version, @base_url, 'subscriptions')
  response = rest_get(url)
  JSON.parse(response.body)['value'].map { |hash| Azure::Armrest::Subscription.new(hash) }
end

#locations(provider = nil) ⇒ Object

Returns a list of all locations for all resource types of the given provider. If you do not specify a provider, then the locations for all providers will be returned.

If you need individual details on a per-provider basis, use the provider_info method instead. –



80
81
82
83
84
85
# File 'lib/azure/armrest/armrest_service.rb', line 80

def locations(provider = nil)
  list = configuration.providers
  list = list.select { |rp| rp.namespace.casecmp(provider) == 0 } if provider

  list.collect { |rp| rp.resource_types.map(&:locations) }.flatten.uniq.sort
end

#poll(response) ⇒ Object

Poll a resource and return its current operations status. The response argument should be a ResponseHeaders object that contains the :azure_asyncoperation header. It may optionally be an object that returns a URL from a .to_s method.

This is meant to check the status of asynchronous operations, such as create or delete.



171
172
173
174
175
# File 'lib/azure/armrest/armrest_service.rb', line 171

def poll(response)
  return 'Succeeded' if [200, 201].include?(response.response_code)
  url = response.try(:azure_asyncoperation) || response.try(:location)
  JSON.parse(rest_get(url))['status']
end

#tagsObject

Returns a list of tags for the current subscription.



143
144
145
146
147
148
149
150
151
152
153
# File 'lib/azure/armrest/armrest_service.rb', line 143

def tags
  url = url_with_api_version(
    configuration.api_version,
    @base_url,
    'subscriptions',
    configuration.subscription_id,
    'tagNames'
  )
  resp = rest_get(url)
  JSON.parse(resp.body)["value"].map{ |hash| Azure::Armrest::Tag.new(hash) }
end

#tenantsObject

Returns a list of tenants that can be accessed.



157
158
159
160
161
# File 'lib/azure/armrest/armrest_service.rb', line 157

def tenants
  url = url_with_api_version(configuration.api_version, @base_url, 'tenants')
  resp = rest_get(url)
  JSON.parse(resp.body)['value'].map{ |hash| Azure::Armrest::Tenant.new(hash) }
end

#wait(response, max_time = 60) ⇒ Object

Wait for the given response to return a status of ‘Succeeded’, up to a maximum of max_time seconds, and return the operations status. The first argument must be a ResponseHeaders object that contains the azure_asyncoperation header.

Internally this will poll the response header every :retry_after seconds (or 10 seconds if that header isn’t found), up to a maximum of 60 seconds by default.

For most resources the max_time argument should be more than sufficient. Certain resources, such as virtual machines, could take longer.



189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/azure/armrest/armrest_service.rb', line 189

def wait(response, max_time = 60)
  sleep_time = response.respond_to?(:retry_after) ? response.retry_after.to_i : 10
  total_time = 0

  while (status = poll(response)).casecmp('Succeeded') != 0
    total_time += sleep_time
    break if total_time >= max_time
    sleep sleep_time
  end

  status
end