app/controllers/katello/api/v2/errata_controller.rb in katello-1.5.0 vs app/controllers/katello/api/v2/errata_controller.rb in katello-2.2.2

- old
+ new

@@ -1,7 +1,7 @@ # -# Copyright 2011 Red Hat, Inc. +# Copyright 2014 Red Hat, Inc. # # This software is licensed to you under the GNU General Public # License as published by the Free Software Foundation; either version # 2 of the License (GPLv2) or (at your option) any later version. # There is NO WARRANTY for this software, express or implied, @@ -9,80 +9,50 @@ # NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should # have received a copy of GPLv2 along with this software; if not, see # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. module Katello -class Api::V2::ErrataController < Api::V2::ApiController + class Api::V2::ErrataController < Api::V2::ApiController + apipie_concern_subst(:a_resource => N_("an erratum"), :resource => "errata") + include Katello::Concerns::Api::V2::RepositoryContentController + include Katello::Concerns::Api::V2::RepositoryDbContentController - resource_description do - error :code => 401, :desc => "Unauthorized" - error :code => 404, :desc => "Not found" + api :GET, "/errata", N_("List errata") + param :content_view_version_id, :identifier, :desc => N_("content view version identifier") + param :content_view_filter_id, :identifier, :desc => N_("content view filter identifier") + param :repository_id, :number, :desc => N_("repository identifier") + param :environment_id, :number, :desc => N_("environment identifier") + param :cve, String, :desc => N_("CVE identifier") + param :errata_restrict_applicable, :bool, :desc => N_("show only errata with one or more applicable systems") + param :errata_restrict_installable, :bool, :desc => N_("show only errata with one or more installable systems") + param_group :search, Api::V2::ApiController + def index + super + end - api_version 'v2' - end + def custom_index_relation(collection) + collection = filter_by_cve(params[:cve], collection) if params[:cve] + if params[:errata_restrict_applicable] && params[:errata_restrict_applicable].to_bool + collection = collection.applicable_to_systems(System.readable) + end - before_filter :find_organization, :only => [:show] - before_filter :find_environment, :only => [:index] - before_filter :find_repository, :only => [:index, :show] - before_filter :find_erratum, :only => [:show] - before_filter :require_repo_or_environment, :only => [:index] - before_filter :authorize + if params[:errata_restrict_installable] && params[:errata_restrict_installable].to_bool + collection = collection.installable_for_systems(System.readable) + end + collection + end - def rules - env_readable = lambda { @environment.contents_readable? } - readable = lambda { Repository.any_readable?(@organization) } - { - :index => env_readable, - :show => readable, - } - end + private - api :GET, "/repositories/:repository_id/errata", "List errata" - api :GET, "/environments/:environment_id/errata", "List errata" - param :environment_id, :number, :desc => "The environment containing the errata." - param :product_id, :number, :desc => "The product which contains errata." - param :repository_id, :number, :desc => "The repository which contains errata." - param :severity, String, :desc => "Severity of errata. Usually one of: Critical, Important, Moderate, Low. Case insensitive." - param :type, String, :desc => "Type of errata. Usually one of: security, bugfix, enhancement. Case insensitive." - def index - filter = params.symbolize_keys.slice(:repository_id, :product_id, :environment_id, :type, :severity) - respond :collection => Errata.filter(filter) - end + def filter_by_cve(cve, collection) + collection.joins(:cves).where('katello_erratum_cves.cve_id' => cve) + end - api :GET, "/repositories/:repository_id/errata/:id", "Show an erratum" - api :GET, "/errata/:id", "Show an erratum" - def show - respond :resource => @erratum - end - - private - - def find_environment - if params.key?(:environment_id) - @environment = KTEnvironment.find(params[:environment_id]) - fail HttpErrors::NotFound, _("Couldn't find environment '%s'") % params[:environment_id] if @environment.nil? - @environment + def filter_by_content_view_filter(filter, collection) + collection.where(:errata_id => filter.erratum_rules.pluck(:errata_id)) end - end - def find_repository - if params.key?(:repository_id) - @repo = Repository.find(params[:repository_id]) - fail HttpErrors::NotFound, _("Couldn't find repository '%s'") % params[:repository_id] if @repo.nil? - @environment ||= @repo.environment - @repo + def default_sort + %w(updated desc) end end - - def find_erratum - @erratum = Errata.find(params[:id]) - @erratum ||= Errata.find_by_errata_id(params[:id]) - fail HttpErrors::NotFound, _("Erratum with id '%s' not found") % params[:id] if @erratum.nil? - fail HttpErrors::NotFound, _("Erratum '%s' not found within the repository") % params[:id] unless @repo.nil? || @erratum.repoids.include?(@repo.pulp_id) - @erratum - end - - def require_repo_or_environment - fail HttpErrors::BadRequest, _("Either repository or environment is required.") % params[:id] if @repo.nil? && @environment.nil? - end -end end