From 6bb5348bd4b6595a76afbacc888e693ac1892cea Mon Sep 17 00:00:00 2001 From: Volker Theile Date: Tue, 23 Jun 2020 12:19:54 +0200 Subject: [PATCH] mgr/dashboard: Bypass current selection when calling CdTableAction::disableDesc(). Signed-off-by: Volker Theile (cherry picked from commit a92baa8523279f663644837304f1e6c71787a63e) --- .../cluster/hosts/hosts.component.spec.ts | 28 ++++++++ .../app/ceph/cluster/hosts/hosts.component.ts | 69 ++++++++++++++++++- .../table-actions/table-actions.component.ts | 2 +- .../src/app/shared/models/cd-table-action.ts | 2 +- 4 files changed, 98 insertions(+), 3 deletions(-) diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/hosts/hosts.component.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/hosts/hosts.component.spec.ts index 69ed5df199647..5b16f2b41805f 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/hosts/hosts.component.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/hosts/hosts.component.spec.ts @@ -92,4 +92,32 @@ describe('HostsComponent', () => { expect(spans[0].textContent).toBe(hostname); }); })); + + describe('getEditDisableDesc', () => { + it('should return message (not managed by Orchestrator)', () => { + component.selection.add({ + sources: { + ceph: true, + orchestrator: false + } + }); + expect(component.getEditDisableDesc(component.selection)).toBe( + 'Host editing is disabled because the host is not managed by Orchestrator.' + ); + }); + + it('should return undefined (no selection)', () => { + expect(component.getEditDisableDesc(component.selection)).toBeUndefined(); + }); + + it('should return undefined (managed by Orchestrator)', () => { + component.selection.add({ + sources: { + ceph: false, + orchestrator: true + } + }); + expect(component.getEditDisableDesc(component.selection)).toBeUndefined(); + }); + }); }); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/hosts/hosts.component.ts b/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/hosts/hosts.component.ts index 91e9faa7f2cbe..50a742c5af912 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/hosts/hosts.component.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/hosts/hosts.component.ts @@ -73,6 +73,21 @@ export class HostsComponent extends ListWithDetails implements OnInit { ); } }, + { + name: this.actionLabels.EDIT, + permission: 'update', + icon: Icons.edit, + click: () => { + this.depCheckerService.checkOrchestratorOrModal( + this.actionLabels.EDIT, + this.i18n('Host'), + () => this.editAction() + ); + }, + disable: (selection: CdTableSelection) => + !selection.hasSingleSelection || !selection.first().sources.orchestrator, + disableDesc: this.getEditDisableDesc.bind(this) + }, { name: this.actionLabels.DELETE, permission: 'delete', @@ -121,7 +136,59 @@ export class HostsComponent extends ListWithDetails implements OnInit { this.selection = selection; } - deleteHostModal() { + editAction() { + this.hostService.getLabels().subscribe((resp: string[]) => { + const host = this.selection.first(); + const allLabels = resp.map((label) => { + return { enabled: true, name: label }; + }); + this.modalService.show(FormModalComponent, { + initialState: { + titleText: this.i18n('Edit Host: {{hostname}}', host), + fields: [ + { + type: 'select-badges', + name: 'labels', + value: host['labels'], + label: this.i18n('Labels'), + typeConfig: { + customBadges: true, + options: allLabels, + messages: new SelectMessages( + { + empty: this.i18n('There are no labels.'), + filter: this.i18n('Filter or add labels'), + add: this.i18n('Add label') + }, + this.i18n + ) + } + } + ], + submitButtonText: this.i18n('Edit Host'), + onSubmit: (values: any) => { + this.hostService.update(host['hostname'], values.labels).subscribe(() => { + this.notificationService.show( + NotificationType.success, + this.i18n('Updated Host "{{hostname}}"', host) + ); + // Reload the data table content. + this.table.refreshBtn(); + }); + } + } + }); + }); + } + + getEditDisableDesc(selection: CdTableSelection): string | undefined { + if (selection && selection.hasSingleSelection && !selection.first().sources.orchestrator) { + return this.i18n('Host editing is disabled because the host is not managed by Orchestrator.'); + } + return undefined; + } + + deleteAction() { const hostname = this.selection.first().hostname; this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, { initialState: { diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/datatable/table-actions/table-actions.component.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/datatable/table-actions/table-actions.component.ts index 57714dbae6b14..334f5563eeb5f 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/datatable/table-actions/table-actions.component.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/datatable/table-actions/table-actions.component.ts @@ -148,6 +148,6 @@ export class TableActionsComponent implements OnInit { } useDisableDesc(action: CdTableAction) { - return action.disableDesc && action.disableDesc(); + return action.disableDesc && action.disableDesc(this.selection); } } diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/models/cd-table-action.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/models/cd-table-action.ts index ac8dcb61a98d0..9af10625ae65b 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/models/cd-table-action.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/models/cd-table-action.ts @@ -29,7 +29,7 @@ export class CdTableAction { * disabled. The specified message will be shown to the user as a button * tooltip. */ - disableDesc?: Function; + disableDesc?: (_: CdTableSelection) => string | undefined; /** * Defines if the button can become 'primary' (displayed as button and not -- 2.39.5