]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
b0f4a9fefab1ebc260354178ec1ee4cacfc63874
[ceph.git] /
1 import { Component, ViewChild } from '@angular/core';
2
3 import { BlockUI, NgBlockUI } from 'ng-block-ui';
4 import { timer as observableTimer } from 'rxjs';
5
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';
18
19 @Component({
20   selector: 'cd-mgr-module-list',
21   templateUrl: './mgr-module-list.component.html',
22   styleUrls: ['./mgr-module-list.component.scss']
23 })
24 export class MgrModuleListComponent extends ListWithDetails {
25   @ViewChild(TableComponent, { static: true })
26   table: TableComponent;
27   @BlockUI()
28   blockUI: NgBlockUI;
29
30   permission: Permission;
31   tableActions: CdTableAction[];
32   columns: CdTableColumn[] = [];
33   modules: object[] = [];
34   selection: CdTableSelection = new CdTableSelection();
35
36   constructor(
37     private authStorageService: AuthStorageService,
38     private mgrModuleService: MgrModuleService,
39     private notificationService: NotificationService
40   ) {
41     super();
42     this.permission = this.authStorageService.getPermissions().configOpt;
43     this.columns = [
44       {
45         name: $localize`Name`,
46         prop: 'name',
47         flexGrow: 1
48       },
49       {
50         name: $localize`Enabled`,
51         prop: 'enabled',
52         flexGrow: 1,
53         cellClass: 'text-center',
54         cellTransformation: CellTemplate.checkIcon
55       },
56       {
57         name: $localize`Always-On`,
58         prop: 'always_on',
59         isHidden: true,
60         flexGrow: 1,
61         cellClass: 'text-center',
62         cellTransformation: CellTemplate.checkIcon
63       }
64     ];
65     const getModuleUri = () =>
66       this.selection.first() && encodeURIComponent(this.selection.first().name);
67     this.tableActions = [
68       {
69         name: $localize`Edit`,
70         permission: 'update',
71         disable: () => {
72           if (!this.selection.hasSelection) {
73             return true;
74           }
75           // Disable the 'edit' button when the module has no options.
76           return Object.values(this.selection.first().options).length === 0;
77         },
78         routerLink: () => `/mgr-modules/edit/${getModuleUri()}`,
79         icon: Icons.edit
80       },
81       {
82         name: $localize`Enable`,
83         permission: 'update',
84         click: () => this.updateModuleState(),
85         disable: () => this.isTableActionDisabled('enabled'),
86         icon: Icons.start
87       },
88       {
89         name: $localize`Disable`,
90         permission: 'update',
91         click: () => this.updateModuleState(),
92         disable: () => () => this.getTableActionDisabledDesc(),
93         icon: Icons.stop
94       }
95     ];
96   }
97
98   getModuleList(context: CdTableFetchDataContext) {
99     this.mgrModuleService.list().subscribe(
100       (resp: object[]) => {
101         this.modules = resp;
102       },
103       () => {
104         context.error();
105       }
106     );
107   }
108
109   updateSelection(selection: CdTableSelection) {
110     this.selection = selection;
111   }
112
113   /**
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``.
118    */
119   isTableActionDisabled(state: 'enabled' | 'disabled') {
120     if (!this.selection.hasSelection) {
121       return true;
122     }
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
126     // able to do so.
127     if (selected.name === 'dashboard') {
128       return true;
129     }
130     // Always-on modules can't be disabled.
131     if (selected.always_on) {
132       return true;
133     }
134     switch (state) {
135       case 'enabled':
136         return selected.enabled;
137       case 'disabled':
138         return !selected.enabled;
139     }
140   }
141
142   getTableActionDisabledDesc(): string | boolean {
143     if (this.selection.first().always_on) {
144       return $localize`This Manager module is always on.`;
145     }
146
147     return this.isTableActionDisabled('disabled');
148   }
149
150   /**
151    * Update the Ceph Mgr module state to enabled or disabled.
152    */
153   updateModuleState() {
154     if (!this.selection.hasSelection) {
155       return;
156     }
157
158     let $obs;
159     const fnWaitUntilReconnected = () => {
160       observableTimer(2000).subscribe(() => {
161         // Trigger an API request to check if the connection is
162         // re-established.
163         this.mgrModuleService.list().subscribe(
164           () => {
165             // Resume showing the notification toasties.
166             this.notificationService.suspendToasties(false);
167             // Unblock the whole UI.
168             this.blockUI.stop();
169             // Reload the data table content.
170             this.table.refreshBtn();
171           },
172           () => {
173             fnWaitUntilReconnected();
174           }
175         );
176       });
177     };
178
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);
184     } else {
185       $obs = this.mgrModuleService.enable(module.name);
186     }
187     $obs.subscribe(
188       () => undefined,
189       () => {
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();
196       }
197     );
198   }
199 }