]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph-ci.git/blob
9e14697ab2eb34de58f608b738646776a9c07b5a
[ceph-ci.git] /
1 import { Component, Input, OnInit, TemplateRef, ViewChild } from '@angular/core';
2 import { Router } from '@angular/router';
3 import { NvmeofService } from '~/app/shared/api/nvmeof.service';
4 import { DeleteConfirmationModalComponent } from '~/app/shared/components/delete-confirmation-modal/delete-confirmation-modal.component';
5 import { ActionLabelsI18n, URLVerbs } from '~/app/shared/constants/app.constants';
6 import { Icons } from '~/app/shared/enum/icons.enum';
7 import { CdTableAction } from '~/app/shared/models/cd-table-action';
8 import { CdTableSelection } from '~/app/shared/models/cd-table-selection';
9 import { FinishedTask } from '~/app/shared/models/finished-task';
10 import { NvmeofSubsystemInitiator } from '~/app/shared/models/nvmeof';
11 import { Permission } from '~/app/shared/models/permissions';
12 import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
13 import { ModalCdsService } from '~/app/shared/services/modal-cds.service';
14 import { TaskWrapperService } from '~/app/shared/services/task-wrapper.service';
15
16 const BASE_URL = 'block/nvmeof/subsystems';
17
18 @Component({
19   selector: 'cd-nvmeof-initiators-list',
20   templateUrl: './nvmeof-initiators-list.component.html',
21   styleUrls: ['./nvmeof-initiators-list.component.scss'],
22   standalone: false
23 })
24 export class NvmeofInitiatorsListComponent implements OnInit {
25   @Input()
26   subsystemNQN: string;
27   @Input()
28   group: string;
29
30   @ViewChild('hostTpl', { static: true })
31   hostTpl: TemplateRef<any>;
32
33   initiatorColumns: any;
34   tableActions: CdTableAction[];
35   selection = new CdTableSelection();
36   permission: Permission;
37   initiators: NvmeofSubsystemInitiator[] = [];
38
39   constructor(
40     public actionLabels: ActionLabelsI18n,
41     private authStorageService: AuthStorageService,
42     private nvmeofService: NvmeofService,
43     private modalService: ModalCdsService,
44     private router: Router,
45     private taskWrapper: TaskWrapperService
46   ) {
47     this.permission = this.authStorageService.getPermissions().nvmeof;
48   }
49
50   ngOnInit() {
51     this.initiatorColumns = [
52       {
53         name: $localize`Initiator`,
54         prop: 'nqn',
55         cellTemplate: this.hostTpl
56       }
57     ];
58     this.tableActions = [
59       {
60         name: this.actionLabels.ADD,
61         permission: 'create',
62         icon: Icons.add,
63         click: () =>
64           this.router.navigate(
65             [BASE_URL, { outlets: { modal: [URLVerbs.ADD, this.subsystemNQN, 'initiator'] } }],
66             { queryParams: { group: this.group } }
67           ),
68         canBePrimary: (selection: CdTableSelection) => !selection.hasSelection
69       },
70       {
71         name: this.actionLabels.REMOVE,
72         permission: 'delete',
73         icon: Icons.destroy,
74         click: () => this.removeInitiatorModal(),
75         disable: () => !this.selection.hasSelection,
76         canBePrimary: (selection: CdTableSelection) => selection.hasSelection
77       }
78     ];
79   }
80
81   getAllowAllHostIndex() {
82     return this.selection.selected.findIndex((selected) => selected.nqn === '*');
83   }
84
85   updateSelection(selection: CdTableSelection) {
86     this.selection = selection;
87   }
88
89   listInitiators() {
90     this.nvmeofService
91       .getInitiators(this.subsystemNQN, this.group)
92       .subscribe((initiators: NvmeofSubsystemInitiator[]) => {
93         this.initiators = initiators;
94       });
95   }
96
97   getSelectedNQNs() {
98     return this.selection.selected.map((selected) => selected.nqn);
99   }
100
101   removeInitiatorModal() {
102     const hostNQNs = this.getSelectedNQNs();
103     const allowAllHostIndex = this.getAllowAllHostIndex();
104     const host_nqn = hostNQNs.join(',');
105     let itemNames = hostNQNs;
106     if (allowAllHostIndex !== -1) {
107       hostNQNs.splice(allowAllHostIndex, 1);
108       itemNames = [...hostNQNs, $localize`Allow any host(*)`];
109     }
110     this.modalService.show(DeleteConfirmationModalComponent, {
111       itemDescription: 'Initiator',
112       itemNames,
113       actionDescription: 'remove',
114       submitActionObservable: () =>
115         this.taskWrapper.wrapTaskAroundCall({
116           task: new FinishedTask('nvmeof/initiator/remove', {
117             nqn: this.subsystemNQN,
118             plural: itemNames.length > 1
119           }),
120           call: this.nvmeofService.removeInitiators(this.subsystemNQN, {
121             host_nqn,
122             gw_group: this.group
123           })
124         })
125     });
126   }
127 }