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 { CephReleaseNamePipe } from '../../../shared/pipes/ceph-release-name.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 { 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[];
42 modalRef: NgbModalRef;
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,
63 private iscsiService: IscsiService,
64 private taskListService: TaskListService,
65 private cephReleaseNamePipe: CephReleaseNamePipe,
66 private notAvailablePipe: NotAvailablePipe,
67 private summaryservice: SummaryService,
68 private modalService: ModalService,
69 private taskWrapper: TaskWrapperService,
70 public actionLabels: ActionLabelsI18n
73 this.permission = this.authStorageService.getPermissions().iscsi;
79 routerLink: () => '/block/iscsi/targets/create',
80 name: this.actionLabels.CREATE
85 routerLink: () => `/block/iscsi/targets/edit/${this.selection.first().target_iqn}`,
86 name: this.actionLabels.EDIT,
87 disable: () => !this.selection.first() || !_.isUndefined(this.getEditDisableDesc()),
88 disableDesc: () => this.getEditDisableDesc()
93 click: () => this.deleteIscsiTargetModal(),
94 name: this.actionLabels.DELETE,
95 disable: () => !this.selection.first() || !_.isUndefined(this.getDeleteDisableDesc()),
96 disableDesc: () => this.getDeleteDisableDesc()
104 name: $localize`Target`,
107 cellTransformation: CellTemplate.executing
110 name: $localize`Portals`,
115 name: $localize`Images`,
120 name: $localize`# Sessions`,
121 prop: 'info.num_sessions',
122 pipe: this.notAvailablePipe,
127 this.iscsiService.status().subscribe((result: any) => {
128 this.available = result.available;
130 if (result.available) {
131 this.iscsiService.version().subscribe((res: any) => {
132 this.cephIscsiConfigVersion = res['ceph_iscsi_config_version'];
133 this.taskListService.init(
134 () => this.iscsiService.listTargets(),
135 (resp) => this.prepareResponse(resp),
136 (targets) => (this.targets = targets),
137 () => this.onFetchError(),
144 this.iscsiService.settings().subscribe((settings: any) => {
145 this.settings = settings;
148 this.summaryservice.subscribeOnce((summary) => {
149 const releaseName = this.cephReleaseNamePipe.transform(summary.version);
150 this.docsUrl = `http://docs.ceph.com/docs/${releaseName}/mgr/dashboard/#enabling-iscsi-management`;
151 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 $localize`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 $localize`Unavailable gateway(s)`;
183 if (first && first['info'] && first['info']['num_sessions']) {
184 return $localize`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, {
223 itemDescription: $localize`iSCSI target`,
224 itemNames: [target_iqn],
225 submitActionObservable: () =>
226 this.taskWrapper.wrapTaskAroundCall({
227 task: new FinishedTask('iscsi/target/delete', {
228 target_iqn: target_iqn
230 call: this.iscsiService.deleteTarget(target_iqn)
235 configureDiscoveryAuth() {
236 this.modalService.show(IscsiTargetDiscoveryModalComponent);