Skip to content

Commit

Permalink
[rackspace|compute] Add attachments model and collection.
Browse files Browse the repository at this point in the history
Provide a method for attaching, listing, and detaching block
storage volumes on Rackspace next-generation cloud servers.
  • Loading branch information
Julio Feijo authored and Brad Gignac committed Sep 13, 2012
1 parent 4a5fc11 commit 8920e7b
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/fog/rackspace/compute_v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class BadRequest < Fog::Rackspace::Errors::BadRequest; end
collection :flavors
model :image
collection :images
model :attachments
collection :attachments

request_path 'fog/rackspace/requests/compute_v2'
request :list_servers
Expand All @@ -44,6 +46,11 @@ class BadRequest < Fog::Rackspace::Errors::BadRequest; end
request :list_flavors
request :get_flavor

request :attach_volume
request :get_attachment
request :list_attachments
request :delete_attachment

class Mock
def request(params)
Fog::Mock.not_implemented
Expand Down
34 changes: 34 additions & 0 deletions lib/fog/rackspace/models/compute_v2/attachment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'fog/core/model'

module Fog
module Compute
class RackspaceV2
class Attachment < Fog::Model
identity :id

attribute :server_id, :aliases => 'serverId'
attribute :volume_id, :aliases => 'volumeId'
attribute :device

def save
requires :server, :identity, :device
data = connection.attach_volume(server.identity, identity, device)
merge_attributes(data.body['volumeAttachment'])
true
end

def destroy
requires :server, :identity
connection.delete_attachment(server.identity, identity)
true
end

private

def server
collection.server
end
end
end
end
end
25 changes: 25 additions & 0 deletions lib/fog/rackspace/models/compute_v2/attachments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'fog/core/collection'
require 'fog/rackspace/models/compute_v2/attachment'

module Fog
module Compute
class RackspaceV2
class Attachments < Fog::Collection

model Fog::Compute::RackspaceV2::Attachment

attr_accessor :server

def all
data = connection.list_attachments(server.id).body['volumeAttachments']
load(data)
end

def get(volume_id)
data = connection.get_attachment(server.id, volume_id).body['volumeAttachment']
data && new(data)
end
end
end
end
end
23 changes: 23 additions & 0 deletions lib/fog/rackspace/requests/compute_v2/attach_volume.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Fog
module Compute
class RackspaceV2
class Real
def attach_volume(server_id, volume_id, device)
data = {
'volumeAttachment' => {
'volumeId' => volume_id,
'device' => device
}
}

request(
:body => Fog::JSON.encode(data),
:expects => [200],
:method => 'POST',
:path => "servers/#{server_id}/os-volume_attachments"
)
end
end
end
end
end
15 changes: 15 additions & 0 deletions lib/fog/rackspace/requests/compute_v2/delete_attachment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Fog
module Compute
class RackspaceV2
class Real
def delete_attachment(server_id, volume_id)
request(
:expects => [202],
:method => 'DELETE',
:path => "servers/#{server_id}/os-volume_attachments/#{volume_id}"
)
end
end
end
end
end
15 changes: 15 additions & 0 deletions lib/fog/rackspace/requests/compute_v2/get_attachment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Fog
module Compute
class RackspaceV2
class Real
def get_attachment(server_id, volume_id)
request(
:expects => [200, 203, 300],
:method => 'GET',
:path => "servers/#{server_id}/os-volume_attachments/#{volume_id}"
)
end
end
end
end
end
15 changes: 15 additions & 0 deletions lib/fog/rackspace/requests/compute_v2/list_attachments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Fog
module Compute
class RackspaceV2
class Real
def list_attachments(server_id)
request(
:expects => [200, 203, 300],
:method => 'GET',
:path => "servers/#{server_id}/os-volume_attachments"
)
end
end
end
end
end

0 comments on commit 8920e7b

Please sign in to comment.