Skip to content

Commit

Permalink
fixes #2360 loading parameters on host edit form when hostgroup or en…
Browse files Browse the repository at this point in the history
…vironment is changed
  • Loading branch information
Joseph Mitchell Magen authored and abenari committed Apr 22, 2013
1 parent d0a190b commit 8bfaec2
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 38 deletions.
9 changes: 5 additions & 4 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,18 +379,19 @@ $(function() {
});

function update_puppetclasses(element) {
var host_id = $(element).attr('data-host-id');
var host_id = $("form").data('id')
var env_id = $('*[id*=environment_id]').val();
var url = $(element).attr('data-url');
var hostgroup_id = $('*[id*=hostgroup_id]').val();
var data = $("form").serialize().replace('method=put', 'method=post');
data = data + '&host_id=' + host_id
if (env_id == "") return;
$.ajax({
type: 'post',
url: url,
data:'host_id=' + host_id + '&hostgroup_id=' + hostgroup_id + '&environment_id=' + env_id,
data: data,
success: function(request) {
$('#puppet_klasses').html(request);
reload_params();
reload_puppetclass_params();
$('[rel="twipsy"]').tooltip();
},
complete: function() {
Expand Down
19 changes: 14 additions & 5 deletions app/assets/javascripts/host_edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,12 @@ function remove_puppet_class(item){

function load_puppet_class_parameters(item) {
var id = $(item).attr('data-class-id');
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

var url = $(item).attr('data-url');
var data = $("form").serialize().replace('method=put', 'method=post');
data = data + '&host_id=' + host_id

if (url == undefined) return; // no parameters
var placeholder = $('<tr id="puppetclass_'+id+'_params_loading">'+
Expand All @@ -200,11 +201,12 @@ function load_puppet_class_parameters(item) {
}

function hostgroup_changed(element) {
var host_id = $(element).data('host-id');
var host_id = $("form").data('id')
if (!host_id){ // a new host
update_form(element);
} else { // edit host
update_puppetclasses(element);
reload_host_params();
}
}

Expand Down Expand Up @@ -289,7 +291,7 @@ function domain_selected(element){
complete: function(){$('#domain_indicator').hide()},
success: function(request) {
$('#subnet_select').html(request);
reload_params();
reload_host_params();
}
})
}
Expand All @@ -316,7 +318,7 @@ function os_selected(element){
url: url,
success: function(request) {
$('#media_select').html(request);
reload_params();
reload_host_params();
}
});
update_provisioning_image();
Expand Down Expand Up @@ -413,12 +415,19 @@ function override_class_param(item){
mark_params_override();
}

function reload_params(){
function reload_host_params(){
var host_id = $("form").data('id');
var url = $('#params-tab').data('url');
var data = $("[data-submit='progress_bar']").serialize().replace('method=put', 'method=post');
data = data + '&host_id=' + host_id;
load_with_placeholder('inherited_parameters', url, data)
}

function reload_puppetclass_params(){
var host_id = $("form").data('id');
var url2 = $('#params-tab').data('url2');
var data = $("[data-submit='progress_bar']").serialize().replace('method=put', 'method=post');
data = data + '&host_id=' + host_id
load_with_placeholder('inherited_puppetclasses_parameters', url2, data)
}

Expand Down
46 changes: 25 additions & 21 deletions app/controllers/hosts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class HostsController < ApplicationController

PUPPETMASTER_ACTIONS=[ :externalNodes, :lookup ]
SEARCHABLE_ACTIONS= %w[index active errors out_of_sync pending disabled ]
AJAX_REQUESTS=%w{compute_resource_selected hostgroup_or_environment_selected current_parameters}
AJAX_REQUESTS=%w{compute_resource_selected hostgroup_or_environment_selected current_parameters puppetclass_parameters}

add_puppetmaster_filters PUPPETMASTER_ACTIONS
before_filter :ajax_request, :only => AJAX_REQUESTS
Expand Down Expand Up @@ -128,23 +128,23 @@ def compute_resource_selected
end

def hostgroup_or_environment_selected
return head(:method_not_allowed) unless request.xhr?

Taxonomy.as_taxonomy @organization, @location do
@environment = Environment.find(params[:environment_id]) unless params[:environment_id].empty?
@hostgroup = Hostgroup.find(params[:hostgroup_id]) unless params[:hostgroup_id].empty?
@host = Host.find(params[:host_id]) if params[:host_id].to_i > 0
if @environment or @hostgroup
@host ||= Host.new
@host.hostgroup = @hostgroup if @hostgroup
@host.environment = @environment if @environment
render :partial => 'puppetclasses/class_selection', :locals => {:obj => (@host)}
if params['host']['environment_id'].present? || params['host']['hostgroup_id'].present?
render :partial => 'puppetclasses/class_selection', :locals => {:obj => (refresh_host)}
else
head(:not_found)
logger.info "environment_id or hostgroup_id is required to render puppetclasses"
end
end
end

def current_parameters
render :partial => "common_parameters/inherited_parameters", :locals => {:inherited_parameters => refresh_host.host_inherited_params(true)}
end

def puppetclass_parameters
render :partial => "puppetclasses/classes_parameters", :locals => { :obj => refresh_host}
end

#returns a yaml file ready to use for puppet external nodes script
#expected a fqdn parameter to provide hostname to lookup
#see example script in extras directory
Expand Down Expand Up @@ -456,18 +456,22 @@ def template_used
render :partial => "provisioning", :locals => {:templates => templates}
end

def current_parameters
@host = Host.new params['host']
render :partial => "common_parameters/inherited_parameters", :locals => {:inherited_parameters => @host.host_inherited_params(true)}
end
private

def puppetclass_parameters
@host = Host.new params['host']
render :partial => "puppetclasses/classes_parameters", :locals => { :obj => @host}
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
return @host
end

private

def set_host_type
return unless params[:host] and params[:host][:type]
type = params[:host].delete(:type) #important, otherwise mass assignment will save the type.
Expand Down
30 changes: 23 additions & 7 deletions app/controllers/puppetclasses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,6 @@ def create
def edit
end

# form AJAX methods
def parameters
puppetclass = Puppetclass.find(params[:id])
host = Host.new(params[:host])
render :partial => "puppetclasses/class_parameters", :locals => {:klass => puppetclass, :host => host}
end

def update
if @puppetclass.update_attributes(params[:puppetclass])
notice "Successfully updated puppetclass."
Expand All @@ -66,4 +59,27 @@ def destroy
redirect_to puppetclasses_url
end

# form AJAX methods
def parameters
puppetclass = Puppetclass.find(params[:id])
render :partial => "puppetclasses/class_parameters", :locals => {:klass => puppetclass, :host => refresh_host}
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
return @host
end


end
2 changes: 1 addition & 1 deletion app/views/hosts/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<%= javascript 'host_edit', 'compute_resource', 'lookup_keys'%>
<%= render "hosts/conflicts" if has_conflicts?(@host.errors) %>
<%= render "hosts/progress" %>
<%= form_for @host, :html => {:'data-submit' => 'progress_bar'} do |f| %>
<%= form_for @host, :html => {:data => {:id => @host.try(:id), :submit => 'progress_bar'}} do |f| %>
<%= base_errors_for @host %>

<ul class="nav nav-tabs" data-tabs="tabs">
Expand Down

0 comments on commit 8bfaec2

Please sign in to comment.