This commit is contained in:
Wesley Beary 2010-01-04 22:03:24 -08:00
Родитель cf0257a596
Коммит 81634c5cd3
13 изменённых файлов: 111 добавлений и 120 удалений

Просмотреть файл

@ -21,18 +21,20 @@ module Fog
end end
def all(public_ip = @public_ip) def all(public_ip = @public_ip)
@public_ip = public_ip
if @loaded
clear
end
@loaded = true
data = connection.describe_addresses(public_ip).body data = connection.describe_addresses(public_ip).body
addresses = Fog::AWS::EC2::Addresses.new({ addresses = []
:connection => connection,
:public_ip => public_ip
}.merge!(attributes))
data['addressesSet'].each do |address| data['addressesSet'].each do |address|
addresses << new(address.reject {|key, value| value.nil? || value.empty? }) addresses << new(address.reject {|key, value| value.nil? || value.empty? })
end end
if instance if instance
addresses = addresses.select {|address| address.instance_id == instance.id} addresses = addresses.select {|address| address.instance_id == instance.id}
end end
addresses self.replace(addresses)
end end
def get(public_ip) def get(public_ip)

Просмотреть файл

@ -18,20 +18,18 @@ module Fog
end end
def all(instance_id = @instance_id) def all(instance_id = @instance_id)
@instance_id = instance_id
if @loaded
clear
end
@loaded = true
data = connection.describe_instances(instance_id).body data = connection.describe_instances(instance_id).body
instances = Fog::AWS::EC2::Instances.new({
:connection => connection,
:instance_id => instance_id
}.merge!(attributes))
data['reservationSet'].each do |reservation| data['reservationSet'].each do |reservation|
reservation['instancesSet'].each do |instance| reservation['instancesSet'].each do |instance|
instances << Fog::AWS::EC2::Instance.new({ self << new(instance)
:collection => instances,
:connection => connection
}.merge!(instance))
end end
end end
instances self
end end
def get(instance_id) def get(instance_id)

Просмотреть файл

@ -18,18 +18,16 @@ module Fog
end end
def all(key_name = @key_name) def all(key_name = @key_name)
data = connection.describe_key_pairs(key_name).body @key_name = key_name
key_pairs = Fog::AWS::EC2::KeyPairs.new({ if @loaded
:connection => connection, clear
:key_name => key_name
}.merge!(attributes))
data['keySet'].each do |key|
key_pairs << Fog::AWS::EC2::KeyPair.new({
:collection => key_pairs,
:connection => connection
}.merge!(key))
end end
key_pairs @loaded = true
data = connection.describe_key_pairs(key_name).body
data['keySet'].each do |key|
self << new(key)
end
self
end end
def get(key_name) def get(key_name)

Просмотреть файл

@ -18,18 +18,16 @@ module Fog
end end
def all(group_name = @group_name) def all(group_name = @group_name)
data = connection.describe_security_groups(group_name).body @group_name = group_name
security_groups = Fog::AWS::EC2::SecurityGroups.new({ if @loaded
:connection => connection, clear
:group_name => group_name
}.merge!(attributes))
data['securityGroupInfo'].each do |security_group|
security_groups << Fog::AWS::EC2::SecurityGroup.new({
:collection => security_groups,
:connection => connection
}.merge!(security_group))
end end
security_groups @loaded = true
data = connection.describe_security_groups(group_name).body
data['securityGroupInfo'].each do |security_group|
self << new(security_group)
end
self
end end
def get(group_name) def get(group_name)

Просмотреть файл

@ -21,21 +21,20 @@ module Fog
end end
def all(snapshot_id = @snapshot_id) def all(snapshot_id = @snapshot_id)
@snapshot_id = snapshot_id
if @loaded
clear
end
@loaded = true
data = connection.describe_snapshots(snapshot_id).body data = connection.describe_snapshots(snapshot_id).body
snapshots = Fog::AWS::EC2::Snapshots.new({ snapshots = []
:connection => connection,
:snapshot_id => snapshot_id
}.merge!(attributes))
data['snapshotSet'].each do |snapshot| data['snapshotSet'].each do |snapshot|
snapshots << Fog::AWS::EC2::Snapshot.new({ snapshots << new(snapshot)
:collection => snapshots,
:connection => connection
}.merge!(snapshot))
end end
if volume if volume
snapshots = snapshots.select {|snapshot| snapshot.volume_id == volume.id} snapshots = snapshots.select {|snapshot| snapshot.volume_id == volume.id}
end end
snapshots self.replace(snapshots)
end end
def get(snapshot_id) def get(snapshot_id)

