From: Michal Fojtik <[email protected]>
Signed-off-by: Michal fojtik <[email protected]> --- server/lib/deltacloud/drivers/mock/mock_driver.rb | 71 +++++++++++++++------ server/lib/sinatra/rabbit.rb | 2 +- server/server.rb | 1 + 3 files changed, 54 insertions(+), 20 deletions(-) diff --git a/server/lib/deltacloud/drivers/mock/mock_driver.rb b/server/lib/deltacloud/drivers/mock/mock_driver.rb index 83a7404..a3522fa 100644 --- a/server/lib/deltacloud/drivers/mock/mock_driver.rb +++ b/server/lib/deltacloud/drivers/mock/mock_driver.rb @@ -21,6 +21,8 @@ require 'yaml' module Deltacloud module Drivers module Mock + require 'nokogiri' + class MockDriver < Deltacloud::BaseDriver # If the provider is set to storage, pretend to be a storage-only @@ -197,37 +199,38 @@ class MockDriver < Deltacloud::BaseDriver count = 0 while true next_id = "inst" + count.to_s - if not ids.include?(next_id) - break - end - count = count + 1 - end - - realm_id = opts[:realm_id] - if ( realm_id.nil? ) - realm = realms(credentials).first - ( realm_id = realm.id ) if realm + break if not ids.include?(next_id) + count += 1 end - hwp = find_hardware_profile(credentials, opts[:hwp_id], image_id) - - name = opts[:name] || "i-#{Time.now.to_i}" - instance = { - :name=>name, - :state=>'RUNNING', - :image_id=>image_id, + :state => 'RUNNING', :owner_id=>credentials.user, :public_addresses=>["#{image_id}.#{next_id}.public.com"], :private_addresses=>["#{image_id}.#{next_id}.private.com"], - :instance_profile => InstanceProfile.new(hwp.name, opts), - :realm_id=>realm_id, :create_image=>true, :actions=>instance_actions_for( 'RUNNING' ) } + + if opts[:instance] + instance.merge!(convert_instance_xml(opts[:instance])) + raise Deltacloud::Validation::Failure.new(:image_id, 'Required element <image id=""/> not found') if instance[:image_id] == '' + else + realm_id = opts[:realm_id] ? opts[:realm_id] : realms(credentials).first.id + hwp = find_hardware_profile(credentials, opts[:hwp_id], image_id) + name = opts[:name] || "i-#{Time.now.to_i}" + instance.merge!({ + :name=>name, + :image_id=>image_id, + :instance_profile => InstanceProfile.new(hwp.name, opts), + :realm_id=>realm_id, + }) + end + File.open( "#{@storage_root}/instances/#{next_id}.yml", 'w' ) {|f| YAML.dump( instance, f ) } + instance[:id] = next_id Instance.new( instance ) end @@ -503,6 +506,36 @@ class MockDriver < Deltacloud::BaseDriver end end + # Convert POST XML to instance + # + # XML example: + # + # <instance> + # <name>inst10</name> + # <image id="img1"/> + # <realm id="us"/> + # <hardware_profile id="m1-small"> + # <memory>512<memory> + # <cpu>2<cpu> + # <storage>10<storage> + # <architecture>i386<architecture> + # </hardware_profile> + # </instance> + # + def convert_instance_xml(instance_xml) + doc = (Nokogiri::XML(instance_xml)/'instance') + hwp_opts = {} + (doc/'hardware_profile/*').each do |attr| + hwp_opts[:"hwp_#{attr.name}"] = attr.text + end + { + :name => (doc/'name').text, + :image_id => (doc/'image/@id').text, + :realm_id => (doc/'realm/@id').text, + :instace_profile => InstanceProfile.new((doc/'image/@id').text, hwp_opts), + } + end + exceptions do on /AuthFailure/ do diff --git a/server/lib/sinatra/rabbit.rb b/server/lib/sinatra/rabbit.rb index 3d40112..bfe58b9 100644 --- a/server/lib/sinatra/rabbit.rb +++ b/server/lib/sinatra/rabbit.rb @@ -110,7 +110,7 @@ module Sinatra @control = Proc.new do op.collection.check_supported(driver) op.check_capability(driver) - op.validate(op.effective_params(driver), params) + op.validate(op.effective_params(driver), params) unless request.body.length > 0 instance_eval(&block) end end diff --git a/server/server.rb b/server/server.rb index 4da8357..02c12f1 100644 --- a/server/server.rb +++ b/server/server.rb @@ -381,6 +381,7 @@ END param :realm_id, :string, :optional param :hwp_id, :string, :optional control do + params[:instance] = request.body.read if request.body.length > 0 @instance = driver.create_instance(credentials, params[:image_id], params) respond_to do |format| format.xml do -- 1.7.4.1
