1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

[core] Deprecates 'connection' accessor

The 'connection' accessor in collections and models actually refered to
a subclassed instance of Fog::Service which creates confusion in the
code.

References to 'connection' could have meant the service or the
Fog::Connection held within that service.

This deprecates the connection accessor and replaces it with `#service`
as a read only value. When a collection or model is initalised then
service should be passed.

This commit also updates all the changes to @connection made by
providers in model initialisers since these depending on the presence of
the 'connection' key. The key is still accepted by outputs a warning.
This commit is contained in:
Paul Thornthwaite 2012-12-19 22:28:04 +00:00
parent 05dffba5bf
commit 7961ad6508
22 changed files with 122 additions and 38 deletions

View file

@ -12,25 +12,25 @@ module Fog
def files def files
@files ||= begin @files ||= begin
Fog::Storage::Atmos::Files.new( Fog::Storage::Atmos::Files.new(
:directory => self, :directory => self,
:connection => connection :service => service
) )
end end
end end
def directories def directories
@directories ||= begin @directories ||= begin
Fog::Storage::Atmos::Directories.new( Fog::Storage::Atmos::Directories.new(
:directory => self, :directory => self,
:connection => connection :service => service
) )
end end
end end
def save def save
self.key = attributes[:directory].key + key if attributes[:directory] self.key = attributes[:directory].key + key if attributes[:directory]
self.key = key + '/' unless key =~ /\/$/ self.key = key + '/' unless key =~ /\/$/
res = connection.post_namespace key res = service.post_namespace key
reload reload
end end
@ -42,7 +42,7 @@ module Fog
d.destroy(opts) d.destroy(opts)
end end
end end
connection.delete_namespace key service.delete_namespace key
end end

View file

@ -53,9 +53,13 @@ module Fog
def initialize(attributes={}) def initialize(attributes={})
self.groups ||= ["default"] unless (attributes[:subnet_id] || attributes[:security_group_ids]) self.groups ||= ["default"] unless (attributes[:subnet_id] || attributes[:security_group_ids])
self.flavor_id ||= 't1.micro' self.flavor_id ||= 't1.micro'
# Old 'connection' is renamed as service and should be used instead
prepare_service_value(attributes)
self.image_id ||= begin self.image_id ||= begin
self.username = 'ubuntu' self.username = 'ubuntu'
case attributes[:connection].instance_variable_get(:@region) # Ubuntu 10.04 LTS 64bit (EBS) case @service.instance_variable_get(:@region) # Ubuntu 10.04 LTS 64bit (EBS)
when 'ap-northeast-1' when 'ap-northeast-1'
'ami-5e0fa45f' 'ami-5e0fa45f'
when 'ap-southeast-1' when 'ap-southeast-1'

View file

@ -43,7 +43,11 @@ module Fog
self.flavor_id ||= 't1.micro' self.flavor_id ||= 't1.micro'
self.image_id ||= begin self.image_id ||= begin
self.username = 'ubuntu' self.username = 'ubuntu'
case attributes[:connection].instance_variable_get(:@region) # Ubuntu 10.04 LTS 64bit (EBS)
# Old 'connection' is renamed as service and should be used instead
prepare_service_value(attributes)
case @service.instance_variable_get(:@region) # Ubuntu 10.04 LTS 64bit (EBS)
when 'ap-northeast-1' when 'ap-northeast-1'
'ami-5e0fa45f' 'ami-5e0fa45f'
when 'ap-southeast-1' when 'ap-southeast-1'

View file

@ -194,7 +194,7 @@ module Fog
def missing_attributes(args) def missing_attributes(args)
missing = [] missing = []
for arg in [:connection] | args for arg in [:service] | args
unless send("#{arg}") || attributes.has_key?(arg) unless send("#{arg}") || attributes.has_key?(arg)
missing << arg missing << arg
end end

View file

