You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@deltacloud.apache.org by mf...@redhat.com on 2011/05/11 11:24:34 UTC

Initial POST body XML support

Hi,

this patch add possibility to create a new instance using POST body XML.

How it works:

instance.xml:
<instance>
  <name>testinst10</name>
  <realm id="us"/>
  <hardware_profile id="m1-small">
    <memory>512</memory>
    <cpu>2</cpu>
    <storage>10</storage>
    <architecture>i386</architecture>
  </hardware_profile>
</instance>

Then:

curl -v -X POST -d "`cat instance.xml`" -H 'Accept: application/xml' http://mockuser:mockpassword@localhost:3001/api/instances

You should get a create instance as reponse.
Let me know what do you think. If we gonna support this way, it should be easy to do a BlockDeviceMappings or handling
firewall configurations.

  -- Michal


[PATCH core] Initial POST body XML support for creating new instances

Posted by mf...@redhat.com.
From: Michal Fojtik <mf...@redhat.com>


Signed-off-by: Michal fojtik <mf...@redhat.com>
---
 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


Re: Initial POST body XML support

Posted by Michal Fojtik <mf...@redhat.com>.
On May 12, 2011, at 1:33 AM, David Lutterkort wrote:

> On Wed, 2011-05-11 at 11:24 +0200, mfojtik@redhat.com wrote:
>> Hi,
>> 
>> this patch add possibility to create a new instance using POST body XML.
>> 
>> How it works:
>> 
>> instance.xml:
>> <instance>
>>  <name>testinst10</name>
>>  <realm id="us"/>
>>  <hardware_profile id="m1-small">
>>    <memory>512</memory>
>>    <cpu>2</cpu>
>>    <storage>10</storage>
>>    <architecture>i386</architecture>
>>  </hardware_profile>
>> </instance>
> 
> I assume the HWP entries are meant to override defaults from m1-small
> here ?

Yes, but in this example they just demonstrate how overriding of default
values should work.

> As for the patch, parsing an instance from XML should be done in a class
> method in Instance; that should probably happen in server.rb, and the
> code there should ensure the params hash only has an :instance entry for
> XML.

Yes, moving parsing to Instance sounds good. With 'server.rb' you mean something
like this? :
 
....
    control do
      params = Instance.parse_body(request.body.read)
      @instance = driver.create_instance(credentials, params[:image_id], params)
      respond_to do |format|
        format.xml do
....

It will match params to be same as 'query' params and then continue as usual.
Also '.parse_body' should distinguish between JSON and XML (or optionally parse
HTTP header which will tell to Instance what content parser to use (JSON/XML/whatever).


  -- Michal

------------------------------------------------------
Michal Fojtik, mfojtik@redhat.com
Deltacloud API: http://deltacloud.org


Re: Initial POST body XML support

Posted by David Lutterkort <lu...@redhat.com>.
On Wed, 2011-05-11 at 11:24 +0200, mfojtik@redhat.com wrote:
> Hi,
> 
> this patch add possibility to create a new instance using POST body XML.
> 
> How it works:
> 
> instance.xml:
> <instance>
>   <name>testinst10</name>
>   <realm id="us"/>
>   <hardware_profile id="m1-small">
>     <memory>512</memory>
>     <cpu>2</cpu>
>     <storage>10</storage>
>     <architecture>i386</architecture>
>   </hardware_profile>
> </instance>

I assume the HWP entries are meant to override defaults from m1-small
here ?

As for the patch, parsing an instance from XML should be done in a class
method in Instance; that should probably happen in server.rb, and the
code there should ensure the params hash only has an :instance entry for
XML.

Also, we should distinguish whether to look for XML or not based on the
content type of the POST (so that we can later easily add posting JSON
or whatnot)

David