]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/blob
666e1f50d35251fe4a79849aa1d4b8d423a5606e
[ceph.git] /
1 import { Component, OnInit } from '@angular/core';
2 import { Step } from 'carbon-components-angular';
3 import { InitiatorRequest, NvmeofService } from '~/app/shared/api/nvmeof.service';
4 import { FinishedTask } from '~/app/shared/models/finished-task';
5 import { HOST_TYPE, NvmeofSubsystemInitiator } from '~/app/shared/models/nvmeof';
6 import { TaskWrapperService } from '~/app/shared/services/task-wrapper.service';
7 import { ActivatedRoute, Router } from '@angular/router';
8 import { SubsystemPayload } from '../nvmeof-subsystems-form/nvmeof-subsystems-form.component';
9
10 @Component({
11   selector: 'cd-nvmeof-initiators-form',
12   templateUrl: './nvmeof-initiators-form.component.html',
13   styleUrls: ['./nvmeof-initiators-form.component.scss'],
14   standalone: false
15 })
16 export class NvmeofInitiatorsFormComponent implements OnInit {
17   group!: string;
18   subsystemNQN!: string;
19   isSubmitLoading = false;
20   existingHosts: string[] = [];
21
22   steps: Step[] = [
23     {
24       label: $localize`Host access control`,
25       invalid: false
26     }
27   ];
28
29   title = $localize`Add Initiator`;
30   description = $localize`Allow specific hosts to run NVMe/TCP commands to the NVMe subsystem.`;
31   pageURL = 'block/nvmeof/subsystems';
32
33   constructor(
34     private nvmeofService: NvmeofService,
35     private taskWrapperService: TaskWrapperService,
36     private router: Router,
37     private route: ActivatedRoute
38   ) {}
39
40   ngOnInit() {
41     this.route.queryParams.subscribe((params) => {
42       this.group = params?.['group'];
43     });
44     this.route.parent.params.subscribe((params: any) => {
45       if (params.subsystem_nqn) {
46         this.subsystemNQN = params.subsystem_nqn;
47       }
48     });
49     this.route.params.subscribe((params: any) => {
50       if (!this.subsystemNQN && params.subsystem_nqn) {
51         this.subsystemNQN = params.subsystem_nqn;
52       }
53       this.fetchExistingHosts();
54     });
55   }
56
57   fetchExistingHosts() {
58     if (!this.subsystemNQN || !this.group) return;
59     this.nvmeofService
60       .getInitiators(this.subsystemNQN, this.group)
61       .subscribe((response: NvmeofSubsystemInitiator[] | { hosts: NvmeofSubsystemInitiator[] }) => {
62         const initiators = Array.isArray(response) ? response : response?.hosts || [];
63         this.existingHosts = initiators.map((i) => i.nqn);
64       });
65   }
66
67   onSubmit(payload: SubsystemPayload) {
68     this.isSubmitLoading = true;
69     const taskUrl = `nvmeof/initiator/add`;
70
71     const request: InitiatorRequest = {
72       host_nqn: payload.hostType === HOST_TYPE.ALL ? '*' : payload.addedHosts.join(','),
73       gw_group: this.group
74     };
75     this.taskWrapperService
76       .wrapTaskAroundCall({
77         task: new FinishedTask(taskUrl, {
78           nqn: this.subsystemNQN
79         }),
80         call: this.nvmeofService.addInitiators(this.subsystemNQN, request)
81       })
82       .subscribe({
83         error: (err) => {
84           this.isSubmitLoading = false;
85           err.preventDefault();
86         },
87         complete: () => {
88           this.isSubmitLoading = false;
89           this.router.navigate([{ outlets: { modal: null } }], {
90             relativeTo: this.route.parent,
91             queryParamsHandling: 'preserve'
92           });
93         }
94       });
95   }
96 }