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/06/21 13:59:09 UTC

Added OPTIONS call to get all constraints associated to parameters for given operation in collection

Hi,

This patch will add body for 'OPTIONS /api/collection' which will contain all parameter constrains
for operations defined for this collection.

You can get constraints using:

curl -XOPTIONS -H 'Accept: application/xml' "http://mockuser:mockpassword@localhost:3001/api;driver=rhevm/instances"
curl -XOPTIONS -H 'Accept: application/xml' "http://mockuser:mockpassword@localhost:3001/api;driver=ec2/buckets"
...

The XML output is:

<api driver='rhevm' version='0.3.0'>
  <collection name='instances'>
    <operation href='/api;driver=rhevm/instances' method='post' name='create' url='/api;driver=rhevm/instances'>
      <param feature='user_name' name='name'>
        <description><![CDATA[The user-defined name]]></description>
        <constrain name='type' value='string'></constrain>
        <constrain name='required' value='false'></constrain>
        <constrain name='max_length' value='50'></constrain>
      </param>
      .....
    </operation>
  </collection>
</api>

This is just an 'idea' proposal, I'm open for discussion about XML/URL format. The goal is to give client a tool to
build a 'form' for executing operations with per-driver validation.

  -- Michal



Re: [PATCH core] Added OPTIONS call to get all constraints associated to parameters for given operation in collection

Posted by Michal Fojtik <mf...@redhat.com>.
On Jun 23, 2011, at 4:56 PM, Chris Lalancette wrote:

> On 06/21/11 - 01:59:10PM, mfojtik@redhat.com wrote:
>> From: Michal Fojtik <mf...@redhat.com>
>> 
>> 
>> Signed-off-by: Michal fojtik <mf...@redhat.com>
> 
> I took a quick look at this.  Overall, the code looks sane (though see a couple
> of minor nits below).  What I'm wondering about is how useful this will be to
> clients.  At the moment I don't see condor/libdeltacloud using this as the
> only constraint we are interested in (user_name max_length) can be fetched via
> api -> instance link -> feature name='user_name' -> constraint.  While this
> patch gives a more full range of parameters, I don't know how clients would use
> it exactly.

My original idea was to offer some kind of 'media-agnostic' form to create a new
resource to clients. Basically client don't know what parameters are need to be send
in order to successfully create the new resource and it force developer of client
to read documentation and search for the correct keys...

With this patch, client can fetch this XML and build a proper request for each
cloud provider we support. I'm also open to offer HTML and JSON form.
Actually I was looking for some 'standard' used to do this 'form' definition thing.

  -- Michal


>> ---
>> server/lib/sinatra/rabbit.rb          |    7 ++++++-
>> server/views/api/constraints.xml.haml |   24 ++++++++++++++++++++++++
>> 2 files changed, 30 insertions(+), 1 deletions(-)
>> create mode 100644 server/views/api/constraints.xml.haml
>> 
>> diff --git a/server/lib/sinatra/rabbit.rb b/server/lib/sinatra/rabbit.rb
>> index 5c1d757..007ba2f 100644
>> --- a/server/lib/sinatra/rabbit.rb
>> +++ b/server/lib/sinatra/rabbit.rb
>> @@ -221,7 +221,12 @@ module Sinatra
>>         ::Sinatra::Application.options("#{Sinatra::UrlForHelper::DEFAULT_URI_PREFIX}/#{name}") do
>>           operations_allowed = current_collection.operations.collect { |o| o[0] }.join(',')
>>           headers 'X-Operations-Allowed' => operations_allowed
>> -          [200, '']
>> +          @collection = current_collection
>> +          @operations = @collection.operations
>> +          @features = driver.features(@collection.name)
>> +          respond_to do |format|
>> +            format.xml { haml :'api/constraints'}
>> +          end
>>         end
>>       end
> 
> A couple of nits here.
> 1)  Would we want to add an HTML/JSON output?
> 2)  This code looks very similar to the next function (generate_documentation).
> Maybe we can have a common function that can do both?  (not a huge deal, but
> I did notice the redundancy)
> 
> -- 
> Chris Lalancette

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


Re: [PATCH core] Added OPTIONS call to get all constraints associated to parameters for given operation in collection

Posted by Chris Lalancette <cl...@redhat.com>.
On 06/21/11 - 01:59:10PM, mfojtik@redhat.com wrote:
> From: Michal Fojtik <mf...@redhat.com>
> 
> 
> Signed-off-by: Michal fojtik <mf...@redhat.com>