@ -1,8 +1,12 @@
require "fog/core/deprecated_connection_accessors"
module Fog module Fog
class Collection < Array class Collection < Array
extend Fog::Attributes::ClassMethods extend Fog::Attributes::ClassMethods
include Fog::Attributes::InstanceMethods include Fog::Attributes::InstanceMethods
include Fog::Core::DeprecatedConnectionAccessors
attr_reader :service
Array.public_instance_methods(false).each do |method| Array.public_instance_methods(false).each do |method|
unless [:reject, :select, :slice].include?(method.to_sym) unless [:reject, :select, :slice].include?(method.to_sym)
@ -37,8 +41,6 @@ module Fog
end end
end end
attr_accessor :connection
remove_method :clear remove_method :clear
def clear def clear
@loaded = true @loaded = true
@ -56,11 +58,20 @@ module Fog
object.destroy object.destroy
end end
# Creates a new Fog::Collection based around the passed service
#
# @param [Hash] attributes
# @option attributes [Fog::Service] service Instance of a service
#
# @return [Fog::Collection]
#
def initialize(attributes = {}) def initialize(attributes = {})
@service = attributes.delete(:service)
@loaded = false @loaded = false
merge_attributes(attributes) merge_attributes(attributes)
end end
remove_method :inspect remove_method :inspect
def inspect def inspect
Thread.current[:formatador] ||= Formatador.new Thread.current[:formatador] ||= Formatador.new
@ -105,7 +116,7 @@ module Fog
model.new( model.new(
{ {
:collection => self, :collection => self,
:connection => connection :service => service
}.merge(attributes) }.merge(attributes)
) )
end end

View file

@ -0,0 +1,41 @@
module Fog
module Core
# This module covers the shared code used by models and collections
# that deprecates the confusing usage of 'connection' which was
# actually intended to be an instance of Fog::Service
module DeprecatedConnectionAccessors
# Sets the Service but using the wrong name!
#
# @deprecated The connection name was wrong and confusing since it refered to the service
# @param [Fog::Service] service An instance of a Fog service this collection is for
#
def connection=(service)
Fog::Logger.deprecation("#connection= is deprecated, pass :service in at creation [light_black](#{caller.first})[/]")
@service = service
end
# Returns the Service the collection is part of
#
# @deprecated #connection is deprecated due to confusing name, use #service instead
# @return [Fog::Service]
#
def connection
Fog::Logger.deprecation("#connection is deprecated, use #service instead [light_black](#{caller.first})[/]")
@service
end
# Prepares the value of the service based on the passed attributes
#
# @note Intended for use where the service is required before the normal
# initializer runs. The logic is run there with deprecation warnings.
#
# @param [Hash] attributes
# @return [Fog::Service]
#
def prepare_service_value(attributes)
@service = attributes[:service] || attributes[:connection]
end
end
end
end

View file

@ -1,12 +1,22 @@
require "fog/core/deprecated_connection_accessors"
module Fog module Fog
class Model class Model
extend Fog::Attributes::ClassMethods extend Fog::Attributes::ClassMethods
include Fog::Attributes::InstanceMethods include Fog::Attributes::InstanceMethods
include Fog::Core::DeprecatedConnectionAccessors
attr_accessor :collection, :connection attr_accessor :collection
attr_reader :service
def initialize(new_attributes = {}) def initialize(new_attributes = {})
# TODO Remove compatibility with old connection option
@service = @service || new_attributes.delete(:service)
if @service.nil? && new_attributes[:connection]
Fog::Logger.deprecation("Passing :connection option is deprecated, use :service instead [light_black](#{caller.first})[/]")
@service = new_attributes[:connection]
end
merge_attributes(new_attributes) merge_attributes(new_attributes)
end end

View file

@ -91,7 +91,7 @@ module Fog
constant = collection.to_s.split('_').map {|characters| characters[0...1].upcase << characters[1..-1]}.join('') constant = collection.to_s.split('_').map {|characters| characters[0...1].upcase << characters[1..-1]}.join('')
service::Collections.module_eval <<-EOS, __FILE__, __LINE__ service::Collections.module_eval <<-EOS, __FILE__, __LINE__
def #{collection}(attributes = {}) def #{collection}(attributes = {})
#{service}::#{constant}.new({:connection => self}.merge(attributes)) #{service}::#{constant}.new({:service => self}.merge(attributes))
end end
EOS EOS
end end

View file

@ -18,7 +18,8 @@ module Fog
attribute :disabled, :aliases => 'OS-FLV-DISABLED:disabled' attribute :disabled, :aliases => 'OS-FLV-DISABLED:disabled'
def initialize(attributes) def initialize(attributes)
@connection = attributes[:connection] # Old 'connection' is renamed as service and should be used instead
prepare_service_value(attributes)
super super
end end

