1 import { Component, ViewChild } from '@angular/core';
3 import { I18n } from '@ngx-translate/i18n-polyfill';
4 import { BlockUI, NgBlockUI } from 'ng-block-ui';
5 import { timer as observableTimer } from 'rxjs';
7 import { MgrModuleService } from '../../../../shared/api/mgr-module.service';
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 {
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: this.i18n('Name'),
50 name: this.i18n('Enabled'),
53 cellClass: 'text-center',
54 cellTransformation: CellTemplate.checkIcon
57 const getModuleUri = () =>
58 this.selection.first() && encodeURIComponent(this.selection.first().name);
61 name: this.i18n('Edit'),
64 if (!this.selection.hasSelection) {
67 // Disable the 'edit' button when the module has no options.
68 return Object.values(this.selection.first().options).length === 0;
70 routerLink: () => `/mgr-modules/edit/${getModuleUri()}`,
74 name: this.i18n('Enable'),
76 click: () => this.updateModuleState(),
77 disable: () => this.isTableActionDisabled('enabled'),
81 name: this.i18n('Disable'),
83 click: () => this.updateModuleState(),
84 disable: () => this.isTableActionDisabled('disabled'),
90 getModuleList(context: CdTableFetchDataContext) {
91 this.mgrModuleService.list().subscribe(
101 updateSelection(selection: CdTableSelection) {
102 this.selection = selection;
106 * Check if the table action is disabled.
107 * @param state The expected module state, e.g. ``enabled`` or ``disabled``.
108 * @returns If the specified state is validated to true or no selection is
109 * done, then ``true`` is returned, otherwise ``false``.
111 isTableActionDisabled(state: 'enabled' | 'disabled') {
112 if (!this.selection.hasSelection) {
115 // Make sure the user can't modify the run state of the 'Dashboard' module.
116 // This check is only done in the UI because the REST API should still be
118 if (this.selection.first().name === 'dashboard') {
123 return this.selection.first().enabled;
125 return !this.selection.first().enabled;
130 * Update the Ceph Mgr module state to enabled or disabled.
132 updateModuleState() {
133 if (!this.selection.hasSelection) {
138 const fnWaitUntilReconnected = () => {
139 observableTimer(2000).subscribe(() => {
140 // Trigger an API request to check if the connection is
142 this.mgrModuleService.list().subscribe(
144 // Resume showing the notification toasties.
145 this.notificationService.suspendToasties(false);
146 // Unblock the whole UI.
148 // Reload the data table content.
149 this.table.refreshBtn();
152 fnWaitUntilReconnected();
158 // Note, the Ceph Mgr is always restarted when a module
159 // is enabled/disabled.
160 const module = this.selection.first();
161 if (module.enabled) {
162 $obs = this.mgrModuleService.disable(module.name);
164 $obs = this.mgrModuleService.enable(module.name);
169 // Suspend showing the notification toasties.
170 this.notificationService.suspendToasties(true);
171 // Block the whole UI to prevent user interactions until
172 // the connection to the backend is reestablished
173 this.blockUI.start(this.i18n('Reconnecting, please wait ...'));
174 fnWaitUntilReconnected();