lib/azure/armrest/resource_provider_service.rb in azure-armrest-0.6.0 vs lib/azure/armrest/resource_provider_service.rb in azure-armrest-0.6.1

- old
+ new

@@ -1,112 +1,75 @@ module Azure module Armrest class ResourceProviderService < ArmrestService + extend Memoist + # The provider used in http requests. The default is 'Microsoft.Resources' attr_reader :provider - # The amount of time in seconds to cache certain methods. The default is 24 hours. - @cache_time = 24 * 60 * 60 - - class << self - # Get or set the cache time for all methods. The default is 24 hours. - attr_accessor :cache_time - end - # Creates and returns a new ResourceProviderService object. # - # Note that many ResourceProviderService instance methods are cached. You - # can set the cache_time for certain methods in the constructor, but keep in - # mind that it is a global setting for the class. You can also set this - # at the class level if desired. The default cache time is 24 hours. + # Note that many ResourceProviderService instance methods are cached. # # You can also set the provider. The default is 'Microsoft.Resources'. # def initialize(configuration, options = {}) super(configuration, 'resourceGroups', 'Microsoft.Resources', options) - - if options[:cache_time] - @cache_time = options[:cache_time] - self.class.send(:cache_time=, @cache_time) - end end # List all the providers for the current subscription. The results of # this method are cached. # def list - _list.map{ |hash| Azure::Armrest::ResourceProvider.new(hash) } - end - - # This is split out for the cache_method feature. - # - def _list response = rest_get(build_url) - JSON.parse(response)["value"] + resources = JSON.parse(response)["value"] + resources.map{ |hash| Azure::Armrest::ResourceProvider.new(hash) } end + memoize :list - private :_list - - # Cannot directly cache list method, because Marshal used by cache_method - # cannot dump anonymous classes, including the ones derived from - # Azure::Armrest::BaseModel. - # - # The same applies to other cached methods in this class. - cache_method(:_list, cache_time) - # List all the providers for Azure. This may include results that are - # not available for the current subscription. + # not available for the current subscription. The results of this method + # are cached. # def list_all - _list_all.map{ |hash| Azure::Armrest::ResourceProvider.new(hash) } - end - - def _list_all url = File.join(configuration.environment.resource_url, 'providers') url << "?api-version=#{@api_version}" response = rest_get(url) - JSON.parse(response)['value'] + resources = JSON.parse(response)['value'] + + resources.map{ |hash| Azure::Armrest::ResourceProvider.new(hash) } end + memoize :list_all - cache_method(:_list_all, cache_time) - # Return information about a specific +namespace+ provider. The results # of this method are cached. # def get(namespace) - Azure::Armrest::ResourceProvider.new(_get(namespace)) - end - - def _get(namespace) url = build_url(namespace) - rest_get(url).body + body = rest_get(url).body + Azure::Armrest::ResourceProvider.new(body) end + memoize :get - private :_get - - cache_method(:_get, cache_time) - # Returns an array of geo-locations for the given +namespace+ provider. # The results of this method are cached. # def list_geo_locations(namespace) url = build_url(namespace) response = rest_get(url) JSON.parse(response)['resourceTypes'].first['locations'] end + memoize :list_geo_locations - cache_method(:list_geo_locations, cache_time) - # Returns an array of supported api-versions for the given +namespace+ provider. # The results of this method are cached. # def list_api_versions(namespace) url = build_url(namespace) response = rest_get(url) JSON.parse(response)['resourceTypes'].first['apiVersions'] end - - cache_method(:list_api_versions, cache_time) + memoize :list_api_versions # Register the current subscription with the +namespace+ provider. # def register(namespace) url = build_url(namespace, 'register')