]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
20cbfc5b9cb7c9fa54e479a2c5711f57c250d8eb
[ceph-ci.git] /
1 import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core';
2
3 import { I18n } from '@ngx-translate/i18n-polyfill';
4 import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal';
5 import { Subscription } from 'rxjs';
6
7 import { IscsiService } from '../../../shared/api/iscsi.service';
8 import { CriticalConfirmationModalComponent } from '../../../shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
9 import { ActionLabelsI18n } from '../../../shared/constants/app.constants';
10 import { TableComponent } from '../../../shared/datatable/table/table.component';
11 import { CellTemplate } from '../../../shared/enum/cell-template.enum';
12 import { Icons } from '../../../shared/enum/icons.enum';
13 import { CdTableAction } from '../../../shared/models/cd-table-action';
14 import { CdTableColumn } from '../../../shared/models/cd-table-column';
15 import { CdTableSelection } from '../../../shared/models/cd-table-selection';
16 import { FinishedTask } from '../../../shared/models/finished-task';
17 import { Permissions } from '../../../shared/models/permissions';
18 import { CephReleaseNamePipe } from '../../../shared/pipes/ceph-release-name.pipe';
19 import { AuthStorageService } from '../../../shared/services/auth-storage.service';
20 import { SummaryService } from '../../../shared/services/summary.service';
21 import { TaskListService } from '../../../shared/services/task-list.service';
22 import { TaskWrapperService } from '../../../shared/services/task-wrapper.service';
23 import { IscsiTargetDiscoveryModalComponent } from '../iscsi-target-discovery-modal/iscsi-target-discovery-modal.component';
24
25 @Component({
26   selector: 'cd-iscsi-target-list',
27   templateUrl: './iscsi-target-list.component.html',
28   styleUrls: ['./iscsi-target-list.component.scss'],
29   providers: [TaskListService]
30 })
31 export class IscsiTargetListComponent implements OnInit, OnDestroy {
32   @ViewChild(TableComponent)
33   table: TableComponent;
34
35   available: boolean = undefined;
36   columns: CdTableColumn[];
37   docsUrl: string;
38   modalRef: BsModalRef;
39   permissions: Permissions;
40   selection = new CdTableSelection();
41   settings: any;
42   status: string;
43   summaryDataSubscription: Subscription;
44   tableActions: CdTableAction[];
45   targets = [];
46   icons = Icons;
47
48   builders = {
49     'iscsi/target/create': (metadata) => {
50       return {
51         target_iqn: metadata['target_iqn']
52       };
53     }
54   };
55
56   constructor(
57     private authStorageService: AuthStorageService,
58     private i18n: I18n,
59     private iscsiService: IscsiService,
60     private taskListService: TaskListService,
61     private cephReleaseNamePipe: CephReleaseNamePipe,
62     private summaryservice: SummaryService,
63     private modalService: BsModalService,
64     private taskWrapper: TaskWrapperService,
65     public actionLabels: ActionLabelsI18n
66   ) {
67     this.permissions = this.authStorageService.getPermissions();
68
69     this.tableActions = [
70       {
71         permission: 'create',
72         icon: Icons.add,
73         routerLink: () => '/block/iscsi/targets/create',
74         name: this.actionLabels.CREATE
75       },
76       {
77         permission: 'update',
78         icon: Icons.edit,
79         routerLink: () => `/block/iscsi/targets/edit/${this.selection.first().target_iqn}`,
80         name: this.actionLabels.EDIT
81       },
82       {
83         permission: 'delete',
84         icon: Icons.destroy,
85         click: () => this.deleteIscsiTargetModal(),
86         name: this.actionLabels.DELETE
87       }
88     ];
89   }
90
91   ngOnInit() {
92     this.columns = [
93       {
94         name: this.i18n('Target'),
95         prop: 'target_iqn',
96         flexGrow: 2,
97         cellTransformation: CellTemplate.executing
98       },
99       {
100         name: this.i18n('Portals'),
101         prop: 'cdPortals',
102         flexGrow: 2
103       },
104       {
105         name: this.i18n('Images'),
106         prop: 'cdImages',
107         flexGrow: 2
108       },
109       {
110         name: this.i18n('# Sessions'),
111         prop: 'info.num_sessions',
112         flexGrow: 1
113       }
114     ];
115
116     this.iscsiService.status().subscribe((result: any) => {
117       this.available = result.available;
118
119       if (result.available) {
120         this.taskListService.init(
121           () => this.iscsiService.listTargets(),
122           (resp) => this.prepareResponse(resp),
123           (targets) => (this.targets = targets),
124           () => this.onFetchError(),
125           this.taskFilter,
126           this.itemFilter,
127           this.builders
128         );
129
130         this.iscsiService.settings().subscribe((settings: any) => {
131           this.settings = settings;
132         });
133       } else {
134         const summary = this.summaryservice.getCurrentSummary();
135         const releaseName = this.cephReleaseNamePipe.transform(summary.version);
136         this.docsUrl = `http://docs.ceph.com/docs/${releaseName}/mgr/dashboard/#enabling-iscsi-management`;
137         this.status = result.message;
138       }
139     });
140   }
141
142   ngOnDestroy() {
143     if (this.summaryDataSubscription) {
144       this.summaryDataSubscription.unsubscribe();
145     }
146   }
147
148   prepareResponse(resp: any): any[] {
149     resp.forEach((element) => {
150       element.cdPortals = element.portals.map((portal) => `${portal.host}:${portal.ip}`);
151       element.cdImages = element.disks.map((disk) => `${disk.pool}/${disk.image}`);
152     });
153
154     return resp;
155   }
156
157   onFetchError() {
158     this.table.reset(); // Disable loading indicator.
159   }
160
161   itemFilter(entry, task) {
162     return entry.target_iqn === task.metadata['target_iqn'];
163   }
164
165   taskFilter(task) {
166     return ['iscsi/target/create', 'iscsi/target/edit', 'iscsi/target/delete'].includes(task.name);
167   }
168
169   updateSelection(selection: CdTableSelection) {
170     this.selection = selection;
171   }
172
173   deleteIscsiTargetModal() {
174     const target_iqn = this.selection.first().target_iqn;
175
176     this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
177       initialState: {
178         itemDescription: this.i18n('iSCSI'),
179         submitActionObservable: () =>
180           this.taskWrapper.wrapTaskAroundCall({
181             task: new FinishedTask('iscsi/target/delete', {
182               target_iqn: target_iqn
183             }),
184             call: this.iscsiService.deleteTarget(target_iqn)
185           })
186       }
187     });
188   }
189
190   configureDiscoveryAuth() {
191     this.modalService.show(IscsiTargetDiscoveryModalComponent, {});
192   }
193 }