blob: 0c2b3d65a9cf465b51cd8bc540deb5c3f9d54569 [file] [log] [blame]
kumaran.m3b4814a2020-05-01 19:48:54 +05301/*
2 Copyright 2020 TATA ELXSI
3
4 Licensed under the Apache License, Version 2.0 (the 'License');
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15
16 Author: KUMARAN M (kumaran.m@tataelxsi.co.in), RAJESH S (rajesh.s@tataelxsi.co.in), BARATH KUMAR R (barath.r@tataelxsi.co.in)
17*/
18/**
19 * @file Vim Account Component.
20 */
21import { HttpHeaders } from '@angular/common/http';
22import { Component, ElementRef, Injector, OnInit, ViewChild } from '@angular/core';
23import { FormBuilder, FormGroup, Validators } from '@angular/forms';
24import { Router } from '@angular/router';
25import { TranslateService } from '@ngx-translate/core';
26import { NotifierService } from 'angular-notifier';
Barath Kumar Rd477b852020-07-07 15:24:05 +053027import 'codemirror/addon/dialog/dialog';
28import 'codemirror/addon/display/autorefresh';
29import 'codemirror/addon/display/fullscreen';
30import 'codemirror/addon/edit/closebrackets';
31import 'codemirror/addon/edit/matchbrackets';
32import 'codemirror/addon/fold/brace-fold';
33import 'codemirror/addon/fold/foldcode';
34import 'codemirror/addon/fold/foldgutter';
35import 'codemirror/addon/search/search';
36import 'codemirror/addon/search/searchcursor';
37import 'codemirror/keymap/sublime';
38import 'codemirror/lib/codemirror';
39import 'codemirror/mode/javascript/javascript';
40import 'codemirror/mode/markdown/markdown';
41import 'codemirror/mode/yaml/yaml';
42import {
43 APIURLHEADER, ERRORDATA, TYPEAWS, TYPEAZURE, TYPEOPENSTACK, TYPEOPENVIMNEBULA, TYPEOTERS,
44 TYPESECTION, TYPEVMWARE, VIM_TYPES
45} from 'CommonModel';
kumaran.m3b4814a2020-05-01 19:48:54 +053046import { environment } from 'environment';
47import * as jsyaml from 'js-yaml';
48import { RestService } from 'RestService';
49import { SharedService } from 'SharedService';
50import { isNullOrUndefined } from 'util';
51import { FEATURES, VIMLOCATION, VIMLOCATIONDATA } from 'VimAccountModel';
Barath Kumar Rd477b852020-07-07 15:24:05 +053052import { VimAccountDetails, VIMData } from 'VimAccountModel';
kumaran.m3b4814a2020-05-01 19:48:54 +053053
54/**
55 * Creating component
56 * @Component takes NewVimaccountComponent.html as template url
57 */
58@Component({
59 selector: 'app-new-vimaccount',
60 templateUrl: './NewVimaccountComponent.html',
61 styleUrls: ['./NewVimaccountComponent.scss']
62})
63/** Exporting a class @exports NewVimaccountComponent */
64export class NewVimaccountComponent implements OnInit {
65 /** To inject services @public */
66 public injector: Injector;
67
68 /** FormGroup vim New Account added to the form @ html @public */
69 public vimNewAccountForm: FormGroup;
70
71 /** Supported Vim type for the dropdown */
72 public vimType: TYPESECTION[];
73
74 /** Supported Vim type for the dropdown */
75 public selectedVimType: string;
76
77 /** Supported true and false value for the dropdown */
78 public boolValue: {}[];
79
80 /** Form submission Add */
81 public submitted: boolean = false;
82
83 /** Showing more details of collapase */
84 public isCollapsed: boolean = false;
85
86 /** Vim location values @public */
87 public getVIMLocation: VIMLOCATIONDATA[] = [];
88
89 /** Check the Projects loading results @public */
90 public isLocationLoadingResults: boolean = false;
91
92 /** Give the message for the loading @public */
93 public message: string = 'PLEASEWAIT';
94
95 /** set the longitude value of the selected place @public */
96 public setLong: number;
97
98 /** set the latitude value of the selected place @public */
99 public setLat: number;
100
Barath Kumar Rd477b852020-07-07 15:24:05 +0530101 /** Handle the formate Change @public */
102 public defaults: {} = {
103 'text/x-yaml': ''
104 };
105
106 /** To Set Mode @public */
107 public mode: string = 'text/x-yaml';
108
109 /** To Set Mode @public */
110 public modeDefault: string = 'yaml';
111
112 /** options @public */
113 public options: {} = {
114 mode: this.modeDefault,
115 showCursorWhenSelecting: true,
116 autofocus: true,
117 autoRefresh: true,
118 lineNumbers: true,
119 lineWrapping: true,
120 foldGutter: true,
121 gutters: ['CodeMirror-linenumbers', 'CodeMirror-foldgutter'],
122 autoCloseBrackets: true,
123 matchBrackets: true,
124 theme: 'neat',
125 keyMap: 'sublime'
126 };
127
128 /** Data @public */
129 public data: string = '';
130
131 /** Controls the File Type List form @public */
132 public fileTypes: { value: string; viewValue: string; }[] = [];
133
kumaran.m3b4814a2020-05-01 19:48:54 +0530134 /** Element ref for fileInput @public */
135 @ViewChild('fileInput', { static: true }) public fileInput: ElementRef;
136
137 /** Element ref for fileInput @public */
138 @ViewChild('fileInputLabel', { static: true }) public fileInputLabel: ElementRef;
139
Barath Kumar Rd477b852020-07-07 15:24:05 +0530140 /** Contains all methods related to shared @private */
141 public sharedService: SharedService;
142
kumaran.m3b4814a2020-05-01 19:48:54 +0530143 /** Instance of the rest service @private */
144 private restService: RestService;
145
146 /** Holds the instance of router class @private */
147 private router: Router;
148
149 /** Controls the header form @private */
150 private headers: HttpHeaders;
151
152 /** FormBuilder instance added to the formBuilder @private */
153 private formBuilder: FormBuilder;
154
155 /** Notifier service to popup notification @private */
156 private notifierService: NotifierService;
157
158 /** Contains tranlsate instance @private */
159 private translateService: TranslateService;
160
Barath Kumar Rd477b852020-07-07 15:24:05 +0530161 /** VIM Details @private */
162 private vimDetail: VimAccountDetails[];
kumaran.m3b4814a2020-05-01 19:48:54 +0530163
Barath Kumar Rd477b852020-07-07 15:24:05 +0530164 /** convenience getter for easy access to form fields */
165 get f(): FormGroup['controls'] { return this.vimNewAccountForm.controls; }
kumaran.m3b4814a2020-05-01 19:48:54 +0530166
167 constructor(injector: Injector) {
168 this.injector = injector;
169 this.restService = this.injector.get(RestService);
170 this.formBuilder = this.injector.get(FormBuilder);
171 this.router = this.injector.get(Router);
172 this.notifierService = this.injector.get(NotifierService);
173 this.translateService = this.injector.get(TranslateService);
174 this.sharedService = this.injector.get(SharedService);
175
176 /** Initializing Form Action */
177 this.vimNewAccountForm = this.formBuilder.group({
178 name: [null, Validators.required],
179 vim_type: [null, Validators.required],
180 vim_tenant_name: [null, Validators.required],
181 description: [null],
182 vim_url: [null, [Validators.required, Validators.pattern(this.sharedService.REGX_URL_PATTERN)]],
183 schema_type: [''],
184 vim_user: [null, Validators.required],
185 vim_password: [null, Validators.required],
Barath Kumar Rd477b852020-07-07 15:24:05 +0530186 config: this.paramsBuilder()
kumaran.m3b4814a2020-05-01 19:48:54 +0530187 });
188 }
189
190 /** Generate params for config @public */
191 public paramsBuilder(): FormGroup {
192 return this.formBuilder.group({
Barath Kumar Rd477b852020-07-07 15:24:05 +0530193 location: [null]
kumaran.m3b4814a2020-05-01 19:48:54 +0530194 });
195 }
196
kumaran.m3b4814a2020-05-01 19:48:54 +0530197 /**
198 * Lifecyle Hooks the trigger before component is instantiate
199 */
200 public ngOnInit(): void {
Barath Kumar Rd477b852020-07-07 15:24:05 +0530201 this.fileTypes = [{ value: 'text/x-yaml', viewValue: 'yaml' }];
kumaran.m3b4814a2020-05-01 19:48:54 +0530202 this.vimType = VIM_TYPES;
203 this.boolValue = [
204 { id: '', name: 'None' },
205 { id: true, name: 'True' },
206 { id: false, name: 'False' }
207 ];
208 this.headers = new HttpHeaders({
209 Accept: 'application/json',
210 'Content-Type': 'application/json',
211 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0'
212 });
Barath Kumar Rd477b852020-07-07 15:24:05 +0530213 this.getVIMDetails();
kumaran.m3b4814a2020-05-01 19:48:54 +0530214 }
215
216 /** On modal submit newVimAccountSubmit will called @public */
217 public newVimAccountSubmit(): void {
218 this.submitted = true;
219 if (!this.vimNewAccountForm.invalid) {
220 this.isLocationLoadingResults = true;
Barath Kumar Rd477b852020-07-07 15:24:05 +0530221 this.sharedService.cleanForm(this.vimNewAccountForm, 'vim');
222 if (!isNullOrUndefined(this.data) && this.data !== '') {
223 Object.assign(this.vimNewAccountForm.value.config, jsyaml.load(this.data.toString(), { json: true }));
224 } else {
225 Object.keys(this.vimNewAccountForm.value.config).forEach((res: string) => {
226 if (res !== 'location') {
227 delete this.vimNewAccountForm.value.config[res];
228 }
229 });
230 }
231
232 if (isNullOrUndefined(this.vimNewAccountForm.value.config.location)) {
233 delete this.vimNewAccountForm.value.config.location;
234 }
235 Object.keys(this.vimNewAccountForm.value.config).forEach((res: string) => {
236 if (isNullOrUndefined(this.vimNewAccountForm.value.config[res]) || this.vimNewAccountForm.value.config[res] === '') {
237 delete this.vimNewAccountForm.value.config[res];
kumaran.m3b4814a2020-05-01 19:48:54 +0530238 }
239 });
kumaran.m3b4814a2020-05-01 19:48:54 +0530240 const apiURLHeader: APIURLHEADER = {
241 url: environment.VIMACCOUNTS_URL,
242 httpOptions: { headers: this.headers }
243 };
244 this.restService.postResource(apiURLHeader, this.vimNewAccountForm.value)
245 .subscribe((result: {}) => {
246 this.notifierService.notify('success', this.translateService.instant('PAGE.VIM.CREATEDSUCCESSFULLY'));
247 this.isLocationLoadingResults = false;
248 this.router.navigate(['vim/details']).catch(() => {
249 // Error Cached;
250 });
251 // Post the New Vim data and reflect in the VIM Details Page.
252 }, (error: ERRORDATA) => {
kumaran.m3b4814a2020-05-01 19:48:54 +0530253 this.restService.handleError(error, 'post');
254 this.isLocationLoadingResults = false;
255 });
256 }
257 }
258
Barath Kumar Rd477b852020-07-07 15:24:05 +0530259 /** HandleChange function @public */
260 public handleChange($event: string): void {
261 this.data = $event;
262 }
263
kumaran.m3b4814a2020-05-01 19:48:54 +0530264 /** Routing to VIM Account Details Page @public */
265 public onVimAccountBack(): void {
266 this.router.navigate(['vim/details']).catch(() => {
267 // Error Cached
268 });
269 }
270
271 /** Fetching the location with name,latitude,longitude @public */
272 public fetchLocationLatLong(value: string): void {
273 this.isLocationLoadingResults = true;
274 const newVIMLocation: VIMLOCATIONDATA[] = [];
275 const locationTrack: string = environment.MAPLATLONGAPI_URL;
276 const locationAPIURL: string = locationTrack.replace('{value}', value);
277 this.restService.getResource(locationAPIURL).subscribe((result: VIMLOCATION) => {
278 result.features.forEach((getFeturesResult: FEATURES) => {
279 if ('extent' in getFeturesResult.properties) {
280 getFeturesResult.properties.extent.forEach((extentResult: number, index: number) => {
281 if (index === 0) {
282 this.setLong = extentResult;
283 }
284 if (index === 1) {
285 this.setLat = extentResult;
286 }
287 });
288 } else {
289 getFeturesResult.geometry.coordinates.forEach((coordinateResult: number, index: number) => {
290 if (index === 0) {
291 this.setLong = coordinateResult;
292 }
293 if (index === 1) {
294 this.setLat = coordinateResult;
295 }
296 });
297 }
298 newVIMLocation.push({
299 label: getFeturesResult.properties.name + ',' + getFeturesResult.properties.state + ', ' + getFeturesResult.properties.country,
300 value: getFeturesResult.properties.name + ',' + this.setLong + ',' + this.setLat
301 });
302 });
303 this.getVIMLocation = newVIMLocation;
304 this.isLocationLoadingResults = false;
305 }, (error: ERRORDATA) => {
306 this.restService.handleError(error, 'get');
307 this.isLocationLoadingResults = false;
308 });
309 }
310
311 /** Drag and drop feature and fetchind the details of files @private */
312 public filesDropped(files: FileList): void {
kumaran.m3b4814a2020-05-01 19:48:54 +0530313 if (files && files.length === 1) {
314 this.sharedService.getFileString(files, 'yaml').then((fileContent: string): void => {
315 const getJson: string = jsyaml.load(fileContent, { json: true });
Barath Kumar Rd477b852020-07-07 15:24:05 +0530316 this.defaults['text/x-yaml'] = fileContent;
317 this.data = fileContent;
kumaran.m3b4814a2020-05-01 19:48:54 +0530318 }).catch((err: string): void => {
319 if (err === 'typeError') {
320 this.notifierService.notify('error', this.translateService.instant('YAMLFILETYPEERRROR'));
321 } else {
322 this.notifierService.notify('error', this.translateService.instant('ERROR'));
323 }
324 this.fileInputLabel.nativeElement.innerText = this.translateService.instant('CHOOSEFILE');
325 this.fileInput.nativeElement.value = null;
326 });
327 } else if (files && files.length > 1) {
328 this.notifierService.notify('error', this.translateService.instant('DROPFILESVALIDATION'));
329 }
330 this.fileInputLabel.nativeElement.innerText = files[0].name;
331 this.fileInput.nativeElement.value = null;
332 }
Barath Kumar Rd477b852020-07-07 15:24:05 +0530333
334 /** Location chnage event in select box @public */
335 public locationChange(data: { value: string }): void {
336 this.vimDetail.forEach((vimAccountData: VimAccountDetails) => {
337 if (!isNullOrUndefined(vimAccountData.config.location) && !isNullOrUndefined(data)) {
338 if (vimAccountData.config.location === data.value) {
339 this.notifierService.notify('error', this.translateService.instant('PAGE.VIMDETAILS.LOCATIONERROR'));
340 // tslint:disable-next-line: no-backbone-get-set-outside-model
341 this.vimNewAccountForm.controls.config.get('location').setValue(null);
342 }
343 }
344 });
345 }
346
347 /** Load sample config based on VIM type @public */
348 public loadSampleConfig(): void {
349 this.clearConfig();
350 if (this.selectedVimType === 'openstack') {
351 this.defaults['text/x-yaml'] = jsyaml.dump(TYPEOPENSTACK);
352 this.data = JSON.stringify(TYPEOPENSTACK, null, '\t');
353 } else if (this.selectedVimType === 'aws') {
354 this.defaults['text/x-yaml'] = jsyaml.dump(TYPEAWS);
355 this.data = JSON.stringify(TYPEAWS, null, '\t');
356 } else if (this.selectedVimType === 'vmware') {
357 this.defaults['text/x-yaml'] = jsyaml.dump(TYPEVMWARE);
358 this.data = JSON.stringify(TYPEVMWARE, null, '\t');
359 } else if (this.selectedVimType === 'openvim' || this.selectedVimType === 'opennebula') {
360 this.defaults['text/x-yaml'] = jsyaml.dump(TYPEOPENVIMNEBULA);
361 this.data = JSON.stringify(TYPEOPENVIMNEBULA, null, '\t');
362 } else if (this.selectedVimType === 'azure' || this.selectedVimType === 'opennebula') {
363 this.defaults['text/x-yaml'] = jsyaml.dump(TYPEAZURE);
364 this.data = JSON.stringify(TYPEAZURE, null, '\t');
365 } else {
366 this.defaults['text/x-yaml'] = jsyaml.dump(TYPEOTERS);
367 this.data = JSON.stringify(TYPEOTERS, null, '\t');
368 }
369 }
370
371 /** Clear config parameters @public */
372 public clearConfig(): void {
373 this.defaults['text/x-yaml'] = '';
374 this.data = '';
375 this.fileInput.nativeElement.value = null;
376 }
377
378 /** Method to get VIM details @private */
379 private getVIMDetails(): void {
380 this.isLocationLoadingResults = true;
381 this.restService.getResource(environment.VIMACCOUNTS_URL).subscribe((vimAccountsData: VimAccountDetails[]) => {
382 this.vimDetail = vimAccountsData;
383 this.isLocationLoadingResults = false;
384 }, (error: ERRORDATA) => {
385 this.restService.handleError(error, 'get');
386 this.isLocationLoadingResults = false;
387 });
388 }
kumaran.m3b4814a2020-05-01 19:48:54 +0530389}