You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltacloud.apache.org by lu...@apache.org on 2012/12/05 02:04:55 UTC
[2/2] git commit: CIMI - Adds Volume creation with template and
config by value - DTACLOUD-385
CIMI - Adds Volume creation with template and config by value - DTACLOUD-385
Signed-off-by: marios <ma...@redhat.com>
TrackedAt: http://tracker-mfojtik.rhcloud.com/patch/90e44c15a62ba73035f2a35708a6ea91417e5cbf
Project: http://git-wip-us.apache.org/repos/asf/deltacloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltacloud/commit/d497f6a7
Tree: http://git-wip-us.apache.org/repos/asf/deltacloud/tree/d497f6a7
Diff: http://git-wip-us.apache.org/repos/asf/deltacloud/diff/d497f6a7
Branch: refs/heads/master
Commit: d497f6a794ca7651d78a5b0d6f6e71367c3d7719
Parents: 2af5edb
Author: marios <ma...@redhat.com>
Authored: Tue Dec 4 14:50:18 2012 +0200
Committer: David Lutterkort <lu...@redhat.com>
Committed: Tue Dec 4 16:40:34 2012 -0800
----------------------------------------------------------------------
server/lib/cimi/collections/volumes.rb | 11 ++-----
server/lib/cimi/helpers/cimi_helper.rb | 41 ++++++++++++++++++++++++--
server/lib/cimi/models/volume.rb | 37 ++++++++++++++----------
3 files changed, 62 insertions(+), 27 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/deltacloud/blob/d497f6a7/server/lib/cimi/collections/volumes.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/collections/volumes.rb b/server/lib/cimi/collections/volumes.rb
index 32912c0..1078014 100644
--- a/server/lib/cimi/collections/volumes.rb
+++ b/server/lib/cimi/collections/volumes.rb
@@ -49,15 +49,10 @@ module CIMI::Collections
operation :create, :with_capability => :create_storage_volume do
description "Create a new Volume."
control do
- content_type = (request.content_type.end_with?("json") ? :json : :xml)
- #((request.content_type.end_with?("xml")) ? :xml : report_error(415) ) FIXME
- case content_type
- when :json
- new_volume = Volume.create_from_json(request.body.read, self)
- when :xml
- new_volume = Volume.create_from_xml(request.body.read, self)
- end
+ content_type = grab_content_type(request.content_type, request.body)
+ new_volume = Volume.create(request.body.read, self, content_type)
status 201
+ headers 'Location' => new_volume.id
respond_to do |format|
format.json { new_volume.to_json }
format.xml { new_volume.to_xml }
http://git-wip-us.apache.org/repos/asf/deltacloud/blob/d497f6a7/server/lib/cimi/helpers/cimi_helper.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/helpers/cimi_helper.rb b/server/lib/cimi/helpers/cimi_helper.rb
index 99af08a..3dd9501 100644
--- a/server/lib/cimi/helpers/cimi_helper.rb
+++ b/server/lib/cimi/helpers/cimi_helper.rb
@@ -32,24 +32,57 @@ module CIMI
end
def to_kibibyte(value, unit)
+ #value may be a string. convert to_f
+ value = value.to_f # not to_i because e.g. 0.5 GB
case unit
when "GB"
- (value.to_i*1024*1024).to_i
+ (value*1024*1024).to_i
when "MB"
- (value.to_i*1024).to_i
+ (value*1024).to_i
else
nil # should probably be exploding something here...
end
end
+ #e.g. convert volume to GB for deltacloud driver
def from_kibibyte(value, unit="GB")
case unit
- when "GB" then ((value.to_i)/1024/1024).to_i
- when "MB" then ((value.to_i)/1024).to_i
+ when "GB" then ((value.to_f)/1024/1024)
+ when "MB" then ((value.to_f)/1024)
else nil
end
end
+ def grab_content_type(request_content_type, request_body)
+ case request_content_type
+ when /xml$/i then :xml
+ when /json$/i then :json
+ else guess_content_type(request_body)
+ end
+ end
+
+ def guess_content_type(request_body)
+ xml = json = false
+ body = request_body.read
+ request_body.rewind
+ begin
+ XmlSimple.xml_in(body)
+ xml = true
+ rescue Exception
+ xml = false
+ end
+ begin
+ JSON.parse(body)
+ json = true
+ rescue Exception
+ json = false
+ end
+ if (json == xml) #both true or both false
+ raise CIMI::Model::BadRequest.new("Couldn't guess content type of: #{body}")
+ end
+ type = (xml)? :xml : :json
+ end
+
end
end
http://git-wip-us.apache.org/repos/asf/deltacloud/blob/d497f6a7/server/lib/cimi/models/volume.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/models/volume.rb b/server/lib/cimi/models/volume.rb
index 6a6d42f..f1d60f7 100644
--- a/server/lib/cimi/models/volume.rb
+++ b/server/lib/cimi/models/volume.rb
@@ -51,21 +51,24 @@ class CIMI::Model::Volume < CIMI::Model::Base
def self.all(context); find(:all, context); end
- def self.create_from_json(json_in, context)
- json = JSON.parse(json_in)
- volume_config_id = json["volumeTemplate"]["volumeConfig"]["href"].split("/").last
- volume_image_id = (json["volumeTemplate"].has_key?("volumeImage") ?
- json["volumeTemplate"]["volumeImage"]["href"].split("/").last : nil)
- create_volume({:volume_config_id=>volume_config_id, :volume_image_id=>volume_image_id}, json, context)
+ def self.create(request_body, context, type)
+ #json = JSON.parse(json_in)
+ input = (type == :xml)? XmlSimple.xml_in(request_body, {"ForceArray"=>false,"NormaliseSpace"=>2}) : JSON.parse(request_body)
+ if input["volumeTemplate"]["href"] #template by reference
+ #FIXME - don't have volumeTemplates yet - datamapper volume_config =
+ else #template by value
+ volume_image_id = (input["volumeTemplate"].has_key?("volumeImage") ?
+ input["volumeTemplate"]["volumeImage"]["href"].split("/").last : nil)
+ if input["volumeTemplate"]["volumeConfig"]["href"] #with config by reference
+ volume_config_id = input["volumeTemplate"]["volumeConfig"]["href"].split("/").last
+ create_volume({:volume_config_id=>volume_config_id, :volume_image_id=>volume_image_id}, input, context)
+ else #with config by value
+ capacity = input["volumeTemplate"]["volumeConfig"]["capacity"]
+ create_volume({:capacity=>capacity, :volume_image_id=>volume_image_id}, input, context)
+ end
+ end
end
- def self.create_from_xml(xml_in, context)
- xml = XmlSimple.xml_in(xml_in)
- volume_config_id = xml["volumeTemplate"][0]["volumeConfig"][0]["href"].split("/").last
- volume_image_id = (xml["volumeTemplate"][0].has_key?("volumeImage") ?
- xml["volumeTemplate"][0]["volumeImage"][0]["href"].split("/").last : nil)
- create_volume({:volume_config_id=>volume_config_id, :volume_image_id=>volume_image_id}, xml, context)
- end
def self.delete!(id, context)
context.driver.destroy_storage_volume(context.credentials, {:id=>id} )
@@ -91,8 +94,12 @@ class CIMI::Model::Volume < CIMI::Model::Base
private
def self.create_volume(params, data, context)
- volume_config = CIMI::Model::VolumeConfiguration.find(params[:volume_config_id], context)
- opts = {:capacity=>context.from_kibibyte(volume_config.capacity, "GB"), :snapshot_id=>params[:volume_image_id] }
+ if params[:volume_config_id]
+ volume_config = CIMI::Model::VolumeConfiguration.find(params[:volume_config_id], context)
+ opts = {:capacity=>context.from_kibibyte(volume_config.capacity, "GB"), :snapshot_id=>params[:volume_image_id] }
+ elsif params[:capacity]
+ opts = {:capacity=>context.from_kibibyte(params[:capacity], "GB"), :snapshot_id=>params[:volume_image_id]}
+ end
storage_volume = context.driver.create_storage_volume(context.credentials, opts)
store_attributes_for(context, storage_volume, data)
from_storage_volume(storage_volume, context)