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