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();
42 cephIscsiConfigVersion: number;
45 summaryDataSubscription: Subscription;
46 tableActions: CdTableAction[];
51 'iscsi/target/create': (metadata) => {
53 target_iqn: metadata['target_iqn']
59 private authStorageService: AuthStorageService,
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
69 this.permission = this.authStorageService.getPermissions().iscsi;
75 routerLink: () => '/block/iscsi/targets/create',
76 name: this.actionLabels.CREATE
81 routerLink: () => `/block/iscsi/targets/edit/${this.selection.first().target_iqn}`,
82 name: this.actionLabels.EDIT
87 click: () => this.deleteIscsiTargetModal(),
88 name: this.actionLabels.DELETE,
89 disable: () => !this.selection.first() || !_.isUndefined(this.getDeleteDisableDesc()),
90 disableDesc: () => this.getDeleteDisableDesc()
98 name: this.i18n('Target'),
101 cellTransformation: CellTemplate.executing
104 name: this.i18n('Portals'),
109 name: this.i18n('Images'),
114 name: this.i18n('# Sessions'),
115 prop: 'info.num_sessions',
120 this.iscsiService.status().subscribe((result: any) => {
121 this.available = result.available;
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(),
137 this.iscsiService.settings().subscribe((settings: any) => {
138 this.settings = settings;
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;
150 if (this.summaryDataSubscription) {
151 this.summaryDataSubscription.unsubscribe();
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');
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}`);
172 this.table.reset(); // Disable loading indicator.
175 itemFilter(entry, task) {
176 return entry.target_iqn === task.metadata['target_iqn'];
180 return ['iscsi/target/create', 'iscsi/target/edit', 'iscsi/target/delete'].includes(task.name);
183 updateSelection(selection: CdTableSelection) {
184 this.selection = selection;
187 deleteIscsiTargetModal() {
188 const target_iqn = this.selection.first().target_iqn;
190 this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
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
199 call: this.iscsiService.deleteTarget(target_iqn)
205 configureDiscoveryAuth() {
206 this.modalService.show(IscsiTargetDiscoveryModalComponent, {});