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