I took a quick look at this.  Overall, the code looks sane (though see a couple
of minor nits below).  What I'm wondering about is how useful this will be to
clients.  At the moment I don't see condor/libdeltacloud using this as the
only constraint we are interested in (user_name max_length) can be fetched via
api -> instance link -> feature name='user_name' -> constraint.  While this
patch gives a more full range of parameters, I don't know how clients would use
it exactly.

> ---
>  server/lib/sinatra/rabbit.rb          |    7 ++++++-
>  server/views/api/constraints.xml.haml |   24 ++++++++++++++++++++++++
>  2 files changed, 30 insertions(+), 1 deletions(-)
>  create mode 100644 server/views/api/constraints.xml.haml
> 
> diff --git a/server/lib/sinatra/rabbit.rb b/server/lib/sinatra/rabbit.rb
> index 5c1d757..007ba2f 100644
> --- a/server/lib/sinatra/rabbit.rb
> +++ b/server/lib/sinatra/rabbit.rb
> @@ -221,7 +221,12 @@ module Sinatra
>          ::Sinatra::Application.options("#{Sinatra::UrlForHelper::DEFAULT_URI_PREFIX}/#{name}") do
>            operations_allowed = current_collection.operations.collect { |o| o[0] }.join(',')
>            headers 'X-Operations-Allowed' => operations_allowed
> -          [200, '']
> +          @collection = current_collection
> +          @operations = @collection.operations
> +          @features = driver.features(@collection.name)
> +          respond_to do |format|
> +            format.xml { haml :'api/constraints'}
> +          end
>          end
>        end

A couple of nits here.
1)  Would we want to add an HTML/JSON output?
2)  This code looks very similar to the next function (generate_documentation).
Maybe we can have a common function that can do both?  (not a huge deal, but
I did notice the redundancy)

-- 
Chris Lalancette

[PATCH core] Added OPTIONS call to get all constraints associated to parameters for given operation in collection

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


Signed-off-by: Michal fojtik <mf...@redhat.com>
---
 server/lib/sinatra/rabbit.rb          |    7 ++++++-
 server/views/api/constraints.xml.haml |   24 ++++++++++++++++++++++++
 2 files changed, 30 insertions(+), 1 deletions(-)
 create mode 100644 server/views/api/constraints.xml.haml

diff --git a/server/lib/sinatra/rabbit.rb b/server/lib/sinatra/rabbit.rb
index 5c1d757..007ba2f 100644
--- a/server/lib/sinatra/rabbit.rb
+++ b/server/lib/sinatra/rabbit.rb
@@ -221,7 +221,12 @@ module Sinatra
         ::Sinatra::Application.options("#{Sinatra::UrlForHelper::DEFAULT_URI_PREFIX}/#{name}") do
           operations_allowed = current_collection.operations.collect { |o| o[0] }.join(',')
           headers 'X-Operations-Allowed' => operations_allowed
-          [200, '']
+          @collection = current_collection
+          @operations = @collection.operations
+          @features = driver.features(@collection.name)
+          respond_to do |format|
+            format.xml { haml :'api/constraints'}
+          end
         end
       end
 
diff --git a/server/views/api/constraints.xml.haml b/server/views/api/constraints.xml.haml
new file mode 100644
index 0000000..b045f58
--- /dev/null
+++ b/server/views/api/constraints.xml.haml
@@ -0,0 +1,24 @@
+%api{ :driver => driver_symbol, :version => settings.version }
+  %collection{ :name => @collection.name.to_s }
+    - @operations.keys.sort_by { |k| k.to_s }.each do |operation|
+      %operation{ :url => api_url_for(@collection.name.to_s), :name => operation, :href => api_url_for(@operations[operation].path), :method => @operations[operation].method }
+        - @operations[operation].params.each do |param_name, param|
+          %param{ :name => param_name }
+            - unless param.description.empty?
+              %description=cdata(param.description)
+            %constraint{ :name => :type, :value => param.klass }
+            %constraint{ :name => :required, :value => (param.type == :required) ? 'true' : 'false' }
+            - unless param.options.empty?
+              %constraint{ :name => :options}
+                - param.options.each do |option|
+                  %option=option
+        - for feature in driver.features(@collection.name.to_sym)
+          - feature.operations.select { |o| o.name == operation }.each do |op|
+            - op.params.each do |param_name, param|
+              %param{ :name => param_name, :feature => feature.name }
+                - unless param.description.empty?
+                  %description=cdata(param.description)
+                %constraint{ :name => :type, :value => param.klass }
+                %constraint{ :name => :required, :value => (param.type == :required) ? 'true' : 'false' }
+                - for name, value in feature.constraints
+                  %constraint{ :name => name, :value => value }
-- 
1.7.4.1