Просмотреть файл

@ -21,21 +21,20 @@ module Fog
end end
def all(volume_id = @volume_id) def all(volume_id = @volume_id)
@volume_id = volume_id
if @loaded
clear
end
@loaded = true
data = connection.describe_volumes(volume_id).body data = connection.describe_volumes(volume_id).body
volumes = Fog::AWS::EC2::Volumes.new({ volumes = []
:connection => connection,
:volume_id => volume_id
}.merge!(attributes))
data['volumeSet'].each do |volume| data['volumeSet'].each do |volume|
volumes << Fog::AWS::EC2::Volume.new({ volumes << new(volume)
:collection => volumes,
:connection => connection
}.merge!(volume))
end end
if instance if instance
volumes = volumes.select {|volume| volume.instance_id == instance.id} volumes = volumes.select {|volume| volume.instance_id == instance.id}
end end
volumes self.replace(volumes)
end end
def get(volume_id) def get(volume_id)

Просмотреть файл

@ -11,15 +11,15 @@ module Fog
model Fog::AWS::S3::Bucket model Fog::AWS::S3::Bucket
def all def all
data = connection.get_service.body if @loaded
buckets = Fog::AWS::S3::Buckets.new(:connection => connection) clear
data['Buckets'].each do |bucket|
buckets << Fog::AWS::S3::Bucket.new({
:collection => buckets,
:connection => connection
}.merge!(bucket))
end end
buckets @loaded = true
data = connection.get_service.body
data['Buckets'].each do |bucket|
self << new(bucket)
end
self
end end
def get(name, options = {}) def get(name, options = {})
@ -27,11 +27,7 @@ module Fog
:max_keys => 'max-keys', :max_keys => 'max-keys',
}) })
data = connection.get_bucket(name, options).body data = connection.get_bucket(name, options).body
bucket = Fog::AWS::S3::Bucket.new({ bucket = new(:name => data['Name'])
:collection => self,
:connection => connection,
:name => data['Name']
})
options = {} options = {}
for key, value in data for key, value in data
if ['Delimiter', 'IsTruncated', 'Marker', 'MaxKeys', 'Prefix'].include?(key) if ['Delimiter', 'IsTruncated', 'Marker', 'MaxKeys', 'Prefix'].include?(key)
@ -39,12 +35,9 @@ module Fog
end end
end end
bucket.objects.merge_attributes(options) bucket.objects.merge_attributes(options)
bucket.objects.instance_variable_set(:@loaded, true)
data['Contents'].each do |object| data['Contents'].each do |object|
bucket.objects << Fog::AWS::S3::Object.new({ bucket.objects << bucket.objects.new(object)
:bucket => bucket,
:connection => connection,
:collection => bucket.objects
}.merge!(object))
end end
bucket bucket
rescue Excon::Errors::NotFound rescue Excon::Errors::NotFound

Просмотреть файл

@ -13,11 +13,20 @@ module Fog
model Fog::AWS::S3::Object model Fog::AWS::S3::Object
def all(options = {}) def all(options = {})
merge_attributes(options)
if @loaded
clear
end
@loaded = true
collection = bucket.collection.get( collection = bucket.collection.get(
bucket.name, bucket.name,
options options
) )
collection && collection.objects if collection
self.replace(collection.objects)
else
nil
end
end end
def bucket def bucket
@ -41,12 +50,7 @@ module Fog
object_data[key] = value object_data[key] = value
end end
end end
object = Fog::AWS::S3::Object.new({ new(object_data)
:bucket => bucket,
:collection => self,
:connection => connection
}.merge!(object_data))
object
rescue Excon::Errors::NotFound rescue Excon::Errors::NotFound
nil nil
end end
@ -65,12 +69,7 @@ module Fog
object_data[key] = value object_data[key] = value
end end
end end
object = Fog::AWS::S3::Object.new({ new(object_data)
:bucket => bucket,
:collection => self,
:connection => connection
}.merge!(object_data))
object
rescue Excon::Errors::NotFound rescue Excon::Errors::NotFound
nil nil
end end

