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';
21 selector: 'cd-nvmeof-listeners-form',
22 templateUrl: './nvmeof-listeners-form.component.html',
23 styleUrls: ['./nvmeof-listeners-form.component.scss']
25 export class NvmeofListenersFormComponent implements OnInit {
27 permission: Permission;
28 hostPermission: Permission;
31 listenerForm: CdFormGroup;
33 hosts: Array<object> = null;
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
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';
56 daemons: this.daemonService.list(['nvmeof']),
57 hosts: this.hostService.getAllHosts()
60 map(({ daemons, hosts }) => {
61 const hostNamesFromDaemon = daemons.map((daemon: any) => daemon.hostname);
62 return hosts.filter((host: any) => hostNamesFromDaemon.includes(host.hostname));
65 .subscribe((nvmeofHosts: any[]) => {
66 this.hosts = nvmeofHosts.map((h) => ({ hostname: h.hostname, addr: h.addr }));
72 this.action = this.actionLabels.CREATE;
73 this.route.params.subscribe((params: { subsystem_nqn: string }) => {
74 this.subsystemNQN = params.subsystem_nqn;
80 this.listenerForm = new CdFormGroup({
81 host: new UntypedFormControl(null, {
82 validators: [Validators.required]
84 trsvcid: new UntypedFormControl(4420, [
86 CdValidators.number(false),
92 buildRequest(): ListenerRequest {
93 const host = this.listenerForm.getValue('host');
94 let trsvcid = Number(this.listenerForm.getValue('trsvcid'));
95 if (!trsvcid) trsvcid = 4420;
97 host_name: host.hostname,
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
114 call: this.nvmeofService.createListener(this.subsystemNQN, request)
118 component.listenerForm.setErrors({ cdSubmitButton: true });
121 this.router.navigate([this.pageURL, { outlets: { modal: null } }]);