1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

[hp|block_storage_v2] Add volume model, fix some mocks, and add volume tests.

This commit is contained in:
Rupak Ganguly 2013-05-22 10:50:39 -04:00
parent d2c3cbeff7
commit c216917285
9 changed files with 345 additions and 159 deletions

View file

@ -11,9 +11,9 @@ module Fog
secrets :hp_secret_key secrets :hp_secret_key
#model_path 'fog/hp/models/block_storage_v2' model_path 'fog/hp/models/block_storage_v2'
#model :volume model :volume
#collection :volumes collection :volumes
#collection :bootable_volumes #collection :bootable_volumes
#model :snapshot #model :snapshot
#collection :snapshots #collection :snapshots

View file

@ -0,0 +1,132 @@
require 'fog/core/model'
module Fog
module HP
class BlockStorageV2
class Volume < Fog::Model
identity :id
attribute :name, :aliases => 'display_name'
attribute :description, :aliases => 'display_description'
attribute :size
attribute :status
attribute :type, :aliases => 'volume_type'
attribute :created_at
attribute :availability_zone
attribute :snapshot_id
attribute :source_volid
attribute :attachments
attribute :metadata
attribute :bootable
attribute :image_metadata, :aliases => 'volume_image_metadata'
#attr_reader :server_id
#attr_reader :device
def initialize(attributes = {})
# assign these attributes first to prevent race condition with new_record?
self.image_id = attributes.delete(:image_id)
super
end
def device
attachments[0]['device'] if has_attachments?
end
def server_id
attachments[0]['serverId'] if has_attachments?
end
# used for creating bootable volumes
def image_id=(new_image_id)
@image_id = new_image_id
end
def image_id
@image_id = image_metadata['image_id'] if image_metadata
end
# a volume can be attached to only one server at a time
def has_attachments?
!(attachments.nil? || attachments.empty? || attachments[0].empty?)
end
def in_use?
self.status == 'in-use'
end
alias :attached? :in_use?
def ready?
self.status == 'available'
end
# volume can be attached to only one server at a time
def attach(new_server_id, device)
requires :id
unless in_use?
data = service.compute.attach_volume(new_server_id, id, device)
merge_attributes(:attachments => attachments << data.body['volumeAttachment'])
true
else
false
end
end
def detach
requires :id
if has_attachments?
service.compute.detach_volume(self.server_id, id)
end
true
end
def destroy
requires :id
service.delete_volume(id)
true
end
def save
identity ? update : create
end
private
def create
options = {
'display_name' => name,
'display_description' => description,
'size' => size,
'metadata' => metadata,
'snapshot_id' => snapshot_id,
'imageRef' => @image_id,
'availability_zone' => availability_zone,
'source_volid' => source_volid,
'volume_type' => type # this parameter is currently ignored
}
options = options.reject {|_, value| value.nil?}
data = service.create_volume(options)
merge_attributes(data.body['volume'])
true
end
def update
requires :id
options = {
'display_name' => name,
'display_description' => description,
'metadata' => metadata
}
options = options.reject {|_, value| value.nil?}
data = service.update_volume(id, options)
merge_attributes(data.body['volume'])
true
end
end
end
end
end

View file

@ -0,0 +1,36 @@
require 'fog/core/collection'
require 'fog/hp/models/block_storage_v2/volume'
module Fog
module HP
class BlockStorageV2
class Volumes < Fog::Collection
attribute :filters
model Fog::HP::BlockStorageV2::Volume
def initialize(attributes)
self.filters ||= {}
super
end
def all(filters = filters)
self.filters = filters
data = service.list_volumes(filters).body['volumes']
load(data)
end
def get(volume_id)
volume = service.get_volume_details(volume_id).body['volume']
new(volume)
rescue Fog::HP::BlockStorageV2::NotFound
nil
end
end
end
end
end

View file