Просмотреть файл

@ -39,7 +39,7 @@ unless Fog.mocking?
end end
query = '' query = ''
for key, value in options for key, value in options
query << "#{key}=#{CGI.escape(value).gsub(/\+/, '%20')};" query << "#{key}=#{CGI.escape(value.to_s).gsub(/\+/, '%20')};"
end end
query.chop! query.chop!
request({ request({

Просмотреть файл

@ -1,6 +1,15 @@
module Fog module Fog
class Collection < Array class Collection < Array
Array.public_instance_methods(false).each do |method|
class_eval <<-RUBY
def #{method}(*args)
lazy_load
super
end
RUBY
end
def self._load(marhsalled) def self._load(marhsalled)
new(Marshal.load(marshalled)) new(Marshal.load(marshalled))
end end
@ -56,6 +65,7 @@ module Fog
def initialize(attributes = {}) def initialize(attributes = {})
merge_attributes(attributes) merge_attributes(attributes)
@loaded = false
end end
def inspect def inspect
@ -101,6 +111,12 @@ module Fog
private private
def lazy_load
unless @loaded
self.all
end
end
def remap_attributes(attributes, mapping) def remap_attributes(attributes, mapping)
for key, value in mapping for key, value in mapping
if attributes.key?(key) if attributes.key?(key)

Просмотреть файл

@ -11,17 +11,15 @@ module Fog
model Fog::Rackspace::Servers::Flavor model Fog::Rackspace::Servers::Flavor
def all def all
data = connection.list_flavors_detail.body if @loaded
flavors = Fog::Rackspace::Servers::Flavors.new({ clear
:connection => connection
})
for flavor in data['flavors']
flavors << Fog::Rackspace::Servers::Flavor.new({
:collection => flavors,
:connection => connection
}.merge!(flavor))
end end
flavors @loaded = true
data = connection.list_flavors_detail.body
for flavor in data['flavors']
self << new(flavor)
end
self
end end
def get(flavor_id) def get(flavor_id)

Просмотреть файл

@ -2,12 +2,6 @@ module Fog
module Rackspace module Rackspace
class Servers class Servers
def addresses(attributes = {})
Fog::AWS::EC2::Addresses.new({
:connection => self
}.merge!(attributes))
end
def images(attributes = {}) def images(attributes = {})
Fog::Rackspace::Servers::Images.new({ Fog::Rackspace::Servers::Images.new({
:connection => self :connection => self
@ -21,20 +15,19 @@ module Fog
attribute :server attribute :server
def all def all
if @loaded
clear
end
@loaded = true
data = connection.list_images_detail.body data = connection.list_images_detail.body
servers = Fog::Rackspace::Servers::Images.new({ images = []
:connection => connection
})
for image in data['images'] for image in data['images']
servers << Fog::Rackspace::Servers::Image.new({ images << new(image)
:collection => images,
:connection => connection
}.merge!(image))
end end
if server if server
images = images.select {|image| image.server_id == server.id} images = images.select {|image| image.server_id == server.id}
end end
images self.replace(images)
end end
def get(image_id) def get(image_id)

Просмотреть файл

@ -11,17 +11,15 @@ module Fog
model Fog::Rackspace::Servers::Server model Fog::Rackspace::Servers::Server
def all def all
data = connection.list_servers_detail.body if @loaded
servers = Fog::Rackspace::Servers::Servers.new({ clear
:connection => connection
})
for server in data['servers']
servers << Fog::Rackspace::Servers::Server.new({
:collection => servers,
:connection => connection
}.merge!(server))
end end
servers @loaded = true
data = connection.list_servers_detail.body
for server in data['servers']
self << new(server)
end
self
end end
def get(server_id) def get(server_id)