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

DRY through more collection abstraction

This commit is contained in:
Wesley Beary 2009-10-29 23:35:28 -07:00
parent d3e4ce15e0
commit 09fb45b7ba
11 changed files with 80 additions and 186 deletions

View file

@ -13,12 +13,14 @@ module Fog
attribute :public_ip
attribute :instance
klass Fog::AWS::EC2::Address
def initialize(attributes)
@public_ip ||= []
super
end
def all(public_ip = [])
def all(public_ip = @public_ip)
data = connection.describe_addresses(public_ip).body
addresses = Fog::AWS::EC2::Addresses.new({
:connection => connection,
@ -36,12 +38,6 @@ module Fog
addresses
end
def create
address = new
address.save
address
end
def get(public_ip)
if public_ip
all(public_ip).first
@ -50,16 +46,8 @@ module Fog
nil
end
def new
Fog::AWS::EC2::Address.new(
:collection => self,
:connection => connection,
:instance => instance
)
end
def reload
self.clear.concat(all(public_ip))
def new(attributes = {})
super({ :instance => instance }.merge!(attributes))
end
end

View file

@ -10,12 +10,14 @@ module Fog
attribute :instance_id
klass Fog::AWS::EC2::Instance
def initialize(attributes)
@instance_id ||= []
super
end
def all(instance_id = [])
def all(instance_id = @instance_id)
data = connection.describe_instances(instance_id).body
instances = Fog::AWS::EC2::Instances.new({
:connection => connection,
@ -32,12 +34,6 @@ module Fog
instances
end
def create(attributes = {})
instance = new(attributes)
instance.save
instance
end
def get(instance_id)
if instance_id
all(instance_id).first
@ -46,19 +42,6 @@ module Fog
nil
end
def new(attributes = {})
Fog::AWS::EC2::Instance.new(
attributes.merge!(
:collection => self,
:connection => connection
)
)
end
def reload
self.clear.concat(all(instance_id))
end
end
end

View file

@ -10,12 +10,14 @@ module Fog
attribute :key_name
klass Fog::AWS::EC2::KeyPair
def initialize(attributes)
@key_name ||= []
super
end
def all(key_name = [])
def all(key_name = @key_name)
data = connection.describe_key_pairs(key_name).body
key_pairs = Fog::AWS::EC2::KeyPairs.new({
:connection => connection,
@ -30,12 +32,6 @@ module Fog
key_pairs
end
def create(attributes = {})
bucket = new(attributes)
bucket.save
bucket
end
def get(key_name)
if key_name
all(key_name).first
@ -44,19 +40,6 @@ module Fog
nil
end
def new(attributes = {})
Fog::AWS::EC2::KeyPair.new(
attributes.merge!(
:collection => self,
:connection => connection
)
)
end
def reload
self.clear.concat(all(key_name))
end
end
end

View file

@ -10,12 +10,14 @@ module Fog
attribute :group_name
klass Fog::AWS::EC2::SecurityGroup
def initialize(attributes)
@group_name ||= []
super
end
def all(group_name = [])
def all(group_name = @group_name)
data = connection.describe_security_groups(group_name).body
security_groups = Fog::AWS::EC2::SecurityGroups.new({
:connection => connection,
@ -30,12 +32,6 @@ module Fog
security_groups
end
def create(attributes = {})
security_group = new(attributes)
security_group.save
security_group
end
def get(group_name)
if group_name
all(group_name).first
@ -44,19 +40,6 @@ module Fog
nil
end
def new(attributes = {})
Fog::AWS::EC2::SecurityGroup.new(
attributes.merge!(
:collection => self,
:connection => connection
)
)
end
def reload
self.clear.concat(all(group_name))
end
end
end

View file

@ -13,12 +13,14 @@ module Fog
attribute :snapshot_id
attribute :volume_id
klass Fog::AWS::EC2::Snapshot
def initialize(attributes)
@snapshot_id ||= []
super
end
def all(snapshot_id = [])
def all(snapshot_id = @snapshot_id)
data = connection.describe_snapshots(snapshot_id).body
snapshots = Fog::AWS::EC2::Snapshots.new({
:connection => connection,
@ -36,12 +38,6 @@ module Fog
snapshots
end
def create(attributes = {})
snapshot = new(attributes)
snapshot.save
snapshot
end
def get(snapshot_id)
if snapshot_id
all(snapshot_id).first
@ -51,22 +47,13 @@ module Fog
end
def new(attributes = {})
snapshot = Fog::AWS::EC2::Snapshot.new(
attributes.merge!(
:collection => self,
:connection => connection
)
)
snapshot = super(attributes)
if volume_id
snapshot.volume_id = volume_id
end
snapshot
end
def reload
self.clear.concat(all(snapshot_id))
end
end
end

View file

@ -13,12 +13,14 @@ module Fog
attribute :volume_id
attribute :instance
klass Fog::AWS::EC2::Volume
def initialize(attributes)
@volume_id ||= []
super
end
def all(volume_id = [])
def all(volume_id = @volume_id)
data = connection.describe_volumes(volume_id).body
volumes = Fog::AWS::EC2::Volumes.new({
:connection => connection,
@ -36,12 +38,6 @@ module Fog
volumes
end
def create(attributes = {})
volume = new(attributes)
volume.save
volume
end
def get(volume_id)
if volume_id
all(volume_id).first
@ -51,17 +47,7 @@ module Fog
end
def new(attributes = {})
volume = Fog::AWS::EC2::Volume.new(
attributes.merge!(
:collection => self,
:connection => connection,
:instance => instance
)
)
end
def reload
self.clear.concat(all(volume_id))
super({ :instance => instance }.merge!(attributes))
end
end

View file

@ -8,6 +8,8 @@ module Fog
class Buckets < Fog::Collection
klass Fog::AWS::S3::Bucket
def all
data = connection.get_service.body
owner = Fog::AWS::S3::Owner.new(data.delete('Owner').merge!(:connection => connection))
@ -22,12 +24,6 @@ module Fog
buckets
end
def create(attributes = {})
bucket = new(attributes)
bucket.save
bucket
end
def get(name, options = {})
remap_attributes(options, {
:max_keys => 'max-keys',
@ -59,19 +55,6 @@ module Fog
nil
end
def new(attributes = {})
Fog::AWS::S3::Bucket.new(
attributes.merge!(
:collection => self,
:connection => connection
)
)
end
def reload
self.clear.concat(all)
end
end
end

View file

@ -10,6 +10,8 @@ module Fog
attribute :max_keys, 'MaxKeys'
attribute :prefix, 'Prefix'
klass Fog::AWS::S3::Object
def all(options = {})
bucket.collection.get(
bucket.name,
@ -21,13 +23,13 @@ module Fog
@bucket
end
def create(attributes = {})
object = new(attributes)
object.save
object
end
def get(key, options = {}, &block)
options = {
'delimiter' => @delimiter,
'marker' => @marker,
'max-keys' => @max_keys,
'prefix' => @prefix
}.merge!(options)
data = connection.get_object(bucket.name, key, options, &block)
object_data = {
:body => data.body,
@ -73,20 +75,7 @@ module Fog
end
def new(attributes = {})
Fog::AWS::S3::Object.new({
:bucket => bucket,
:collection => self,
:connection => connection
}.merge!(attributes))
end
def reload
self.clear.concat(all({
'delimiter' => @delimiter,
'marker' => @marker,
'max-keys' => @max_keys,
'prefix' => @prefix
}))
super({ :bucket => bucket }.merge!(attributes))
end
private

View file

@ -11,6 +11,10 @@ module Fog
end
end
def self.klass(new_klass)
@instance = new_klass
end
def self.aliases
@aliases ||= {}
end
@ -19,6 +23,20 @@ module Fog
@attributes ||= []
end
def attributes
attributes = {}
for attribute in self.class.attributes
attributes[attribute] = send("#{attribute}")
end
attributes
end
def create(attributes = {})
object = new(attributes)
object.save
object
end
def initialize(attributes = {})
merge_attributes(attributes)
end
@ -36,12 +54,8 @@ module Fog
data << "]>"
end
def attributes
attributes = {}
for attribute in self.class.attributes
attributes[attribute] = send("#{attribute}")
end
attributes
def klass
self.class.instance_variable_get('@instance')
end
def merge_attributes(new_attributes = {})
@ -55,6 +69,19 @@ module Fog
self
end
def new(attributes = {})
klass.new(
attributes.merge!(
:collection => self,
:connection => connection
)
)
end
def reload
self.clear.concat(all)
end
private
def connection=(new_connection)

View file

@ -24,6 +24,18 @@ module Fog
@attributes ||= []
end
def attributes
attributes = {}
for attribute in self.class.attributes
attributes[attribute] = send("#{attribute}")
end
attributes
end
def collection
@collection
end
def identity
send(self.class.instance_variable_get('@identity'))
end
@ -40,18 +52,6 @@ module Fog
data << ">"
end
def attributes
attributes = {}
for attribute in self.class.attributes
attributes[attribute] = send("#{attribute}")
end
attributes
end
def collection
@collection
end
def merge_attributes(new_attributes = {})
for key, value in new_attributes
if aliased_key = self.class.aliases[key]

View file

@ -8,6 +8,8 @@ module Fog
class Servers < Fog::Collection
klass Fog::Rackspace::Servers::Server
def all
data = connection.list_servers_details.body
servers = Fog::Rackspace::Servers::Servers.new({
@ -22,29 +24,12 @@ module Fog
servers
end
def create(attributes = {})
server = new(attributes)
server.save
server
end
def get(id)
connection.get_server_details(id)
rescue Fog::Errors::NotFound
nil
end
def new(attributes = {})
Fog::Rackspace::Servers::Server.new({
:collection => self,
:connection => connection
}.merge!(attributes))
end
def reload
self.clear.concat(all)
end
end
end