From 75fcad5964fd430145f7f5c9b58603ce1a5b9444 Mon Sep 17 00:00:00 2001 From: MariaAga Date: Thu, 29 Aug 2024 12:17:07 +0100 Subject: [PATCH] Fixes #37714 - inherit overrides deploy on in hostgroup --- app/assets/javascripts/host_edit.js | 49 +++++++++++++++++++++++- app/controllers/hostgroups_controller.rb | 23 ++++++----- app/helpers/hostgroups_helper.rb | 7 ++++ app/views/hostgroups/_form.html.erb | 12 +++++- 4 files changed, 78 insertions(+), 13 deletions(-) diff --git a/app/assets/javascripts/host_edit.js b/app/assets/javascripts/host_edit.js index 742a5ea7ad8..7c2f18ab8b6 100644 --- a/app/assets/javascripts/host_edit.js +++ b/app/assets/javascripts/host_edit.js @@ -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')) { @@ -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) { @@ -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'); @@ -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) { diff --git a/app/controllers/hostgroups_controller.rb b/app/controllers/hostgroups_controller.rb index 1bbc7dd2497..ca27a0e46b5 100644 --- a/app/controllers/hostgroups_controller.rb +++ b/app/controllers/hostgroups_controller.rb @@ -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 @@ -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 @@ -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 diff --git a/app/helpers/hostgroups_helper.rb b/app/helpers/hostgroups_helper.rb index 9fdddc637ba..46719ff505b 100644 --- a/app/helpers/hostgroups_helper.rb +++ b/app/helpers/hostgroups_helper.rb @@ -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 diff --git a/app/views/hostgroups/_form.html.erb b/app/views/hostgroups/_form.html.erb index 83b83a069f0..5d17d7b533b 100644 --- a/app/views/hostgroups/_form.html.erb +++ b/app/views/hostgroups/_form.html.erb @@ -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 } %>