[katello-devel] [lzap at fedoraproject.org: [katello-commits] [katello] tdl validation - model code]

Mike McCune mmccune at redhat.com
Mon Nov 28 15:34:21 UTC 2011


can do, comments below:

On 11/28/2011 04:56 AM, Lukas Zapletal wrote:
> Hello,
>
> there is new method in system_template model object called validate_tdl
> now. It should be used before exporting the TDL manifest.
>
> I have implemented this for our CLI, Mike could you please add a task of
> adding some validation hook to the UI code as well? Should be piece of
> cake.
>
> Thanks
>
> LZ
>
> ----- Forwarded message from lzap<lzap at fedoraproject.org>  -----
>
> Date: Mon, 28 Nov 2011 12:32:54 +0000 (UTC)
> From: lzap<lzap at fedoraproject.org>
> To: katello-commits at lists.fedorahosted.org
> Subject: [katello-commits] [katello] tdl validation - model code
>
> commit bbe56ee30775cc215e39778ed9a720e1c4067def
> Author: Lukas Zapletal<lzap+git at redhat.com>
> Date:   Mon Nov 28 10:11:15 2011 +0100
>
>      tdl validation - model code
>
>   src/app/models/errors.rb                |   17 +++++++++++
>   src/app/models/system_template.rb       |   49 ++++++++++++++++++++++++++-----
>   src/spec/models/system_template_spec.rb |   14 +++++++++
>   3 files changed, 72 insertions(+), 8 deletions(-)
> ---
> diff --git a/src/app/models/errors.rb b/src/app/models/errors.rb
> index b11c6d3..4808a4a 100644
> --- a/src/app/models/errors.rb
> +++ b/src/app/models/errors.rb
> @@ -29,4 +29,21 @@ module Errors
>     class ConflictException<  StandardError; end
>
>     class CurrentOrganizationNotFoundException<  ActiveRecord::RecordNotFound; end
> +
> +  class TemplateValidationException<  StandardError
> +    attr_accessor :errors
> +
> +    def initialize(msg, errors = [])
> +      @errors = errors
> +      super(msg)
> +    end
> +
> +    def message
> +      if @errors.nil?
> +        "#{to_s}: No errors"

should this string be localized as well as all the other error messages?

> +      else
> +        "#{to_s}: #{errors.join(', ')}"
> +      end
> +    end
> +  end
>   end
> diff --git a/src/app/models/system_template.rb b/src/app/models/system_template.rb
> index b97072e..95f25c2 100644
> --- a/src/app/models/system_template.rb
> +++ b/src/app/models/system_template.rb
> @@ -114,26 +114,63 @@ class SystemTemplate<  ActiveRecord::Base
>       self.export_as_hash.to_json
>     end
>
> +  # Validates if this template can be exported in TDL:
> +  # - at least one product is present (1)
> +  # - exactly one distribution is present (2)
> +  # - ueber certificate for it's organization has been generated (3)
> +  #
> +  # Throws exception when template does not pass all validations.
> +  def validate_tdl
> +    verrors = []
> +

see above, if these errors are going to show up in the UI should we not 
localize them?

> +    # (1)
> +    verrors<<  "At least one product must be present to export a TDL" if self.products.count<  1
> +
> +    # (2)
> +    verrors<<  "Exactly one distribution must be present to export a TDL" if self.distributions.count != 1
> +
> +    # (3)
> +    begin
> +      Candlepin::Owner.get_ueber_cert(environment.organization.cp_key)
> +    rescue RestClient::ResourceNotFound
> +      verrors<<  "Uebercert for #{environment.organization.name} has not been generated."
> +    end
> +
> +    raise Errors::TemplateValidationException.new("Template cannot be exported", verrors) if verrors.count>  0
> +    true
> +  end
> +
>     # Returns template in XML TDL format:
>     # https://github.com/aeolusproject/imagefactory/blob/master/Documentation/TDL.xsd
> +  #
> +  # Method validate_tdl MUST be called before exporting, this method expects
> +  # validated system template.
>     def export_as_tdl
>
> +    xm = Builder::XmlMarkup.new
> +    xm.instruct!
> +
> +    begin
> +      validate_tdl
> +    rescue Errors::TemplateValidationException =>  e
> +      xm.comment! "Template is not complete and will likely fail."
> +      e.errors.each do |e|
> +        xm.comment! " - #{e}"
> +      end
> +    end
> +
>       begin
>         uebercert = Candlepin::Owner.get_ueber_cert(environment.organization.cp_key)
>       rescue RestClient::ResourceNotFound =>  e
>         uebercert = nil
> -      Rails.logger.info "Uebercert for #{environment.organization.name} has not been generated. Using empty cert and key fields."
>       end
>
> -    xm = Builder::XmlMarkup.new
> -    xm.instruct!
>       xm.template {
>         # mandatory tags
>         xm.name self.name
>         if self.distributions.count == 1
>           xm.os {
>             distro = self.distributions.first
> -          # TODO this will probably need a "mapping" table (Pulp->Aeolus Family-Version)
>             family, version = Mapping::ImageFactoryNaming.translate(distro.family, distro.version)
>             xm.name family
>             xm.version version
> @@ -146,10 +183,6 @@ class SystemTemplate<  ActiveRecord::Base
>             # TODO root password is hardcoded for now
>             xm.rootpw "redhat"
>           }
> -      elsif self.distributions.count<  1
> -        Rails.logger.info "Template '%s' is missing distribution" % self.name
> -      else
> -        Rails.logger.info "Template '%s' contains more than one distribution" % self.name
>         end
>         # optional tags
>         xm.description self.description unless self.description.nil?
> diff --git a/src/spec/models/system_template_spec.rb b/src/spec/models/system_template_spec.rb
> index 9429508..16d9ac4 100644
> --- a/src/spec/models/system_template_spec.rb
> +++ b/src/spec/models/system_template_spec.rb
> @@ -595,7 +595,21 @@ describe SystemTemplate do
>           subject.xpath("/template/os/install/url").text.should == "https://localhost/pulp/ks/ACME_Corporation/Dev/isos/xxx/"
>         end
>
> +      it "should be valid" do
> +        @tpl1.validate_tdl.should be_true
> +      end
> +
>         it_should_behave_like "valid tdl"
> +
> +      it "should not be valid without a product" do
> +        @tpl1.products.clear
> +        expect { @tpl1.validate_tdl }.to raise_error(Errors::TemplateValidationException)
> +      end
> +
> +      it "should not be valid without a distribution" do
> +        @tpl1.distributions.clear
> +        expect { @tpl1.validate_tdl }.to raise_error(Errors::TemplateValidationException)
> +      end
>       end
>     end
>
> _______________________________________________
> katello-commits mailing list
> katello-commits at lists.fedorahosted.org
> https://fedorahosted.org/mailman/listinfo/katello-commits
>
> ----- End forwarded message -----
>

-- 
Mike McCune
mmccune AT redhat.com
Red Hat Engineering       | Portland, OR
Systems Management        | 650.254.4248




More information about the katello-devel mailing list