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.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/azure/armrest/armrest_service.rb', line 40

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

  if configuration.subscription_id.nil?
    raise ArgumentError, 'subscription_id must be specified for this Service class'
  end

  # Base URL used for REST calls. Modify within method calls as needed.
  @base_url = File.join(
    configuration.environment.resource_url,
    'subscriptions',
    configuration.subscription_id
  )

  set_service_api_version(options, service_name)
end

Instance Attribute Details

#api_versionObject

The api-version string for this particular service



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

def api_version
  @api_version
end

#armrest_configurationObject Also known as: configuration

Configuration to access azure APIs



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

def armrest_configuration
  @armrest_configuration
end

#base_urlObject

Base url with subscription information used for most REST calls.



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

def base_url
  @base_url
end

#providerObject

Provider for service specific API calls



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

def provider
  @provider
end

#service_nameObject

The service name for the Service class



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

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.



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

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.



69
70
71
# File 'lib/azure/armrest/armrest_service.rb', line 69

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.



114
115
116
117
# File 'lib/azure/armrest/armrest_service.rb', line 114

def get_subscription(subscription_id = configuration.subscription_id)
  subs = Azure::Armrest::SubscriptionService.new(configuration)
  subs.get(subscription_id)
end

#list_locationsObject

Returns a list of Location objects for the current subscription.



96
97
98
99
100
# File 'lib/azure/armrest/armrest_service.rb', line 96

def list_locations
  url = url_with_api_version(configuration.api_version, base_url, 'locations')
  response = rest_get(url)
  Azure::Armrest::ArmrestCollection.create_from_response(response, Location)
end

#list_resource_groupsObject Also known as: resource_groups

Returns an array of ResourceGroup objects for the current subscription.



139
140
141
# File 'lib/azure/armrest/armrest_service.rb', line 139

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.



126
127
128
129
130
131
132
# File 'lib/azure/armrest/armrest_service.rb', line 126

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 subscriptions for the current tenant.



103
104
105
# File 'lib/azure/armrest/armrest_service.rb', line 103

def list_subscriptions
  Azure::Armrest::SubscriptionService.new(configuration).list
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 methods of the ResourceProviderService instead.

Deprecated.



86
87
88
89
90
# File 'lib/azure/armrest/armrest_service.rb', line 86

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.



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

def poll(response)
  return 'Succeeded' if [200, 201].include?(response.response_code)
  url = response.try(:azure_asyncoperation) || response.try(:location)
  response = rest_get(url).body
  unless response.blank?
    status = JSON.parse(response)['status']
  end
  status || 'Succeeded' # assume succeeded otherwise the wait method may hang
end

#tagsObject

Returns a list of tags for the current subscription.



148
149
150
151
152
# File 'lib/azure/armrest/armrest_service.rb', line 148

def tags
  url = url_with_api_version(configuration.api_version, base_url, '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.



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

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

#wait(response, max_time = 60, default_interval = 10) ⇒ 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. There is no timeout limit if max_time is 0.

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



192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/azure/armrest/armrest_service.rb', line 192

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

  until (status = poll(response)) =~ /^succe/i # success or succeeded
    total_time += sleep_time
    break if max_time > 0 && total_time >= max_time
    sleep sleep_time
  end

  status
end