| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 21 | import { HttpHeaders } from '@angular/common/http'; |
| 22 | import { Component, ElementRef, Injector, Input, OnInit, ViewChild } from '@angular/core'; |
| 23 | import { AbstractControl, FormArray, FormBuilder, FormGroup, Validators } from '@angular/forms'; |
| 24 | import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap'; |
| 25 | import { TranslateService } from '@ngx-translate/core'; |
| 26 | import { NotifierService } from 'angular-notifier'; |
| 27 | import { APIURLHEADER, ERRORDATA, MODALCLOSERESPONSEDATA, TYPESECTION, URLPARAMS } from 'CommonModel'; |
| 28 | import { environment } from 'environment'; |
| 29 | import * as jsyaml from 'js-yaml'; |
| 30 | import { INFRACONFIGPAYLOAD, KSU, OKA } from 'K8sModel'; |
| 31 | import { RestService } from 'RestService'; |
| 32 | import { 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 */ |
| 43 | export 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(); |
| 170 | } else { |
| 171 | this.isKsu = true; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | /** Generate primitive params @public */ |
| 176 | get okaParamsBuilder(): FormGroup { |
| 177 | return this.formBuilder.group({ |
| 178 | _id: [null], |
| 179 | sw_catalog_path: [null], |
| 180 | transformation: [null] |
| 181 | }); |
| 182 | } |
| 183 | |
| 184 | |
| 185 | /** On modal initializing forms @public */ |
| 186 | public KsuFormAction(): void { |
| 187 | this.KsuForm = this.formBuilder.group({ |
| 188 | name: ['', [Validators.required]], |
| 189 | description: ['', [Validators.required]], |
| 190 | profile_type: [null, [Validators.required]], |
| 191 | id: [null, [Validators.required]], |
| 192 | oka: this.formBuilder.array([]) |
| 193 | }); |
| 194 | } |
| 195 | |
| 196 | /** Handle FormArray Controls @public */ |
| 197 | public getControls(): AbstractControl[] { |
| 198 | return (this.KsuForm.get('oka') as FormArray).controls; |
| 199 | } |
| 200 | |
| 201 | /** convenience getter for easy access to form fields */ |
| 202 | get f(): FormGroup['controls'] { return this.KsuForm.controls; } |
| 203 | |
| 204 | /** Get OKA details @public */ |
| 205 | public okaDetails(): void { |
| 206 | this.isLoadingResults = true; |
| 207 | this.restService.getResource(environment.OKAPACKAGES_URL) |
| 208 | .subscribe((okaData: []): void => { |
| 209 | okaData.forEach((okaDetail: INFRACONFIGPAYLOAD): void => { |
| 210 | const oka: {} = { |
| 211 | name: okaDetail.name, |
| 212 | id: okaDetail._id |
| 213 | }; |
| 214 | this.okaData.push(oka); |
| 215 | }); |
| 216 | this.okaDetail = this.okaData; |
| 217 | this.isLoadingResults = false; |
| 218 | }, (error: ERRORDATA): void => { |
| 219 | this.isLoadingResults = false; |
| 220 | this.restService.handleError(error, 'get'); |
| 221 | }); |
| 222 | } |
| 223 | |
| 224 | /** Get KSU details @public */ |
| 225 | public getksuDetails(): void { |
| 226 | this.restService.getResource(environment.KSU_URL + '/' + this.profileID) |
| 227 | .subscribe((ksuData: KSU): void => { |
| 228 | this.KsuForm.patchValue({ id: ksuData.profile._id, name: ksuData.name, description: ksuData.description, profile_type: ksuData.profile.profile_type }); |
| 229 | this.getprofileDetails(this.KsuForm.value.profile_type, ksuData.profile.name); |
| 230 | ksuData.oka.forEach((data: OKA): void => { |
| 231 | this.okaArray = this.KsuForm.get('oka') as FormArray; |
| 232 | const transformations: string = JSON.stringify(data.transformation); |
| 233 | if (!isNullOrUndefined(data._id)) { |
| 234 | data.sw_catalog_path = null; |
| 235 | } else if (!isNullOrUndefined(data.sw_catalog_path)) { |
| 236 | data._id = null; |
| 237 | } |
| 238 | const okaFormGroup = this.formBuilder.group({ |
| 239 | _id: data._id, |
| 240 | sw_catalog_path: data.sw_catalog_path, |
| 241 | transformation: transformations |
| 242 | }); |
| 243 | this.okaArray.push(okaFormGroup); |
| 244 | }); |
| 245 | }, (error: ERRORDATA): void => { |
| 246 | this.restService.handleError(error, 'get'); |
| 247 | }); |
| 248 | } |
| 249 | |
| 250 | |
| 251 | /** On modal submit ksuAddSubmit will called @public */ |
| 252 | public KSUAddSubmit(): void { |
| 253 | if (this.profileType === 'move') { |
| 254 | this.profileUrl = environment.KSU_URL + '/' + this.profileID + '/move'; |
| 255 | this.payload = { |
| 256 | profile: { |
| 257 | _id: this.KsuForm.value.id, |
| 258 | profile_type: this.KsuForm.value.profile_type |
| 259 | } |
| 260 | }; |
| 261 | this.getFormControl('description').disable(); |
| 262 | this.getFormControl('oka').disable(); |
| 263 | this.getFormControl('name').disable(); |
| 264 | } else if (this.profileType === 'clone') { |
| 265 | this.getFormControl('description').disable(); |
| 266 | this.getFormControl('oka').disable(); |
| 267 | this.profileUrl = environment.KSU_URL + '/' + this.profileID + '/clone'; |
| 268 | this.payload = { |
| 269 | name: this.KsuForm.value.name, |
| 270 | profile: { |
| 271 | _id: this.KsuForm.value.id, |
| 272 | profile_type: this.KsuForm.value.profile_type |
| 273 | } |
| 274 | }; |
| 275 | } else if (this.profileType === 'edit' || this.profileType === 'add') { |
| 276 | this.addEditKSU(); |
| 277 | } |
| 278 | this.submitted = true; |
| 279 | this.sharedService.cleanForm(this.KsuForm); |
| 280 | if (this.KsuForm.invalid) { |
| 281 | return; |
| 282 | } |
| 283 | if (this.profileType === 'edit') { |
| 284 | this.editKSU(); |
| 285 | } else { |
| 286 | this.addKSU(); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | /** Add/Edit KSU @public */ |
| 291 | public addEditKSU(): void { |
| 292 | this.okaArray = this.KsuForm.get('oka') as FormArray; |
| 293 | const transValue = this.okaArray.at(0).get('transformation').value; |
| 294 | const validJSON: boolean = this.sharedService.checkJson(transValue); |
| 295 | if (validJSON) { |
| 296 | for (let i = 0; i < this.okaArray.length; i++) { |
| 297 | const formGroup = this.okaArray.at(i) as FormGroup; |
| 298 | formGroup.patchValue({ |
| 299 | transformation: JSON.parse(transValue) |
| 300 | }); |
| 301 | } |
| 302 | } else { |
| 303 | const getConfigJson: string = jsyaml.load(transValue, { json: true }); |
| 304 | for (let i = 0; i < this.okaArray.length; i++) { |
| 305 | const formGroup = this.okaArray.at(i) as FormGroup; |
| 306 | formGroup.patchValue({ |
| 307 | transformation: JSON.parse(getConfigJson) |
| 308 | }); |
| 309 | } |
| 310 | } |
| 311 | if (this.profileType === 'edit') { |
| 312 | this.profileUrl = environment.KSU_URL + '/' + this.profileID; |
| 313 | this.payload = { |
| 314 | name: this.KsuForm.value.name, |
| 315 | description: this.KsuForm.value.description, |
| 316 | profile: { |
| 317 | _id: this.KsuForm.value.id, |
| 318 | sw_catalog_path: this.KsuForm.value.sw_catalog_path, |
| 319 | profile_type: this.KsuForm.value.profile_type |
| 320 | }, |
| 321 | oka: this.KsuForm.value.oka |
| 322 | }; |
| 323 | } else { |
| 324 | this.profileUrl = environment.KSU_URL; |
| 325 | this.payload = { |
| 326 | ksus: [{ |
| 327 | name: this.KsuForm.value.name, |
| 328 | description: this.KsuForm.value.description, |
| 329 | profile: { |
| 330 | _id: this.KsuForm.value.id, |
| 331 | sw_catalog_path: this.KsuForm.value.sw_catalog_path, |
| 332 | profile_type: this.KsuForm.value.profile_type |
| 333 | }, |
| 334 | oka: this.KsuForm.value.oka |
| 335 | }] |
| 336 | }; |
| 337 | } |
| 338 | } |
| 339 | /** Add/move/clone KSU @public */ |
| 340 | public addKSU(): void { |
| 341 | this.isLoadingResults = true; |
| 342 | const modalData: MODALCLOSERESPONSEDATA = { |
| 343 | message: 'Done' |
| 344 | }; |
| 345 | const apiURLHeader: APIURLHEADER = { |
| 346 | url: this.profileUrl, |
| 347 | httpOptions: { headers: this.headers } |
| 348 | }; |
| 349 | this.restService.postResource(apiURLHeader, this.payload).subscribe((result: {}) => { |
| 350 | this.activeModal.close(modalData); |
| 351 | this.isLoadingResults = false; |
| 352 | if (this.profileType === 'move') { |
| 353 | this.notifierService.notify('success', |
| 354 | this.translateService.instant('PAGE.K8S.MOVEDSUCCESSFULLY')); |
| 355 | } else if (this.profileType === 'clone') { |
| 356 | this.notifierService.notify('success', |
| 357 | this.translateService.instant('PAGE.K8S.CLONEDSUCCESSFULLY')); |
| 358 | } else { |
| 359 | this.notifierService.notify('success', |
| 360 | this.translateService.instant('PAGE.K8S.KSUCREATEDSUCCESSFULLY')); |
| 361 | } |
| 362 | }, (error: ERRORDATA) => { |
| 363 | this.restService.handleError(error, 'post'); |
| 364 | // eslint-disable-next-line @typescript-eslint/no-magic-numbers |
| 365 | if (error.error.status === 422 || error.error.status === 400) { |
| 366 | this.activeModal.close(modalData); |
| 367 | } |
| 368 | this.isLoadingResults = false; |
| 369 | }); |
| 370 | } |
| 371 | |
| 372 | /** Edit KSU @public */ |
| 373 | public editKSU(): void { |
| 374 | this.isLoadingResults = true; |
| 375 | const modalData: MODALCLOSERESPONSEDATA = { |
| 376 | message: 'Done' |
| 377 | }; |
| 378 | const apiURLHeader: APIURLHEADER = { |
| 379 | url: this.profileUrl, |
| 380 | httpOptions: { headers: this.headers } |
| 381 | }; |
| 382 | this.restService.patchResource(apiURLHeader, this.payload).subscribe((result: {}) => { |
| 383 | this.activeModal.close(modalData); |
| 384 | this.isLoadingResults = false; |
| 385 | this.notifierService.notify('success', |
| 386 | this.translateService.instant('PAGE.K8S.KSUEDITEDSUCCESSFULLY')); |
| 387 | }, (error: ERRORDATA) => { |
| 388 | this.restService.handleError(error, 'post'); |
| 389 | // eslint-disable-next-line @typescript-eslint/no-magic-numbers |
| 390 | if (error.error.status === 422 || error.error.status === 400) { |
| 391 | this.activeModal.close(modalData); |
| 392 | } |
| 393 | this.isLoadingResults = false; |
| 394 | }); |
| 395 | } |
| 396 | |
| 397 | /** Config file process @private */ |
| 398 | public configFile(files: FileList): void { |
| 399 | if (files && files.length === 1) { |
| 400 | const fileFormat: string = this.sharedService.fetchFileExtension(files).toLocaleLowerCase(); |
| 401 | if (fileFormat === 'yaml' || fileFormat === 'yml') { |
| 402 | this.sharedService.getFileString(files, 'yaml').then((fileContent: string): void => { |
| 403 | this.okaArray = this.KsuForm.get('oka') as FormArray; |
| 404 | for (let i = 0; i < this.okaArray.length; i++) { |
| 405 | const formGroup = this.okaArray.at(i) as FormGroup; |
| 406 | formGroup.patchValue({ |
| 407 | transformation: fileContent // Set the value for the specified key |
| 408 | }); |
| 409 | } |
| 410 | }).catch((err: string): void => { |
| 411 | if (err === 'typeError') { |
| 412 | this.notifierService.notify('error', this.translateService.instant('YAMLFILETYPEERRROR')); |
| 413 | } else { |
| 414 | this.notifierService.notify('error', this.translateService.instant('ERROR')); |
| 415 | } |
| 416 | this.fileInputConfigLabel.nativeElement.innerText = this.translateService.instant('CHOOSEFILE'); |
| 417 | this.fileInputConfig.nativeElement.value = null; |
| 418 | }); |
| 419 | } else if (fileFormat === 'json') { |
| 420 | this.sharedService.getFileString(files, 'json').then((fileContent: string): void => { |
| 421 | const getConfigJson: string = jsyaml.load(fileContent, { json: true }); |
| 422 | this.KsuForm.get('transformation').setValue(JSON.stringify(getConfigJson)); |
| 423 | }).catch((err: string): void => { |
| 424 | if (err === 'typeError') { |
| 425 | this.notifierService.notify('error', this.translateService.instant('JSONFILETYPEERRROR')); |
| 426 | } else { |
| 427 | this.notifierService.notify('error', this.translateService.instant('ERROR')); |
| 428 | } |
| 429 | this.fileInputConfigLabel.nativeElement.innerText = this.translateService.instant('CHOOSEFILE'); |
| 430 | this.fileInputConfig.nativeElement.value = null; |
| 431 | }); |
| 432 | } |
| 433 | } else if (files && files.length > 1) { |
| 434 | this.notifierService.notify('error', this.translateService.instant('DROPFILESVALIDATION')); |
| 435 | } |
| 436 | this.fileInputConfigLabel.nativeElement.innerText = files[0].name; |
| 437 | this.fileInputConfig.nativeElement.value = null; |
| 438 | } |
| 439 | /** Add OKA @public */ |
| 440 | public addOka(): void { |
| 441 | this.okaFormArray = this.KsuForm.get('oka') as FormArray; |
| 442 | this.okaFormArray.push(this.okaParamsBuilder); |
| 443 | } |
| 444 | |
| 445 | /** Remove OKA @public */ |
| 446 | public removeMapping(index: number): void { |
| 447 | this.okaFormArray.removeAt(index); |
| 448 | } |
| 449 | |
| 450 | /** Get profile details @public */ |
| 451 | public getprofileDetails(event: string, name?: string): void { |
| 452 | this.profileData = []; |
| 453 | if (event === 'infra_config_profiles') { |
| 454 | this.profileUrl = environment.K8SINFRACONFIGPROFILE_URL; |
| 455 | } else if (event === 'infra_controller_profiles') { |
| 456 | this.profileUrl = environment.K8SINFRACONTROLLERPROFILE_URL; |
| 457 | } else if (event === 'app_profiles') { |
| 458 | this.profileUrl = environment.K8SAPPPROFILE_URL; |
| 459 | } else if (event === 'resource_profiles') { |
| 460 | this.profileUrl = environment.K8SRESOURCEPROFILE_URL; |
| 461 | } |
| 462 | this.restService.getResource(this.profileUrl) |
| 463 | .subscribe((profileData: []): void => { |
| 464 | profileData.forEach((profileDetail: INFRACONFIGPAYLOAD): void => { |
| 465 | const profile: {} = { |
| 466 | name: profileDetail.name, |
| 467 | id: profileDetail._id |
| 468 | }; |
| 469 | this.profileData.push(profile); |
| 470 | }); |
| 471 | this.profileDetails = this.profileData; |
| 472 | }, (error: ERRORDATA): void => { |
| 473 | this.restService.handleError(error, 'get'); |
| 474 | }); |
| 475 | } |
| 476 | /** Used to get the AbstractControl of controlName passed @private */ |
| 477 | private getFormControl(controlName: string): AbstractControl { |
| 478 | // eslint-disable-next-line security/detect-object-injection |
| 479 | return this.KsuForm.controls[controlName]; |
| 480 | } |
| 481 | } |