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.



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

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



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

def api_version
  @api_version
end

#armrest_configurationObject Also known as: configuration

Configuration to access azure APIs



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

def armrest_configuration
  @armrest_configuration
end

#base_urlObject

Base url with subscription information used for most REST calls.



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

def base_url
  @base_url
end

#providerObject

Provider for service specific API calls



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

def provider
  @provider
end

#service_nameObject

The service name for the Service class



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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
176
177
178
179
# 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)
  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.



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

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.



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

def tenants
  url = url_with_api_version(configuration.api_version, configuration.environment.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.



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

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