зеркало из https://github.com/microsoft/fog.git
nicer handling of instance to volume/address associations
This commit is contained in:
Родитель
06a0998a3d
Коммит
31b1936efe
|
@ -24,8 +24,13 @@ module Fog
|
|||
end
|
||||
|
||||
def instance=(new_instance)
|
||||
@instance_id = new_instance.instance_id
|
||||
connection.associate_address(@instance_id, @public_ip)
|
||||
if !@public_ip
|
||||
@instance = new_instance
|
||||
elsif new_instance
|
||||
@instance = nil
|
||||
@instance_id = new_instance.instance_id
|
||||
connection.associate_address(@instance_id, @public_ip)
|
||||
end
|
||||
end
|
||||
|
||||
def reload
|
||||
|
@ -36,6 +41,9 @@ module Fog
|
|||
def save
|
||||
data = connection.allocate_address
|
||||
@public_ip = data.body['publicIp']
|
||||
if @instance
|
||||
instance = @instance
|
||||
end
|
||||
true
|
||||
end
|
||||
|
||||
|
|
|
@ -2,14 +2,16 @@ module Fog
|
|||
module AWS
|
||||
class EC2
|
||||
|
||||
def addresses
|
||||
Fog::AWS::EC2::Addresses.new(:connection => self)
|
||||
def addresses(attributes = {})
|
||||
Fog::AWS::EC2::Addresses.new({
|
||||
:connection => self
|
||||
}.merge!(attributes))
|
||||
end
|
||||
|
||||
class Addresses < Fog::Collection
|
||||
|
||||
attribute :public_ip
|
||||
attribute :instance_id
|
||||
attribute :instance
|
||||
|
||||
def initialize(attributes)
|
||||
@public_ip ||= []
|
||||
|
@ -28,8 +30,8 @@ module Fog
|
|||
:connection => connection
|
||||
}.merge!(address))
|
||||
end
|
||||
if instance_id
|
||||
addresses = addresses.select {|address| address.instance_id == instance_id}
|
||||
if instance
|
||||
addresses = addresses.select {|address| address.instance_id == instance.id}
|
||||
end
|
||||
addresses
|
||||
end
|
||||
|
@ -49,7 +51,8 @@ module Fog
|
|||
def new
|
||||
Fog::AWS::EC2::Address.new(
|
||||
:addresses => self,
|
||||
:connection => connection
|
||||
:connection => connection,
|
||||
:instance => instance
|
||||
)
|
||||
end
|
||||
|
||||
|
|
|
@ -23,8 +23,8 @@ module Fog
|
|||
attribute :reason
|
||||
attribute :user_data
|
||||
|
||||
def address
|
||||
connection.addresses(:instance_id => instance_id).all.first
|
||||
def addresses
|
||||
connection.addresses(:instance => self)
|
||||
end
|
||||
|
||||
def destroy
|
||||
|
@ -105,7 +105,7 @@ module Fog
|
|||
end
|
||||
|
||||
def volumes
|
||||
connection.volumes(:instance_id => instance_id)
|
||||
connection.volumes(:instance => self)
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
@ -27,8 +27,13 @@ module Fog
|
|||
end
|
||||
|
||||
def instance=(new_instance)
|
||||
@instance_id = new_instance.instance_id
|
||||
connection.attach_volume(@instance_id, @volume_id, @device)
|
||||
if !@volume_id
|
||||
@instance = new_instance
|
||||
elsif new_instance
|
||||
@instance = nil
|
||||
@instance_id = new_instance.instance_id
|
||||
connection.attach_volume(@instance_id, @volume_id, @device)
|
||||
end
|
||||
end
|
||||
|
||||
def reload
|
||||
|
@ -40,6 +45,9 @@ module Fog
|
|||
data = connection.create_volume(@availability_zone, @size, @snapshot_id).body
|
||||
new_attributes = data.reject {|key,value| key == 'requestId'}
|
||||
merge_attributes(new_attributes)
|
||||
if @instance
|
||||
instance = @instance
|
||||
end
|
||||
true
|
||||
end
|
||||
|
||||
|
|
|
@ -11,7 +11,7 @@ module Fog
|
|||
class Volumes < Fog::Collection
|
||||
|
||||
attribute :volume_id
|
||||
attribute :instance_id
|
||||
attribute :instance
|
||||
|
||||
def initialize(attributes)
|
||||
@volume_id ||= []
|
||||
|
@ -30,8 +30,8 @@ module Fog
|
|||
:volumes => self
|
||||
}.merge!(volume))
|
||||
end
|
||||
if instance_id
|
||||
volumes = volumes.select {|volume| volume.instance_id == instance_id}
|
||||
if instance
|
||||
volumes = volumes.select {|volume| volume.instance_id == instance.id}
|
||||
end
|
||||
volumes
|
||||
end
|
||||
|
@ -49,10 +49,11 @@ module Fog
|
|||
end
|
||||
|
||||
def new(attributes = {})
|
||||
Fog::AWS::EC2::Volume.new(
|
||||
volume = Fog::AWS::EC2::Volume.new(
|
||||
attributes.merge!(
|
||||
:connection => connection,
|
||||
:volumes => self
|
||||
:connection => connection,
|
||||
:instance => instance,
|
||||
:volumes => self
|
||||
)
|
||||
)
|
||||
end
|
||||
|
|
|
@ -39,8 +39,13 @@ describe 'Fog::AWS::EC2::Instance' do
|
|||
|
||||
end
|
||||
|
||||
describe "#address" do
|
||||
it "should have tests"
|
||||
describe "#addresses" do
|
||||
|
||||
it "should return a Fog::AWS::EC2::Addresses" do
|
||||
instance = ec2.instances.new
|
||||
instance.addresses.should be_a(Fog::AWS::EC2::Addresses)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
describe "#destroy" do
|
||||
|
|
Загрузка…
Ссылка в новой задаче