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