mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
[hp|compute_v2] Refactor server model to add volume attachments.
This commit is contained in:
parent
6a97d92cc1
commit
a0d6b018a5
6 changed files with 145 additions and 28 deletions
|
@ -28,6 +28,8 @@ module Fog
|
|||
collection :metadata
|
||||
model :server
|
||||
collection :servers
|
||||
model :volume_attachment
|
||||
collection :volume_attachments
|
||||
|
||||
request_path 'fog/hp/requests/compute_v2'
|
||||
request :allocate_address
|
||||
|
@ -130,12 +132,14 @@ module Fog
|
|||
:images => {},
|
||||
:key_pairs => {},
|
||||
:servers => {},
|
||||
:addresses => {}
|
||||
:addresses => {},
|
||||
:volume_attachments => {}
|
||||
},
|
||||
:images => {},
|
||||
:key_pairs => {},
|
||||
:servers => {},
|
||||
:addresses => {}
|
||||
:addresses => {},
|
||||
:volume_attachments => {}
|
||||
}
|
||||
end
|
||||
end
|
||||
|
|
|
@ -7,12 +7,10 @@ module Fog
|
|||
|
||||
class Server < Fog::Compute::Server
|
||||
|
||||
identity :id
|
||||
identity :id
|
||||
|
||||
attribute :addresses
|
||||
#attribute :flavor
|
||||
attribute :host_id, :aliases => 'hostId'
|
||||
#attribute :image
|
||||
attribute :metadata
|
||||
attribute :name
|
||||
attribute :personality
|
||||
|
@ -221,30 +219,12 @@ module Fog
|
|||
end
|
||||
end
|
||||
|
||||
def attach_volume(volume_id, device)
|
||||
requires :id
|
||||
if vols = service.attach_volume(id, volume_id, device).body
|
||||
vols['volumeAttachment']
|
||||
end
|
||||
end
|
||||
|
||||
def detach_volume(volume_id)
|
||||
requires :id
|
||||
service.detach_volume(id, volume_id)
|
||||
true
|
||||
end
|
||||
|
||||
def volume_attachment_details(volume_id)
|
||||
requires :id
|
||||
if vols = service.get_server_volume_details(id, volume_id).body
|
||||
vols['volumeAttachment']
|
||||
end
|
||||
end
|
||||
|
||||
def volume_attachments
|
||||
requires :id
|
||||
if vols = service.list_server_volumes(id).body
|
||||
vols['volumeAttachments']
|
||||
@volume_attachments ||= begin
|
||||
Fog::Compute::HPV2::VolumeAttachments.new({
|
||||
:service => service,
|
||||
:server => self
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
|
|
42
lib/fog/hp/models/compute_v2/volume_attachment.rb
Normal file
42
lib/fog/hp/models/compute_v2/volume_attachment.rb
Normal file
|
@ -0,0 +1,42 @@
|
|||
require 'fog/core/model'
|
||||
|
||||
module Fog
|
||||
module Compute
|
||||
class HPV2
|
||||
class VolumeAttachment < Fog::Model
|
||||
|
||||
identity :id
|
||||
|
||||
attribute :server_id, :aliases => 'serverId'
|
||||
attribute :volume_id, :aliases => 'volumeId'
|
||||
attribute :device
|
||||
|
||||
def initialize(new_attributes = {})
|
||||
super(new_attributes)
|
||||
# server_id needs to be the same as the server from the attachments collection
|
||||
server_id = server.id if server
|
||||
self
|
||||
end
|
||||
|
||||
def save
|
||||
requires :server_id, :volume_id, :device
|
||||
data = service.attach_volume(server_id, volume_id, device)
|
||||
merge_attributes(data.body['volumeAttachment'])
|
||||
true
|
||||
end
|
||||
|
||||
def destroy
|
||||
requires :server_id, :volume_id
|
||||
service.detach_volume(server_id, volume_id)
|
||||
true
|
||||
end
|
||||
alias :detach :destroy
|
||||
|
||||
private
|
||||
def server
|
||||
collection.server
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
30
lib/fog/hp/models/compute_v2/volume_attachments.rb
Normal file
30
lib/fog/hp/models/compute_v2/volume_attachments.rb
Normal file
|
@ -0,0 +1,30 @@
|
|||
require 'fog/core/collection'
|
||||
require 'fog/hp/models/compute_v2/volume_attachment'
|
||||
|
||||
module Fog
|
||||
module Compute
|
||||
class HPV2
|
||||
class VolumeAttachments < Fog::Collection
|
||||
|
||||
model Fog::Compute::HPV2::VolumeAttachment
|
||||
|
||||
attr_accessor :server
|
||||
|
||||
def all
|
||||
requires :server
|
||||
data = service.list_server_volumes(server.id).body['volumeAttachments']
|
||||
load(data)
|
||||
end
|
||||
|
||||
def get(volume_id)
|
||||
requires :server
|
||||
if data = service.get_server_volume_details(server.id, volume_id).body['volumeAttachment']
|
||||
new(data)
|
||||
end
|
||||
rescue Fog::Compute::HPV2::NotFound
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
44
tests/hp/models/compute_v2/volume_attachment_tests.rb
Normal file
44
tests/hp/models/compute_v2/volume_attachment_tests.rb
Normal file
|
@ -0,0 +1,44 @@
|
|||
#Shindo.tests("Fog::Compute::HPV2 | volume_attachment model", ['hp', 'v2', 'compute']) do
|
||||
#
|
||||
# service = Fog::Compute.new(:provider => 'HP', :version => :v2)
|
||||
#
|
||||
# @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.wait_for { ready? }
|
||||
# @volume = HP[:block_storage_v2].volumes.create(:display_name => 'fogvolumetest', :size => 1)
|
||||
# @volume.wait_for { ready? }
|
||||
#
|
||||
# tests('success') do
|
||||
#
|
||||
# tests('#create').succeeds do
|
||||
# volume_attachment = @server.volume_attachments.create(:server_id => @server.id, :volume_id => @volume.id, :device => '/dev/sdf')
|
||||
# test('volume attached to server') do
|
||||
# volume_attachment.server_id == @server.id
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# tests('#all').succeeds do
|
||||
# volume_attachment = @server.volume_attachments.all
|
||||
# test('list server in volume attachment') do
|
||||
# volume_attachment.server_id == @server.id
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# tests('#get').succeeds do
|
||||
# volume_attachment = @server.volume_attachments.get(@volume.id)
|
||||
# test('get server in volume attachment') do
|
||||
# volume_attachment.id == @volume.id
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# tests('#detach').succeeds do
|
||||
# volume = @server.volume_attachments.get(@volume.id)
|
||||
# volume.detach
|
||||
# end
|
||||
#
|
||||
# end
|
||||
#
|
||||
# @volume.destroy
|
||||
# @server.destroy
|
||||
#end
|
17
tests/hp/models/compute_v2/volume_attachments_tests.rb
Normal file
17
tests/hp/models/compute_v2/volume_attachments_tests.rb
Normal file
|
@ -0,0 +1,17 @@
|
|||
#Shindo.tests("Fog::Compute::HPV2 | volume attachments collection", ['hp', 'v2', 'compute']) do
|
||||
#
|
||||
# service = Fog::Compute.new(:provider => 'HP', :version => :v2)
|
||||
#
|
||||
# @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.wait_for { ready? }
|
||||
# @volume = HP[:block_storage_v2].volumes.create(:display_name => 'fogvolumetest', :size => 1)
|
||||
# @volume.wait_for { ready? }
|
||||
#
|
||||
# collection_tests(service.volume_attachments, {:server_id => @server.id, :volume_id => @volume.id, :device => '/dev/sdf'}, true)
|
||||
#
|
||||
# @volume.destroy
|
||||
# @server.destroy
|
||||
#
|
||||
#end
|
Loading…
Add table
Reference in a new issue