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 { 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';
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)
34 table: TableComponent;
36 available: boolean = undefined;
37 columns: CdTableColumn[];
40 permissions: Permissions;
41 selection = new CdTableSelection();
42 cephIscsiConfigVersion: number;
45 summaryDataSubscription: Subscription;
46 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 notAvailablePipe: NotAvailablePipe,
64 private summaryservice: SummaryService,
65 private modalService: BsModalService,
66 private taskWrapper: TaskWrapperService,
67 public actionLabels: ActionLabelsI18n
69 this.permissions = this.authStorageService.getPermissions();
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,
83 disable: () => !this.selection.first() || !_.isUndefined(this.getDeleteDisableDesc()),
84 disableDesc: () => this.getEditDisableDesc()
89 click: () => this.deleteIscsiTargetModal(),
90 name: this.actionLabels.DELETE,
91 disable: () => !this.selection.first() || !_.isUndefined(this.getDeleteDisableDesc()),
92 disableDesc: () => this.getDeleteDisableDesc()
100 name: this.i18n('Target'),
103 cellTransformation: CellTemplate.executing
106 name: this.i18n('Portals'),
111 name: this.i18n('Images'),
116 name: this.i18n('# Sessions'),
117 prop: 'info.num_sessions',
118 pipe: this.notAvailablePipe,
123 this.iscsiService.status().subscribe((result: any) => {
124 this.available = result.available;
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(),
140 this.iscsiService.settings().subscribe((settings: any) => {
141 this.settings = settings;
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;
153 if (this.summaryDataSubscription) {
154 this.summaryDataSubscription.unsubscribe();
158 getEditDisableDesc(): string | undefined {
159 const first = this.selection.first();
160 if (first && first.cdExecuting) {
161 return first.cdExecuting;
163 if (first && _.isUndefined(first['info'])) {
164 return this.i18n('Unavailable gateway(s)');
168 getDeleteDisableDesc(): string | undefined {
169 const first = this.selection.first();
170 if (first && first.cdExecuting) {
171 return first.cdExecuting;
173 if (first && _.isUndefined(first['info'])) {
174 return this.i18n('Unavailable gateway(s)');
176 if (first && first['info'] && first['info']['num_sessions']) {
177 return this.i18n('Target has active sessions');
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}`);
191 this.table.reset(); // Disable loading indicator.
194 itemFilter(entry, task) {
195 return entry.target_iqn === task.metadata['target_iqn'];
199 return ['iscsi/target/create', 'iscsi/target/edit', 'iscsi/target/delete'].includes(task.name);
202 updateSelection(selection: CdTableSelection) {
203 this.selection = selection;
206 deleteIscsiTargetModal() {
207 const target_iqn = this.selection.first().target_iqn;
209 this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
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
218 call: this.iscsiService.deleteTarget(target_iqn)
224 configureDiscoveryAuth() {
225 this.modalService.show(IscsiTargetDiscoveryModalComponent, {});