View file

@ -12,7 +12,8 @@ module Fog
attribute :details attribute :details
def initialize(attributes) def initialize(attributes)
@connection = attributes[:connection] # Old 'connection' is renamed as service and should be used instead
prepare_service_value(attributes)
super super
end end

View file

@ -21,7 +21,8 @@ module Fog
attribute :links attribute :links
def initialize(attributes) def initialize(attributes)
@connection = attributes[:connection] # Old 'connection' is renamed as service and should be used instead
prepare_service_value(attributes)
super super
end end

View file

@ -44,7 +44,8 @@ module Fog
def initialize(attributes={}) def initialize(attributes={})
@connection = attributes[:connection] # Old 'connection' is renamed as service and should be used instead
prepare_service_value(attributes)
attributes[:metadata] = {} attributes[:metadata] = {}
self.security_groups = attributes.delete(:security_groups) self.security_groups = attributes.delete(:security_groups)

View file

@ -18,7 +18,8 @@ module Fog
def initialize(attributes) def initialize(attributes)
@connection = attributes[:connection] # Old 'connection' is renamed as service and should be used instead
prepare_service_value(attributes)
super super
end end

View file

@ -21,7 +21,8 @@ module Fog
def initialize(attributes) def initialize(attributes)
@connection = attributes[:connection] # Old 'connection' is renamed as service and should be used instead
prepare_service_value(attributes)
super super
end end

View file

@ -11,7 +11,8 @@ module Fog
attribute :user_id attribute :user_id
def initialize(attributes) def initialize(attributes)
@connection = attributes[:connection] # Old 'connection' is renamed as service and should be used instead
prepare_service_value(attributes)
super super
end end

View file

@ -15,7 +15,8 @@ module Fog
attr_accessor :email, :name, :tenant_id, :enabled, :password attr_accessor :email, :name, :tenant_id, :enabled, :password
def initialize(attributes) def initialize(attributes)
@connection = attributes[:connection] # Old 'connection' is renamed as service and should be used instead
prepare_service_value(attributes)
super super
end end

View file

@ -32,7 +32,8 @@ module Fog
def initialize(attributes) def initialize(attributes)
@connection = attributes[:connection] # Old 'connection' is renamed as service and should be used instead
prepare_service_value(attributes)
super super
end end

View file

@ -14,7 +14,8 @@ module Fog
attribute :tenant_id attribute :tenant_id
def initialize(attributes) def initialize(attributes)
@connection = attributes[:connection] # Old 'connection' is renamed as service and should be used instead
prepare_service_value(attributes)
super super
end end
@ -43,4 +44,4 @@ module Fog
end end
end end
end end
end end

View file

@ -17,7 +17,8 @@ module Fog
attribute :tenant_id attribute :tenant_id
def initialize(attributes) def initialize(attributes)
@connection = attributes[:connection] # Old 'connection' is renamed as service and should be used instead
prepare_service_value(attributes)
super super
end end
@ -49,4 +50,4 @@ module Fog
end end
end end
end end
end end

View file

@ -18,7 +18,8 @@ module Fog
attribute :tenant_id attribute :tenant_id
def initialize(attributes) def initialize(attributes)
@connection = attributes[:connection] # Old 'connection' is renamed as service and should be used instead
prepare_service_value(attributes)
super super
end end
@ -52,4 +53,4 @@ module Fog
end end
end end
end end
end end

View file

@ -20,7 +20,8 @@ module Fog
def initialize(attributes) def initialize(attributes)
@connection = attributes[:connection] # Old 'connection' is renamed as service and should be used instead
prepare_service_value(attributes)
super super
end end

View file

@ -29,8 +29,9 @@ module Fog
attribute :nodes attribute :nodes
def initialize(attributes) def initialize(attributes)
#HACK - Since we are hacking how sub-collections work, we have to make sure the connection is valid first. #HACK - Since we are hacking how sub-collections work, we have to make sure the service is valid first.
@connection = attributes[:connection] # Old 'connection' is renamed as service and should be used instead
@service = attributes[:service] || attributes[:connection]
super super
end end