@ -45,20 +45,22 @@ module Fog
response.body = '{"badRequest": {"message": "Volume status must be available", "code": 400}}' response.body = '{"badRequest": {"message": "Volume status must be available", "code": 400}}'
raise(Excon::Errors.status_error({:expects => 200}, response)) raise(Excon::Errors.status_error({:expects => 200}, response))
else else
resp_data = { "volumeAttachment" => resp_data = { 'volumeAttachment' =>
{ {
"volumeId" => volume_id, 'device' => device,
"id" => volume_id 'serverId' => server_id,
'id' => volume_id,
'volumeId' => volume_id,
} }
} }
response.body = resp_data response.body = resp_data
response.status = 200 response.status = 200
data = { data = {
"device" => device, 'device' => device,
"serverId" => server_id, 'serverId' => server_id,
"id" => volume_id, 'id' => volume_id,
"volumeId" => volume_id, 'volumeId' => volume_id,
} }
if server['volumeAttachments'] if server['volumeAttachments']
server['volumeAttachments'] << data server['volumeAttachments'] << data

View file

@ -30,7 +30,7 @@ module Fog
if server = self.data[:servers][server_id] if server = self.data[:servers][server_id]
if server['volumeAttachments'] && server['volumeAttachments'].select {|v| v['volumeId'] == volume_id} if server['volumeAttachments'] && server['volumeAttachments'].select {|v| v['volumeId'] == volume_id}
data = server['volumeAttachments'].select {|v| v['volumeId'] == volume_id} data = server['volumeAttachments'].select {|v| v['volumeId'] == volume_id}
response.body = { 'volumeAttachment' => data } response.body = { 'volumeAttachment' => data[0] }
response.status = 200 response.status = 200
else else
raise Fog::Compute::HPV2::NotFound raise Fog::Compute::HPV2::NotFound

View file

@ -0,0 +1,22 @@
Shindo.tests("HP::BlockStorage | volume model", ['hp', 'v2', 'block_storage', 'volumes']) do
model_tests(HP[:block_storage_v2].volumes, {:name => 'fogvol2tests', :description => 'fogvol2tests-desc', :size => 1}, true) do
test("get(#{@instance.id})") do
HP[:block_storage_v2].volumes.get(@instance.id) != nil?
end
test("update(#{@instance.id})") do
@instance.name = 'fogvol2tests Updated'
@instance.save
@instance.reload
@instance.name == 'fogvol2tests Updated'
end
test("has_attachments?") do
@instance.has_attachments? == false
end
end
end

View file

@ -1,44 +1,40 @@
#Shindo.tests("Fog::Compute::HPV2 | volume_attachment model", ['hp', 'v2', 'compute']) do Shindo.tests("Fog::Compute::HPV2 | volume_attachment model", ['hp', 'v2', 'compute']) do
#
# service = Fog::Compute.new(:provider => 'HP', :version => :v2) service = Fog::Compute.new(:provider => 'HP', :version => :v2)
#
# @base_image_id = ENV['BASE_IMAGE_ID'] || '7f60b54c-cd15-433f-8bed-00acbcd25a17' @base_image_id = ENV['BASE_IMAGE_ID'] || '7f60b54c-cd15-433f-8bed-00acbcd25a17'
#
# @server = service.servers.create(:name => 'fogserattachtests', :flavor_id => 100, :image_id => @base_image_id) @server = service.servers.create(:name => 'fogserattachtests', :flavor_id => 100, :image_id => @base_image_id)
# @server.wait_for { ready? } @server.wait_for { ready? }
# @volume = HP[:block_storage_v2].volumes.create(:display_name => 'fogvolumetest', :size => 1) @volume = HP[:block_storage_v2].volumes.create(:name => 'fogvolumetest', :size => 1)
# @volume.wait_for { ready? } @volume.wait_for { ready? }
#
# tests('success') do tests('success') do
#
# tests('#create').succeeds do tests('#create').succeeds do
# volume_attachment = @server.volume_attachments.create(:server_id => @server.id, :volume_id => @volume.id, :device => '/dev/sdf') volume_attachment = @server.volume_attachments.create(:server_id => @server.id, :volume_id => @volume.id, :device => '/dev/sdf')
# test('volume attached to server') do test('volume attached to server') do
# volume_attachment.server_id == @server.id volume_attachment.server_id == @server.id
# end end
# end end
#
# tests('#all').succeeds do tests('#get').succeeds do
# volume_attachment = @server.volume_attachments.all volume_attachment = @server.volume_attachments.get(@volume.id)
# test('list server in volume attachment') do test('get server in volume attachment') do
# volume_attachment.server_id == @server.id volume_attachment.server_id == @server.id
# end end
# end test('get volume in volume attachment') do
# volume_attachment.id == @volume.id
# tests('#get').succeeds do end
# volume_attachment = @server.volume_attachments.get(@volume.id) end
# test('get server in volume attachment') do
# volume_attachment.id == @volume.id tests('#detach').succeeds do
# end volume_attachment = @server.volume_attachments.get(@volume.id)
# end volume_attachment.detach
# end
# tests('#detach').succeeds do
# volume = @server.volume_attachments.get(@volume.id) end
# volume.detach
# end @volume.destroy
# @server.destroy
# end end
#
# @volume.destroy
# @server.destroy
#end

