Revision 0ba99bba
Added by Joseph Magen almost 11 years ago
app/assets/javascripts/host_edit.js | ||
---|---|---|
|
||
function load_puppet_class_parameters(item) {
|
||
var id = $(item).attr('data-class-id');
|
||
// host_id could be either host.id OR hostgroup.id depending on which form
|
||
var host_id = $("form").data('id')
|
||
if ($('#puppetclass_' + id + '_params_loading').length > 0) return; // already loading
|
||
if ($('[id^="#puppetclass_' + id + '_params\\["]').length > 0) return; // already loaded
|
app/controllers/puppetclasses_controller.rb | ||
---|---|---|
# form AJAX methods
|
||
def parameters
|
||
puppetclass = Puppetclass.find(params[:id])
|
||
obj = params['host'] ? refresh_host : refresh_hostgroup
|
||
render :partial => "puppetclasses/class_parameters", :locals => {
|
||
:puppetclass => puppetclass,
|
||
:obj => obj}
|
||
:obj => get_host_or_hostgroup}
|
||
end
|
||
|
||
private
|
||
|
||
def refresh_host
|
||
@host = Host::Base.find_by_id(params['host_id'])
|
||
if @host
|
||
unless @host.kind_of?(Host::Managed)
|
||
@host = @host.becomes(Host::Managed)
|
||
@host.type = "Host::Managed"
|
||
end
|
||
@host.attributes = params['host']
|
||
else
|
||
@host = Host::Managed.new(params['host'])
|
||
end
|
||
@host
|
||
end
|
||
|
||
def refresh_hostgroup
|
||
@hostgroup = Hostgroup.find_by_id(params['host_id'])
|
||
if @hostgroup
|
||
@hostgroup.attributes = params['hostgroup']
|
||
def get_host_or_hostgroup
|
||
# params['host_id'] = 'null' if NEW since hosts/form and hostgroups/form has data-id="null"
|
||
if params['host_id'] == 'null'
|
||
@obj = Host::Managed.new(params['host']) if params['host']
|
||
@obj ||= Hostgroup.new(params['hostgroup']) if params['hostgroup']
|
||
else
|
||
@hostgroup = Hostgroup.new(params['hostgroup'])
|
||
if params['host']
|
||
@obj = Host::Base.find(params['host_id'])
|
||
unless @obj.kind_of?(Host::Managed)
|
||
@obj = @obj.becomes(Host::Managed)
|
||
@obj.type = "Host::Managed"
|
||
end
|
||
# puppetclass_ids is removed since it causes an insert on host_classes before form is submitted
|
||
@obj.attributes = params['host'].except!(:puppetclass_ids)
|
||
elsif params['hostgroup']
|
||
# hostgroup.id is assigned to params['host_id'] by host_edit.js#load_puppet_class_parameters
|
||
@obj = Hostgroup.find(params['host_id'])
|
||
# puppetclass_ids is removed since it causes an insert on hostgroup_classes before form is submitted
|
||
@obj.attributes = params['hostgroup'].except!(:puppetclass_ids)
|
||
end
|
||
end
|
||
@hostgroup
|
||
@obj
|
||
end
|
||
|
||
def reset_redirect_to_url
|
test/functional/puppetclasses_controller_test.rb | ||
---|---|---|
require 'test_helper'
|
||
|
||
class PuppetclassesControllerTest < ActionController::TestCase
|
||
include LookupKeysHelper
|
||
def test_index
|
||
get :index, {}, set_session_user
|
||
assert_template 'index'
|
||
... | ... | |
get :index, {}, set_session_user
|
||
assert_response :success
|
||
end
|
||
|
||
test 'new db rows are not added to HostClass when POST to parameters' do
|
||
host = hosts(:one)
|
||
puppetclass = puppetclasses(:four) #puppetclass to be added to host
|
||
host_puppetclass_ids = host.puppetclass_ids
|
||
assert_difference('HostClass.count', 0) do
|
||
post :parameters, {:id => puppetclass.id, :host_id => host.id, :host => {:puppetclass_ids => (host_puppetclass_ids + [puppetclass.id])}}, set_session_user
|
||
end
|
||
end
|
||
|
||
test 'new db rows are not added to HostgroupClass when POST to parameters' do
|
||
hostgroup = hostgroups(:common)
|
||
puppetclass = puppetclasses(:four) #puppetclass to be added to hostgroup
|
||
hostgroup_puppetclass_ids = hostgroup.puppetclass_ids
|
||
assert_difference('HostgroupClass.count', 0) do
|
||
post :parameters, {:id => puppetclass.id, :host_id => hostgroup.id, :hostgroup => {:puppetclass_ids => (hostgroup_puppetclass_ids + [puppetclass.id])}}, set_session_user
|
||
end
|
||
end
|
||
|
||
# NOTES: for tests below testing ajax POST to method parameters
|
||
# puppetclass(:two) has TWO overridable lookup keys: 1) special_info and 2) custom_class_param
|
||
# special_info is a smart_variable that is added independant of environment
|
||
# custom_class_param is a smart_class_param for production environment only AND is marked as :override => TRUE
|
||
test 'puppetclass lookup keys are added to partial _class_paramters on EXISTING host form through ajax POST to parameters' do
|
||
host = hosts(:one)
|
||
puppetclass = puppetclasses(:two)
|
||
post :parameters, {:id => puppetclass.id, :host_id => host.id, :host => host.attributes }, set_session_user
|
||
assert_response :success
|
||
lookup_keys_added = overridable_lookup_keys(puppetclass, host)
|
||
assert_equal 2, lookup_keys_added.count
|
||
assert lookup_keys_added.map(&:key).include?("special_info")
|
||
assert lookup_keys_added.map(&:key).include?("custom_class_param")
|
||
end
|
||
|
||
test 'puppetclass smart class parameters are NOT added if environment does not match' do
|
||
# below is the same test as above, except environment is changed from production to global_puppetmaster, so custom_class_param is NOT added
|
||
host = hosts(:one)
|
||
puppetclass = puppetclasses(:two)
|
||
post :parameters, {:id => puppetclass.id, :host_id => host.id, :host => host.attributes.merge!('environment_id' => environments(:global_puppetmaster).id) }, set_session_user
|
||
assert_response :success
|
||
lookup_keys_added = overridable_lookup_keys(puppetclass, assigns(:obj))
|
||
assert_equal 1, lookup_keys_added.count
|
||
assert lookup_keys_added.map(&:key).include?("special_info")
|
||
refute lookup_keys_added.map(&:key).include?("custom_class_param")
|
||
end
|
||
|
||
|
||
test 'puppetclass lookup keys are added to partial _class_paramters on EXISTING hostgroup form through ajax POST to parameters' do
|
||
hostgroup = hostgroups(:common)
|
||
puppetclass = puppetclasses(:two)
|
||
# host_id is posted instead of hostgroup_id per host_edit.js#load_puppet_class_parameters
|
||
post :parameters, {:id => puppetclass.id, :host_id => hostgroup.id, :hostgroup => hostgroup.attributes }, set_session_user
|
||
assert_response :success
|
||
lookup_keys_added = overridable_lookup_keys(puppetclass, hostgroup)
|
||
assert_equal 2, lookup_keys_added.count
|
||
assert lookup_keys_added.map(&:key).include?("special_info")
|
||
assert lookup_keys_added.map(&:key).include?("custom_class_param")
|
||
end
|
||
|
||
test 'puppetclass lookup keys are added to partial _class_paramters on NEW host form through ajax POST to parameters' do
|
||
host = Host::Managed.new(:name => "new_host", :environment_id => environments(:production).id)
|
||
puppetclass = puppetclasses(:two)
|
||
post :parameters, {:id => puppetclass.id, :host_id => 'null', :host => host.attributes }, set_session_user
|
||
assert_response :success
|
||
lookup_keys_added = overridable_lookup_keys(puppetclass, host)
|
||
assert_equal 2, lookup_keys_added.count
|
||
assert lookup_keys_added.map(&:key).include?("special_info")
|
||
assert lookup_keys_added.map(&:key).include?("custom_class_param")
|
||
end
|
||
|
||
test 'puppetclass lookup keys are added to partial _class_paramters on NEW hostgroup form through ajax POST to parameters' do
|
||
hostgroup = Hostgroup.new(:name => "new_hostgroup", :environment_id => environments(:production).id)
|
||
puppetclass = puppetclasses(:two)
|
||
# host_id is posted instead of hostgroup_id per host_edit.js#load_puppet_class_parameters
|
||
post :parameters, {:id => puppetclass.id, :host_id => 'null', :hostgroup => hostgroup.attributes }, set_session_user
|
||
assert_response :success
|
||
lookup_keys_added = overridable_lookup_keys(puppetclass, hostgroup)
|
||
assert_equal 2, lookup_keys_added.count
|
||
assert lookup_keys_added.map(&:key).include?("special_info")
|
||
assert lookup_keys_added.map(&:key).include?("custom_class_param")
|
||
end
|
||
|
||
end
|
Also available in: Unified diff
fixes #2788 - adding / removing puppet classes updates the change even if the form is not submitted