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';
18 selector: 'cd-nvmeof-listeners-form',
19 templateUrl: './nvmeof-listeners-form.component.html',
20 styleUrls: ['./nvmeof-listeners-form.component.scss']
22 export class NvmeofListenersFormComponent implements OnInit {
24 permission: Permission;
25 hostPermission: Permission;
28 listenerForm: CdFormGroup;
30 hosts: Array<object> = null;
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
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';
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')
56 this.hosts = nvmeofHosts.map((h) => ({ hostname: h.hostname, addr: h.addr }));
62 this.action = this.actionLabels.CREATE;
63 this.route.params.subscribe((params: { subsystem_nqn: string }) => {
64 this.subsystemNQN = params.subsystem_nqn;
70 this.listenerForm = new CdFormGroup({
71 host: new UntypedFormControl(null, {
72 validators: [Validators.required]
74 trsvcid: new UntypedFormControl(4420, [
76 CdValidators.number(false),
82 buildRequest(): ListenerRequest {
83 const host = this.listenerForm.getValue('host');
84 let trsvcid = Number(this.listenerForm.getValue('trsvcid'));
85 if (!trsvcid) trsvcid = 4420;
87 host_name: host.hostname,
95 const component = this;
96 const taskUrl: string = `nvmeof/listener/${URLVerbs.CREATE}`;
97 const request = this.buildRequest();
98 this.taskWrapperService
100 task: new FinishedTask(taskUrl, {
101 nqn: this.subsystemNQN,
102 host_name: request.host_name
104 call: this.nvmeofService.createListener(this.subsystemNQN, request)
108 component.listenerForm.setErrors({ cdSubmitButton: true });
111 this.router.navigate([this.pageURL, { outlets: { modal: null } }]);