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/10/07 15:15:58 UTC

[PATCH core] Core: Fixed hardware_profile validation engine

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


Signed-off-by: Michal fojtik <mf...@redhat.com>
---
 server/lib/deltacloud/hardware_profile.rb   |   19 ++++++++---------
 server/lib/deltacloud/validation.rb         |   29 +++++++++++++++++++++++---
 server/lib/sinatra/rabbit.rb                |    2 +-
 server/tests/common.rb                      |   22 ++++++++++++++------
 server/tests/drivers/mock/instances_test.rb |   17 +++++++++++++++
 server/tests/rabbit_test.rb                 |    2 +-
 6 files changed, 68 insertions(+), 23 deletions(-)

diff --git a/server/lib/deltacloud/hardware_profile.rb b/server/lib/deltacloud/hardware_profile.rb
index e304d7c..11e49ce 100644
--- a/server/lib/deltacloud/hardware_profile.rb
+++ b/server/lib/deltacloud/hardware_profile.rb
@@ -68,22 +68,21 @@ module Deltacloud
         kind == :fixed
       end
 
-      def to_param
-        return nil if kind == :fixed
-        if kind == :range
-          # FIXME: We can't validate ranges currently
-          args = [param, :string, :optional]
-        else
-          args = [param, :string, :optional, values.collect { |v| v.to_s} ]
+      def valid?(v)
+        case kind
+          when :fixed then (v == @default.to_s)
+          when [:range, :enum] then (value.include?(v.to_i))
+          else false
         end
-        Validation::Param.new(args)
+      end
+
+      def to_param
+        Validation::Param.new([param, :string, :optional, []])
       end
 
       def include?(v)
         if kind == :fixed
           return v == value
-        elsif kind == :range
-          return v >= first && v <= last
         else
           return values.include?(v)
         end
diff --git a/server/lib/deltacloud/validation.rb b/server/lib/deltacloud/validation.rb
index 5c75d18..aa35f4e 100644
--- a/server/lib/deltacloud/validation.rb
+++ b/server/lib/deltacloud/validation.rb
@@ -46,6 +46,19 @@ module Deltacloud::Validation
     def optional?
       type.eql?(:optional)
     end
+
+    def valid_value?(value)
+      true if (options.kind_of?(Range) or options.kind_of?(Array)) and options.include?(value)
+      true if options.kind_of?(String) and not options.empty?
+    end
+
+    def valid_hwp_value?(profile, value)
+      profile.property(@name.to_s.tr('hwp_', '')).valid?(value)
+    end
+
+    def hwp_property?
+      true if name.to_s =~ /^hwp_(cpu|memory|storage|architecture)/
+    end
   end
 
   def param(*args)
@@ -72,15 +85,23 @@ module Deltacloud::Validation
     params.each_value { |p| yield p }
   end
 
-  def validate(all_params, values)
+  def validate(all_params, values, credentials)
     all_params.each_value do |p|
       if p.required? and not values[p.name]
         raise Failure.new(p, "Required parameter #{p.name} not found")
       end
-      if values[p.name] and not p.options.empty? and
-          not p.options.include?(values[p.name])
-        raise Failure.new(p, "Parameter #{p.name} has value #{values[p.name]} which is not in #{p.options.join(", ")}")
+      next unless values[p.name]
+      if p.hwp_property?
+        profile = driver.hardware_profile(credentials, values['hwp_id'])
+        unless p.valid_hwp_value?(profile, values[p.name])
+          raise Failure.new(p, "Hardware profile property #{p.name} has invalid value #{values[p.name]}")
+        end
+      else
+        if not p.options.empty? and p.valid_value?(values[p.name])
+          raise Failure.new(p, "Parameter #{p.name} has value #{values[p.name]} which is not in #{p.options.join(", ")}")
+        end
       end
     end
   end
+
 end
diff --git a/server/lib/sinatra/rabbit.rb b/server/lib/sinatra/rabbit.rb
index e83d5a8..cf8c14f 100644
--- a/server/lib/sinatra/rabbit.rb
+++ b/server/lib/sinatra/rabbit.rb
@@ -121,7 +121,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, credentials)
           instance_eval(&block)
         end
       end
