]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/ceph/cluster/hosts/host-form/host-form.component.ts
import 15.2.0 Octopus source
[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 import { I18n } from '@ngx-translate/i18n-polyfill';
5 import { HostService } from '../../../../shared/api/host.service';
6 import { ActionLabelsI18n, URLVerbs } from '../../../../shared/constants/app.constants';
7 import { CdFormGroup } from '../../../../shared/forms/cd-form-group';
8 import { CdValidators } from '../../../../shared/forms/cd-validators';
9 import { FinishedTask } from '../../../../shared/models/finished-task';
10 import { TaskWrapperService } from '../../../../shared/services/task-wrapper.service';
11
12 @Component({
13 selector: 'cd-host-form',
14 templateUrl: './host-form.component.html',
15 styleUrls: ['./host-form.component.scss']
16 })
17 export class HostFormComponent implements OnInit {
18 hostForm: CdFormGroup;
19 action: string;
20 resource: string;
21 loading = true;
22 hostnames: string[];
23
24 constructor(
25 private router: Router,
26 private i18n: I18n,
27 private actionLabels: ActionLabelsI18n,
28 private hostService: HostService,
29 private taskWrapper: TaskWrapperService
30 ) {
31 this.resource = this.i18n('host');
32 this.action = this.actionLabels.CREATE;
33 this.createForm();
34 }
35
36 ngOnInit() {
37 this.hostService.list().subscribe((resp: any[]) => {
38 this.hostnames = resp.map((host) => {
39 return host['hostname'];
40 });
41 this.loading = false;
42 });
43 }
44
45 private createForm() {
46 this.hostForm = new CdFormGroup({
47 hostname: new FormControl('', {
48 validators: [
49 Validators.required,
50 CdValidators.custom('uniqueName', (hostname: string) => {
51 return this.hostnames && this.hostnames.indexOf(hostname) !== -1;
52 })
53 ]
54 })
55 });
56 }
57
58 submit() {
59 const hostname = this.hostForm.get('hostname').value;
60 this.taskWrapper
61 .wrapTaskAroundCall({
62 task: new FinishedTask('host/' + URLVerbs.CREATE, {
63 hostname: hostname
64 }),
65 call: this.hostService.create(hostname)
66 })
67 .subscribe(
68 undefined,
69 () => {
70 this.hostForm.setErrors({ cdSubmitButton: true });
71 },
72 () => {
73 this.router.navigate(['/hosts']);
74 }
75 );
76 }
77 }