diff --git a/ui/desktop/app/routes/scopes/scope/projects/targets/target.js b/ui/desktop/app/routes/scopes/scope/projects/targets/target.js index 22f6057c9..19eeded64 100644 --- a/ui/desktop/app/routes/scopes/scope/projects/targets/target.js +++ b/ui/desktop/app/routes/scopes/scope/projects/targets/target.js @@ -23,19 +23,46 @@ export default class ScopesScopeProjectsTargetsTargetRoute extends Route { * Load a target * @param {object} params * @param {string} params.target_id - * @param {bool} params.isConnecting - * @return {TargetModel} + * @return {{ target: TargetModel, hosts: [HostModel] }} */ - async model({ target_id, isConnecting }) { + async model({ target_id }) { + let hosts = []; + const target = await this.store.findRecord('target', target_id, { reload: true, }); - if (isConnecting) { - await this.preConnect(target); + if (target.host_sources) { + const hostSets = await Promise.all( + target.host_sources.map(({ host_source_id }) => + this.store.findRecord('host-set', host_source_id) + ) + ); + + // Extract host ids from all host sets + const hostIds = hostSets.flatMap(({ host_ids }) => host_ids); + + // Load unique hosts + const uniqueHostIds = new Set(hostIds); + + hosts = await Promise.all( + [...uniqueHostIds].map((hostId) => + this.store.findRecord('host', hostId) + ) + ); } - return target; + return { target, hosts }; + } + + async afterModel(model, transition) { + const { isConnecting } = transition.to.queryParams; + + if (isConnecting) { + if (model.target.address || model.hosts.length === 1) { + await this.connect(model.target); + } + } } /** @@ -44,9 +71,9 @@ export default class ScopesScopeProjectsTargetsTargetRoute extends Route { */ @action @loading - async preConnect(target) { - if (target.address) { - await this.connect(target); + async preConnect(model) { + if (model.target.address || model.hosts.length === 1) { + await this.connect(model.target); } } diff --git a/ui/desktop/app/templates/scopes/scope/projects/targets/target.hbs b/ui/desktop/app/templates/scopes/scope/projects/targets/target.hbs index a23662719..d9df72b12 100644 --- a/ui/desktop/app/templates/scopes/scope/projects/targets/target.hbs +++ b/ui/desktop/app/templates/scopes/scope/projects/targets/target.hbs @@ -3,14 +3,14 @@ SPDX-License-Identifier: MPL-2.0 }} -{{page-title @model.displayName}} +{{page-title @model.target.displayName}}

- {{@model.displayName}} + {{@model.target.displayName}}

@@ -48,21 +48,21 @@ {{! Target name }} - {{@model.name}} + {{@model.target.name}} {{! Project name }} - {{@model.project.displayName}} + {{@model.target.project.displayName}} {{! Target description }} - {{#if @model.description}} + {{#if @model.target.description}} - {{@model.description}} + {{@model.target.description}} {{/if}} {{! Target type }} - {{#if @model.isSSH}} + {{#if @model.target.isSSH}} {{t 'resources.target.types.ssh'}} {{else}} {{t 'resources.target.types.tcp'}} @@ -71,11 +71,11 @@ {{! Target ID }} - {{@model.id}} + {{@model.target.id}}