]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
bc02ea99c98e67475b1bea994b13f1b1e04810f1
[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 { CdTableFetchDataContext } from '~/app/shared/models/cd-table-fetch-data-context';
17 @Component({
18   selector: 'cd-nvmeof-listeners-form',
19   templateUrl: './nvmeof-listeners-form.component.html',
20   styleUrls: ['./nvmeof-listeners-form.component.scss']
21 })
22 export class NvmeofListenersFormComponent implements OnInit {
23   action: string;
24   permission: Permission;
25   hostPermission: Permission;
26   resource: string;
27   pageURL: string;
28   listenerForm: CdFormGroup;
29   subsystemNQN: string;
30   hosts: Array<object> = null;
31
32   constructor(
33     public actionLabels: ActionLabelsI18n,
34     private authStorageService: AuthStorageService,
35     private taskWrapperService: TaskWrapperService,
36     private nvmeofService: NvmeofService,
37     private hostService: HostService,
38     private router: Router,
39     private route: ActivatedRoute,
40     public activeModal: NgbActiveModal,
41     public formatterService: FormatterService,
42     public dimlessBinaryPipe: DimlessBinaryPipe
43   ) {
44     this.permission = this.authStorageService.getPermissions().nvmeof;
45     this.hostPermission = this.authStorageService.getPermissions().hosts;
46     this.resource = $localize`Listener`;
47     this.pageURL = 'block/nvmeof/subsystems';
48   }
49
50   setHosts() {
51     const hostContext = new CdTableFetchDataContext(() => undefined);
52     this.hostService.list(hostContext.toParams(), 'false').subscribe((resp: any[]) => {
53       const nvmeofHosts = resp.filter((r) =>
54         r.service_instances.some((si: any) => si.type === 'nvmeof')
55       );
56       this.hosts = nvmeofHosts.map((h) => ({ hostname: h.hostname, addr: h.addr }));
57     });
58   }
59
60   ngOnInit() {
61     this.createForm();
62     this.action = this.actionLabels.CREATE;
63     this.route.params.subscribe((params: { subsystem_nqn: string }) => {
64       this.subsystemNQN = params.subsystem_nqn;
65     });
66     this.setHosts();
67   }
68
69   createForm() {
70     this.listenerForm = new CdFormGroup({
71       host: new UntypedFormControl(null, {
72         validators: [Validators.required]
73       }),
74       trsvcid: new UntypedFormControl(4420, [
75         Validators.required,
76         CdValidators.number(false),
77         Validators.max(65535)
78       ])
79     });
80   }
81
82   buildRequest(): ListenerRequest {
83     const host = this.listenerForm.getValue('host');
84     let trsvcid = Number(this.listenerForm.getValue('trsvcid'));
85     if (!trsvcid) trsvcid = 4420;
86     const request = {
87       host_name: host.hostname,
88       traddr: host.addr,
89       trsvcid
90     };
91     return request;
92   }
93
94   onSubmit() {
95     const component = this;
96     const taskUrl: string = `nvmeof/listener/${URLVerbs.CREATE}`;
97     const request = this.buildRequest();
98     this.taskWrapperService
99       .wrapTaskAroundCall({
100         task: new FinishedTask(taskUrl, {
101           nqn: this.subsystemNQN,
102           host_name: request.host_name
103         }),
104         call: this.nvmeofService.createListener(this.subsystemNQN, request)
105       })
106       .subscribe({
107         error() {
108           component.listenerForm.setErrors({ cdSubmitButton: true });
109         },
110         complete: () => {
111           this.router.navigate([this.pageURL, { outlets: { modal: null } }]);
112         }
113       });
114   }
115 }