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)
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 this.summaryservice.subscribeOnce((summary) => {
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;
159 if (this.summaryDataSubscription) {
160 this.summaryDataSubscription.unsubscribe();
164 getEditDisableDesc(): string | undefined {
165 const first = this.selection.first();
166 if (first && first.cdExecuting) {
167 return first.cdExecuting;
169 if (first && _.isUndefined(first['info'])) {
170 return this.i18n('Unavailable gateway(s)');
176 getDeleteDisableDesc(): string | undefined {
177 const first = this.selection.first();
178 if (first && first.cdExecuting) {
179 return first.cdExecuting;
181 if (first && _.isUndefined(first['info'])) {
182 return this.i18n('Unavailable gateway(s)');
184 if (first && first['info'] && first['info']['num_sessions']) {
185 return this.i18n('Target has active sessions');
191 prepareResponse(resp: any): any[] {
192 resp.forEach((element: Record<string, any>) => {
193 element.cdPortals = element.portals.map(
194 (portal: Record<string, any>) => `${portal.host}:${portal.ip}`
196 element.cdImages = element.disks.map(
197 (disk: Record<string, any>) => `${disk.pool}/${disk.image}`
205 this.table.reset(); // Disable loading indicator.
208 itemFilter(entry: Record<string, any>, task: Task) {
209 return entry.target_iqn === task.metadata['target_iqn'];
212 taskFilter(task: Task) {
213 return ['iscsi/target/create', 'iscsi/target/edit', 'iscsi/target/delete'].includes(task.name);
216 updateSelection(selection: CdTableSelection) {
217 this.selection = selection;
220 deleteIscsiTargetModal() {
221 const target_iqn = this.selection.first().target_iqn;
223 this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
225 itemDescription: this.i18n('iSCSI target'),
226 itemNames: [target_iqn],
227 submitActionObservable: () =>
228 this.taskWrapper.wrapTaskAroundCall({
229 task: new FinishedTask('iscsi/target/delete', {
230 target_iqn: target_iqn
232 call: this.iscsiService.deleteTarget(target_iqn)
238 configureDiscoveryAuth() {
239 this.modalService.show(IscsiTargetDiscoveryModalComponent, {});