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 { map } from 'rxjs/operators';
17 import { forkJoin } from 'rxjs';
18 import { CephServiceSpec } from '~/app/shared/models/service.interface';
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;
37 public actionLabels: ActionLabelsI18n,
38 private authStorageService: AuthStorageService,
39 private taskWrapperService: TaskWrapperService,
40 private nvmeofService: NvmeofService,
41 private hostService: HostService,
42 private router: Router,
43 private route: ActivatedRoute,
44 public activeModal: NgbActiveModal,
45 public formatterService: FormatterService,
46 public dimlessBinaryPipe: DimlessBinaryPipe
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 gwGroups: this.nvmeofService.listGatewayGroups(),
57 hosts: this.hostService.getAllHosts()
60 map(({ gwGroups, hosts }) => {
61 // Find the gateway hosts in current group
62 const selectedGwGroup: CephServiceSpec = gwGroups?.[0]?.find(
63 (gwGroup: CephServiceSpec) => gwGroup?.spec?.group === this.group
65 const gatewayHosts: string[] = selectedGwGroup?.placement?.hosts;
66 // Return the gateway hosts in current group with their metadata
68 ? hosts.filter((host: any) => gatewayHosts.includes(host.hostname))
72 .subscribe((nvmeofHosts: any[]) => {
73 this.hosts = nvmeofHosts.map((h) => ({ hostname: h.hostname, addr: h.addr }));
79 this.action = this.actionLabels.CREATE;
80 this.route.params.subscribe((params: { subsystem_nqn: string }) => {
81 this.subsystemNQN = params?.subsystem_nqn;
83 this.route.queryParams.subscribe((params) => {
84 this.group = params?.['group'];
90 this.listenerForm = new CdFormGroup({
91 host: new UntypedFormControl(null, {
92 validators: [Validators.required]
94 trsvcid: new UntypedFormControl(4420, [
96 CdValidators.number(false),
102 buildRequest(): ListenerRequest {
103 const host = this.listenerForm.getValue('host');
104 let trsvcid = Number(this.listenerForm.getValue('trsvcid'));
105 if (!trsvcid) trsvcid = 4420;
106 const request: ListenerRequest = {
107 gw_group: this.group,
108 host_name: host.hostname,
116 const component = this;
117 const taskUrl: string = `nvmeof/listener/${URLVerbs.CREATE}`;
118 const request = this.buildRequest();
119 this.taskWrapperService
120 .wrapTaskAroundCall({
121 task: new FinishedTask(taskUrl, {
122 nqn: this.subsystemNQN,
123 host_name: request.host_name
125 call: this.nvmeofService.createListener(this.subsystemNQN, request)
129 component.listenerForm.setErrors({ cdSubmitButton: true });
132 this.router.navigate([this.pageURL, { outlets: { modal: null } }]);