[PATCH aeolus-cli 1/9] bz#747719: validation of import parameters

Petr Blaho pblaho at redhat.com
Thu Dec 22 22:05:47 UTC 2011


From: Maros Zatko <mzatko at redhat.com>

---
 lib/aeolus_cli/command/import_command.rb |   50 +++++++++++++++++++++++++++---
 1 files changed, 45 insertions(+), 5 deletions(-)

diff --git a/lib/aeolus_cli/command/import_command.rb b/lib/aeolus_cli/command/import_command.rb
index 591a999..91a5d54 100644
--- a/lib/aeolus_cli/command/import_command.rb
+++ b/lib/aeolus_cli/command/import_command.rb
@@ -18,11 +18,7 @@ module Aeolus
       def initialize(opts={}, logger=nil)
         super(opts, logger)
         default = {
-          :image => '',
-          :build => '', # TODO - Is this used anywhere?
-          :id => '',
           :description => '<image><name>' + @options[:id] + '</name></image>',
-          :provider_account => ''
         }
         @options = default.merge(@options)
       end
@@ -33,7 +29,9 @@ module Aeolus
           if !description.nil?
             @options[:description] = description
           end
-          # TODO: Validate Description XML
+
+          import_params_valid!(@options)
+
           image = Aeolus::CLI::Image.new({:target_identifier => @options[:id],
                                           :image_descriptor => @options[:description],
                                           :provider_account_name => @options[:provider_account].first})
@@ -49,6 +47,48 @@ module Aeolus
           handle_exception(e)
         end
       end
+
+      private
+
+      def import_params_valid!(params)
+        if not params.is_a? Hash
+          raise TypeError, "params should be Hash instead of #{params.class}"
+        end
+
+        if not params.values.all?
+          raise ArgumentError, "params should not contain nil"
+        end
+
+        required_keys = [:id, :target, :description, :provider]
+
+        isect = params.keys & required_keys
+        diff  = params.keys - required_keys
+
+        missing = required_keys - isect
+
+        if not missing == []
+          raise ArgumentError, "missing #{missing*', '}"
+        end
+
+        if not diff == []
+          raise ArgumentError, "unexpected #{diff*', '}"
+        end
+
+        validate_description_xml!(params[:description])
+      end
+
+      def validate_description_xml!(xml)
+        errors = validate_xml_document(File.dirname(__FILE__) + "/../../../examples/image_desc.rng", xml)
+        if errors.length > 0
+          puts "ERROR: The given image descrition does not conform to the xml schema, see below for specific details:"
+          errors.each do |error|
+            puts "- " + error.message
+          end
+          raise ArgumentError, "Invalid description"
+        end
+        true
+      end
+
     end
   end
 end
-- 
1.7.7.4




More information about the aeolus-devel mailing list