From d4feba66df202b15f98b0025cc13b6d227b4ceef Mon Sep 17 00:00:00 2001 From: Rupak Ganguly Date: Wed, 14 Nov 2012 15:09:48 -0500 Subject: [PATCH] Update volume model and collection to handle bootable volumes as well. --- lib/fog/hp/models/block_storage/volume.rb | 16 +++++++++++++++- lib/fog/hp/models/block_storage/volumes.rb | 15 +++++++++++---- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/lib/fog/hp/models/block_storage/volume.rb b/lib/fog/hp/models/block_storage/volume.rb index 43c7b354c..07b7d2c26 100644 --- a/lib/fog/hp/models/block_storage/volume.rb +++ b/lib/fog/hp/models/block_storage/volume.rb @@ -16,12 +16,20 @@ module Fog attribute :created_at, :aliases => 'createdAt' attribute :availability_zone, :aliases => 'availabilityZone' attribute :snapshot_id, :aliases => 'snapshotId' + attribute :source_image_id, :aliases => 'sourceImageRef' # only for bootable volumes attribute :attachments attribute :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) + @connection = attributes[:connection] + super + end + def device attachments[0]['device'] if has_attachments? end @@ -30,6 +38,11 @@ module Fog attachments[0]['serverId'] if has_attachments? end + # used for creating bootable volumes + def image_id=(new_image_id) + @image_id = new_image_id + end + # a volume can be attached to only one server at a time def has_attachments? !(attachments.nil? || attachments.empty? || attachments[0].empty?) @@ -74,7 +87,8 @@ module Fog requires :name, :size options = { 'metadata' => metadata, - 'snapshot_id' => snapshot_id + 'snapshot_id' => snapshot_id, + 'imageRef' => @image_id } options = options.reject {|key, value| value.nil?} data = connection.create_volume(name, description, size, options) diff --git a/lib/fog/hp/models/block_storage/volumes.rb b/lib/fog/hp/models/block_storage/volumes.rb index 0388de232..d8b854811 100644 --- a/lib/fog/hp/models/block_storage/volumes.rb +++ b/lib/fog/hp/models/block_storage/volumes.rb @@ -9,15 +9,22 @@ module Fog model Fog::BlockStorage::HP::Volume - def all - data = connection.list_volumes.body['volumes'] + def all(options={}) + if @bootable = options[:only_bootable] + data = connection.list_bootable_volumes.body['volumes'] + else + data = connection.list_volumes.body['volumes'] + end load(data) end def get(volume_id) - if volume = connection.get_volume_details(volume_id).body['volume'] - new(volume) + if @bootable + volume = connection.get_bootable_volume_details(volume_id).body['volume'] + else + volume = connection.get_volume_details(volume_id).body['volume'] end + new(volume) rescue Fog::BlockStorage::HP::NotFound nil end