зеркало из https://github.com/microsoft/fog.git
lazy array style collections
This commit is contained in:
Родитель
cf0257a596
Коммит
81634c5cd3
|
@ -21,18 +21,20 @@ module Fog
|
|||
end
|
||||
|
||||
def all(public_ip = @public_ip)
|
||||
@public_ip = public_ip
|
||||
if @loaded
|
||||
clear
|
||||
end
|
||||
@loaded = true
|
||||
data = connection.describe_addresses(public_ip).body
|
||||
addresses = Fog::AWS::EC2::Addresses.new({
|
||||
:connection => connection,
|
||||
:public_ip => public_ip
|
||||
}.merge!(attributes))
|
||||
addresses = []
|
||||
data['addressesSet'].each do |address|
|
||||
addresses << new(address.reject {|key, value| value.nil? || value.empty? })
|
||||
end
|
||||
if instance
|
||||
addresses = addresses.select {|address| address.instance_id == instance.id}
|
||||
end
|
||||
addresses
|
||||
self.replace(addresses)
|
||||
end
|
||||
|
||||
def get(public_ip)
|
||||
|
|
|
@ -18,20 +18,18 @@ module Fog
|
|||
end
|
||||
|
||||
def all(instance_id = @instance_id)
|
||||
@instance_id = instance_id
|
||||
if @loaded
|
||||
clear
|
||||
end
|
||||
@loaded = true
|
||||
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|
|
||||
reservation['instancesSet'].each do |instance|
|
||||
instances << Fog::AWS::EC2::Instance.new({
|
||||
:collection => instances,
|
||||
:connection => connection
|
||||
}.merge!(instance))
|
||||
self << new(instance)
|
||||
end
|
||||
end
|
||||
instances
|
||||
self
|
||||
end
|
||||
|
||||
def get(instance_id)
|
||||
|
|
|
@ -18,18 +18,16 @@ module Fog
|
|||
end
|
||||
|
||||
def all(key_name = @key_name)
|
||||
data = connection.describe_key_pairs(key_name).body
|
||||
key_pairs = Fog::AWS::EC2::KeyPairs.new({
|
||||
:connection => connection,
|
||||
: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))
|
||||
@key_name = key_name
|
||||
if @loaded
|
||||
clear
|
||||
end
|
||||
key_pairs
|
||||
@loaded = true
|
||||
data = connection.describe_key_pairs(key_name).body
|
||||
data['keySet'].each do |key|
|
||||
self << new(key)
|
||||
end
|
||||
self
|
||||
end
|
||||
|
||||
def get(key_name)
|
||||
|
|
|
@ -18,18 +18,16 @@ module Fog
|
|||
end
|
||||
|
||||
def all(group_name = @group_name)
|
||||
data = connection.describe_security_groups(group_name).body
|
||||
security_groups = Fog::AWS::EC2::SecurityGroups.new({
|
||||
:connection => connection,
|
||||
: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))
|
||||
@group_name = group_name
|
||||
if @loaded
|
||||
clear
|
||||
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
|
||||
|
||||
def get(group_name)
|
||||
|
|
|
@ -21,21 +21,20 @@ module Fog
|
|||
end
|
||||
|
||||
def all(snapshot_id = @snapshot_id)
|
||||
@snapshot_id = snapshot_id
|
||||
if @loaded
|
||||
clear
|
||||
end
|
||||
@loaded = true
|
||||
data = connection.describe_snapshots(snapshot_id).body
|
||||
snapshots = Fog::AWS::EC2::Snapshots.new({
|
||||
:connection => connection,
|
||||
:snapshot_id => snapshot_id
|
||||
}.merge!(attributes))
|
||||
snapshots = []
|
||||
data['snapshotSet'].each do |snapshot|
|
||||
snapshots << Fog::AWS::EC2::Snapshot.new({
|
||||
:collection => snapshots,
|
||||
:connection => connection
|
||||
}.merge!(snapshot))
|
||||
snapshots << new(snapshot)
|
||||
end
|
||||
if volume
|
||||
snapshots = snapshots.select {|snapshot| snapshot.volume_id == volume.id}
|
||||
end
|
||||
snapshots
|
||||
self.replace(snapshots)
|
||||
end
|
||||
|
||||
def get(snapshot_id)
|
||||
|
|
|
@ -21,21 +21,20 @@ module Fog
|
|||
end
|
||||
|
||||
def all(volume_id = @volume_id)
|
||||
@volume_id = volume_id
|
||||
if @loaded
|
||||
clear
|
||||
end
|
||||
@loaded = true
|
||||
data = connection.describe_volumes(volume_id).body
|
||||
volumes = Fog::AWS::EC2::Volumes.new({
|
||||
:connection => connection,
|
||||
:volume_id => volume_id
|
||||
}.merge!(attributes))
|
||||
volumes = []
|
||||
data['volumeSet'].each do |volume|
|
||||
volumes << Fog::AWS::EC2::Volume.new({
|
||||
:collection => volumes,
|
||||
:connection => connection
|
||||
}.merge!(volume))
|
||||
volumes << new(volume)
|
||||
end
|
||||
if instance
|
||||
volumes = volumes.select {|volume| volume.instance_id == instance.id}
|
||||
end
|
||||
volumes
|
||||
self.replace(volumes)
|
||||
end
|
||||
|
||||
def get(volume_id)
|
||||
|
|
|
@ -11,15 +11,15 @@ module Fog
|
|||
model Fog::AWS::S3::Bucket
|
||||
|
||||
def all
|
||||
data = connection.get_service.body
|
||||
buckets = Fog::AWS::S3::Buckets.new(:connection => connection)
|
||||
data['Buckets'].each do |bucket|
|
||||
buckets << Fog::AWS::S3::Bucket.new({
|
||||
:collection => buckets,
|
||||
:connection => connection
|
||||
}.merge!(bucket))
|
||||
if @loaded
|
||||
clear
|
||||
end
|
||||
buckets
|
||||
@loaded = true
|
||||
data = connection.get_service.body
|
||||
data['Buckets'].each do |bucket|
|
||||
self << new(bucket)
|
||||
end
|
||||
self
|
||||
end
|
||||
|
||||
def get(name, options = {})
|
||||
|
@ -27,11 +27,7 @@ module Fog
|
|||
:max_keys => 'max-keys',
|
||||
})
|
||||
data = connection.get_bucket(name, options).body
|
||||
bucket = Fog::AWS::S3::Bucket.new({
|
||||
:collection => self,
|
||||
:connection => connection,
|
||||
:name => data['Name']
|
||||
})
|
||||
bucket = new(:name => data['Name'])
|
||||
options = {}
|
||||
for key, value in data
|
||||
if ['Delimiter', 'IsTruncated', 'Marker', 'MaxKeys', 'Prefix'].include?(key)
|
||||
|
@ -39,12 +35,9 @@ module Fog
|
|||
end
|
||||
end
|
||||
bucket.objects.merge_attributes(options)
|
||||
bucket.objects.instance_variable_set(:@loaded, true)
|
||||
data['Contents'].each do |object|
|
||||
bucket.objects << Fog::AWS::S3::Object.new({
|
||||
:bucket => bucket,
|
||||
:connection => connection,
|
||||
:collection => bucket.objects
|
||||
}.merge!(object))
|
||||
bucket.objects << bucket.objects.new(object)
|
||||
end
|
||||
bucket
|
||||
rescue Excon::Errors::NotFound
|
||||
|
|
|
@ -13,11 +13,20 @@ module Fog
|
|||
model Fog::AWS::S3::Object
|
||||
|
||||
def all(options = {})
|
||||
merge_attributes(options)
|
||||
if @loaded
|
||||
clear
|
||||
end
|
||||
@loaded = true
|
||||
collection = bucket.collection.get(
|
||||
bucket.name,
|
||||
options
|
||||
)
|
||||
collection && collection.objects
|
||||
if collection
|
||||
self.replace(collection.objects)
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
def bucket
|
||||
|
@ -41,12 +50,7 @@ module Fog
|
|||
object_data[key] = value
|
||||
end
|
||||
end
|
||||
object = Fog::AWS::S3::Object.new({
|
||||
:bucket => bucket,
|
||||
:collection => self,
|
||||
:connection => connection
|
||||
}.merge!(object_data))
|
||||
object
|
||||
new(object_data)
|
||||
rescue Excon::Errors::NotFound
|
||||
nil
|
||||
end
|
||||
|
@ -65,12 +69,7 @@ module Fog
|
|||
object_data[key] = value
|
||||
end
|
||||
end
|
||||
object = Fog::AWS::S3::Object.new({
|
||||
:bucket => bucket,
|
||||
:collection => self,
|
||||
:connection => connection
|
||||
}.merge!(object_data))
|
||||
object
|
||||
new(object_data)
|
||||
rescue Excon::Errors::NotFound
|
||||
nil
|
||||
end
|
||||
|
|
|
@ -39,7 +39,7 @@ unless Fog.mocking?
|
|||
end
|
||||
query = ''
|
||||
for key, value in options
|
||||
query << "#{key}=#{CGI.escape(value).gsub(/\+/, '%20')};"
|
||||
query << "#{key}=#{CGI.escape(value.to_s).gsub(/\+/, '%20')};"
|
||||
end
|
||||
query.chop!
|
||||
request({
|
||||
|
|
|
@ -1,6 +1,15 @@
|
|||
module Fog
|
||||
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)
|
||||
new(Marshal.load(marshalled))
|
||||
end
|
||||
|
@ -56,6 +65,7 @@ module Fog
|
|||
|
||||
def initialize(attributes = {})
|
||||
merge_attributes(attributes)
|
||||
@loaded = false
|
||||
end
|
||||
|
||||
def inspect
|
||||
|
@ -101,6 +111,12 @@ module Fog
|
|||
|
||||
private
|
||||
|
||||
def lazy_load
|
||||
unless @loaded
|
||||
self.all
|
||||
end
|
||||
end
|
||||
|
||||
def remap_attributes(attributes, mapping)
|
||||
for key, value in mapping
|
||||
if attributes.key?(key)
|
||||
|
|
|
@ -11,17 +11,15 @@ module Fog
|
|||
model Fog::Rackspace::Servers::Flavor
|
||||
|
||||
def all
|
||||
data = connection.list_flavors_detail.body
|
||||
flavors = Fog::Rackspace::Servers::Flavors.new({
|
||||
:connection => connection
|
||||
})
|
||||
for flavor in data['flavors']
|
||||
flavors << Fog::Rackspace::Servers::Flavor.new({
|
||||
:collection => flavors,
|
||||
:connection => connection
|
||||
}.merge!(flavor))
|
||||
if @loaded
|
||||
clear
|
||||
end
|
||||
flavors
|
||||
@loaded = true
|
||||
data = connection.list_flavors_detail.body
|
||||
for flavor in data['flavors']
|
||||
self << new(flavor)
|
||||
end
|
||||
self
|
||||
end
|
||||
|
||||
def get(flavor_id)
|
||||
|
|
|
@ -2,12 +2,6 @@ module Fog
|
|||
module Rackspace
|
||||
class Servers
|
||||
|
||||
def addresses(attributes = {})
|
||||
Fog::AWS::EC2::Addresses.new({
|
||||
:connection => self
|
||||
}.merge!(attributes))
|
||||
end
|
||||
|
||||
def images(attributes = {})
|
||||
Fog::Rackspace::Servers::Images.new({
|
||||
:connection => self
|
||||
|
@ -21,20 +15,19 @@ module Fog
|
|||
attribute :server
|
||||
|
||||
def all
|
||||
if @loaded
|
||||
clear
|
||||
end
|
||||
@loaded = true
|
||||
data = connection.list_images_detail.body
|
||||
servers = Fog::Rackspace::Servers::Images.new({
|
||||
:connection => connection
|
||||
})
|
||||
images = []
|
||||
for image in data['images']
|
||||
servers << Fog::Rackspace::Servers::Image.new({
|
||||
:collection => images,
|
||||
:connection => connection
|
||||
}.merge!(image))
|
||||
images << new(image)
|
||||
end
|
||||
if server
|
||||
images = images.select {|image| image.server_id == server.id}
|
||||
end
|
||||
images
|
||||
self.replace(images)
|
||||
end
|
||||
|
||||
def get(image_id)
|
||||
|
|
|
@ -11,17 +11,15 @@ module Fog
|
|||
model Fog::Rackspace::Servers::Server
|
||||
|
||||
def all
|
||||
data = connection.list_servers_detail.body
|
||||
servers = Fog::Rackspace::Servers::Servers.new({
|
||||
:connection => connection
|
||||
})
|
||||
for server in data['servers']
|
||||
servers << Fog::Rackspace::Servers::Server.new({
|
||||
:collection => servers,
|
||||
:connection => connection
|
||||
}.merge!(server))
|
||||
if @loaded
|
||||
clear
|
||||
end
|
||||
servers
|
||||
@loaded = true
|
||||
data = connection.list_servers_detail.body
|
||||
for server in data['servers']
|
||||
self << new(server)
|
||||
end
|
||||
self
|
||||
end
|
||||
|
||||
def get(server_id)
|
||||
|
|
Загрузка…
Ссылка в новой задаче