]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
a5575a9c9267eabe99d7ef44c7757378685185be
[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 { CriticalConfirmationModalComponent } from '~/app/shared/components/critical-confirmation-modal/critical-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 })
23 export class NvmeofInitiatorsListComponent implements OnInit {
24   @Input()
25   subsystemNQN: string;
26   @Input()
27   group: string;
28
29   @ViewChild('hostTpl', { static: true })
30   hostTpl: TemplateRef<any>;
31
32   initiatorColumns: any;
33   tableActions: CdTableAction[];
34   selection = new CdTableSelection();
35   permission: Permission;
36   initiators: NvmeofSubsystemInitiator[] = [];
37
38   constructor(
39     public actionLabels: ActionLabelsI18n,
40     private authStorageService: AuthStorageService,
41     private nvmeofService: NvmeofService,
42     private modalService: ModalCdsService,
43     private router: Router,
44     private taskWrapper: TaskWrapperService
45   ) {
46     this.permission = this.authStorageService.getPermissions().nvmeof;
47   }
48
49   ngOnInit() {
50     this.initiatorColumns = [
51       {
52         name: $localize`Initiator`,
53         prop: 'nqn',
54         cellTemplate: this.hostTpl
55       }
56     ];
57     this.tableActions = [
58       {
59         name: this.actionLabels.ADD,
60         permission: 'create',
61         icon: Icons.add,
62         click: () =>
63           this.router.navigate(
64             [BASE_URL, { outlets: { modal: [URLVerbs.ADD, this.subsystemNQN, 'initiator'] } }],
65             { queryParams: { group: this.group } }
66           ),
67         canBePrimary: (selection: CdTableSelection) => !selection.hasSelection
68       },
69       {
70         name: this.actionLabels.REMOVE,
71         permission: 'delete',
72         icon: Icons.destroy,
73         click: () => this.removeInitiatorModal(),
74         disable: () => !this.selection.hasSelection,
75         canBePrimary: (selection: CdTableSelection) => selection.hasSelection
76       }
77     ];
78   }
79
80   getAllowAllHostIndex() {
81     return this.selection.selected.findIndex((selected) => selected.nqn === '*');
82   }
83
84   updateSelection(selection: CdTableSelection) {
85     this.selection = selection;
86   }
87
88   listInitiators() {
89     this.nvmeofService
90       .getInitiators(this.subsystemNQN, this.group)
91       .subscribe((initiators: NvmeofSubsystemInitiator[]) => {
92         this.initiators = initiators;
93       });
94   }
95
96   getSelectedNQNs() {
97     return this.selection.selected.map((selected) => selected.nqn);
98   }
99
100   removeInitiatorModal() {
101     const hostNQNs = this.getSelectedNQNs();
102     const allowAllHostIndex = this.getAllowAllHostIndex();
103     const host_nqn = hostNQNs.join(',');
104     let itemNames = hostNQNs;
105     if (allowAllHostIndex !== -1) {
106       hostNQNs.splice(allowAllHostIndex, 1);
107       itemNames = [...hostNQNs, $localize`Allow any host(*)`];
108     }
109     this.modalService.show(CriticalConfirmationModalComponent, {
110       itemDescription: 'Initiator',
111       itemNames,
112       actionDescription: 'remove',
113       submitActionObservable: () =>
114         this.taskWrapper.wrapTaskAroundCall({
115           task: new FinishedTask('nvmeof/initiator/remove', {
116             nqn: this.subsystemNQN,
117             plural: itemNames.length > 1
118           }),
119           call: this.nvmeofService.removeInitiators(this.subsystemNQN, {
120             host_nqn,
121             gw_group: this.group
122           })
123         })
124     });
125   }
126 }