Fix mod ui build issues
[dcaegen2/platform.git] / mod2 / ui / src / app / ms-add-change / ms-add-change.component.ts
1 /* 
2  *  # ============LICENSE_START=======================================================
3  *  # Copyright (c) 2020 AT&T Intellectual Property. All rights reserved.
4  *  # ================================================================================
5  *  # Licensed under the Apache License, Version 2.0 (the "License");
6  *  # you may not use this file except in compliance with the License.
7  *  # You may obtain a copy of the License at
8  *  #
9  *  #      http://www.apache.org/licenses/LICENSE-2.0
10  *  #
11  *  # Unless required by applicable law or agreed to in writing, software
12  *  # distributed under the License is distributed on an "AS IS" BASIS,
13  *  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  # See the License for the specific language governing permissions and
15  *  # limitations under the License.
16  *  # ============LICENSE_END=========================================================
17  */
18
19 import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
20 import { InputTextModule } from 'primeng/inputtext';
21 import { DropdownModule } from 'primeng/dropdown';
22 import { FormGroup, FormBuilder, Validators, FormControl } from '@angular/forms';
23 import { AuthService } from '../services/auth.service';
24
25 interface Type {
26     type: string;
27 }
28 interface Location {
29     location: string;
30 }
31
32 @Component({
33     selector: 'app-ms-add-change',
34     templateUrl: './ms-add-change.component.html',
35     styleUrls: ['./ms-add-change.component.css']
36 })
37
38 export class MsAddChangeComponent implements OnInit {
39
40     guiHeader:    string = "Microservice ADD"
41     // Used for the Add/Update button label
42     addOrUpdate: string = "Add";
43
44     msAddForm: FormGroup;
45     // The loggged in user
46     username: string;
47
48     // Input form fields
49     name:        string;
50     tag:         string;
51     serviceName: string = "";
52     type:        string;
53     location:    string;
54     namespace:   string;
55     labels:      string;
56     notes:       string;
57
58     // Dropdowns
59     types:     Type[];
60     locations: Location[];
61
62     // Return JSON to parent component
63     msAddChangeString: any;
64     msAddChangeJson:   any;
65
66     constructor(private fb: FormBuilder, private authService: AuthService) {
67     }
68
69     @Input() visible: boolean;
70     @Input() currentRow: any;
71     @Output() handler: EventEmitter<any> = new EventEmitter();
72
73     ngOnInit() {
74         // The logged in user
75         this.username = this.authService.getUser().username;
76
77         this.msAddForm = new FormGroup({
78             name:        new FormControl(),
79             tag:         new FormControl(),
80             serviceName: new FormControl(),
81             type:        new FormControl(),
82             //location:    new FormControl(),
83             namespace:   new FormControl(),
84             labels:      new FormControl(),
85             notes:       new FormControl()
86         }); 
87
88         // FORM fields and validations
89         this.msAddForm = this.fb.group({
90             name:        ['', [Validators.required]],
91             tag:         ['', [Validators.required, Validators.pattern('^([a-z0-9](-[a-z0-9])*)+$'), Validators.minLength(5), Validators.maxLength(50)]],
92             serviceName: ['', [Validators.pattern('^([a-z](-[a-z])*)+$'), Validators.maxLength(25)]],
93             type:        ['', [Validators.required]],
94             //location:    ['', [Validators.required]],
95             namespace:   ['', [Validators.pattern('^([a-z0-9](-[a-z0-9])*)+$')]],
96             labels:      ['', []],
97             notes:       ['', []]
98             },
99             {updateOn: "blur"}
100         );
101
102         // TYPE Dropdown
103         this.types = [
104             { type: 'FM_COLLECTOR' },
105             { type: 'PM_COLLECTOR' },
106             { type: 'ANALYTIC' },
107             { type: 'TICK' },
108             { type: 'OTHER' }
109         ];
110         // LOCATION Dropdown
111         this.locations = [
112             { location: 'EDGE' },
113             { location: 'CENTRAL' },
114             { location: 'UNSPECIFIED' }
115         ];
116
117         // "Update" was selected, so populate the current row data in the GUI
118         if (this.currentRow) {
119             this.guiHeader = "Microservice Update";
120             this.addOrUpdate = "Update";
121             this.populateFields();
122         }
123
124     }
125
126     populateFields() {
127         let labelsStr: string;
128         if (this.currentRow['metadata']['labels']) {
129             labelsStr = this.currentRow['metadata']['labels'].join(' ')
130         }
131
132         // Prevent validation (length check) from failing in the html
133         if (this.currentRow['serviceName']) {
134              this.serviceName = this.currentRow['serviceName']
135         }
136
137         this.msAddForm.patchValue({
138             name:        this.currentRow['name'],
139             tag:         this.currentRow['tag'],
140             serviceName: this.serviceName,
141             type:        {type: this.currentRow['type']},
142             //location:    {location: this.currentRow['location']},
143             namespace:   this.currentRow['namespace'],
144             labels:      labelsStr,
145             notes:       this.currentRow['metadata']['notes']
146         })
147     }
148
149     // The handler emits 'null' back to parent to close dialog and make it available again when clicked
150     closeDialog() {
151         this.visible = false;
152         this.handler.emit(null);
153     }
154
155     // Create the JSON to be sent to the parent component
156     // The "labels" functions below take into account leading/trailing spaces, multiple spaces between labels, and conversion into an array
157     createOutputJson() {
158         this.msAddChangeString = {
159         name:        this.msAddForm.value['name'].trim(),
160         tag:         this.msAddForm.value['tag'],
161         serviceName: this.msAddForm.value['serviceName'],
162         type:        this.msAddForm.value['type'].type,
163         location:    'UNSPECIFIED',
164         //location:    this.msAddForm.value['location'].location,
165         namespace:   this.msAddForm.value['namespace'].trim(),
166         metadata: {
167             labels: this.msAddForm.value['labels'].trim().replace(/\s{2,}/g, ' ').split(" "),
168             notes:  this.msAddForm.value['notes']
169         },
170         user: this.username
171         };
172     }
173
174     saveMs() {
175         this.createOutputJson();
176         this.msAddChangeJson = JSON.stringify(this.msAddChangeString);
177         this.handler.emit(this.msAddChangeJson);
178     }
179
180 }