Skip to content

Commit

Permalink
Fixes #11775: Allow database ID for adding puppet modules to CV
Browse files Browse the repository at this point in the history
  • Loading branch information
ehelms committed Sep 16, 2015
1 parent a0641f7 commit 58bbf1c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
Expand Up @@ -3,7 +3,8 @@ class Api::V2::ContentViewPuppetModulesController < Api::V2::ApiController
include Katello::Concerns::FilteredAutoCompleteSearch

before_filter :find_content_view, :except => [:autocomplete_search]
before_filter :find_puppet_module, :only => [:show, :update, :destroy]
before_filter :find_puppet_module, :only => [:create]
before_filter :find_content_view_puppet_module, :only => [:show, :update, :destroy]

api :GET, "/content_views/:content_view_id/content_view_puppet_modules", N_("List content view puppet modules")
param :content_view_id, :identifier, :desc => N_("content view identifier"), :required => true
Expand All @@ -20,8 +21,11 @@ def index
param :content_view_id, :identifier, :desc => N_("content view identifier"), :required => true
param :name, String, :desc => N_("name of the puppet module")
param :author, String, :desc => N_("author of the puppet module")
param :id, String, :desc => N_("the id of the puppet module to associate")
param :uuid, String, :desc => N_("the uuid of the puppet module to associate")
def create
params[:content_view_puppet_module][:uuid] = @puppet_module.uuid if @puppet_module

@puppet_module = ContentViewPuppetModule.create!(puppet_module_params) do |puppet_module|
puppet_module.content_view = @view
end
Expand Down Expand Up @@ -71,10 +75,14 @@ def find_content_view
@view = ContentView.non_default.find(params[:content_view_id])
end

def find_puppet_module
def find_content_view_puppet_module
@puppet_module = ContentViewPuppetModule.find(params[:id])
end

def find_puppet_module
@puppet_module = PuppetModule.find(params[:id]) if params[:id]
end

def puppet_module_params
attrs = [:name, :author, :uuid]
params.require(:content_view_puppet_module).permit(*attrs)
Expand Down
14 changes: 1 addition & 13 deletions app/models/katello/content_view_puppet_module.rb
Expand Up @@ -41,23 +41,11 @@ def computed_version
def set_attributes
return unless Katello.config.use_pulp
if self.uuid.present?
puppet_module = PuppetModule.find_by_uuid(self.uuid)
puppet_module = PuppetModule.with_identifiers(self.uuid).first
fail Errors::NotFound, _("Couldn't find Puppet Module with id '%s'") % self.uuid unless puppet_module

self.name = puppet_module.name
self.author = puppet_module.author
elsif (self.name.present? && !self.author.present?)
puppet_modules = PuppetModule.latest_modules_search(
[{:name => self.name, :author => '*'}],
self.content_view.puppet_repos.map(&:pulp_id))

if puppet_modules.empty?
fail Errors::NotFound, _("Couldn't find Puppet Module '%s'.") % self.name
elsif puppet_modules.length > 1
fail Errors::NotFound, _("Puppet Module '%s' found more than once. Please specify the author.") % self.name
else
self.author = puppet_modules.first.author
end
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/models/katello_puppet_modules.yml
Expand Up @@ -15,3 +15,9 @@ foreman:
author: "theforeman"
version: 1.0
uuid: 3bd47a52-0847-42b5-90ff-206307b48b22

foreman_proxy:
name: "foreman_proxy"
author: "theforeman"
version: 1.0
uuid: 3bd47a52-0847-42b5-90ff-adslkdkkda32
18 changes: 18 additions & 0 deletions test/models/content_view_puppet_module_test.rb
Expand Up @@ -8,6 +8,24 @@ def setup
@puppet_module = ContentViewPuppetModule.find(katello_content_view_puppet_modules(:library_view_abrt_module).id)
end

def test_create_with_name_author
assert ContentViewPuppetModule.create!(:name => 'dhcp', :author => 'johndoe', :content_view => @library_view)
end

def test_create_with_name
assert_raises ActiveRecord::RecordInvalid do
ContentViewPuppetModule.create!(:name => 'dhcp', :content_view => @library_view)
end
end

def test_create_with_uuid
content_view_puppet_module = ContentViewPuppetModule.new(
:uuid => katello_puppet_modules(:foreman_proxy).uuid,
:content_view => @library_view
)
assert content_view_puppet_module.save!
end

def test_search_name
assert_equal @puppet_module, ContentViewPuppetModule.search_for("name = \"#{@puppet_module.name}\"").first
end
Expand Down

0 comments on commit 58bbf1c

Please sign in to comment.