1 import { Component, ViewChild } from '@angular/core';
3 import { BlockUI, NgBlockUI } from 'ng-block-ui';
4 import { timer as observableTimer } from 'rxjs';
6 import { MgrModuleService } from '../../../../shared/api/mgr-module.service';
7 import { ListWithDetails } from '../../../../shared/classes/list-with-details.class';
8 import { TableComponent } from '../../../../shared/datatable/table/table.component';
9 import { CellTemplate } from '../../../../shared/enum/cell-template.enum';
10 import { Icons } from '../../../../shared/enum/icons.enum';
11 import { CdTableAction } from '../../../../shared/models/cd-table-action';
12 import { CdTableColumn } from '../../../../shared/models/cd-table-column';
13 import { CdTableFetchDataContext } from '../../../../shared/models/cd-table-fetch-data-context';
14 import { CdTableSelection } from '../../../../shared/models/cd-table-selection';
15 import { Permission } from '../../../../shared/models/permissions';
16 import { AuthStorageService } from '../../../../shared/services/auth-storage.service';
17 import { NotificationService } from '../../../../shared/services/notification.service';
20 selector: 'cd-mgr-module-list',
21 templateUrl: './mgr-module-list.component.html',
22 styleUrls: ['./mgr-module-list.component.scss']
24 export class MgrModuleListComponent extends ListWithDetails {
25 @ViewChild(TableComponent, { static: true })
26 table: TableComponent;
30 permission: Permission;
31 tableActions: CdTableAction[];
32 columns: CdTableColumn[] = [];
33 modules: object[] = [];
34 selection: CdTableSelection = new CdTableSelection();
37 private authStorageService: AuthStorageService,
38 private mgrModuleService: MgrModuleService,
39 private notificationService: NotificationService
42 this.permission = this.authStorageService.getPermissions().configOpt;
45 name: $localize`Name`,
50 name: $localize`Enabled`,
53 cellClass: 'text-center',
54 cellTransformation: CellTemplate.checkIcon
57 name: $localize`Always-On`,
61 cellClass: 'text-center',
62 cellTransformation: CellTemplate.checkIcon
65 const getModuleUri = () =>
66 this.selection.first() && encodeURIComponent(this.selection.first().name);
69 name: $localize`Edit`,
72 if (!this.selection.hasSelection) {
75 // Disable the 'edit' button when the module has no options.
76 return Object.values(this.selection.first().options).length === 0;
78 routerLink: () => `/mgr-modules/edit/${getModuleUri()}`,
82 name: $localize`Enable`,
84 click: () => this.updateModuleState(),
85 disable: () => this.isTableActionDisabled('enabled'),
89 name: $localize`Disable`,
91 click: () => this.updateModuleState(),
92 disable: () => () => this.getTableActionDisabledDesc(),
98 getModuleList(context: CdTableFetchDataContext) {
99 this.mgrModuleService.list().subscribe(
100 (resp: object[]) => {
109 updateSelection(selection: CdTableSelection) {
110 this.selection = selection;
114 * Check if the table action is disabled.
115 * @param state The expected module state, e.g. ``enabled`` or ``disabled``.
116 * @returns If the specified state is validated to true or no selection is
117 * done, then ``true`` is returned, otherwise ``false``.
119 isTableActionDisabled(state: 'enabled' | 'disabled') {
120 if (!this.selection.hasSelection) {
123 const selected = this.selection.first();
124 // Make sure the user can't modify the run state of the 'Dashboard' module.
125 // This check is only done in the UI because the REST API should still be
127 if (selected.name === 'dashboard') {
130 // Always-on modules can't be disabled.
131 if (selected.always_on) {
136 return selected.enabled;
138 return !selected.enabled;
142 getTableActionDisabledDesc(): string | boolean {
143 if (this.selection.first().always_on) {
144 return $localize`This Manager module is always on.`;
147 return this.isTableActionDisabled('disabled');
151 * Update the Ceph Mgr module state to enabled or disabled.
153 updateModuleState() {
154 if (!this.selection.hasSelection) {
159 const fnWaitUntilReconnected = () => {
160 observableTimer(2000).subscribe(() => {
161 // Trigger an API request to check if the connection is
163 this.mgrModuleService.list().subscribe(
165 // Resume showing the notification toasties.
166 this.notificationService.suspendToasties(false);
167 // Unblock the whole UI.
169 // Reload the data table content.
170 this.table.refreshBtn();
173 fnWaitUntilReconnected();
179 // Note, the Ceph Mgr is always restarted when a module
180 // is enabled/disabled.
181 const module = this.selection.first();
182 if (module.enabled) {
183 $obs = this.mgrModuleService.disable(module.name);
185 $obs = this.mgrModuleService.enable(module.name);
190 // Suspend showing the notification toasties.
191 this.notificationService.suspendToasties(true);
192 // Block the whole UI to prevent user interactions until
193 // the connection to the backend is reestablished
194 this.blockUI.start($localize`Reconnecting, please wait ...`);
195 fnWaitUntilReconnected();