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 { ListWithDetails } from '../../../shared/classes/list-with-details.class';
10 import { CriticalConfirmationModalComponent } from '../../../shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
11 import { ActionLabelsI18n } from '../../../shared/constants/app.constants';
12 import { TableComponent } from '../../../shared/datatable/table/table.component';
13 import { CellTemplate } from '../../../shared/enum/cell-template.enum';
14 import { Icons } from '../../../shared/enum/icons.enum';
15 import { CdTableAction } from '../../../shared/models/cd-table-action';
16 import { CdTableColumn } from '../../../shared/models/cd-table-column';
17 import { CdTableSelection } from '../../../shared/models/cd-table-selection';
18 import { FinishedTask } from '../../../shared/models/finished-task';
19 import { Permission } from '../../../shared/models/permissions';
20 import { Task } from '../../../shared/models/task';
21 import { CephReleaseNamePipe } from '../../../shared/pipes/ceph-release-name.pipe';
22 import { NotAvailablePipe } from '../../../shared/pipes/not-available.pipe';
23 import { AuthStorageService } from '../../../shared/services/auth-storage.service';
24 import { SummaryService } from '../../../shared/services/summary.service';
25 import { TaskListService } from '../../../shared/services/task-list.service';
26 import { TaskWrapperService } from '../../../shared/services/task-wrapper.service';
27 import { IscsiTargetDiscoveryModalComponent } from '../iscsi-target-discovery-modal/iscsi-target-discovery-modal.component';
30 selector: 'cd-iscsi-target-list',
31 templateUrl: './iscsi-target-list.component.html',
32 styleUrls: ['./iscsi-target-list.component.scss'],
33 providers: [TaskListService]
35 export class IscsiTargetListComponent extends ListWithDetails implements OnInit, OnDestroy {
36 @ViewChild(TableComponent, { static: false })
37 table: TableComponent;
39 available: boolean = undefined;
40 columns: CdTableColumn[];
43 permission: Permission;
44 selection = new CdTableSelection();
45 cephIscsiConfigVersion: number;
48 summaryDataSubscription: Subscription;
49 tableActions: CdTableAction[];
54 'iscsi/target/create': (metadata: object) => {
56 target_iqn: metadata['target_iqn']
62 private authStorageService: AuthStorageService,
64 private iscsiService: IscsiService,
65 private taskListService: TaskListService,
66 private cephReleaseNamePipe: CephReleaseNamePipe,
67 private notAvailablePipe: NotAvailablePipe,
68 private summaryservice: SummaryService,
69 private modalService: BsModalService,
70 private taskWrapper: TaskWrapperService,
71 public actionLabels: ActionLabelsI18n
74 this.permission = this.authStorageService.getPermissions().iscsi;
80 routerLink: () => '/block/iscsi/targets/create',
81 name: this.actionLabels.CREATE
86 routerLink: () => `/block/iscsi/targets/edit/${this.selection.first().target_iqn}`,
87 name: this.actionLabels.EDIT,
88 disable: () => !this.selection.first() || !_.isUndefined(this.getDeleteDisableDesc()),
89 disableDesc: () => this.getEditDisableDesc()
94 click: () => this.deleteIscsiTargetModal(),
95 name: this.actionLabels.DELETE,
96 disable: () => !this.selection.first() || !_.isUndefined(this.getDeleteDisableDesc()),
97 disableDesc: () => this.getDeleteDisableDesc()
105 name: this.i18n('Target'),
108 cellTransformation: CellTemplate.executing
111 name: this.i18n('Portals'),
116 name: this.i18n('Images'),
121 name: this.i18n('# Sessions'),
122 prop: 'info.num_sessions',
123 pipe: this.notAvailablePipe,
128 this.iscsiService.status().subscribe((result: any) => {
129 this.available = result.available;
131 if (result.available) {
132 this.iscsiService.version().subscribe((res: any) => {
133 this.cephIscsiConfigVersion = res['ceph_iscsi_config_version'];
134 this.taskListService.init(
135 () => this.iscsiService.listTargets(),
136 (resp) => this.prepareResponse(resp),
137 (targets) => (this.targets = targets),
138 () => this.onFetchError(),
145 this.iscsiService.settings().subscribe((settings: any) => {
146 this.settings = settings;
149 const summary = this.summaryservice.getCurrentSummary();
150 const releaseName = this.cephReleaseNamePipe.transform(summary.version);
151 this.docsUrl = `http://docs.ceph.com/docs/${releaseName}/mgr/dashboard/#enabling-iscsi-management`;
152 this.status = result.message;
158 if (this.summaryDataSubscription) {
159 this.summaryDataSubscription.unsubscribe();
163 getEditDisableDesc(): string | undefined {
164 const first = this.selection.first();
165 if (first && first.cdExecuting) {
166 return first.cdExecuting;
168 if (first && _.isUndefined(first['info'])) {
169 return this.i18n('Unavailable gateway(s)');
175 getDeleteDisableDesc(): string | undefined {
176 const first = this.selection.first();
177 if (first && first.cdExecuting) {
178 return first.cdExecuting;
180 if (first && _.isUndefined(first['info'])) {
181 return this.i18n('Unavailable gateway(s)');
183 if (first && first['info'] && first['info']['num_sessions']) {
184 return this.i18n('Target has active sessions');
190 prepareResponse(resp: any): any[] {
191 resp.forEach((element: Record<string, any>) => {
192 element.cdPortals = element.portals.map(
193 (portal: Record<string, any>) => `${portal.host}:${portal.ip}`
195 element.cdImages = element.disks.map(
196 (disk: Record<string, any>) => `${disk.pool}/${disk.image}`
204 this.table.reset(); // Disable loading indicator.
207 itemFilter(entry: Record<string, any>, task: Task) {
208 return entry.target_iqn === task.metadata['target_iqn'];
211 taskFilter(task: Task) {
212 return ['iscsi/target/create', 'iscsi/target/edit', 'iscsi/target/delete'].includes(task.name);
215 updateSelection(selection: CdTableSelection) {
216 this.selection = selection;
219 deleteIscsiTargetModal() {
220 const target_iqn = this.selection.first().target_iqn;
222 this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
224 itemDescription: this.i18n('iSCSI target'),
225 itemNames: [target_iqn],
226 submitActionObservable: () =>
227 this.taskWrapper.wrapTaskAroundCall({
228 task: new FinishedTask('iscsi/target/delete', {
229 target_iqn: target_iqn
231 call: this.iscsiService.deleteTarget(target_iqn)
237 configureDiscoveryAuth() {
238 this.modalService.show(IscsiTargetDiscoveryModalComponent, {});