1 import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core';
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';
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';
27 selector: 'cd-iscsi-target-list',
28 templateUrl: './iscsi-target-list.component.html',
29 styleUrls: ['./iscsi-target-list.component.scss'],
30 providers: [TaskListService]
32 export class IscsiTargetListComponent implements OnInit, OnDestroy {
33 @ViewChild(TableComponent, { static: false })
34 table: TableComponent;
36 available: boolean = undefined;
37 columns: CdTableColumn[];
40 permission: Permission;
41 selection = new CdTableSelection();
44 summaryDataSubscription: Subscription;
45 tableActions: CdTableAction[];
50 'iscsi/target/create': (metadata) => {
52 target_iqn: metadata['target_iqn']
58 private authStorageService: AuthStorageService,
60 private iscsiService: IscsiService,
61 private taskListService: TaskListService,
62 private cephReleaseNamePipe: CephReleaseNamePipe,
63 private summaryservice: SummaryService,
64 private modalService: BsModalService,
65 private taskWrapper: TaskWrapperService,
66 public actionLabels: ActionLabelsI18n
68 this.permission = this.authStorageService.getPermissions().iscsi;
74 routerLink: () => '/block/iscsi/targets/create',
75 name: this.actionLabels.CREATE
80 routerLink: () => `/block/iscsi/targets/edit/${this.selection.first().target_iqn}`,
81 name: this.actionLabels.EDIT
86 click: () => this.deleteIscsiTargetModal(),
87 name: this.actionLabels.DELETE,
88 disable: () => !this.selection.first() || !_.isUndefined(this.getDeleteDisableDesc()),
89 disableDesc: () => this.getDeleteDisableDesc()
97 name: this.i18n('Target'),
100 cellTransformation: CellTemplate.executing
103 name: this.i18n('Portals'),
108 name: this.i18n('Images'),
113 name: this.i18n('# Sessions'),
114 prop: 'info.num_sessions',
119 this.iscsiService.status().subscribe((result: any) => {
120 this.available = result.available;
122 if (result.available) {
123 this.taskListService.init(
124 () => this.iscsiService.listTargets(),
125 (resp) => this.prepareResponse(resp),
126 (targets) => (this.targets = targets),
127 () => this.onFetchError(),
133 this.iscsiService.settings().subscribe((settings: any) => {
134 this.settings = settings;
137 const summary = this.summaryservice.getCurrentSummary();
138 const releaseName = this.cephReleaseNamePipe.transform(summary.version);
139 this.docsUrl = `http://docs.ceph.com/docs/${releaseName}/mgr/dashboard/#enabling-iscsi-management`;
140 this.status = result.message;
146 if (this.summaryDataSubscription) {
147 this.summaryDataSubscription.unsubscribe();
151 getDeleteDisableDesc(): string | undefined {
152 const first = this.selection.first();
153 if (first && first['info'] && first['info']['num_sessions']) {
154 return this.i18n('Target has active sessions');
158 prepareResponse(resp: any): any[] {
159 resp.forEach((element) => {
160 element.cdPortals = element.portals.map((portal) => `${portal.host}:${portal.ip}`);
161 element.cdImages = element.disks.map((disk) => `${disk.pool}/${disk.image}`);
168 this.table.reset(); // Disable loading indicator.
171 itemFilter(entry, task) {
172 return entry.target_iqn === task.metadata['target_iqn'];
176 return ['iscsi/target/create', 'iscsi/target/edit', 'iscsi/target/delete'].includes(task.name);
179 updateSelection(selection: CdTableSelection) {
180 this.selection = selection;
183 deleteIscsiTargetModal() {
184 const target_iqn = this.selection.first().target_iqn;
186 this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
188 itemDescription: this.i18n('iSCSI'),
189 submitActionObservable: () =>
190 this.taskWrapper.wrapTaskAroundCall({
191 task: new FinishedTask('iscsi/target/delete', {
192 target_iqn: target_iqn
194 call: this.iscsiService.deleteTarget(target_iqn)
200 configureDiscoveryAuth() {
201 this.modalService.show(IscsiTargetDiscoveryModalComponent, {});