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