1 import { Component, OnInit } from '@angular/core';
2 import { FormControl, Validators } from '@angular/forms';
3 import { Router } from '@angular/router';
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';
15 selector: 'cd-host-form',
16 templateUrl: './host-form.component.html',
17 styleUrls: ['./host-form.component.scss']
19 export class HostFormComponent extends CdForm implements OnInit {
20 hostForm: CdFormGroup;
28 messages = new SelectMessages({
29 empty: $localize`There are no labels.`,
30 filter: $localize`Filter or add labels`,
31 add: $localize`Add label`
35 private router: Router,
36 private actionLabels: ActionLabelsI18n,
37 private hostService: HostService,
38 private taskWrapper: TaskWrapperService
41 this.resource = $localize`host`;
42 this.action = this.actionLabels.CREATE;
47 this.hostService.list().subscribe((resp: any[]) => {
48 this.hostnames = resp.map((host) => {
49 return host['hostname'];
55 private createForm() {
56 this.hostForm = new CdFormGroup({
57 hostname: new FormControl('', {
60 CdValidators.custom('uniqueName', (hostname: string) => {
61 return this.hostnames && this.hostnames.indexOf(hostname) !== -1;
65 addr: new FormControl('', {
66 validators: [CdValidators.ip()]
68 labels: new FormControl([]),
69 maintenance: new FormControl(false)
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;
80 task: new FinishedTask('host/' + URLVerbs.CREATE, {
83 call: this.hostService.create(hostname, this.addr, this.allLabels, this.status)
87 this.hostForm.setErrors({ cdSubmitButton: true });
90 this.router.navigate(['/hosts']);