View file

@ -1,17 +1,17 @@
#Shindo.tests("Fog::Compute::HPV2 | volume attachments collection", ['hp', 'v2', 'compute']) do Shindo.tests("Fog::Compute::HPV2 | volume attachments collection", ['hp', 'v2', 'compute']) do
#
# service = Fog::Compute.new(:provider => 'HP', :version => :v2) service = Fog::Compute.new(:provider => 'HP', :version => :v2)
#
# @base_image_id = ENV['BASE_IMAGE_ID'] || '7f60b54c-cd15-433f-8bed-00acbcd25a17' @base_image_id = ENV['BASE_IMAGE_ID'] || '7f60b54c-cd15-433f-8bed-00acbcd25a17'
#
# @server = service.servers.create(:name => 'fogserverattachtest', :flavor_id => 100, :image_id => @base_image_id) @server = service.servers.create(:name => 'fogserverattachtest', :flavor_id => 100, :image_id => @base_image_id)
# @server.wait_for { ready? } @server.wait_for { ready? }
# @volume = HP[:block_storage_v2].volumes.create(:display_name => 'fogvolumetest', :size => 1) @volume = HP[:block_storage_v2].volumes.create(:name => 'fogvolumetest', :size => 1)
# @volume.wait_for { ready? } @volume.wait_for { ready? }
#
# collection_tests(service.volume_attachments, {:server_id => @server.id, :volume_id => @volume.id, :device => '/dev/sdf'}, true) collection_tests(@server.volume_attachments, {:server_id => @server.id, :volume_id => @volume.id, :device => '/dev/sdf'}, true)
#
# @volume.destroy @volume.destroy
# @server.destroy @server.destroy
#
#end end

View file

