]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
1d7559f7285780dcea63702465d39ef5268b27d8
[ceph-ci.git] /
1 import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core';
2
3 import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
4 import * as _ from 'lodash';
5 import { Subscription } from 'rxjs';
6
7 import { IscsiService } from '../../../shared/api/iscsi.service';
8 import { ListWithDetails } from '../../../shared/classes/list-with-details.class';
9 import { CriticalConfirmationModalComponent } from '../../../shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
10 import { ActionLabelsI18n } from '../../../shared/constants/app.constants';
11 import { TableComponent } from '../../../shared/datatable/table/table.component';
12 import { CellTemplate } from '../../../shared/enum/cell-template.enum';
13 import { Icons } from '../../../shared/enum/icons.enum';
14 import { CdTableAction } from '../../../shared/models/cd-table-action';
15 import { CdTableColumn } from '../../../shared/models/cd-table-column';
16 import { CdTableSelection } from '../../../shared/models/cd-table-selection';
17 import { FinishedTask } from '../../../shared/models/finished-task';
18 import { Permission } from '../../../shared/models/permissions';
19 import { Task } from '../../../shared/models/task';
20 import { JoinPipe } from '../../../shared/pipes/join.pipe';
21 import { NotAvailablePipe } from '../../../shared/pipes/not-available.pipe';
22 import { AuthStorageService } from '../../../shared/services/auth-storage.service';
23 import { ModalService } from '../../../shared/services/modal.service';
24 import { TaskListService } from '../../../shared/services/task-list.service';
25 import { TaskWrapperService } from '../../../shared/services/task-wrapper.service';
26 import { IscsiTargetDiscoveryModalComponent } from '../iscsi-target-discovery-modal/iscsi-target-discovery-modal.component';
27
28 @Component({
29   selector: 'cd-iscsi-target-list',
30   templateUrl: './iscsi-target-list.component.html',
31   styleUrls: ['./iscsi-target-list.component.scss'],
32   providers: [TaskListService]
33 })
34 export class IscsiTargetListComponent extends ListWithDetails implements OnInit, OnDestroy {
35   @ViewChild(TableComponent)
36   table: TableComponent;
37
38   available: boolean = undefined;
39   columns: CdTableColumn[];
40   modalRef: NgbModalRef;
41   permission: Permission;
42   selection = new CdTableSelection();
43   cephIscsiConfigVersion: number;
44   settings: any;
45   status: string;
46   summaryDataSubscription: Subscription;
47   tableActions: CdTableAction[];
48   targets: any[] = [];
49   icons = Icons;
50
51   builders = {
52     'iscsi/target/create': (metadata: object) => {
53       return {
54         target_iqn: metadata['target_iqn']
55       };
56     }
57   };
58
59   constructor(
60     private authStorageService: AuthStorageService,
61     private iscsiService: IscsiService,
62     private joinPipe: JoinPipe,
63     private taskListService: TaskListService,
64     private notAvailablePipe: NotAvailablePipe,
65     private modalService: ModalService,
66     private taskWrapper: TaskWrapperService,
67     public actionLabels: ActionLabelsI18n
68   ) {
69     super();
70     this.permission = this.authStorageService.getPermissions().iscsi;
71
72     this.tableActions = [
73       {
74         permission: 'create',
75         icon: Icons.add,
76         routerLink: () => '/block/iscsi/targets/create',
77         name: this.actionLabels.CREATE
78       },
79       {
80         permission: 'update',
81         icon: Icons.edit,
82         routerLink: () => `/block/iscsi/targets/edit/${this.selection.first().target_iqn}`,
83         name: this.actionLabels.EDIT,
84         disable: () => this.getEditDisableDesc()
85       },
86       {
87         permission: 'delete',
88         icon: Icons.destroy,
89         click: () => this.deleteIscsiTargetModal(),
90         name: this.actionLabels.DELETE,
91         disable: () => this.getDeleteDisableDesc()
92       }
93     ];
94   }
95
96   ngOnInit() {
97     this.columns = [
98       {
99         name: $localize`Target`,
100         prop: 'target_iqn',
101         flexGrow: 2,
102         cellTransformation: CellTemplate.executing
103       },
104       {
105         name: $localize`Portals`,
106         prop: 'cdPortals',
107         pipe: this.joinPipe,
108         flexGrow: 2
109       },
110       {
111         name: $localize`Images`,
112         prop: 'cdImages',
113         pipe: this.joinPipe,
114         flexGrow: 2
115       },
116       {
117         name: $localize`# Sessions`,
118         prop: 'info.num_sessions',
119         pipe: this.notAvailablePipe,
120         flexGrow: 1
121       }
122     ];
123
124     this.iscsiService.status().subscribe((result: any) => {
125       this.available = result.available;
126
127       if (result.available) {
128         this.iscsiService.version().subscribe((res: any) => {
129           this.cephIscsiConfigVersion = res['ceph_iscsi_config_version'];
130           this.taskListService.init(
131             () => this.iscsiService.listTargets(),
132             (resp) => this.prepareResponse(resp),
133             (targets) => (this.targets = targets),
134             () => this.onFetchError(),
135             this.taskFilter,
136             this.itemFilter,
137             this.builders
138           );
139         });
140
141         this.iscsiService.settings().subscribe((settings: any) => {
142           this.settings = settings;
143         });
144       } else {
145         this.status = result.message;
146       }
147     });
148   }
149
150   ngOnDestroy() {
151     if (this.summaryDataSubscription) {
152       this.summaryDataSubscription.unsubscribe();
153     }
154   }
155
156   getEditDisableDesc(): string | boolean {
157     const first = this.selection.first();
158
159     if (first && first?.cdExecuting) {
160       return first.cdExecuting;
161     }
162
163     if (first && _.isUndefined(first?.['info'])) {
164       return $localize`Unavailable gateway(s)`;
165     }
166
167     return !first;
168   }
169
170   getDeleteDisableDesc(): string | boolean {
171     const first = this.selection.first();
172
173     if (first?.cdExecuting) {
174       return first.cdExecuting;
175     }
176
177     if (first && _.isUndefined(first?.['info'])) {
178       return $localize`Unavailable gateway(s)`;
179     }
180
181     if (first && first?.['info']?.['num_sessions']) {
182       return $localize`Target has active sessions`;
183     }
184
185     return !first;
186   }
187
188   prepareResponse(resp: any): any[] {
189     resp.forEach((element: Record<string, any>) => {
190       element.cdPortals = element.portals.map(
191         (portal: Record<string, any>) => `${portal.host}:${portal.ip}`
192       );
193       element.cdImages = element.disks.map(
194         (disk: Record<string, any>) => `${disk.pool}/${disk.image}`
195       );
196     });
197
198     return resp;
199   }
200
201   onFetchError() {
202     this.table.reset(); // Disable loading indicator.
203   }
204
205   itemFilter(entry: Record<string, any>, task: Task) {
206     return entry.target_iqn === task.metadata['target_iqn'];
207   }
208
209   taskFilter(task: Task) {
210     return ['iscsi/target/create', 'iscsi/target/edit', 'iscsi/target/delete'].includes(task.name);
211   }
212
213   updateSelection(selection: CdTableSelection) {
214     this.selection = selection;
215   }
216
217   deleteIscsiTargetModal() {
218     const target_iqn = this.selection.first().target_iqn;
219
220     this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
221       itemDescription: $localize`iSCSI target`,
222       itemNames: [target_iqn],
223       submitActionObservable: () =>
224         this.taskWrapper.wrapTaskAroundCall({
225           task: new FinishedTask('iscsi/target/delete', {
226             target_iqn: target_iqn
227           }),
228           call: this.iscsiService.deleteTarget(target_iqn)
229         })
230     });
231   }
232
233   configureDiscoveryAuth() {
234     this.modalService.show(IscsiTargetDiscoveryModalComponent);
235   }
236 }