]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/hosts/host-form/host-form.component.ts
bump version to 16.2.6-pve2
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / ceph / cluster / hosts / host-form / host-form.component.ts
1 import { Component, OnInit } from '@angular/core';
2 import { FormControl, Validators } from '@angular/forms';
3 import { Router } from '@angular/router';
4
5 import { HostService } from '~/app/shared/api/host.service';
6 import { SelectMessages } from '~/app/shared/components/select/select-messages.model';
7 import { ActionLabelsI18n, URLVerbs } from '~/app/shared/constants/app.constants';
8 import { CdForm } from '~/app/shared/forms/cd-form';
9 import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
10 import { CdValidators } from '~/app/shared/forms/cd-validators';
11 import { FinishedTask } from '~/app/shared/models/finished-task';
12 import { TaskWrapperService } from '~/app/shared/services/task-wrapper.service';
13
14 @Component({
15 selector: 'cd-host-form',
16 templateUrl: './host-form.component.html',
17 styleUrls: ['./host-form.component.scss']
18 })
19 export class HostFormComponent extends CdForm implements OnInit {
20 hostForm: CdFormGroup;
21 action: string;
22 resource: string;
23 hostnames: string[];
24 addr: string;
25 status: string;
26 allLabels: any;
27
28 messages = new SelectMessages({
29 empty: $localize`There are no labels.`,
30 filter: $localize`Filter or add labels`,
31 add: $localize`Add label`
32 });
33
34 constructor(
35 private router: Router,
36 private actionLabels: ActionLabelsI18n,
37 private hostService: HostService,
38 private taskWrapper: TaskWrapperService
39 ) {
40 super();
41 this.resource = $localize`host`;
42 this.action = this.actionLabels.CREATE;
43 this.createForm();
44 }
45
46 ngOnInit() {
47 this.hostService.list().subscribe((resp: any[]) => {
48 this.hostnames = resp.map((host) => {
49 return host['hostname'];
50 });
51 this.loadingReady();
52 });
53 }
54
55 private createForm() {
56 this.hostForm = new CdFormGroup({
57 hostname: new FormControl('', {
58 validators: [
59 Validators.required,
60 CdValidators.custom('uniqueName', (hostname: string) => {
61 return this.hostnames && this.hostnames.indexOf(hostname) !== -1;
62 })
63 ]
64 }),
65 addr: new FormControl('', {
66 validators: [CdValidators.ip()]
67 }),
68 labels: new FormControl([]),
69 maintenance: new FormControl(false)
70 });
71 }
72
73 submit() {
74 const hostname = this.hostForm.get('hostname').value;
75 this.addr = this.hostForm.get('addr').value;
76 this.status = this.hostForm.get('maintenance').value ? 'maintenance' : '';
77 this.allLabels = this.hostForm.get('labels').value;
78 this.taskWrapper
79 .wrapTaskAroundCall({
80 task: new FinishedTask('host/' + URLVerbs.CREATE, {
81 hostname: hostname
82 }),
83 call: this.hostService.create(hostname, this.addr, this.allLabels, this.status)
84 })
85 .subscribe({
86 error: () => {
87 this.hostForm.setErrors({ cdSubmitButton: true });
88 },
89 complete: () => {
90 this.router.navigate(['/hosts']);
91 }
92 });
93 }
94 }