1 import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core';
3 import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
4 import * as _ from 'lodash';
5 import { Subscription } from 'rxjs';
7 import { IscsiService } from '../../../shared/api/iscsi.service';
8 import { ListWithDetails } from '../../../shared/classes/list-with-details.class';
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 { Task } from '../../../shared/models/task';
20 import { JoinPipe } from '../../../shared/pipes/join.pipe';
21 import { NotAvailablePipe } from '../../../shared/pipes/not-available.pipe';
22 import { AuthStorageService } from '../../../shared/services/auth-storage.service';
23 import { ModalService } from '../../../shared/services/modal.service';
24 import { TaskListService } from '../../../shared/services/task-list.service';
25 import { TaskWrapperService } from '../../../shared/services/task-wrapper.service';
26 import { IscsiTargetDiscoveryModalComponent } from '../iscsi-target-discovery-modal/iscsi-target-discovery-modal.component';
29 selector: 'cd-iscsi-target-list',
30 templateUrl: './iscsi-target-list.component.html',
31 styleUrls: ['./iscsi-target-list.component.scss'],
32 providers: [TaskListService]
34 export class IscsiTargetListComponent extends ListWithDetails implements OnInit, OnDestroy {
35 @ViewChild(TableComponent)
36 table: TableComponent;
38 available: boolean = undefined;
39 columns: CdTableColumn[];
40 modalRef: NgbModalRef;
41 permission: Permission;
42 selection = new CdTableSelection();
43 cephIscsiConfigVersion: number;
46 summaryDataSubscription: Subscription;
47 tableActions: CdTableAction[];
52 'iscsi/target/create': (metadata: object) => {
54 target_iqn: metadata['target_iqn']
60 private authStorageService: AuthStorageService,
61 private iscsiService: IscsiService,
62 private joinPipe: JoinPipe,
63 private taskListService: TaskListService,
64 private notAvailablePipe: NotAvailablePipe,
65 private modalService: ModalService,
66 private taskWrapper: TaskWrapperService,
67 public actionLabels: ActionLabelsI18n
70 this.permission = this.authStorageService.getPermissions().iscsi;
76 routerLink: () => '/block/iscsi/targets/create',
77 name: this.actionLabels.CREATE
82 routerLink: () => `/block/iscsi/targets/edit/${this.selection.first().target_iqn}`,
83 name: this.actionLabels.EDIT,
84 disable: () => this.getEditDisableDesc()
89 click: () => this.deleteIscsiTargetModal(),
90 name: this.actionLabels.DELETE,
91 disable: () => this.getDeleteDisableDesc()
99 name: $localize`Target`,
102 cellTransformation: CellTemplate.executing
105 name: $localize`Portals`,
111 name: $localize`Images`,
117 name: $localize`# Sessions`,
118 prop: 'info.num_sessions',
119 pipe: this.notAvailablePipe,
124 this.iscsiService.status().subscribe((result: any) => {
125 this.available = result.available;
127 if (result.available) {
128 this.iscsiService.version().subscribe((res: any) => {
129 this.cephIscsiConfigVersion = res['ceph_iscsi_config_version'];
130 this.taskListService.init(
131 () => this.iscsiService.listTargets(),
132 (resp) => this.prepareResponse(resp),
133 (targets) => (this.targets = targets),
134 () => this.onFetchError(),
141 this.iscsiService.settings().subscribe((settings: any) => {
142 this.settings = settings;
145 this.status = result.message;
151 if (this.summaryDataSubscription) {
152 this.summaryDataSubscription.unsubscribe();
156 getEditDisableDesc(): string | boolean {
157 const first = this.selection.first();
159 if (first && first?.cdExecuting) {
160 return first.cdExecuting;
163 if (first && _.isUndefined(first?.['info'])) {
164 return $localize`Unavailable gateway(s)`;
170 getDeleteDisableDesc(): string | boolean {
171 const first = this.selection.first();
173 if (first?.cdExecuting) {
174 return first.cdExecuting;
177 if (first && _.isUndefined(first?.['info'])) {
178 return $localize`Unavailable gateway(s)`;
181 if (first && first?.['info']?.['num_sessions']) {
182 return $localize`Target has active sessions`;
188 prepareResponse(resp: any): any[] {
189 resp.forEach((element: Record<string, any>) => {
190 element.cdPortals = element.portals.map(
191 (portal: Record<string, any>) => `${portal.host}:${portal.ip}`
193 element.cdImages = element.disks.map(
194 (disk: Record<string, any>) => `${disk.pool}/${disk.image}`
202 this.table.reset(); // Disable loading indicator.
205 itemFilter(entry: Record<string, any>, task: Task) {
206 return entry.target_iqn === task.metadata['target_iqn'];
209 taskFilter(task: Task) {
210 return ['iscsi/target/create', 'iscsi/target/edit', 'iscsi/target/delete'].includes(task.name);
213 updateSelection(selection: CdTableSelection) {
214 this.selection = selection;
217 deleteIscsiTargetModal() {
218 const target_iqn = this.selection.first().target_iqn;
220 this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
221 itemDescription: $localize`iSCSI target`,
222 itemNames: [target_iqn],
223 submitActionObservable: () =>
224 this.taskWrapper.wrapTaskAroundCall({
225 task: new FinishedTask('iscsi/target/delete', {
226 target_iqn: target_iqn
228 call: this.iscsiService.deleteTarget(target_iqn)
233 configureDiscoveryAuth() {
234 this.modalService.show(IscsiTargetDiscoveryModalComponent);