Skip to content

Commit

Permalink
Fixes #36982 - display short hostname on REX job pages
Browse files Browse the repository at this point in the history
If the `display_fqdn_for_hosts` setting is set to false,
show the short hostname on REX job pages.
  • Loading branch information
nofaralfasi committed Jan 7, 2024
1 parent 9c0e2e4 commit d8f5135
Show file tree
Hide file tree
Showing 16 changed files with 56 additions and 28 deletions.
2 changes: 1 addition & 1 deletion app/helpers/job_invocations_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def preview_hosts(template_invocation)
end

def collapsed_preview(target)
title = target.try(:name) || 'N/A'
title = target.try(:to_label) || 'N/A'
content_tag(:h5,
:class => "expander collapsed out",
:data => { :toggle => 'collapse',
Expand Down
4 changes: 2 additions & 2 deletions app/helpers/remote_execution_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def template_invocation_actions(task, host, job_invocation, template_invocation)
action: { href: current_host_details_path(host), 'data-method': 'get', id: "#{host.name}-actions-detail" } }
end
if authorized_for(controller: :job_invocations, action: :create) && (!host.infrastructure_host? || User.current.can?(:execute_jobs_on_infrastructure_hosts))
links << { title: (_('Rerun on %s') % host.name),
links << { title: (_('Rerun on %s') % host),
action: { href: rerun_job_invocation_path(job_invocation, host_ids: [ host.id ]),
'data-method': 'get', id: "#{host.name}-actions-rerun" } }
end
Expand Down Expand Up @@ -274,7 +274,7 @@ def targeting_hosts(job_invocation, hosts)
task = template_invocation.try(:run_host_job_task)
link_authorized = !task.nil? && authorized_for(hash_for_template_invocation_path(:id => template_invocation).merge(:auth_object => host, :permission => :view_hosts, :authorizer => job_hosts_authorizer))

{ name: host.name,
{ name: host.to_label,
link: link_authorized ? template_invocation_path(:id => template_invocation) : '',
status: template_invocation_status(task, job_invocation.task),
actions: template_invocation_actions(task, host, job_invocation, template_invocation) }
Expand Down
2 changes: 1 addition & 1 deletion app/views/job_invocations/_preview_hosts_list.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<% if @hosts.any? -%>
<ul>
<% @hosts.each do |host| -%>
<li><%= link_to h(host.name), current_host_details_path(host), :target => '_blank' %></li>
<li><%= link_to h(host.to_label), current_host_details_path(host), :target => '_blank' %></li>
<% end -%>

<% if @additional > 0 -%>
Expand Down
4 changes: 2 additions & 2 deletions app/views/template_invocations/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
:url => job_invocation_path(@template_invocation.job_invocation_id) }]

if @host
items << { :caption => _('Template Invocation for %s') % @host.name }
items << { :caption => _('Template Invocation for %s') % @host }
breadcrumbs(:resource_url => template_invocations_api_job_invocation_path(@template_invocation.job_invocation_id),
:name_field => 'host_name',
:switcher_item_url => template_invocation_path(':id'),
Expand All @@ -30,7 +30,7 @@ end
<% if @host %>
<% proxy_id = @template_invocation_task.input[:proxy_id] %>
<h3>
<%= _('Target: ') %><%= link_to(@host.name, current_host_details_path(@host)) %>
<%= _('Target: ') %><%= link_to(@host, current_host_details_path(@host)) %>
<% if proxy_id && proxy = SmartProxy.find_by(id: proxy_id) %>
<%= _('using Smart Proxy') %> <%= link_to(proxy.name, smart_proxy_path(proxy)) %>
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion lib/foreman_remote_execution/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Engine < ::Rails::Engine

initializer 'foreman_remote_execution.register_plugin', before: :finisher_hook do |_app|
Foreman::Plugin.register :foreman_remote_execution do
requires_foreman '>= 3.9'
requires_foreman '>= 3.10'
register_global_js_file 'global'
register_gettext

Expand Down
9 changes: 7 additions & 2 deletions webpack/JobWizard/JobWizardSelectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,13 @@ export const selectTemplateInputs = state =>
export const selectHostCount = state =>
selectAPIResponse(state, HOSTS_API).subtotal || 0;

export const selectHosts = state =>
(selectAPIResponse(state, HOSTS_API).results || []).map(host => host.name);
export const selectHosts = state => {
const hosts = selectAPIResponse(state, HOSTS_API).results || [];
return hosts.map(host => ({
name: host.name,
display_name: host.display_name,
}));
};

export const selectIsLoadingHosts = state =>
!selectAPIStatus(state, HOSTS_API) ||
Expand Down
6 changes: 3 additions & 3 deletions webpack/JobWizard/__tests__/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,9 @@ export const gqlMock = [
hosts: {
totalCount: 3,
nodes: [
{ id: 'MDE6SG9zdC0x', name: 'host1' },
{ id: 'MDE6SG9zdC0y', name: 'host2' },
{ id: 'MDE6SG9zdC0z', name: 'host3' },
{ id: 'MDE6SG9zdC0x', name: 'host1', displayName: 'host1' },
{ id: 'MDE6SG9zdC0y', name: 'host2', displayName: 'host2' },
{ id: 'MDE6SG9zdC0z', name: 'host3', displayName: 'host3' },
],
},
},
Expand Down
12 changes: 8 additions & 4 deletions webpack/JobWizard/autofill.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,14 @@ export const useAutoFill = ({
handleSuccess: ({ data }) => {
setSelectedTargets(currentTargets => ({
...currentTargets,
hosts: (data.results || []).map(({ id, name }) => ({
id,
name,
})),
hosts: (data.results || []).map(
// eslint-disable-next-line camelcase
({ id, name, display_name }) => ({
id,
// eslint-disable-next-line camelcase
name: display_name || name,
})
),
}));
},
})
Expand Down
6 changes: 3 additions & 3 deletions webpack/JobWizard/steps/HostsAndInputs/HostPreviewModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ export const HostPreviewModal = ({ isOpen, setIsOpen, searchQuery }) => {
>
<List isPlain>
{hosts.map(host => (
<ListItem key={host}>
<ListItem key={host.name}>
<Button
component="a"
href={foremanUrl(`/hosts/${host}`)}
href={foremanUrl(`/new/hosts/${host.name}`)}
variant="link"
target="_blank"
rel="noreferrer"
isInline
>
{host}
{host.display_name}
</Button>
</ListItem>
))}
Expand Down
1 change: 1 addition & 0 deletions webpack/JobWizard/steps/HostsAndInputs/SelectGQL.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const useNameSearchGQL = apiKey => {
data?.[dataName[apiKey]]?.nodes.map(node => ({
id: decodeId(node.id),
name: node.name,
displayName: node.displayName,
})) || [],
},
loading,
Expand Down
19 changes: 13 additions & 6 deletions webpack/JobWizard/steps/HostsAndInputs/SelectedChips.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Chip, ChipGroup, Button } from '@patternfly/react-core';
import { sprintf, translate as __ } from 'foremanReact/common/I18n';
import { hostMethods } from '../../JobWizardConstants';

const SelectedChip = ({ selected, setSelected, categoryName }) => {
const SelectedChip = ({ selected, setSelected, categoryName, setLabel }) => {
const deleteItem = itemToRemove => {
setSelected(oldSelected =>
oldSelected.filter(({ id }) => id !== itemToRemove)
Expand All @@ -24,14 +24,14 @@ const SelectedChip = ({ selected, setSelected, categoryName }) => {
setSelected(() => []);
}}
>
{selected.map(({ name, id }, index) => (
{selected.map((result, index) => (
<Chip
key={index}
id={`${categoryName}-${id}`}
onClick={() => deleteItem(id)}
closeBtnAriaLabel={`Remove ${name}`}
id={`${categoryName}-${result.id}`}
onClick={() => deleteItem(result.id)}
closeBtnAriaLabel={`Remove ${result.name}`}
>
{name}
{setLabel(result)}
</Chip>
))}
</ChipGroup>
Expand All @@ -49,6 +49,7 @@ export const SelectedChips = ({
setSelectedHostGroups,
hostsSearchQuery,
clearSearch,
setLabel,
}) => {
const clearAll = () => {
setSelectedHosts(() => []);
Expand All @@ -67,16 +68,19 @@ export const SelectedChips = ({
selected={selectedHosts}
categoryName={hostMethods.hosts}
setSelected={setSelectedHosts}
setLabel={setLabel}
/>
<SelectedChip
selected={selectedHostCollections}
categoryName={hostMethods.hostCollections}
setSelected={setSelectedHostCollections}
setLabel={setLabel}
/>
<SelectedChip
selected={selectedHostGroups}
categoryName={hostMethods.hostGroups}
setSelected={setSelectedHostGroups}
setLabel={setLabel}
/>
<SelectedChip
selected={
Expand All @@ -86,6 +90,7 @@ export const SelectedChips = ({
}
categoryName={hostMethods.searchQuery}
setSelected={clearSearch}
setLabel={setLabel}
/>
{showClear && (
<Button variant="link" className="clear-chips" onClick={clearAll}>
Expand All @@ -105,10 +110,12 @@ SelectedChips.propTypes = {
setSelectedHostGroups: PropTypes.func.isRequired,
hostsSearchQuery: PropTypes.string.isRequired,
clearSearch: PropTypes.func.isRequired,
setLabel: PropTypes.func.isRequired,
};

SelectedChip.propTypes = {
categoryName: PropTypes.string.isRequired,
selected: PropTypes.array.isRequired,
setSelected: PropTypes.func.isRequired,
setLabel: PropTypes.func.isRequired,
};
1 change: 1 addition & 0 deletions webpack/JobWizard/steps/HostsAndInputs/hosts.gql
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ query($search: String!) {
nodes {
id
name
displayName
}
}
}
5 changes: 5 additions & 0 deletions webpack/JobWizard/steps/HostsAndInputs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ const HostsAndInputs = ({
const dispatch = useDispatch();

const selectedHosts = selected.hosts;
const setLabel = result => result.displayName || result.name;
const setSelectedHosts = newSelected =>
setSelected(prevSelected => ({
...prevSelected,
Expand Down Expand Up @@ -187,6 +188,7 @@ const HostsAndInputs = ({
apiKey={HOSTS}
name="hosts"
placeholderText={__('Filter by hosts')}
setLabel={setLabel}
/>
)}
{hostMethod === hostMethods.hostCollections && (
Expand All @@ -197,6 +199,7 @@ const HostsAndInputs = ({
name="host collections"
url="/katello/api/host_collections?per_page=100"
placeholderText={__('Filter by host collections')}
setLabel={setLabel}
/>
)}
{hostMethod === hostMethods.hostGroups && (
Expand All @@ -206,6 +209,7 @@ const HostsAndInputs = ({
apiKey={HOST_GROUPS}
name="host groups"
placeholderText={__('Filter by host groups')}
setLabel={setLabel}
/>
)}
</InputGroup>
Expand All @@ -219,6 +223,7 @@ const HostsAndInputs = ({
setSelectedHostGroups={setSelectedHostGroups}
hostsSearchQuery={hostsSearchQuery}
clearSearch={clearSearch}
setLabel={setLabel}
/>
<Text>
{__('Apply to')}{' '}
Expand Down
5 changes: 3 additions & 2 deletions webpack/JobWizard/steps/ReviewDetails/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,13 @@ const ReviewDetails = ({

const hostsCount = useSelector(selectHostCount);
const [hostPreviewOpen, setHostPreviewOpen] = useState(false);
const NUM_CHIPS = 3;
const stringHosts = () => {
if (hosts.length === 0) {
return __('No Target Hosts');
}
if (hosts.length === 1 || hosts.length === 2) {
return hosts.join(', ');
if (hosts.length < NUM_CHIPS) {
return hosts.map(host => host.display_name).join(', ');
}
return (
<div>
Expand Down
4 changes: 3 additions & 1 deletion webpack/JobWizard/steps/form/SearchSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const SearchSelect = ({
apiKey,
url,
variant,
setLabel,
}) => {
const [onSearch, response, isLoading] = useNameSearch(apiKey, url);
const [isOpen, setIsOpen] = useState(false);
Expand Down Expand Up @@ -48,7 +49,7 @@ export const SearchSelect = ({
...selectOptions,
...Immutable.asMutable(response?.results || [])?.map((result, index) => (
<SelectOption key={index + 1} value={result.id}>
{result.name}
{setLabel(result)}
</SelectOption>
)),
];
Expand Down Expand Up @@ -104,6 +105,7 @@ SearchSelect.propTypes = {
name: PropTypes.string,
selected: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
setSelected: PropTypes.func.isRequired,
setLabel: PropTypes.func.isRequired,
placeholderText: PropTypes.string,
apiKey: PropTypes.string.isRequired,
url: PropTypes.string,
Expand Down
2 changes: 2 additions & 0 deletions webpack/JobWizard/steps/form/__tests__/SelectSearch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const apiKey = 'HOSTS_KEY';
describe('SearchSelect', () => {
it('too many', () => {
const onSearch = jest.fn();
const setLabel = jest.fn();
render(
<SearchSelect
selected={['hosts1,host2']}
Expand All @@ -19,6 +20,7 @@ describe('SearchSelect', () => {
{ results: ['host1', 'host2', 'host3'], subtotal: 101 },
false,
]}
setLabel={setLabel}
/>
);
const openSelectbutton = screen.getByRole('button', {
Expand Down

0 comments on commit d8f5135

Please sign in to comment.