diff --git a/server/tests/common.rb b/server/tests/common.rb
index 71f1442..6615465 100644
--- a/server/tests/common.rb
+++ b/server/tests/common.rb
@@ -100,7 +100,9 @@ module DeltacloudTestCommon
       get(uri, params || {}, opts[:auth] ? authenticate(opts) : {})
       if last_response.status.to_s =~ /5(\d{2})/
         puts "============= [ ERROR ] ================"
-        puts last_response.inspect
+        puts last_response.body
+        puts "============= [ RESPONSE ] ================"
+        puts last_response.errors
         puts "========================================"
       end
     end
@@ -112,7 +114,9 @@ module DeltacloudTestCommon
     get_url(uri, params, opts)
     if last_response.status.to_s =~ /5(\d{2})/
       puts "============= [ ERROR ] ================"
-      puts last_response.inspect
+      puts last_response.body
+      puts "============= [ RESPONSE ] ================"
+      puts last_response.errors
       puts "========================================"
     end
   end
@@ -127,7 +131,9 @@ module DeltacloudTestCommon
       post(uri, params || {}, authenticate(opts))
       if last_response.status.to_s =~ /5(\d{2})/
         puts "============= [ ERROR ] ================"
-        puts last_response.inspect
+        puts last_response.body
+        puts "============= [ RESPONSE ] ================"
+        puts last_response.errors
         puts "========================================"
       end
     end
@@ -143,8 +149,9 @@ module DeltacloudTestCommon
       delete(uri, params || {}, authenticate(opts))
       if last_response.status.to_s =~ /5(\d{2})/
         puts "============= [ ERROR ] ================"
-        puts last_response.inspect
-        puts "========================================"
+        puts last_response.body
+        puts "============= [ RESPONSE ] ================"
+        puts last_response.errors
       end
     end
   end
@@ -159,8 +166,9 @@ module DeltacloudTestCommon
        put(uri, params || {}, authenticate(opts))
       if last_response.status.to_s =~ /5(\d{2})/
         puts "============= [ ERROR ] ================"
-        puts last_response.inspect
-        puts "========================================"
+        puts last_response.body
+        puts "============= [ RESPONSE ] ================"
+        puts last_response.errors
       end
     end
   end
diff --git a/server/tests/drivers/mock/instances_test.rb b/server/tests/drivers/mock/instances_test.rb
index 0e97c19..056f399 100644
--- a/server/tests/drivers/mock/instances_test.rb
+++ b/server/tests/drivers/mock/instances_test.rb
@@ -125,6 +125,23 @@ module DeltacloudUnitTest
       test_instance_attributes(last_xml_response/'instance')
     end
 
+    def test_it_create_a_new_instance_using_image_id_and_name_and_hwp_storage
+      params = {
+        :image_id => 'img1',
+        :name => "unit_test_instance2",
+        :hwp_id => "m1-small",
+        :hwp_storage => "160"
+      }
+      post_url '/api/instances', params
+      last_response.status.should == 201
+      last_response.headers['Location'].should_not == nil
+      get_auth_url last_response.headers['Location'], {}
+      (last_xml_response/'instance/name').text.should == 'unit_test_instance2'
+      (last_xml_response/'instance/hardware_profile').first['id'].should == 'm1-small'
+      add_created_instance (last_xml_response/'instance').first['id']
+      test_instance_attributes(last_xml_response/'instance')
+    end
+
     def test_it_z0_stop_and_start_instance
       $created_instances.each do |instance_id|
         get_auth_url "/api/instances/#{instance_id}", {}
diff --git a/server/tests/rabbit_test.rb b/server/tests/rabbit_test.rb
index 336545f..bfbce91 100644
--- a/server/tests/rabbit_test.rb
+++ b/server/tests/rabbit_test.rb
@@ -44,7 +44,7 @@ module DeltacloudUnitTest
       features.should =~ [:hardware_profiles, :user_name, :authentication_key, :user_data]
 
       op = @app.collections[:instances].operations[:create]
-      op.effective_params(@driver).keys.should =~ [:image_id, :hwp_memory, :hwp_id, :keyname, :name, :hwp_storage, :realm_id, :user_data]
+      op.effective_params(@driver).keys.should =~ [:image_id, :hwp_memory, :hwp_id, :keyname, :name, :hwp_storage, :realm_id, :user_data, :hwp_architecture, :hwp_cpu]
 
       op.params.keys =~ [:realm_id, :image_id, :hwp_id]
     end
-- 
1.7.4.4