Revision 1b983127
Added by Adam Ruzicka almost 10 years ago
app/controllers/katello/api/v2/activation_keys_controller.rb | ||
---|---|---|
param :max_content_hosts, :number, :desc => N_("maximum number of registered content hosts")
|
||
param :unlimited_content_hosts, :bool, :desc => N_("can the activation key have unlimited content hosts")
|
||
def create
|
||
@activation_key = ActivationKey.create!(activation_key_params) do |activation_key|
|
||
@activation_key = ActivationKey.new(activation_key_params) do |activation_key|
|
||
activation_key.environment = @environment if @environment
|
||
activation_key.organization = @organization
|
||
activation_key.user = current_user
|
||
end
|
||
sync_task(::Actions::Katello::ActivationKey::Create, @activation_key)
|
||
@activation_key.reload
|
||
respond
|
||
end
|
||
|
app/lib/actions/candlepin/activation_key/create.rb | ||
---|---|---|
#
|
||
# 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,
|
||
# including the implied warranties of MERCHANTABILITY,
|
||
# 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 Actions
|
||
module Candlepin
|
||
class ActivationKey::Create < Candlepin::Abstract
|
||
|
||
input_format do
|
||
param :organization_label
|
||
end
|
||
|
||
def run
|
||
output[:response] = ::Katello::Resources::Candlepin::ActivationKey.create(::Katello::Util::Model.uuid,
|
||
input[:organization_label])
|
||
end
|
||
end
|
||
end
|
||
end
|
app/lib/actions/katello/activation_key/create.rb | ||
---|---|---|
#
|
||
# 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,
|
||
# including the implied warranties of MERCHANTABILITY,
|
||
# 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 Actions
|
||
module Katello
|
||
module ActivationKey
|
||
class Create < Actions::EntryAction
|
||
|
||
def plan(activation_key)
|
||
activation_key.disable_auto_reindex!
|
||
activation_key.save!
|
||
if ::Katello.config.use_cp
|
||
cp_create = plan_action(Candlepin::ActivationKey::Create,
|
||
organization_label: activation_key.organization.label)
|
||
cp_id = cp_create.output[:response][:id]
|
||
end
|
||
action_subject(activation_key, :cp_id => cp_id)
|
||
plan_self
|
||
plan_action ElasticSearch::Reindex, activation_key if ::Katello.config.use_elasticsearch
|
||
end
|
||
|
||
def humanized_name
|
||
_("Create")
|
||
end
|
||
|
||
def finalize
|
||
activation_key = ::Katello::ActivationKey.find(input[:activation_key][:id])
|
||
activation_key.disable_auto_reindex!
|
||
activation_key.cp_id = input[:cp_id]
|
||
activation_key.save!
|
||
end
|
||
|
||
end
|
||
end
|
||
end
|
||
end
|
app/models/katello/activation_key.rb | ||
---|---|---|
system_activation_keys.count
|
||
end
|
||
|
||
def related_resources
|
||
self.organization
|
||
end
|
||
|
||
def available_releases
|
||
if self.environment
|
||
self.environment.available_releases
|
app/models/katello/glue/candlepin/activation_key.rb | ||
---|---|---|
pools
|
||
end
|
||
|
||
def set_activation_key
|
||
Rails.logger.debug "Creating an activation key in candlepin: #{name}"
|
||
json = Resources::Candlepin::ActivationKey.create(Util::Model.uuid, self.organization.label)
|
||
self.cp_id = json[:id]
|
||
rescue => e
|
||
Rails.logger.error _("Failed to create candlepin activation_key %s") % "#{self.name}: #{e}, #{e.backtrace.join("\n")}"
|
||
raise e
|
||
end
|
||
|
||
def update_activation_key
|
||
Rails.logger.debug "Updating an activation key in candlepin: #{name}"
|
||
Resources::Candlepin::ActivationKey.update(self.cp_id, self.release_version, @service_level)
|
||
... | ... | |
|
||
def save_activation_key_orchestration
|
||
case self.orchestration_for
|
||
when :create
|
||
pre_queue.create(:name => "candlepin activation_key: #{self.name}", :priority => 2, :action => [self, :set_activation_key])
|
||
when :update
|
||
pre_queue.create(:name => "update candlepin activation_key: #{self.name}", :priority => 2, :action => [self, :update_activation_key])
|
||
end
|
test/actions/katello/activation_key_test.rb | ||
---|---|---|
include FactoryGirl::Syntax::Methods
|
||
|
||
let(:action) { create_action action_class }
|
||
let(:activation_key) { katello_activation_keys(:simple_key) }
|
||
|
||
end
|
||
|
||
class CreateTest < TestBase
|
||
let(:action_class) { ::Actions::Katello::ActivationKey::Create }
|
||
|
||
it 'plans' do
|
||
activation_key.expects(:save!)
|
||
action.expects(:action_subject)
|
||
plan_action action, activation_key
|
||
assert_action_planed_with(action,
|
||
::Actions::Candlepin::ActivationKey::Create,
|
||
:organization_label => activation_key.organization.label)
|
||
assert_action_planed action, ::Actions::ElasticSearch::Reindex
|
||
end
|
||
|
||
it 'raises error when validation fails' do
|
||
activation_key.name = nil
|
||
proc { plan_action action, activation_key }.must_raise(ActiveRecord::RecordInvalid)
|
||
end
|
||
end
|
||
|
||
class DestroyTest < TestBase
|
||
let(:action_class) { ::Actions::Katello::ActivationKey::Destroy }
|
||
|
||
let(:activation_key) { katello_activation_keys(:simple_key) }
|
||
|
||
it 'plans' do
|
||
activation_key.expects(:destroy!)
|
||
|
test/controllers/api/v2/activation_keys_controller_test.rb | ||
---|---|---|
module Katello
|
||
class Api::V2::ActivationKeysControllerTest < ActionController::TestCase
|
||
|
||
include Support::ForemanTasks::Task
|
||
|
||
def self.before_suite
|
||
models = ["ActivationKey", "KTEnvironment",
|
||
"ContentView", "ContentViewEnvironment", "ContentViewVersion"]
|
||
... | ... | |
@library = @organization.library
|
||
|
||
@activation_key.stubs(:get_key_pools).returns([])
|
||
|
||
::Katello::ActivationKey.stubs(:find).returns(@activation_key)
|
||
|
||
stub_find_organization(@organization)
|
||
end
|
||
|
||
... | ... | |
end
|
||
end
|
||
|
||
def test_create
|
||
post :create, :environment_id => @library.id, :content_view_id => @view.id,
|
||
:activation_key => {:name => 'Key A', :description => 'Key A, Key to the World'}
|
||
|
||
results = JSON.parse(response.body)
|
||
assert_equal results['name'], 'Key A'
|
||
assert_equal results['description'], 'Key A, Key to the World'
|
||
|
||
assert_response :success
|
||
assert_template 'katello/api/v2/common/create'
|
||
end
|
||
|
||
def test_create_protected
|
||
allowed_perms = [@create_permission]
|
||
denied_perms = [@view_permission, @update_permission, @destroy_permission]
|
||
... | ... | |
end
|
||
end
|
||
|
||
def test_create_nested
|
||
post :create, :environment => { :id => @library.id }, :content_view => { :id => @view.id },
|
||
:activation_key => {:name => 'Key A2', :description => 'Key A2, Key to the World'}
|
||
|
||
results = JSON.parse(response.body)
|
||
assert_equal results['name'], 'Key A2'
|
||
assert_equal results['description'], 'Key A2, Key to the World'
|
||
|
||
assert_response :success
|
||
assert_template 'katello/api/v2/common/create'
|
||
end
|
||
def test_create_unlimited
|
||
assert_sync_task(::Actions::Katello::ActivationKey::Create) do |activation_key|
|
||
activation_key.max_content_hosts.must_be_nil
|
||
end
|
||
|
||
def test_create_unlimited_content_hosts
|
||
post :create, :organization_id => @organization.id,
|
||
:activation_key => {:name => 'Unlimited Key', :unlimited_content_hosts => true}
|
||
|
||
results = JSON.parse(response.body)
|
||
assert_equal results['name'], 'Unlimited Key'
|
||
assert_equal results['unlimited_content_hosts'], true
|
||
|
||
assert_response :success
|
||
assert_template 'katello/api/v2/common/create'
|
||
end
|
||
|
||
def test_create_zero_limit
|
||
post :create, :organization_id => @organization.id,
|
||
:activation_key => {:name => 'Zero Key', :max_content_hosts => 0, :unlimited_content_hosts => false}
|
||
|
||
assert_response 422
|
||
end
|
||
|
||
def test_create_23_limit
|
||
post :create, :organization_id => @organization.id,
|
||
:activation_key => {:name => '23 Limited Key', :max_content_hosts => 23, :unlimited_content_hosts => false}
|
||
|
||
results = JSON.parse(response.body)
|
||
assert_equal results['name'], '23 Limited Key'
|
||
assert_equal results['max_content_hosts'], 23
|
||
|
||
assert_response :success
|
||
assert_template 'katello/api/v2/common/create'
|
||
end
|
||
... | ... | |
assert_response :success
|
||
assert_template 'api/v2/activation_keys/show'
|
||
end
|
||
|
||
def test_failed_validator
|
||
results = JSON.parse(post(:create, :organization_id => @organization.id,
|
||
:activation_key => { :max_content_hosts => 0 }).body)
|
||
|
||
assert_response 422
|
||
assert_includes results['errors']['name'], 'cannot be blank'
|
||
end
|
||
|
||
end
|
||
end
|
Also available in: Unified diff
Fixes #6188 - Dynflowized activation key creation