@ -1,87 +1,85 @@
#Shindo.tests("Fog::Compute::HPV2 | volume requests", ['hp', 'v2', 'compute', 'volumes']) do Shindo.tests("Fog::Compute::HPV2 | volume requests", ['hp', 'v2', 'compute', 'volumes']) do
#
# service = Fog::Compute.new(:provider => 'HP', :version => :v2) service = Fog::Compute.new(:provider => 'HP', :version => :v2)
#
# @list_volume_attachments_format = { @list_volume_attachments_format = {
# 'volumeAttachments' => [{ 'volumeAttachments' => [{
# 'device' => String, 'device' => String,
# 'serverId' => String, 'serverId' => String,
# 'id' => String, 'id' => String,
# 'volumeId' => String 'volumeId' => String
# }] }]
# } }
#
# @volume_attachment_format = { @volume_attachment_format = {
# 'volumeAttachment' => { 'volumeAttachment' => {
# "volumeId" => String, 'device' => String,
# "id" => String 'serverId' => String,
# } 'id' => String,
# } 'volumeId' => String
# }
# @base_image_id = ENV['BASE_IMAGE_ID'] || '7f60b54c-cd15-433f-8bed-00acbcd25a17' }
#
# @server = service.servers.create(:name => 'fogservoltests', :flavor_id => 100, :image_id => @base_image_id) @base_image_id = ENV['BASE_IMAGE_ID'] || '7f60b54c-cd15-433f-8bed-00acbcd25a17'
# @server.wait_for { ready? }
# @server = service.servers.create(:name => 'fogservoltests', :flavor_id => 100, :image_id => @base_image_id)
# tests('success') do @server.wait_for { ready? }
# response = HP[:block_storage].create_volume('fogvoltest', 'fogvoltest desc', 1)
# @volume_id = response.body['volume']['id'] tests('success') do
# @device = "\/dev\/sdf" response = HP[:block_storage_v2].create_volume('display_name' => 'fogvoltest', 'display_description' => 'fogvoltest desc', 'size' => 1)
# @volume_id = response.body['volume']['id']
# HP[:block_storage].volumes.get(@volume_id).wait_for { ready? } @device = "\/dev\/sdf"
# tests("#attach_volume(#{@server.id}, #{@volume_id}, #{@device}").formats(@volume_attachment_format) do
# service.attach_volume(@server.id, @volume_id, @device).body tests("#attach_volume(#{@server.id}, #{@volume_id}, #{@device}").formats(@volume_attachment_format) do
# end service.attach_volume(@server.id, @volume_id, @device).body
# end
# HP[:block_storage].volumes.get(@volume_id).wait_for { ready? }
# tests("#list_server_volumes(#{@server.id})").formats(@list_volume_attachments_format) do tests("#list_server_volumes(#{@server.id})").formats(@list_volume_attachments_format) do
# service.list_server_volumes(@server.id).body service.list_server_volumes(@server.id).body
# end end
#
# HP[:block_storage].volumes.get(@volume_id).wait_for { ready? } tests("#get_server_volume_details(#{@server.id}, #{@volume_id})").formats(@volume_attachment_format) do
# tests("#get_server_volume_details(#{@server.id}, #{@volume_id})").formats(@list_volume_attachments_format) do service.get_server_volume_details(@server.id, @volume_id).body
# service.get_server_volume_details(@server.id, @volume_id).body end
# end
# tests("#detach_volume(#{@server.id}, #{@volume_id}").succeeds do
# HP[:block_storage].volumes.get(@volume_id).wait_for { in_use? } unless Fog.mocking? service.detach_volume(@server.id, @volume_id)
# tests("#detach_volume(#{@server.id}, #{@volume_id}").succeeds do end
# service.detach_volume(@server.id, @volume_id)
# end end
#
# end
# tests('failure') do
#
# tests('failure') do tests('#list_server_volumes(0)').raises(Fog::Compute::HPV2::NotFound) do
# service.list_server_volumes(0)
# tests('#list_server_volumes(0)').raises(Fog::Compute::HPV2::NotFound) do end
# service.list_server_volumes(0)
# end tests('#get_server_volume_details(0, 0)').raises(Fog::Compute::HPV2::NotFound) do
# service.get_server_volume_details(0, 0)
# tests('#get_server_volume_details(0, 0)').raises(Fog::Compute::HPV2::NotFound) do end
# service.get_server_volume_details(0, 0)
# end tests("#attach_volume(#{@server.id}, 0, #{@device})").raises(Fog::Compute::HPV2::NotFound) do
# pending if Fog.mocking?
# tests("#attach_volume(#{@server.id}, 0, #{@device})").raises(Fog::Compute::HPV2::NotFound) do service.attach_volume(@server.id, 0, @device)
# pending if Fog.mocking? end
# service.attach_volume(@server.id, 0, @device)
# end tests("#attach_volume(0, #{@volume_id}, #{@device})").raises(Fog::Compute::HPV2::NotFound) do
# service.attach_volume(0, @volume_id, @device)
# tests("#attach_volume(0, #{@volume_id}, #{@device})").raises(Fog::Compute::HPV2::NotFound) do end
# service.attach_volume(0, @volume_id, @device)
# end tests("#detach_volume(#{@server.id}, 0)").raises(Fog::Compute::HPV2::NotFound) do
# pending if Fog.mocking?
# tests("#detach_volume(#{@server.id}, 0)").raises(Fog::Compute::HPV2::NotFound) do service.detach_volume(@server.id, 0)
# pending if Fog.mocking? end
# service.detach_volume(@server.id, 0)
# end tests("#detach_volume(0, #{@volume_id})").raises(Fog::Compute::HPV2::NotFound) do
# service.detach_volume(0, @volume_id)
# tests("#detach_volume(0, #{@volume_id})").raises(Fog::Compute::HP::NotFound) do end
# service.detach_volume(0, @volume_id)
# end end
#
# end HP[:block_storage_v2].delete_volume(@volume_id)
# service.delete_server(@server.id)
# HP[:block_storage].delete_volume(@volume_id)
# service.delete_server(@server.id) end
#
#end