Skip to content

Commit

Permalink
Fixes #37714 - inherit overrides deploy on in hostgroup
Browse files Browse the repository at this point in the history
  • Loading branch information
MariaAga committed Aug 22, 2024
1 parent f824a88 commit bc47f1b
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 13 deletions.
49 changes: 48 additions & 1 deletion app/assets/javascripts/host_edit.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//= require parameter_override

var compute_resource_id = null;
$(document).ready(function() {
var searchParams = new URLSearchParams(window.location.search);
if(searchParams.has('hostgroup_id')) {
Expand All @@ -9,6 +9,32 @@ $(document).ready(function() {
});
$(document).on('ContentLoad', function() {
onHostEditLoad();
document
.querySelector('[name=is_overridden_btn]')
.addEventListener('click', function(event) {
const item = event.target;
var formControl = $(item)
.closest('.input-group')
.find('.form-control');
const itemId = formControl.attr('id');
if (itemId.includes('compute_resource_id')) {
const select2Id = itemId.replace('s2id_', '');
const isDisabled = formControl.attr('disabled');
if (isDisabled) {
$(`#${select2Id}`)
.val(compute_resource_id)
.trigger('change');
}
}
});
if (window.location.href.includes('hostgroup')) {
document.querySelector('form').addEventListener('submit', function() {
// making sure inherited compute resource is included in the form
const hostgroup_compute_resource_id = $('#hostgroup_compute_resource_id');
hostgroup_compute_resource_id.prop('disabled', false);
});
}
update_default_compute_resource($('.hostgroup-select').val());
});
$(document)
.on('change', '.hostgroup-select', function(evt) {
Expand Down Expand Up @@ -246,6 +272,24 @@ function update_progress(data) {
$('#tasks_progress').replaceWith(data);
}

function update_default_compute_resource(hostgroup_id) {
if(hostgroup_id) {
tfm.tools.showSpinner();
$.ajax({
type: 'get',
url: '/api/hostgroups/' + hostgroup_id,
complete: function() {
tfm.tools.hideSpinner();
},
success: function(response) {
compute_resource_id = response['compute_resource_id'];
}});
}
else {
compute_resource_id = null
}
}

function hostgroup_changed(element) {
var host_id = $('form').data('id');
var host_changed = $('form').data('type-changed');
Expand All @@ -255,6 +299,9 @@ function hostgroup_changed(element) {
// a new host
handleHostgroupChangedNew(element);
}
const hostgroup_id = element.value;
update_default_compute_resource(hostgroup_id);

}

function handleHostgroupChangeEdit(element, host_id, host_changed) {
Expand Down
23 changes: 13 additions & 10 deletions app/controllers/hostgroups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def nest
@hostgroup = Hostgroup.new(:parent_id => @parent.id)

load_vars_for_ajax
@hostgroup.compute_resource_id = @parent.compute_resource_id
@hostgroup.locations = @parent.locations
@hostgroup.organizations = @parent.organizations
# Clone any parameters as well
Expand Down Expand Up @@ -101,11 +102,12 @@ def csv_columns
def load_vars_for_ajax
return unless @hostgroup.present?

@architecture = @hostgroup.architecture
@operatingsystem = @hostgroup.operatingsystem
@domain = @hostgroup.domain
@subnet = @hostgroup.subnet
@realm = @hostgroup.realm
@compute_resource_id = @hostgroup.compute_resource_id
@architecture = @hostgroup.architecture
@operatingsystem = @hostgroup.operatingsystem
@domain = @hostgroup.domain
@subnet = @hostgroup.subnet
@realm = @hostgroup.realm
end

def users_in_ancestors
Expand Down Expand Up @@ -144,11 +146,12 @@ def refresh_hostgroup
def inherit_parent_attributes
return unless @parent.present?

@hostgroup.architecture ||= @parent.architecture
@hostgroup.operatingsystem ||= @parent.operatingsystem
@hostgroup.domain ||= @parent.domain
@hostgroup.subnet ||= @parent.subnet
@hostgroup.realm ||= @parent.realm
@hostgroup.compute_resource_id ||= @parent.compute_resource_id
@hostgroup.architecture ||= @parent.architecture
@hostgroup.operatingsystem ||= @parent.operatingsystem
@hostgroup.domain ||= @parent.domain
@hostgroup.subnet ||= @parent.subnet
@hostgroup.realm ||= @parent.realm
end

def reset_explicit_attributes
Expand Down
7 changes: 7 additions & 0 deletions app/helpers/hostgroups_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,11 @@ def prioritized_members(list, value_key)
sort_by { |member| member[:priority] }.
map { |member_hash| member_hash[value_key] }
end

def hostgroup_inherited_by_default?(field, hostgroup)
return false if hostgroup.ancestry.nil?
return false if params[:action] == 'clone'
return true unless params[:hostgroup]
!params[:hostgroup][field]
end
end
12 changes: 10 additions & 2 deletions app/views/hostgroups/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,16 @@
<%= text_f f, :name %>
<%= textarea_f f, :description, :rows => :auto %>
<%= select_f f, :compute_resource_id, accessible_resource(@hostgroup, :compute_resource), :id, :to_label,
{ :include_blank => blank_or_inherit_f(f, :compute_resource, blank_value: _('Bare Metal')) },
<%= select_f f, :compute_resource_id,
accessible_resource(@hostgroup, :compute_resource),
:id,
:to_label,
{
:disable_button_enabled => hostgroup_inherited_by_default?(:compute_resource_id, @hostgroup),
:disable_button => _(HostsAndHostgroupsHelper::INHERIT_TEXT),
:user_set => user_set?(:compute_resource_id),
:include_blank => _('Bare Metal'),
},
{ :label => _('Deploy on'),
:help_inline => :indicator } %>
Expand Down

0 comments on commit bc47f1b

Please sign in to comment.