Skip to content

Commit

Permalink
[ec2] better behaved volume models in relation to servers and attach/…
Browse files Browse the repository at this point in the history
…detach
  • Loading branch information
geemus committed Apr 27, 2010
1 parent d43ccd2 commit 6d4127a
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/fog/aws/models/ec2/volume.rb
Expand Up @@ -20,6 +20,8 @@ class Volume < Fog::Model
attribute :state, 'status'

def initialize(attributes = {})
# assign server first to prevent race condition with new_record?
self.server = attributes.delete(:server)
if attributes['attachmentSet']
attributes.merge!(attributes.delete('attachmentSet').first || {})
end
Expand All @@ -33,6 +35,10 @@ def destroy
true
end

def ready?
state == 'available'
end

def server=(new_server)
if new_server
attach(new_server)
Expand Down Expand Up @@ -70,6 +76,7 @@ def attach(new_server)
@server = nil
@server_id = new_server.id
connection.attach_volume(@server_id, @id, @device)
reload
end
end

Expand All @@ -78,6 +85,7 @@ def detach
@server_id = nil
unless new_record?
connection.detach_volume(@id)
reload
end
end

Expand Down

0 comments on commit 6d4127a

Please sign in to comment.