blob: db13e4af3f5837dd4605b1e6eb8fdf11f3c4a8eb [file] [log] [blame]
SANDHYA.JS26570112024-07-05 21:35:46 +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: SANDHYA JS (sandhya.j@tataelxsi.co.in)
17*/
18/**
19 * @file KSUAddComponent.ts.
20 */
21import { HttpHeaders } from '@angular/common/http';
22import { Component, ElementRef, Injector, Input, OnInit, ViewChild } from '@angular/core';
23import { AbstractControl, FormArray, FormBuilder, FormGroup, Validators } from '@angular/forms';
24import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
25import { TranslateService } from '@ngx-translate/core';
26import { NotifierService } from 'angular-notifier';
27import { APIURLHEADER, ERRORDATA, MODALCLOSERESPONSEDATA, TYPESECTION, URLPARAMS } from 'CommonModel';
28import { environment } from 'environment';
29import * as jsyaml from 'js-yaml';
30import { INFRACONFIGPAYLOAD, KSU, OKA } from 'K8sModel';
31import { RestService } from 'RestService';
32import { isNullOrUndefined, SharedService } from 'SharedService';
33/**
34 * Creating Component
35 * @Component takes KSUAddComponent.html as template url
36 */
37@Component({
38 selector: 'app-ksu-add',
39 templateUrl: './KSUAddComponent.html',
40 styleUrls: ['./KSUAddComponent.scss']
41})
42/** Exporting a class @exports KSUAddComponent */
43export class KSUAddComponent implements OnInit {
44 /** To inject services @public */
45 public injector: Injector;
46
47 /** FormGroup instance added to the form @ html @public */
48 public KsuForm: FormGroup;
49
50 /** Contains all deployment methods */
51 public profileSelect: TYPESECTION[] = [];
52
53 /** Instance for active modal service @public */
54 public activeModal: NgbActiveModal;
55
56 /** Form submission Add */
57 public submitted: boolean = false;
58
59 /** check move action */
60 public isMove: boolean = false;
61
62 /** check clone action */
63 public isClone: boolean = false;
64
65 /** check KSU */
66 public isKsu: boolean = false;
67
68 /** Check the loading results @public */
69 public isLoadingResults: boolean = false;
70
71 /** Give the message for the loading @public */
72 public message: string = 'PLEASEWAIT';
73
74 /** contains url @public */
75 public profileUrl: string;
76
77 /** contains profileData @public */
78 public profileData: {}[] = [];
79
80 /** OKA array @private */
81 private okaArray: FormArray;
82
83 /** contains profile details @public */
84 public profileDetails: {}[];
85
86 /** contains Oka Data @public */
87 public okaData: {}[] = [];
88
89 /** contains OKA details @public */
90 public okaDetail: {}[];
91
92 /** contains payload @public */
93 public payload: INFRACONFIGPAYLOAD | KSU;
94
95 /** oka Form array @private */
96 private okaFormArray: FormArray;
97
98 /** Input contains Modal dialog component Instance @public */
99 @Input() public profileType: string;
100
101 /** Input contains Modal dialog component Instance @public */
102 @Input() public profileID: string;
103
104 /** Element ref for fileInputConfig @public */
105 @ViewChild('fileInputConfig') fileInputConfig: ElementRef<HTMLInputElement>;
106
107 /** Element ref for fileInputConfigLabel @public */
108 @ViewChild('fileInputConfigLabel') fileInputConfigLabel: ElementRef<HTMLLabelElement>;
109
110 /** FormBuilder instance added to the formBuilder @private */
111 private formBuilder: FormBuilder;
112
113 /** Utilizes rest service for any CRUD operations @private */
114 private restService: RestService;
115
116 /** Notifier service to popup notification @private */
117 private notifierService: NotifierService;
118
119 /** Contains tranlsate instance @private */
120 private translateService: TranslateService;
121
122 /** Controls the header form @private */
123 private headers: HttpHeaders;
124
125 /** Contains all methods related to shared @private */
126 private sharedService: SharedService;
127
128 constructor(injector: Injector) {
129 this.injector = injector;
130 this.restService = this.injector.get(RestService);
131 this.activeModal = this.injector.get(NgbActiveModal);
132 this.formBuilder = this.injector.get(FormBuilder);
133 this.notifierService = this.injector.get(NotifierService);
134 this.translateService = this.injector.get(TranslateService);
135 this.sharedService = this.injector.get(SharedService);
136 this.profileSelect = [
137 {
138 title: 'Infra Config Profile',
139 value: 'infra_config_profiles'
140 },
141 {
142 title: 'Infra Controller Profile',
143 value: 'infra_controller_profiles'
144 }, {
145 title: 'App Profile',
146 value: 'app_profiles'
147 }, {
148 title: 'Resource Profile',
149 value: 'resource_profiles'
150 }
151 ];
152 }
153
154 public ngOnInit(): void {
155 /** On Initializing call the methods */
156 this.KsuFormAction();
157 this.headers = new HttpHeaders({
158 Accept: 'application/json',
159 'Content-Type': 'application/json',
160 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0'
161 });
162 this.okaDetails();
163 if (this.profileType === 'clone') {
164 this.isClone = true;
165 } else if (this.profileType === 'move') {
166 this.isMove = true;
167 } else if (this.profileType === 'edit') {
168 this.isKsu = true;
169 this.getksuDetails();
SANDHYA.JS92379ec2025-06-13 17:29:35 +0530170 } else if (this.profileType === 'card-add') {
171 this.isKsu = true;
SANDHYA.JS26570112024-07-05 21:35:46 +0530172 } else {
173 this.isKsu = true;
174 }
175 }
176
177 /** Generate primitive params @public */
178 get okaParamsBuilder(): FormGroup {
179 return this.formBuilder.group({
180 _id: [null],
181 sw_catalog_path: [null],
182 transformation: [null]
183 });
184 }
185
186
187 /** On modal initializing forms @public */
188 public KsuFormAction(): void {
189 this.KsuForm = this.formBuilder.group({
190 name: ['', [Validators.required]],
191 description: ['', [Validators.required]],
192 profile_type: [null, [Validators.required]],
193 id: [null, [Validators.required]],
194 oka: this.formBuilder.array([])
195 });
196 }
197
198 /** Handle FormArray Controls @public */
199 public getControls(): AbstractControl[] {
200 return (this.KsuForm.get('oka') as FormArray).controls;
201 }
202
203 /** convenience getter for easy access to form fields */
204 get f(): FormGroup['controls'] { return this.KsuForm.controls; }
205
206 /** Get OKA details @public */
207 public okaDetails(): void {
208 this.isLoadingResults = true;
209 this.restService.getResource(environment.OKAPACKAGES_URL)
210 .subscribe((okaData: []): void => {
211 okaData.forEach((okaDetail: INFRACONFIGPAYLOAD): void => {
212 const oka: {} = {
213 name: okaDetail.name,
214 id: okaDetail._id
215 };
216 this.okaData.push(oka);
217 });
218 this.okaDetail = this.okaData;
219 this.isLoadingResults = false;
220 }, (error: ERRORDATA): void => {
221 this.isLoadingResults = false;
222 this.restService.handleError(error, 'get');
223 });
224 }
225
226 /** Get KSU details @public */
227 public getksuDetails(): void {
228 this.restService.getResource(environment.KSU_URL + '/' + this.profileID)
229 .subscribe((ksuData: KSU): void => {
230 this.KsuForm.patchValue({ id: ksuData.profile._id, name: ksuData.name, description: ksuData.description, profile_type: ksuData.profile.profile_type });
231 this.getprofileDetails(this.KsuForm.value.profile_type, ksuData.profile.name);
232 ksuData.oka.forEach((data: OKA): void => {
233 this.okaArray = this.KsuForm.get('oka') as FormArray;
234 const transformations: string = JSON.stringify(data.transformation);
235 if (!isNullOrUndefined(data._id)) {
236 data.sw_catalog_path = null;
237 } else if (!isNullOrUndefined(data.sw_catalog_path)) {
238 data._id = null;
239 }
240 const okaFormGroup = this.formBuilder.group({
241 _id: data._id,
242 sw_catalog_path: data.sw_catalog_path,
243 transformation: transformations
244 });
245 this.okaArray.push(okaFormGroup);
246 });
247 }, (error: ERRORDATA): void => {
248 this.restService.handleError(error, 'get');
249 });
250 }
251
252
253 /** On modal submit ksuAddSubmit will called @public */
254 public KSUAddSubmit(): void {
255 if (this.profileType === 'move') {
256 this.profileUrl = environment.KSU_URL + '/' + this.profileID + '/move';
257 this.payload = {
258 profile: {
259 _id: this.KsuForm.value.id,
260 profile_type: this.KsuForm.value.profile_type
261 }
262 };
263 this.getFormControl('description').disable();
264 this.getFormControl('oka').disable();
265 this.getFormControl('name').disable();
266 } else if (this.profileType === 'clone') {
267 this.getFormControl('description').disable();
268 this.getFormControl('oka').disable();
269 this.profileUrl = environment.KSU_URL + '/' + this.profileID + '/clone';
270 this.payload = {
271 name: this.KsuForm.value.name,
272 profile: {
273 _id: this.KsuForm.value.id,
274 profile_type: this.KsuForm.value.profile_type
275 }
276 };
SANDHYA.JS92379ec2025-06-13 17:29:35 +0530277 } else if (this.profileType === 'edit' || this.profileType === 'add' || this.profileType === 'card-add') {
SANDHYA.JS26570112024-07-05 21:35:46 +0530278 this.addEditKSU();
279 }
280 this.submitted = true;
281 this.sharedService.cleanForm(this.KsuForm);
282 if (this.KsuForm.invalid) {
283 return;
284 }
285 if (this.profileType === 'edit') {
286 this.editKSU();
287 } else {
288 this.addKSU();
289 }
290 }
291
292 /** Add/Edit KSU @public */
293 public addEditKSU(): void {
294 this.okaArray = this.KsuForm.get('oka') as FormArray;
SANDHYA.JS92379ec2025-06-13 17:29:35 +0530295 const transValue = this.okaArray?.at(0)?.get('transformation').value;
296 if (!isNullOrUndefined(transValue)) {
297 const validJSON: boolean = this.sharedService.checkJson(transValue);
298 if (validJSON) {
299 for (let i = 0; i < this.okaArray.length; i++) {
300 const formGroup = this.okaArray.at(i) as FormGroup;
301 formGroup.patchValue({
302 transformation: JSON.parse(transValue)
303 });
304 }
305 } else {
306 const getConfigJson: string = jsyaml.load(transValue, { json: true });
307 for (let i = 0; i < this.okaArray.length; i++) {
308 const formGroup = this.okaArray.at(i) as FormGroup;
309 formGroup.patchValue({
310 transformation: JSON.parse(getConfigJson)
311 });
312 }
SANDHYA.JS26570112024-07-05 21:35:46 +0530313 }
314 }
315 if (this.profileType === 'edit') {
316 this.profileUrl = environment.KSU_URL + '/' + this.profileID;
317 this.payload = {
318 name: this.KsuForm.value.name,
319 description: this.KsuForm.value.description,
320 profile: {
321 _id: this.KsuForm.value.id,
322 sw_catalog_path: this.KsuForm.value.sw_catalog_path,
323 profile_type: this.KsuForm.value.profile_type
324 },
325 oka: this.KsuForm.value.oka
326 };
327 } else {
328 this.profileUrl = environment.KSU_URL;
329 this.payload = {
330 ksus: [{
331 name: this.KsuForm.value.name,
332 description: this.KsuForm.value.description,
333 profile: {
334 _id: this.KsuForm.value.id,
335 sw_catalog_path: this.KsuForm.value.sw_catalog_path,
336 profile_type: this.KsuForm.value.profile_type
337 },
338 oka: this.KsuForm.value.oka
339 }]
340 };
341 }
342 }
343 /** Add/move/clone KSU @public */
344 public addKSU(): void {
345 this.isLoadingResults = true;
346 const modalData: MODALCLOSERESPONSEDATA = {
347 message: 'Done'
348 };
349 const apiURLHeader: APIURLHEADER = {
350 url: this.profileUrl,
351 httpOptions: { headers: this.headers }
352 };
353 this.restService.postResource(apiURLHeader, this.payload).subscribe((result: {}) => {
354 this.activeModal.close(modalData);
355 this.isLoadingResults = false;
356 if (this.profileType === 'move') {
357 this.notifierService.notify('success',
358 this.translateService.instant('PAGE.K8S.MOVEDSUCCESSFULLY'));
359 } else if (this.profileType === 'clone') {
360 this.notifierService.notify('success',
361 this.translateService.instant('PAGE.K8S.CLONEDSUCCESSFULLY'));
362 } else {
363 this.notifierService.notify('success',
364 this.translateService.instant('PAGE.K8S.KSUCREATEDSUCCESSFULLY'));
365 }
366 }, (error: ERRORDATA) => {
367 this.restService.handleError(error, 'post');
368 // eslint-disable-next-line @typescript-eslint/no-magic-numbers
369 if (error.error.status === 422 || error.error.status === 400) {
370 this.activeModal.close(modalData);
371 }
372 this.isLoadingResults = false;
373 });
374 }
375
376 /** Edit KSU @public */
377 public editKSU(): void {
378 this.isLoadingResults = true;
379 const modalData: MODALCLOSERESPONSEDATA = {
380 message: 'Done'
381 };
382 const apiURLHeader: APIURLHEADER = {
383 url: this.profileUrl,
384 httpOptions: { headers: this.headers }
385 };
386 this.restService.patchResource(apiURLHeader, this.payload).subscribe((result: {}) => {
387 this.activeModal.close(modalData);
388 this.isLoadingResults = false;
389 this.notifierService.notify('success',
390 this.translateService.instant('PAGE.K8S.KSUEDITEDSUCCESSFULLY'));
391 }, (error: ERRORDATA) => {
392 this.restService.handleError(error, 'post');
393 // eslint-disable-next-line @typescript-eslint/no-magic-numbers
394 if (error.error.status === 422 || error.error.status === 400) {
395 this.activeModal.close(modalData);
396 }
397 this.isLoadingResults = false;
398 });
399 }
400
401 /** Config file process @private */
402 public configFile(files: FileList): void {
403 if (files && files.length === 1) {
404 const fileFormat: string = this.sharedService.fetchFileExtension(files).toLocaleLowerCase();
405 if (fileFormat === 'yaml' || fileFormat === 'yml') {
406 this.sharedService.getFileString(files, 'yaml').then((fileContent: string): void => {
407 this.okaArray = this.KsuForm.get('oka') as FormArray;
408 for (let i = 0; i < this.okaArray.length; i++) {
409 const formGroup = this.okaArray.at(i) as FormGroup;
410 formGroup.patchValue({
411 transformation: fileContent // Set the value for the specified key
412 });
413 }
414 }).catch((err: string): void => {
415 if (err === 'typeError') {
416 this.notifierService.notify('error', this.translateService.instant('YAMLFILETYPEERRROR'));
417 } else {
418 this.notifierService.notify('error', this.translateService.instant('ERROR'));
419 }
420 this.fileInputConfigLabel.nativeElement.innerText = this.translateService.instant('CHOOSEFILE');
421 this.fileInputConfig.nativeElement.value = null;
422 });
423 } else if (fileFormat === 'json') {
424 this.sharedService.getFileString(files, 'json').then((fileContent: string): void => {
425 const getConfigJson: string = jsyaml.load(fileContent, { json: true });
426 this.KsuForm.get('transformation').setValue(JSON.stringify(getConfigJson));
427 }).catch((err: string): void => {
428 if (err === 'typeError') {
429 this.notifierService.notify('error', this.translateService.instant('JSONFILETYPEERRROR'));
430 } else {
431 this.notifierService.notify('error', this.translateService.instant('ERROR'));
432 }
433 this.fileInputConfigLabel.nativeElement.innerText = this.translateService.instant('CHOOSEFILE');
434 this.fileInputConfig.nativeElement.value = null;
435 });
436 }
437 } else if (files && files.length > 1) {
438 this.notifierService.notify('error', this.translateService.instant('DROPFILESVALIDATION'));
439 }
440 this.fileInputConfigLabel.nativeElement.innerText = files[0].name;
441 this.fileInputConfig.nativeElement.value = null;
442 }
443 /** Add OKA @public */
444 public addOka(): void {
445 this.okaFormArray = this.KsuForm.get('oka') as FormArray;
446 this.okaFormArray.push(this.okaParamsBuilder);
447 }
448
449 /** Remove OKA @public */
450 public removeMapping(index: number): void {
451 this.okaFormArray.removeAt(index);
452 }
453
454 /** Get profile details @public */
455 public getprofileDetails(event: string, name?: string): void {
456 this.profileData = [];
SANDHYA.JS92379ec2025-06-13 17:29:35 +0530457 if (this.profileType === 'card-add') {
458 if (event === 'infra_config_profiles') {
459 this.profileUrl = environment.K8SCREATECLUSTER_URL + '/' + this.profileID + '/' + event;
460 } else if (event === 'infra_controller_profiles') {
461 this.profileUrl = environment.K8SCREATECLUSTER_URL + '/' + this.profileID + '/' + event;
462 } else if (event === 'app_profiles') {
463 this.profileUrl = environment.K8SCREATECLUSTER_URL + '/' + this.profileID + '/' + event;
464 } else if (event === 'resource_profiles') {
465 this.profileUrl = environment.K8SCREATECLUSTER_URL + '/' + this.profileID + '/' + event;
466 }
467 }
468 else {
469 if (event === 'infra_config_profiles') {
470 this.profileUrl = environment.K8SINFRACONFIGPROFILE_URL;
471 } else if (event === 'infra_controller_profiles') {
472 this.profileUrl = environment.K8SINFRACONTROLLERPROFILE_URL;
473 } else if (event === 'app_profiles') {
474 this.profileUrl = environment.K8SAPPPROFILE_URL;
475 } else if (event === 'resource_profiles') {
476 this.profileUrl = environment.K8SRESOURCEPROFILE_URL;
477 }
SANDHYA.JS26570112024-07-05 21:35:46 +0530478 }
479 this.restService.getResource(this.profileUrl)
480 .subscribe((profileData: []): void => {
481 profileData.forEach((profileDetail: INFRACONFIGPAYLOAD): void => {
482 const profile: {} = {
483 name: profileDetail.name,
484 id: profileDetail._id
485 };
486 this.profileData.push(profile);
487 });
488 this.profileDetails = this.profileData;
489 }, (error: ERRORDATA): void => {
490 this.restService.handleError(error, 'get');
491 });
492 }
493 /** Used to get the AbstractControl of controlName passed @private */
494 private getFormControl(controlName: string): AbstractControl {
495 // eslint-disable-next-line security/detect-object-injection
496 return this.KsuForm.controls[controlName];
497 }
498}