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