]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
412286bda2098087ea94304d6a1c8968a41ba486
[ceph.git] /
1 import { Component, OnInit } from '@angular/core';
2 import { UntypedFormControl, Validators } from '@angular/forms';
3 import { ActivatedRoute, Router } from '@angular/router';
4 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
5 import { ListenerRequest, NvmeofService } from '~/app/shared/api/nvmeof.service';
6 import { ActionLabelsI18n, URLVerbs } from '~/app/shared/constants/app.constants';
7 import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
8 import { FinishedTask } from '~/app/shared/models/finished-task';
9 import { Permission } from '~/app/shared/models/permissions';
10 import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
11 import { TaskWrapperService } from '~/app/shared/services/task-wrapper.service';
12 import { FormatterService } from '~/app/shared/services/formatter.service';
13 import { CdValidators } from '~/app/shared/forms/cd-validators';
14 import { DimlessBinaryPipe } from '~/app/shared/pipes/dimless-binary.pipe';
15 import { HostService } from '~/app/shared/api/host.service';
16 import { DaemonService } from '~/app/shared/api/daemon.service';
17 import { map } from 'rxjs/operators';
18 import { forkJoin } from 'rxjs';
19
20 @Component({
21   selector: 'cd-nvmeof-listeners-form',
22   templateUrl: './nvmeof-listeners-form.component.html',
23   styleUrls: ['./nvmeof-listeners-form.component.scss']
24 })
25 export class NvmeofListenersFormComponent implements OnInit {
26   action: string;
27   permission: Permission;
28   hostPermission: Permission;
29   resource: string;
30   pageURL: string;
31   listenerForm: CdFormGroup;
32   subsystemNQN: string;
33   hosts: Array<object> = null;
34
35   constructor(
36     public actionLabels: ActionLabelsI18n,
37     private authStorageService: AuthStorageService,
38     private taskWrapperService: TaskWrapperService,
39     private nvmeofService: NvmeofService,
40     private hostService: HostService,
41     private router: Router,
42     private route: ActivatedRoute,
43     public activeModal: NgbActiveModal,
44     public formatterService: FormatterService,
45     public dimlessBinaryPipe: DimlessBinaryPipe,
46     private daemonService: DaemonService
47   ) {
48     this.permission = this.authStorageService.getPermissions().nvmeof;
49     this.hostPermission = this.authStorageService.getPermissions().hosts;
50     this.resource = $localize`Listener`;
51     this.pageURL = 'block/nvmeof/subsystems';
52   }
53
54   setHosts() {
55     forkJoin({
56       daemons: this.daemonService.list(['nvmeof']),
57       hosts: this.hostService.getAllHosts()
58     })
59       .pipe(
60         map(({ daemons, hosts }) => {
61           const hostNamesFromDaemon = daemons.map((daemon: any) => daemon.hostname);
62           return hosts.filter((host: any) => hostNamesFromDaemon.includes(host.hostname));
63         })
64       )
65       .subscribe((nvmeofHosts: any[]) => {
66         this.hosts = nvmeofHosts.map((h) => ({ hostname: h.hostname, addr: h.addr }));
67       });
68   }
69
70   ngOnInit() {
71     this.createForm();
72     this.action = this.actionLabels.CREATE;
73     this.route.params.subscribe((params: { subsystem_nqn: string }) => {
74       this.subsystemNQN = params.subsystem_nqn;
75     });
76     this.setHosts();
77   }
78
79   createForm() {
80     this.listenerForm = new CdFormGroup({
81       host: new UntypedFormControl(null, {
82         validators: [Validators.required]
83       }),
84       trsvcid: new UntypedFormControl(4420, [
85         Validators.required,
86         CdValidators.number(false),
87         Validators.max(65535)
88       ])
89     });
90   }
91
92   buildRequest(): ListenerRequest {
93     const host = this.listenerForm.getValue('host');
94     let trsvcid = Number(this.listenerForm.getValue('trsvcid'));
95     if (!trsvcid) trsvcid = 4420;
96     const request = {
97       host_name: host.hostname,
98       traddr: host.addr,
99       trsvcid
100     };
101     return request;
102   }
103
104   onSubmit() {
105     const component = this;
106     const taskUrl: string = `nvmeof/listener/${URLVerbs.CREATE}`;
107     const request = this.buildRequest();
108     this.taskWrapperService
109       .wrapTaskAroundCall({
110         task: new FinishedTask(taskUrl, {
111           nqn: this.subsystemNQN,
112           host_name: request.host_name
113         }),
114         call: this.nvmeofService.createListener(this.subsystemNQN, request)
115       })
116       .subscribe({
117         error() {
118           component.listenerForm.setErrors({ cdSubmitButton: true });
119         },
120         complete: () => {
121           this.router.navigate([this.pageURL, { outlets: { modal: null } }]);
122         }
123       });
124   }
125 }