| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +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: 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 | */ |
| 21 | import { HttpHeaders } from '@angular/common/http'; |
| 22 | import { Component, ElementRef, Injector, OnInit, ViewChild } from '@angular/core'; |
| SANDHYA.JS | eb9c482 | 2023-10-11 16:29:27 +0530 | [diff] [blame] | 23 | import { AbstractControl, FormBuilder, FormGroup, Validators } from '@angular/forms'; |
| 24 | import { ActivatedRoute, Router } from '@angular/router'; |
| 25 | import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap'; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 26 | import { TranslateService } from '@ngx-translate/core'; |
| 27 | import { NotifierService } from 'angular-notifier'; |
| Barath Kumar R | d477b85 | 2020-07-07 15:24:05 +0530 | [diff] [blame] | 28 | import 'codemirror/addon/dialog/dialog'; |
| 29 | import 'codemirror/addon/display/autorefresh'; |
| 30 | import 'codemirror/addon/display/fullscreen'; |
| 31 | import 'codemirror/addon/edit/closebrackets'; |
| 32 | import 'codemirror/addon/edit/matchbrackets'; |
| 33 | import 'codemirror/addon/fold/brace-fold'; |
| 34 | import 'codemirror/addon/fold/foldcode'; |
| 35 | import 'codemirror/addon/fold/foldgutter'; |
| 36 | import 'codemirror/addon/search/search'; |
| 37 | import 'codemirror/addon/search/searchcursor'; |
| 38 | import 'codemirror/keymap/sublime'; |
| 39 | import 'codemirror/lib/codemirror'; |
| 40 | import 'codemirror/mode/javascript/javascript'; |
| 41 | import 'codemirror/mode/markdown/markdown'; |
| 42 | import 'codemirror/mode/yaml/yaml'; |
| 43 | import { |
| SANDHYA.JS | eb9c482 | 2023-10-11 16:29:27 +0530 | [diff] [blame] | 44 | APIURLHEADER, CONFIGCONSTANT, ERRORDATA, MODALCLOSERESPONSEDATA, TYPEAWS, TYPEAZURE, TYPEOPENSTACK, TYPEOPENVIMNEBULA, TYPEOTERS, |
| lloretgalleg | 3e906e2 | 2025-03-21 10:27:22 +0000 | [diff] [blame^] | 45 | TYPESECTION, TYPEVMWARE, TYPEVCENTER, VIM_TYPES |
| Barath Kumar R | d477b85 | 2020-07-07 15:24:05 +0530 | [diff] [blame] | 46 | } from 'CommonModel'; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 47 | import { environment } from 'environment'; |
| 48 | import * as jsyaml from 'js-yaml'; |
| 49 | import { RestService } from 'RestService'; |
| SANDHYA.JS | c84f112 | 2024-06-04 21:50:03 +0530 | [diff] [blame] | 50 | import { SharedService, isNullOrUndefined } from 'SharedService'; |
| SANDHYA.JS | 4a7a542 | 2021-05-15 15:35:22 +0530 | [diff] [blame] | 51 | import { VimAccountDetails } from 'VimAccountModel'; |
| SANDHYA.JS | eb9c482 | 2023-10-11 16:29:27 +0530 | [diff] [blame] | 52 | import { WarningComponent } from 'WarningComponent'; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 53 | /** |
| 54 | * Creating component |
| 55 | * @Component takes NewVimaccountComponent.html as template url |
| 56 | */ |
| 57 | @Component({ |
| 58 | selector: 'app-new-vimaccount', |
| 59 | templateUrl: './NewVimaccountComponent.html', |
| 60 | styleUrls: ['./NewVimaccountComponent.scss'] |
| 61 | }) |
| 62 | /** Exporting a class @exports NewVimaccountComponent */ |
| 63 | export class NewVimaccountComponent implements OnInit { |
| 64 | /** To inject services @public */ |
| 65 | public injector: Injector; |
| 66 | |
| 67 | /** FormGroup vim New Account added to the form @ html @public */ |
| 68 | public vimNewAccountForm: FormGroup; |
| 69 | |
| 70 | /** Supported Vim type for the dropdown */ |
| 71 | public vimType: TYPESECTION[]; |
| 72 | |
| 73 | /** Supported Vim type for the dropdown */ |
| 74 | public selectedVimType: string; |
| 75 | |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 76 | /** Form submission Add */ |
| 77 | public submitted: boolean = false; |
| 78 | |
| 79 | /** Showing more details of collapase */ |
| 80 | public isCollapsed: boolean = false; |
| 81 | |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 82 | /** Check the Projects loading results @public */ |
| 83 | public isLocationLoadingResults: boolean = false; |
| 84 | |
| 85 | /** Give the message for the loading @public */ |
| 86 | public message: string = 'PLEASEWAIT'; |
| 87 | |
| SANDHYA.JS | eb9c482 | 2023-10-11 16:29:27 +0530 | [diff] [blame] | 88 | /** Set the check value @public */ |
| 89 | public check: boolean = false; |
| 90 | |
| Barath Kumar R | d477b85 | 2020-07-07 15:24:05 +0530 | [diff] [blame] | 91 | /** Handle the formate Change @public */ |
| 92 | public defaults: {} = { |
| 93 | 'text/x-yaml': '' |
| 94 | }; |
| 95 | |
| 96 | /** To Set Mode @public */ |
| 97 | public mode: string = 'text/x-yaml'; |
| 98 | |
| 99 | /** To Set Mode @public */ |
| 100 | public modeDefault: string = 'yaml'; |
| 101 | |
| 102 | /** options @public */ |
| 103 | public options: {} = { |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame] | 104 | // eslint-disable-next-line no-invalid-this |
| Barath Kumar R | d477b85 | 2020-07-07 15:24:05 +0530 | [diff] [blame] | 105 | mode: this.modeDefault, |
| 106 | showCursorWhenSelecting: true, |
| SANDHYA.JS | eb9c482 | 2023-10-11 16:29:27 +0530 | [diff] [blame] | 107 | autofocus: false, |
| Barath Kumar R | d477b85 | 2020-07-07 15:24:05 +0530 | [diff] [blame] | 108 | autoRefresh: true, |
| 109 | lineNumbers: true, |
| 110 | lineWrapping: true, |
| 111 | foldGutter: true, |
| 112 | gutters: ['CodeMirror-linenumbers', 'CodeMirror-foldgutter'], |
| 113 | autoCloseBrackets: true, |
| 114 | matchBrackets: true, |
| 115 | theme: 'neat', |
| 116 | keyMap: 'sublime' |
| 117 | }; |
| 118 | |
| 119 | /** Data @public */ |
| 120 | public data: string = ''; |
| 121 | |
| SANDHYA.JS | eb9c482 | 2023-10-11 16:29:27 +0530 | [diff] [blame] | 122 | /** contains vim ID @public */ |
| 123 | public vimID: string; |
| 124 | |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 125 | /** Element ref for fileInput @public */ |
| 126 | @ViewChild('fileInput', { static: true }) public fileInput: ElementRef; |
| 127 | |
| 128 | /** Element ref for fileInput @public */ |
| 129 | @ViewChild('fileInputLabel', { static: true }) public fileInputLabel: ElementRef; |
| 130 | |
| SANDHYA.JS | db033b5 | 2024-09-20 12:17:59 +0530 | [diff] [blame] | 131 | /** Element ref for filecredsInput @public */ |
| 132 | @ViewChild('fileInputcreds', { static: true }) public fileInputcreds: ElementRef; |
| 133 | |
| 134 | /** Element ref for filecredsInput @public */ |
| 135 | @ViewChild('fileInputcredsLabel', { static: true }) public fileInputcredsLabel: ElementRef; |
| 136 | |
| Barath Kumar R | d477b85 | 2020-07-07 15:24:05 +0530 | [diff] [blame] | 137 | /** Contains all methods related to shared @private */ |
| 138 | public sharedService: SharedService; |
| 139 | |
| SANDHYA.JS | eb9c482 | 2023-10-11 16:29:27 +0530 | [diff] [blame] | 140 | /** Check key for the Edit form @public */ |
| 141 | public checkFormKeys: string[] = |
| 142 | [ |
| 143 | 'name', |
| 144 | 'vim_type', |
| 145 | 'vim_tenant_name', |
| 146 | 'description', |
| 147 | 'vim_url', |
| 148 | 'schema_type', |
| 149 | 'vim_user', |
| 150 | 'vim_password', |
| 151 | 'locationName', |
| 152 | 'latitude', |
| 153 | 'longitude', |
| 154 | 'config' |
| 155 | ]; |
| 156 | |
| 157 | /** Contains config details in edit @public */ |
| 158 | public config: {}; |
| 159 | |
| 160 | /** Contains latitude value @public */ |
| 161 | public latitude: string; |
| 162 | |
| 163 | /** Contains longitude value @public */ |
| 164 | public longitude: string; |
| 165 | |
| SANDHYA.JS | db033b5 | 2024-09-20 12:17:59 +0530 | [diff] [blame] | 166 | /** Contains credentials value in base64 @public */ |
| 167 | public credentialsAs64: string; |
| 168 | |
| SANDHYA.JS | eb9c482 | 2023-10-11 16:29:27 +0530 | [diff] [blame] | 169 | /** Contains location value @public */ |
| 170 | public locationName: string; |
| 171 | |
| 172 | /** Contains VIMAccount Details @private */ |
| 173 | private details: VimAccountDetails; |
| 174 | |
| 175 | /** Contains config with location @private */ |
| 176 | private configLocation: string; |
| 177 | |
| 178 | /** Check for config length @private */ |
| 179 | // eslint-disable-next-line @typescript-eslint/no-magic-numbers |
| 180 | private configLength: number = 3; |
| 181 | |
| SANDHYA.JS | 46ea49d | 2023-11-09 12:50:28 +0530 | [diff] [blame] | 182 | /** Contains config length from get api @private */ |
| 183 | private getConfigLength: number; |
| 184 | |
| 185 | /** Contains config when update @private */ |
| 186 | private updateConfig: object; |
| 187 | |
| 188 | /** Contains config length when update @private */ |
| 189 | private updateConfigLength: number; |
| 190 | |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 191 | /** Instance of the rest service @private */ |
| 192 | private restService: RestService; |
| 193 | |
| 194 | /** Holds the instance of router class @private */ |
| 195 | private router: Router; |
| 196 | |
| 197 | /** Controls the header form @private */ |
| 198 | private headers: HttpHeaders; |
| 199 | |
| 200 | /** FormBuilder instance added to the formBuilder @private */ |
| 201 | private formBuilder: FormBuilder; |
| 202 | |
| 203 | /** Notifier service to popup notification @private */ |
| 204 | private notifierService: NotifierService; |
| 205 | |
| 206 | /** Contains tranlsate instance @private */ |
| 207 | private translateService: TranslateService; |
| 208 | |
| SANDHYA.JS | eb9c482 | 2023-10-11 16:29:27 +0530 | [diff] [blame] | 209 | /** Holds teh instance of AuthService class of type AuthService @private */ |
| 210 | private activatedRoute: ActivatedRoute; |
| 211 | |
| 212 | /** Instance of the modal service @private */ |
| 213 | private modalService: NgbModal; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 214 | |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 215 | constructor(injector: Injector) { |
| 216 | this.injector = injector; |
| 217 | this.restService = this.injector.get(RestService); |
| 218 | this.formBuilder = this.injector.get(FormBuilder); |
| 219 | this.router = this.injector.get(Router); |
| 220 | this.notifierService = this.injector.get(NotifierService); |
| 221 | this.translateService = this.injector.get(TranslateService); |
| 222 | this.sharedService = this.injector.get(SharedService); |
| SANDHYA.JS | eb9c482 | 2023-10-11 16:29:27 +0530 | [diff] [blame] | 223 | this.activatedRoute = this.injector.get(ActivatedRoute); |
| 224 | this.modalService = this.injector.get(NgbModal); |
| Barath Kumar R | 1245fc8 | 2021-04-16 13:34:06 +0530 | [diff] [blame] | 225 | } |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 226 | |
| Barath Kumar R | 1245fc8 | 2021-04-16 13:34:06 +0530 | [diff] [blame] | 227 | /** convenience getter for easy access to form fields */ |
| 228 | get f(): FormGroup['controls'] { return this.vimNewAccountForm.controls; } |
| 229 | |
| 230 | /** |
| 231 | * Lifecyle Hooks the trigger before component is instantiate |
| 232 | */ |
| 233 | public ngOnInit(): void { |
| SANDHYA.JS | eb9c482 | 2023-10-11 16:29:27 +0530 | [diff] [blame] | 234 | this.vimID = this.activatedRoute.snapshot.paramMap.get('id'); |
| Barath Kumar R | 1245fc8 | 2021-04-16 13:34:06 +0530 | [diff] [blame] | 235 | this.vimType = VIM_TYPES; |
| 236 | this.headers = new HttpHeaders({ |
| 237 | Accept: 'application/json', |
| 238 | 'Content-Type': 'application/json', |
| 239 | 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0' |
| 240 | }); |
| 241 | this.initializeForm(); |
| SANDHYA.JS | eb9c482 | 2023-10-11 16:29:27 +0530 | [diff] [blame] | 242 | if (!isNullOrUndefined(this.vimID)) { |
| 243 | this.getVIMDetails(this.vimID); |
| 244 | } |
| Barath Kumar R | 1245fc8 | 2021-04-16 13:34:06 +0530 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | /** VIM Initialize Forms @public */ |
| 248 | public initializeForm(): void { |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 249 | this.vimNewAccountForm = this.formBuilder.group({ |
| 250 | name: [null, Validators.required], |
| 251 | vim_type: [null, Validators.required], |
| 252 | vim_tenant_name: [null, Validators.required], |
| 253 | description: [null], |
| 254 | vim_url: [null, [Validators.required, Validators.pattern(this.sharedService.REGX_URL_PATTERN)]], |
| 255 | schema_type: [''], |
| 256 | vim_user: [null, Validators.required], |
| 257 | vim_password: [null, Validators.required], |
| Barath Kumar R | 1245fc8 | 2021-04-16 13:34:06 +0530 | [diff] [blame] | 258 | locationName: [''], |
| 259 | latitude: ['', Validators.pattern(this.sharedService.REGX_LAT_PATTERN)], |
| 260 | longitude: ['', Validators.pattern(this.sharedService.REGX_LONG_PATTERN)], |
| Barath Kumar R | d477b85 | 2020-07-07 15:24:05 +0530 | [diff] [blame] | 261 | config: this.paramsBuilder() |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 262 | }); |
| 263 | } |
| 264 | |
| 265 | /** Generate params for config @public */ |
| 266 | public paramsBuilder(): FormGroup { |
| 267 | return this.formBuilder.group({ |
| Barath Kumar R | d477b85 | 2020-07-07 15:24:05 +0530 | [diff] [blame] | 268 | location: [null] |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 269 | }); |
| 270 | } |
| 271 | |
| SANDHYA.JS | eb9c482 | 2023-10-11 16:29:27 +0530 | [diff] [blame] | 272 | /** Fetching the vim details from get api @protected */ |
| 273 | private getVIMDetails(id: string): void { |
| 274 | this.isLocationLoadingResults = true; |
| 275 | this.restService.getResource(environment.VIMACCOUNTS_URL + '/' + id).subscribe((vimAccountsData: VimAccountDetails) => { |
| 276 | this.details = vimAccountsData; |
| 277 | if (!isNullOrUndefined(this.details.config.location)) { |
| 278 | this.configLocation = this.details.config.location; |
| 279 | if (this.configLocation.indexOf(',') !== -1) { |
| 280 | this.locationName = this.configLocation.split(',')[0]; |
| 281 | this.latitude = this.configLocation.split(',')[1]; |
| 282 | this.longitude = this.configLocation.split(',')[2]; |
| 283 | } |
| 284 | } |
| 285 | delete this.details.config.location; |
| SANDHYA.JS | 46ea49d | 2023-11-09 12:50:28 +0530 | [diff] [blame] | 286 | this.getConfigLength = Object.keys(this.details.config).length; |
| SANDHYA.JS | eb9c482 | 2023-10-11 16:29:27 +0530 | [diff] [blame] | 287 | this.getFormControl('schema_type').disable(); |
| 288 | this.getFormControl('vim_url').disable(); |
| 289 | this.getFormControl('vim_type').disable(); |
| 290 | this.config = { ...this.details.config }; |
| 291 | this.details.vim_password = ''; |
| 292 | this.setEditValue(this.details, this.checkFormKeys); |
| 293 | this.isLocationLoadingResults = false; |
| 294 | }, (error: ERRORDATA) => { |
| 295 | this.restService.handleError(error, 'get'); |
| 296 | this.isLocationLoadingResults = false; |
| 297 | }); |
| 298 | } |
| 299 | |
| 300 | /** Set the value for the Edit Section @public */ |
| 301 | public setEditValue(formValues: VimAccountDetails, checkKey: string[]): void { |
| 302 | Object.keys(formValues).forEach((name: string): void => { |
| 303 | if (checkKey.includes(name)) { |
| 304 | if (name === 'config') { |
| 305 | this.loadConfig(); |
| 306 | this.getFormControl('locationName').patchValue(this.locationName); |
| 307 | this.getFormControl('latitude').patchValue(this.latitude); |
| 308 | this.getFormControl('longitude').patchValue(this.longitude); |
| 309 | } |
| 310 | else { |
| 311 | // eslint-disable-next-line security/detect-object-injection |
| 312 | this.getFormControl(name).setValue(formValues[name], { onlySelf: true }); |
| 313 | this.getFormControl(name).updateValueAndValidity(); |
| 314 | } |
| 315 | } |
| 316 | }); |
| 317 | } |
| 318 | |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 319 | /** On modal submit newVimAccountSubmit will called @public */ |
| 320 | public newVimAccountSubmit(): void { |
| 321 | this.submitted = true; |
| Barath Kumar R | 1245fc8 | 2021-04-16 13:34:06 +0530 | [diff] [blame] | 322 | |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 323 | if (!this.vimNewAccountForm.invalid) { |
| 324 | this.isLocationLoadingResults = true; |
| Barath Kumar R | d477b85 | 2020-07-07 15:24:05 +0530 | [diff] [blame] | 325 | this.sharedService.cleanForm(this.vimNewAccountForm, 'vim'); |
| 326 | if (!isNullOrUndefined(this.data) && this.data !== '') { |
| 327 | Object.assign(this.vimNewAccountForm.value.config, jsyaml.load(this.data.toString(), { json: true })); |
| 328 | } else { |
| Barath Kumar R | 1245fc8 | 2021-04-16 13:34:06 +0530 | [diff] [blame] | 329 | Object.keys(this.vimNewAccountForm.value.config).forEach((res: string): void => { |
| Barath Kumar R | d477b85 | 2020-07-07 15:24:05 +0530 | [diff] [blame] | 330 | if (res !== 'location') { |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame] | 331 | // eslint-disable-next-line @typescript-eslint/no-dynamic-delete, security/detect-object-injection |
| Barath Kumar R | d477b85 | 2020-07-07 15:24:05 +0530 | [diff] [blame] | 332 | delete this.vimNewAccountForm.value.config[res]; |
| 333 | } |
| 334 | }); |
| 335 | } |
| Barath Kumar R | 1245fc8 | 2021-04-16 13:34:06 +0530 | [diff] [blame] | 336 | if (!isNullOrUndefined(this.vimNewAccountForm.value.latitude) && !isNullOrUndefined(this.vimNewAccountForm.value.longitude)) { |
| 337 | this.vimNewAccountForm.value.config.location = this.vimNewAccountForm.value.locationName + ',' + |
| 338 | this.vimNewAccountForm.value.longitude + ',' + |
| 339 | this.vimNewAccountForm.value.latitude; |
| 340 | } |
| Barath Kumar R | d477b85 | 2020-07-07 15:24:05 +0530 | [diff] [blame] | 341 | |
| SANDHYA.JS | db033b5 | 2024-09-20 12:17:59 +0530 | [diff] [blame] | 342 | if (!isNullOrUndefined(this.credentialsAs64)) { |
| 343 | this.vimNewAccountForm.value.config.credentials_base64 = this.credentialsAs64; |
| 344 | } |
| 345 | |
| Barath Kumar R | d477b85 | 2020-07-07 15:24:05 +0530 | [diff] [blame] | 346 | if (isNullOrUndefined(this.vimNewAccountForm.value.config.location)) { |
| 347 | delete this.vimNewAccountForm.value.config.location; |
| 348 | } |
| Barath Kumar R | 1245fc8 | 2021-04-16 13:34:06 +0530 | [diff] [blame] | 349 | |
| 350 | Object.keys(this.vimNewAccountForm.value.config).forEach((res: string): void => { |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame] | 351 | // eslint-disable-next-line security/detect-object-injection |
| Barath Kumar R | d477b85 | 2020-07-07 15:24:05 +0530 | [diff] [blame] | 352 | if (isNullOrUndefined(this.vimNewAccountForm.value.config[res]) || this.vimNewAccountForm.value.config[res] === '') { |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame] | 353 | // eslint-disable-next-line @typescript-eslint/no-dynamic-delete, security/detect-object-injection |
| Barath Kumar R | d477b85 | 2020-07-07 15:24:05 +0530 | [diff] [blame] | 354 | delete this.vimNewAccountForm.value.config[res]; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 355 | } |
| 356 | }); |
| SANDHYA.JS | 46ea49d | 2023-11-09 12:50:28 +0530 | [diff] [blame] | 357 | delete this.vimNewAccountForm.value.config.location; |
| 358 | if (!isNullOrUndefined(this.data)) { |
| 359 | this.updateConfig = jsyaml.load(this.data, { json: true }); |
| 360 | if (!isNullOrUndefined(this.updateConfig)) { |
| 361 | this.updateConfigLength = Object.keys(this.updateConfig).length; |
| 362 | } |
| 363 | } |
| 364 | if (this.updateConfig === undefined) { |
| 365 | this.notifierService.notify('warning', this.translateService.instant('PAGE.VIMDETAILS.VIMDELETE')); |
| 366 | this.isLocationLoadingResults = false; |
| 367 | } else if (this.getConfigLength > this.updateConfigLength) { |
| 368 | this.notifierService.notify('warning', this.translateService.instant('PAGE.VIMDETAILS.VIMEMPTY')); |
| 369 | this.isLocationLoadingResults = false; |
| 370 | } |
| 371 | if (!isNullOrUndefined(this.vimID) && ((this.getConfigLength <= this.updateConfigLength))) { |
| SANDHYA.JS | eb9c482 | 2023-10-11 16:29:27 +0530 | [diff] [blame] | 372 | this.editVIM(); |
| SANDHYA.JS | 46ea49d | 2023-11-09 12:50:28 +0530 | [diff] [blame] | 373 | } else if (isNullOrUndefined(this.vimID)) { |
| SANDHYA.JS | eb9c482 | 2023-10-11 16:29:27 +0530 | [diff] [blame] | 374 | this.createNewVIM(); |
| 375 | } |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | |
| Barath Kumar R | 1245fc8 | 2021-04-16 13:34:06 +0530 | [diff] [blame] | 379 | /** Create a new VIM Account @public */ |
| 380 | public createNewVIM(): void { |
| 381 | const apiURLHeader: APIURLHEADER = { |
| 382 | url: environment.VIMACCOUNTS_URL, |
| 383 | httpOptions: { headers: this.headers } |
| 384 | }; |
| 385 | delete this.vimNewAccountForm.value.locationName; |
| 386 | delete this.vimNewAccountForm.value.latitude; |
| 387 | delete this.vimNewAccountForm.value.longitude; |
| 388 | this.restService.postResource(apiURLHeader, this.vimNewAccountForm.value) |
| SANDHYA.JS | eb9c482 | 2023-10-11 16:29:27 +0530 | [diff] [blame] | 389 | .subscribe((result: { id: string }): void => { |
| Barath Kumar R | 1245fc8 | 2021-04-16 13:34:06 +0530 | [diff] [blame] | 390 | this.notifierService.notify('success', this.translateService.instant('PAGE.VIM.CREATEDSUCCESSFULLY')); |
| 391 | this.isLocationLoadingResults = false; |
| 392 | this.router.navigate(['vim/info/' + result.id]).catch((): void => { |
| 393 | // Error Cached; |
| 394 | }); |
| 395 | }, (error: ERRORDATA): void => { |
| 396 | this.restService.handleError(error, 'post'); |
| 397 | this.isLocationLoadingResults = false; |
| 398 | }); |
| 399 | } |
| 400 | |
| SANDHYA.JS | eb9c482 | 2023-10-11 16:29:27 +0530 | [diff] [blame] | 401 | /** Create a edit VIM Account @public */ |
| 402 | public editVIM(): void { |
| 403 | const apiURLHeader: APIURLHEADER = { |
| 404 | url: environment.VIMACCOUNTS_URL + '/' + this.vimID, |
| 405 | httpOptions: { headers: this.headers } |
| 406 | }; |
| 407 | delete this.vimNewAccountForm.value.locationName; |
| 408 | delete this.vimNewAccountForm.value.latitude; |
| 409 | delete this.vimNewAccountForm.value.longitude; |
| 410 | this.restService.patchResource(apiURLHeader, this.vimNewAccountForm.value) |
| 411 | .subscribe((result: { id: string }): void => { |
| 412 | this.notifierService.notify('success', this.translateService.instant('PAGE.VIM.UPDATEDSUCCESSFULLY')); |
| 413 | this.isLocationLoadingResults = false; |
| 414 | this.router.navigate(['vim/info/' + this.vimID]).catch((): void => { |
| 415 | // Error Cached; |
| 416 | }); |
| 417 | }, (error: ERRORDATA): void => { |
| 418 | this.restService.handleError(error, 'post'); |
| 419 | this.isLocationLoadingResults = false; |
| 420 | }); |
| 421 | } |
| Barath Kumar R | d477b85 | 2020-07-07 15:24:05 +0530 | [diff] [blame] | 422 | /** HandleChange function @public */ |
| 423 | public handleChange($event: string): void { |
| 424 | this.data = $event; |
| 425 | } |
| 426 | |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 427 | /** Routing to VIM Account Details Page @public */ |
| 428 | public onVimAccountBack(): void { |
| Barath Kumar R | 1245fc8 | 2021-04-16 13:34:06 +0530 | [diff] [blame] | 429 | this.router.navigate(['vim/details']).catch((): void => { |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 430 | // Error Cached |
| 431 | }); |
| 432 | } |
| 433 | |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 434 | /** Drag and drop feature and fetchind the details of files @private */ |
| 435 | public filesDropped(files: FileList): void { |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 436 | if (files && files.length === 1) { |
| 437 | this.sharedService.getFileString(files, 'yaml').then((fileContent: string): void => { |
| 438 | const getJson: string = jsyaml.load(fileContent, { json: true }); |
| Barath Kumar R | d477b85 | 2020-07-07 15:24:05 +0530 | [diff] [blame] | 439 | this.defaults['text/x-yaml'] = fileContent; |
| 440 | this.data = fileContent; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 441 | }).catch((err: string): void => { |
| 442 | if (err === 'typeError') { |
| 443 | this.notifierService.notify('error', this.translateService.instant('YAMLFILETYPEERRROR')); |
| 444 | } else { |
| 445 | this.notifierService.notify('error', this.translateService.instant('ERROR')); |
| 446 | } |
| 447 | this.fileInputLabel.nativeElement.innerText = this.translateService.instant('CHOOSEFILE'); |
| 448 | this.fileInput.nativeElement.value = null; |
| 449 | }); |
| 450 | } else if (files && files.length > 1) { |
| 451 | this.notifierService.notify('error', this.translateService.instant('DROPFILESVALIDATION')); |
| 452 | } |
| 453 | this.fileInputLabel.nativeElement.innerText = files[0].name; |
| 454 | this.fileInput.nativeElement.value = null; |
| 455 | } |
| Barath Kumar R | d477b85 | 2020-07-07 15:24:05 +0530 | [diff] [blame] | 456 | |
| SANDHYA.JS | db033b5 | 2024-09-20 12:17:59 +0530 | [diff] [blame] | 457 | /** Drag and drop feature and fetchind the details of credential files @private */ |
| 458 | public onFileSelected(event: Event): void { |
| 459 | const input = event.target as HTMLInputElement; |
| 460 | if (input && input.files && input.files.length > 0) { |
| 461 | const file: File = input.files[0]; |
| 462 | const reader: FileReader = new FileReader(); |
| 463 | reader.onload = (e: ProgressEvent<FileReader>) => { |
| 464 | const tomlContent = e.target?.result as string; |
| 465 | this.credentialsAs64 = btoa(tomlContent); |
| 466 | }; |
| 467 | reader.readAsText(file); |
| 468 | } |
| 469 | this.fileInputcredsLabel.nativeElement.innerText = input.files[0].name; |
| 470 | this.fileInputcreds.nativeElement.value = null; |
| 471 | } |
| 472 | |
| SANDHYA.JS | eb9c482 | 2023-10-11 16:29:27 +0530 | [diff] [blame] | 473 | /** Check data is empty or not to load config @public */ |
| 474 | public checkData(): void { |
| 475 | if (this.data !== '' && this.data.length > this.configLength) { |
| 476 | // eslint-disable-next-line security/detect-non-literal-fs-filename |
| 477 | const modalRef: NgbModalRef = this.modalService.open(WarningComponent, { backdrop: 'static' }); |
| 478 | modalRef.componentInstance.heading = this.translateService.instant('PAGE.VIMDETAILS.VIMHEADER'); |
| 479 | modalRef.componentInstance.confirmationMessage = this.translateService.instant('PAGE.VIMDETAILS.VIMCONTENT'); |
| 480 | modalRef.componentInstance.submitMessage = this.translateService.instant('PAGE.VIMDETAILS.VIMSUBMIT'); |
| 481 | modalRef.result.then((result: MODALCLOSERESPONSEDATA): void => { |
| 482 | if (result.message === CONFIGCONSTANT.done) { |
| 483 | this.loadSampleConfig(); |
| 484 | } |
| 485 | }).catch((): void => { |
| 486 | // Catch Navigation Error |
| 487 | }); |
| 488 | } else if (this.data.length < this.configLength || this.data === '') { |
| 489 | this.loadSampleConfig(); |
| 490 | } |
| 491 | } |
| 492 | |
| Barath Kumar R | d477b85 | 2020-07-07 15:24:05 +0530 | [diff] [blame] | 493 | /** Load sample config based on VIM type @public */ |
| 494 | public loadSampleConfig(): void { |
| Barath Kumar R | d477b85 | 2020-07-07 15:24:05 +0530 | [diff] [blame] | 495 | if (this.selectedVimType === 'openstack') { |
| 496 | this.defaults['text/x-yaml'] = jsyaml.dump(TYPEOPENSTACK); |
| 497 | this.data = JSON.stringify(TYPEOPENSTACK, null, '\t'); |
| 498 | } else if (this.selectedVimType === 'aws') { |
| 499 | this.defaults['text/x-yaml'] = jsyaml.dump(TYPEAWS); |
| 500 | this.data = JSON.stringify(TYPEAWS, null, '\t'); |
| lloretgalleg | 3e906e2 | 2025-03-21 10:27:22 +0000 | [diff] [blame^] | 501 | } else if (this.selectedVimType === 'vcenter') { |
| 502 | this.defaults['text/x-yaml'] = jsyaml.dump(TYPEVCENTER); |
| 503 | this.data = JSON.stringify(TYPEVCENTER, null, '\t'); |
| Barath Kumar R | d477b85 | 2020-07-07 15:24:05 +0530 | [diff] [blame] | 504 | } else if (this.selectedVimType === 'vmware') { |
| 505 | this.defaults['text/x-yaml'] = jsyaml.dump(TYPEVMWARE); |
| 506 | this.data = JSON.stringify(TYPEVMWARE, null, '\t'); |
| 507 | } else if (this.selectedVimType === 'openvim' || this.selectedVimType === 'opennebula') { |
| 508 | this.defaults['text/x-yaml'] = jsyaml.dump(TYPEOPENVIMNEBULA); |
| 509 | this.data = JSON.stringify(TYPEOPENVIMNEBULA, null, '\t'); |
| 510 | } else if (this.selectedVimType === 'azure' || this.selectedVimType === 'opennebula') { |
| 511 | this.defaults['text/x-yaml'] = jsyaml.dump(TYPEAZURE); |
| 512 | this.data = JSON.stringify(TYPEAZURE, null, '\t'); |
| 513 | } else { |
| 514 | this.defaults['text/x-yaml'] = jsyaml.dump(TYPEOTERS); |
| 515 | this.data = JSON.stringify(TYPEOTERS, null, '\t'); |
| 516 | } |
| 517 | } |
| 518 | |
| SANDHYA.JS | eb9c482 | 2023-10-11 16:29:27 +0530 | [diff] [blame] | 519 | /** Load sample config based on VIM type in edit @public */ |
| 520 | public loadConfig(): void { |
| 521 | this.clearConfig(); |
| 522 | if (this.details.vim_type === 'openstack') { |
| 523 | this.defaults['text/x-yaml'] = jsyaml.dump(this.config); |
| 524 | this.data = JSON.stringify(this.config, null, '\t'); |
| 525 | } else if (this.details.vim_type === 'aws') { |
| 526 | this.defaults['text/x-yaml'] = jsyaml.dump(this.config); |
| 527 | this.data = JSON.stringify(this.config, null, '\t'); |
| 528 | } else if (this.details.vim_type === 'vmware') { |
| 529 | this.defaults['text/x-yaml'] = jsyaml.dump(this.config); |
| 530 | this.data = JSON.stringify(this.config, null, '\t'); |
| 531 | } else if (this.details.vim_type === 'openvim' || this.details.vim_type === 'opennebula') { |
| 532 | this.defaults['text/x-yaml'] = jsyaml.dump(this.config); |
| 533 | this.data = JSON.stringify(this.config, null, '\t'); |
| 534 | } else if (this.details.vim_type === 'azure' || this.details.vim_type === 'opennebula') { |
| 535 | this.defaults['text/x-yaml'] = jsyaml.dump(this.config); |
| 536 | this.data = JSON.stringify(this.config, null, '\t'); |
| 537 | } else { |
| 538 | this.defaults['text/x-yaml'] = jsyaml.dump(this.config); |
| 539 | this.data = JSON.stringify(this.config, null, '\t'); |
| 540 | } |
| 541 | } |
| 542 | |
| Barath Kumar R | d477b85 | 2020-07-07 15:24:05 +0530 | [diff] [blame] | 543 | /** Clear config parameters @public */ |
| 544 | public clearConfig(): void { |
| SANDHYA.JS | eb9c482 | 2023-10-11 16:29:27 +0530 | [diff] [blame] | 545 | this.check = true; |
| 546 | if (this.data !== '' && this.data.length > this.configLength) { |
| 547 | // eslint-disable-next-line security/detect-non-literal-fs-filename |
| 548 | const modalRef: NgbModalRef = this.modalService.open(WarningComponent, { backdrop: 'static' }); |
| 549 | modalRef.componentInstance.heading = this.translateService.instant('PAGE.VIMDETAILS.VIMHEADER'); |
| 550 | modalRef.componentInstance.confirmationMessage = this.translateService.instant('PAGE.VIMDETAILS.CLEARCONTENT'); |
| 551 | modalRef.componentInstance.submitMessage = this.translateService.instant('PAGE.VIMDETAILS.VIMSUBMIT'); |
| 552 | modalRef.result.then((result: MODALCLOSERESPONSEDATA): void => { |
| 553 | if (result.message === CONFIGCONSTANT.done) { |
| 554 | this.defaults['text/x-yaml'] = ''; |
| 555 | this.data = ''; |
| 556 | this.fileInput.nativeElement.value = null; |
| 557 | } |
| 558 | }).catch((): void => { |
| 559 | // Catch Navigation Error |
| 560 | }); |
| 561 | } else { |
| 562 | this.defaults['text/x-yaml'] = ''; |
| 563 | this.data = ''; |
| 564 | this.fileInput.nativeElement.value = null; |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | /** Used to get the AbstractControl of controlName passed @private */ |
| 569 | private getFormControl(controlName: string): AbstractControl { |
| 570 | // eslint-disable-next-line security/detect-object-injection |
| 571 | return this.vimNewAccountForm.controls[controlName]; |
| Barath Kumar R | d477b85 | 2020-07-07 15:24:05 +0530 | [diff] [blame] | 572 | } |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 573 | } |