| 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 K8sAddClusterComponent.ts. |
| 20 | */ |
| 21 | import { HttpHeaders } from '@angular/common/http'; |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 22 | import { Component, ElementRef, Injector, Input, OnInit, ViewChild } from '@angular/core'; |
| SANDHYA.JS | 92379ec | 2025-06-13 17:29:35 +0530 | [diff] [blame^] | 23 | import { AbstractControl, FormArray, FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms'; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 24 | import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap'; |
| 25 | import { TranslateService } from '@ngx-translate/core'; |
| 26 | import { NotifierService } from 'angular-notifier'; |
| bacigalupo | f633dbf | 2022-03-25 17:24:56 +0000 | [diff] [blame] | 27 | import { APIURLHEADER, ERRORDATA, MODALCLOSERESPONSEDATA, TYPESECTION } from 'CommonModel'; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 28 | import { environment } from 'environment'; |
| 29 | import * as jsyaml from 'js-yaml'; |
| SANDHYA.JS | a3ff32a | 2025-02-13 16:24:46 +0530 | [diff] [blame] | 30 | import { K8SCLUSTERDATA, K8SPayload } from 'K8sModel'; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 31 | import { RestService } from 'RestService'; |
| SANDHYA.JS | b772de0 | 2024-12-10 15:21:03 +0530 | [diff] [blame] | 32 | import { isNullOrUndefined, SharedService } from 'SharedService'; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 33 | import { VimAccountDetails } from 'VimAccountModel'; |
| 34 | /** |
| 35 | * Creating Component |
| 36 | * @Component takes K8sAddClusterComponent.html as template url |
| 37 | */ |
| 38 | @Component({ |
| 39 | selector: 'app-k8s-add-cluster', |
| 40 | templateUrl: './K8sAddClusterComponent.html', |
| 41 | styleUrls: ['./K8sAddClusterComponent.scss'] |
| 42 | }) |
| 43 | /** Exporting a class @exports K8sAddClusterComponent */ |
| 44 | export class K8sAddClusterComponent implements OnInit { |
| 45 | /** To inject services @public */ |
| 46 | public injector: Injector; |
| 47 | |
| 48 | /** FormGroup instance added to the form @ html @public */ |
| 49 | public k8sclusterForm: FormGroup; |
| 50 | |
| 51 | /** Contains all vim account collections */ |
| 52 | public vimAccountSelect: VimAccountDetails; |
| 53 | |
| SANDHYA.JS | 92379ec | 2025-06-13 17:29:35 +0530 | [diff] [blame^] | 54 | /** Contains selected vim */ |
| 55 | public vimType: string; |
| 56 | |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 57 | /** Input contains Modal dialog component Instance @public */ |
| 58 | @Input() public profileType: string; |
| 59 | |
| 60 | /** Input contains Modal dialog component Instance @public */ |
| 61 | @Input() public profileID: string; |
| 62 | |
| bacigalupo | f633dbf | 2022-03-25 17:24:56 +0000 | [diff] [blame] | 63 | /** Contains all deployment methods */ |
| 64 | public deploymentMethodsSelect: TYPESECTION[] = []; |
| 65 | |
| 66 | /** Submited deployments methods format */ |
| 67 | public deploymentMethodsSubmit: Map<string, boolean>; |
| 68 | |
| 69 | /** Contains all deployment methods selected */ |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 70 | public selectedDeploymentMethods: string[] = ['helm-chart-v3', 'juju-bundle']; |
| bacigalupo | f633dbf | 2022-03-25 17:24:56 +0000 | [diff] [blame] | 71 | |
| SANDHYA.JS | a3ff32a | 2025-02-13 16:24:46 +0530 | [diff] [blame] | 72 | /** Contains all action types */ |
| SANDHYA.JS | 52af480 | 2025-05-22 17:00:15 +0530 | [diff] [blame] | 73 | public actionTypes: string[] = ['update', 'upgrade']; |
| SANDHYA.JS | a3ff32a | 2025-02-13 16:24:46 +0530 | [diff] [blame] | 74 | |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 75 | /** Instance for active modal service @public */ |
| 76 | public activeModal: NgbActiveModal; |
| 77 | |
| 78 | /** Variable set for twoway bindng @public */ |
| 79 | public vimAccountId: string; |
| 80 | |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 81 | /** contains url @public */ |
| 82 | public clusterUrl: string; |
| 83 | |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 84 | /** Form submission Add */ |
| 85 | public submitted: boolean = false; |
| 86 | |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 87 | /** contains payload */ |
| 88 | public payload: K8SPayload; |
| 89 | |
| SANDHYA.JS | b772de0 | 2024-12-10 15:21:03 +0530 | [diff] [blame] | 90 | /** Check the checkbox status */ |
| 91 | public isChecked: boolean = true; |
| 92 | |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 93 | /** Check the loading results @public */ |
| 94 | public isLoadingResults: boolean = false; |
| 95 | |
| 96 | /** Give the message for the loading @public */ |
| 97 | public message: string = 'PLEASEWAIT'; |
| 98 | |
| 99 | /** Element ref for fileInputNets @public */ |
| 100 | @ViewChild('fileInputNets', { static: true }) public fileInputNets: ElementRef; |
| 101 | |
| 102 | /** Element ref for fileInputNetsLabel @public */ |
| 103 | @ViewChild('fileInputNetsLabel', { static: true }) public fileInputNetsLabel: ElementRef; |
| 104 | |
| 105 | /** Element ref for fileInputCredentials @public */ |
| 106 | @ViewChild('fileInputCredentials', { static: true }) public fileInputCredentials: ElementRef; |
| 107 | |
| 108 | /** Element ref for fileInputCredentialsLabel @public */ |
| 109 | @ViewChild('fileInputCredentialsLabel', { static: true }) public fileInputCredentialsLabel: ElementRef; |
| 110 | |
| 111 | /** FormBuilder instance added to the formBuilder @private */ |
| 112 | private formBuilder: FormBuilder; |
| 113 | |
| 114 | /** Utilizes rest service for any CRUD operations @private */ |
| 115 | private restService: RestService; |
| 116 | |
| 117 | /** Notifier service to popup notification @private */ |
| 118 | private notifierService: NotifierService; |
| 119 | |
| 120 | /** Contains tranlsate instance @private */ |
| 121 | private translateService: TranslateService; |
| 122 | |
| 123 | /** Controls the header form @private */ |
| 124 | private headers: HttpHeaders; |
| 125 | |
| 126 | /** Contains all methods related to shared @private */ |
| 127 | private sharedService: SharedService; |
| 128 | |
| 129 | constructor(injector: Injector) { |
| 130 | this.injector = injector; |
| 131 | this.restService = this.injector.get(RestService); |
| 132 | this.activeModal = this.injector.get(NgbActiveModal); |
| 133 | this.formBuilder = this.injector.get(FormBuilder); |
| 134 | this.notifierService = this.injector.get(NotifierService); |
| 135 | this.translateService = this.injector.get(TranslateService); |
| 136 | this.sharedService = this.injector.get(SharedService); |
| bacigalupo | f633dbf | 2022-03-25 17:24:56 +0000 | [diff] [blame] | 137 | this.deploymentMethodsSelect = [ |
| 138 | { |
| bacigalupo | f633dbf | 2022-03-25 17:24:56 +0000 | [diff] [blame] | 139 | title: 'Helm v3', |
| 140 | value: 'helm-chart-v3' |
| 141 | }, |
| 142 | { |
| 143 | title: 'Juju bundle', |
| 144 | value: 'juju-bundle' |
| 145 | } |
| 146 | ]; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | public ngOnInit(): void { |
| 150 | /** On Initializing call the methods */ |
| 151 | this.k8sclusterFormAction(); |
| 152 | this.getDetailsvimAccount(); |
| 153 | this.headers = new HttpHeaders({ |
| 154 | Accept: 'application/json', |
| 155 | 'Content-Type': 'application/json', |
| 156 | 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0' |
| 157 | }); |
| SANDHYA.JS | a3ff32a | 2025-02-13 16:24:46 +0530 | [diff] [blame] | 158 | this.actionTypes.forEach((type: string): void => { |
| 159 | if (type === this.profileType) { |
| 160 | this.k8sClusterDetail(); |
| 161 | } |
| 162 | }); |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /** On modal initializing forms @public */ |
| 166 | public k8sclusterFormAction(): void { |
| 167 | this.k8sclusterForm = this.formBuilder.group({ |
| 168 | name: ['', [Validators.required]], |
| SANDHYA.JS | 92379ec | 2025-06-13 17:29:35 +0530 | [diff] [blame^] | 169 | k8s_version: [''], |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 170 | vim_account: [null, [Validators.required]], |
| SANDHYA.JS | b772de0 | 2024-12-10 15:21:03 +0530 | [diff] [blame] | 171 | description: [''], |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 172 | nets: ['', [Validators.required]], |
| bacigalupo | f633dbf | 2022-03-25 17:24:56 +0000 | [diff] [blame] | 173 | deployment_methods: ['', [Validators.required]], |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 174 | credentials: ['', [Validators.required]], |
| 175 | region_name: [''], |
| 176 | resource_group: [''], |
| 177 | node_count: ['', [Validators.required]], |
| SANDHYA.JS | b772de0 | 2024-12-10 15:21:03 +0530 | [diff] [blame] | 178 | node_size: ['', [Validators.required]], |
| 179 | bootstrap: [true], |
| 180 | k8sVersion: ['', [Validators.required]], |
| SANDHYA.JS | 92379ec | 2025-06-13 17:29:35 +0530 | [diff] [blame^] | 181 | iam_role: [''], |
| 182 | private_subnet: this.formBuilder.array([]), |
| 183 | public_subnet: this.formBuilder.array([]), |
| SANDHYA.JS | a3ff32a | 2025-02-13 16:24:46 +0530 | [diff] [blame] | 184 | update: [''] |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 185 | }); |
| 186 | } |
| 187 | |
| 188 | /** convenience getter for easy access to form fields */ |
| 189 | get f(): FormGroup['controls'] { return this.k8sclusterForm.controls; } |
| 190 | |
| SANDHYA.JS | 92379ec | 2025-06-13 17:29:35 +0530 | [diff] [blame^] | 191 | /** convenience getter for easy access to form array privatesubnet */ |
| 192 | get chipsprivateArray(): FormArray { |
| 193 | return this.k8sclusterForm.get('private_subnet') as FormArray; |
| 194 | } |
| 195 | |
| 196 | /** To add privatesubnet chips */ |
| 197 | public addprivateChips() { |
| 198 | const input = document.getElementById('private_subnet') as HTMLInputElement; |
| 199 | const value = input.value.trim(); |
| 200 | |
| 201 | if (value) { |
| 202 | this.chipsprivateArray.push(new FormControl(value)); |
| 203 | input.value = ''; |
| 204 | } |
| 205 | } |
| 206 | /** convenience getter for easy access to form array publicsubnet */ |
| 207 | get chipspublicArray(): FormArray { |
| 208 | return this.k8sclusterForm.get('public_subnet') as FormArray; |
| 209 | } |
| 210 | |
| 211 | /** To add publicsubnet chips */ |
| 212 | public addpublicChips() { |
| 213 | const input = document.getElementById('public_subnet') as HTMLInputElement; |
| 214 | const value = input.value.trim(); |
| 215 | |
| 216 | if (value) { |
| 217 | this.chipspublicArray.push(new FormControl(value)); |
| 218 | input.value = ''; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | /** To remove privatesubnet chips */ |
| 223 | public removeprivateChip(index: number) { |
| 224 | this.chipsprivateArray.removeAt(index); |
| 225 | } |
| 226 | |
| 227 | /** To remove privatesubnet last chip */ |
| 228 | public removeprivateLastChip(event: KeyboardEvent) { |
| 229 | const input = event.target as HTMLInputElement; |
| 230 | if (event.key === 'Backspace' && input.value === '' && this.chipsprivateArray.length > 0) { |
| 231 | this.chipsprivateArray.removeAt(this.chipsprivateArray.length - 1); // Remove last chip if input is empty |
| 232 | event.preventDefault(); |
| 233 | } |
| 234 | } |
| 235 | /** To remove publicsubnet chips */ |
| 236 | public removepublicChip(index: number) { |
| 237 | this.chipspublicArray.removeAt(index); |
| 238 | } |
| 239 | |
| 240 | /** To remove publicsubnet last chip */ |
| 241 | public removepublicLastChip(event: KeyboardEvent) { |
| 242 | const input = event.target as HTMLInputElement; |
| 243 | if (event.key === 'Backspace' && input.value === '' && this.chipspublicArray.length > 0) { |
| 244 | this.chipspublicArray.removeAt(this.chipspublicArray.length - 1); // Remove last chip if input is empty |
| 245 | event.preventDefault(); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 250 | /** Call the vimAccount details in the selection options @public */ |
| 251 | public getDetailsvimAccount(): void { |
| 252 | this.isLoadingResults = true; |
| 253 | this.restService.getResource(environment.VIMACCOUNTS_URL).subscribe((vimAccounts: VimAccountDetails) => { |
| 254 | this.vimAccountSelect = vimAccounts; |
| 255 | this.isLoadingResults = false; |
| 256 | }, (error: ERRORDATA) => { |
| 257 | this.restService.handleError(error, 'get'); |
| 258 | this.isLoadingResults = false; |
| 259 | }); |
| 260 | } |
| 261 | |
| SANDHYA.JS | a3ff32a | 2025-02-13 16:24:46 +0530 | [diff] [blame] | 262 | /** patch the form values for edit @public */ |
| 263 | public k8sClusterDetail(): void { |
| 264 | this.isLoadingResults = true; |
| 265 | this.restService.getResource(environment.K8SCREATECLUSTER_URL + '/' + this.profileID).subscribe((k8sData: K8SCLUSTERDATA) => { |
| 266 | if (this.profileType === 'update') { |
| 267 | this.k8sclusterForm.patchValue({ update: k8sData.name, description: !isNullOrUndefined(k8sData.description) ? k8sData.description : '' }); |
| 268 | } else if (this.profileType === 'upgrade') { |
| 269 | this.k8sclusterForm.patchValue({ k8sVersion: !isNullOrUndefined(k8sData.k8s_version) ? k8sData.k8s_version : '' }); |
| SANDHYA.JS | a3ff32a | 2025-02-13 16:24:46 +0530 | [diff] [blame] | 270 | } |
| 271 | this.isLoadingResults = false; |
| 272 | }, (error: ERRORDATA) => { |
| 273 | this.restService.handleError(error, 'get'); |
| 274 | this.isLoadingResults = false; |
| 275 | }); |
| 276 | } |
| 277 | |
| SANDHYA.JS | b772de0 | 2024-12-10 15:21:03 +0530 | [diff] [blame] | 278 | /** Call the event when checkbox is checked @public */ |
| 279 | public getValue(event: Event): void { |
| 280 | this.isChecked = (event.target as HTMLInputElement).checked; |
| 281 | } |
| 282 | |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 283 | |
| 284 | /** Contain selected vimAccount details @public */ |
| 285 | public getDetailsvim(event: VimAccountDetails): void { |
| 286 | this.vimAccountId = event._id; |
| SANDHYA.JS | 92379ec | 2025-06-13 17:29:35 +0530 | [diff] [blame^] | 287 | this.vimType = event.vim_type; |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 288 | } |
| 289 | |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 290 | /** On modal submit k8sAddClusterSubmit will called @public */ |
| 291 | public k8sAddClusterSubmit(): void { |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 292 | if (this.profileType === 'Manage') { |
| 293 | this.getFormControl('nets').disable(); |
| 294 | this.getFormControl('credentials').disable(); |
| 295 | this.getFormControl('deployment_methods').disable(); |
| SANDHYA.JS | b772de0 | 2024-12-10 15:21:03 +0530 | [diff] [blame] | 296 | this.getFormControl('k8sVersion').disable(); |
| SANDHYA.JS | a3ff32a | 2025-02-13 16:24:46 +0530 | [diff] [blame] | 297 | this.getFormControl('update').disable(); |
| SANDHYA.JS | 92379ec | 2025-06-13 17:29:35 +0530 | [diff] [blame^] | 298 | this.clusterUrl = environment.K8SCREATECLUSTER_URL; |
| 299 | if (this.vimType === 'aws') { |
| 300 | this.getFormControl('resource_group').disable(); |
| 301 | this.getFormControl('k8sVersion').disable(); |
| 302 | this.getFormControl('node_size').disable(); |
| 303 | this.getFormControl('node_count').disable(); |
| 304 | this.getFormControl('nets').disable(); |
| 305 | this.getFormControl('credentials').disable(); |
| 306 | this.getFormControl('update').disable(); |
| 307 | this.getFormControl('deployment_methods').disable(); |
| 308 | } else if (this.vimType !== 'aws') { |
| 309 | this.getFormControl('nets').disable(); |
| 310 | this.getFormControl('credentials').disable(); |
| 311 | this.getFormControl('deployment_methods').disable(); |
| 312 | this.getFormControl('k8sVersion').disable(); |
| 313 | this.getFormControl('update').disable(); |
| 314 | } |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 315 | this.manageCluster(); |
| SANDHYA.JS | b772de0 | 2024-12-10 15:21:03 +0530 | [diff] [blame] | 316 | } else if (this.profileType === 'Register' && this.isChecked === true) { |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 317 | this.clusterUrl = environment.K8SCREATECLUSTER_URL + '/register'; |
| 318 | this.getFormControl('region_name').disable(); |
| 319 | this.getFormControl('resource_group').disable(); |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 320 | this.getFormControl('k8s_version').disable(); |
| 321 | this.getFormControl('node_size').disable(); |
| SANDHYA.JS | b772de0 | 2024-12-10 15:21:03 +0530 | [diff] [blame] | 322 | this.getFormControl('node_count').disable(); |
| 323 | this.getFormControl('nets').disable(); |
| 324 | this.getFormControl('deployment_methods').disable(); |
| 325 | this.getFormControl('k8sVersion').disable(); |
| SANDHYA.JS | 92379ec | 2025-06-13 17:29:35 +0530 | [diff] [blame^] | 326 | this.getFormControl('iam_role').disable(); |
| 327 | this.getFormControl('private_subnet').disable(); |
| 328 | this.getFormControl('public_subnet').disable(); |
| SANDHYA.JS | a3ff32a | 2025-02-13 16:24:46 +0530 | [diff] [blame] | 329 | this.getFormControl('update').disable(); |
| SANDHYA.JS | b772de0 | 2024-12-10 15:21:03 +0530 | [diff] [blame] | 330 | this.registerCluster(); |
| 331 | } if (this.isChecked === false && this.profileType === 'Register') { |
| 332 | this.clusterUrl = environment.K8SCLUSTER_URL; |
| 333 | this.getFormControl('bootstrap').disable(); |
| 334 | this.getFormControl('region_name').disable(); |
| 335 | this.getFormControl('resource_group').disable(); |
| 336 | this.getFormControl('node_count').disable(); |
| 337 | this.getFormControl('node_size').disable(); |
| 338 | this.getFormControl('k8sVersion').disable(); |
| SANDHYA.JS | 92379ec | 2025-06-13 17:29:35 +0530 | [diff] [blame^] | 339 | this.getFormControl('iam_role').disable(); |
| 340 | this.getFormControl('private_subnet').disable(); |
| 341 | this.getFormControl('public_subnet').disable(); |
| SANDHYA.JS | a3ff32a | 2025-02-13 16:24:46 +0530 | [diff] [blame] | 342 | this.getFormControl('update').disable(); |
| SANDHYA.JS | b772de0 | 2024-12-10 15:21:03 +0530 | [diff] [blame] | 343 | this.oldregisterCluster(); |
| SANDHYA.JS | 52af480 | 2025-05-22 17:00:15 +0530 | [diff] [blame] | 344 | } else if (this.profileType === 'upgrade') { |
| 345 | this.clusterUrl = environment.K8SCREATECLUSTER_URL + '/' + this.profileID + '/' + 'upgrade'; |
| SANDHYA.JS | b772de0 | 2024-12-10 15:21:03 +0530 | [diff] [blame] | 346 | this.getFormControl('region_name').disable(); |
| 347 | this.getFormControl('resource_group').disable(); |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 348 | this.getFormControl('nets').disable(); |
| 349 | this.getFormControl('credentials').disable(); |
| 350 | this.getFormControl('deployment_methods').disable(); |
| 351 | this.getFormControl('name').disable(); |
| 352 | this.getFormControl('vim_account').disable(); |
| 353 | this.getFormControl('description').disable(); |
| SANDHYA.JS | b772de0 | 2024-12-10 15:21:03 +0530 | [diff] [blame] | 354 | this.getFormControl('bootstrap').disable(); |
| 355 | this.getFormControl('node_count').disable(); |
| 356 | this.getFormControl('node_size').disable(); |
| SANDHYA.JS | 92379ec | 2025-06-13 17:29:35 +0530 | [diff] [blame^] | 357 | this.getFormControl('iam_role').disable(); |
| 358 | this.getFormControl('private_subnet').disable(); |
| 359 | this.getFormControl('public_subnet').disable(); |
| SANDHYA.JS | a3ff32a | 2025-02-13 16:24:46 +0530 | [diff] [blame] | 360 | this.getFormControl('update').disable(); |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 361 | this.updateCluster(); |
| SANDHYA.JS | a3ff32a | 2025-02-13 16:24:46 +0530 | [diff] [blame] | 362 | } else if (this.profileType === 'update') { |
| 363 | this.clusterUrl = environment.K8SCREATECLUSTER_URL + '/' + this.profileID; |
| 364 | this.getFormControl('bootstrap').disable(); |
| 365 | this.getFormControl('region_name').disable(); |
| 366 | this.getFormControl('resource_group').disable(); |
| 367 | this.getFormControl('nets').disable(); |
| 368 | this.getFormControl('credentials').disable(); |
| 369 | this.getFormControl('deployment_methods').disable(); |
| 370 | this.getFormControl('vim_account').disable(); |
| 371 | this.getFormControl('node_count').disable(); |
| 372 | this.getFormControl('node_size').disable(); |
| 373 | this.getFormControl('k8s_version').disable(); |
| SANDHYA.JS | a3ff32a | 2025-02-13 16:24:46 +0530 | [diff] [blame] | 374 | this.getFormControl('k8sVersion').disable(); |
| SANDHYA.JS | 92379ec | 2025-06-13 17:29:35 +0530 | [diff] [blame^] | 375 | this.getFormControl('iam_role').disable(); |
| 376 | this.getFormControl('private_subnet').disable(); |
| 377 | this.getFormControl('public_subnet').disable(); |
| SANDHYA.JS | a3ff32a | 2025-02-13 16:24:46 +0530 | [diff] [blame] | 378 | this.getFormControl('name').disable(); |
| SANDHYA.JS | a3ff32a | 2025-02-13 16:24:46 +0530 | [diff] [blame] | 379 | this.editCluster(); |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 380 | } |
| 381 | } |
| 382 | |
| SANDHYA.JS | b772de0 | 2024-12-10 15:21:03 +0530 | [diff] [blame] | 383 | /** Old Register cluster flow @public */ |
| 384 | public oldregisterCluster(): void { |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 385 | this.submitted = true; |
| 386 | this.sharedService.cleanForm(this.k8sclusterForm); |
| 387 | if (this.k8sclusterForm.invalid) { |
| 388 | return; |
| 389 | } |
| 390 | const modalData: MODALCLOSERESPONSEDATA = { |
| 391 | message: 'Done' |
| 392 | }; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 393 | const validJSONCredentails: boolean = this.sharedService.checkJson(this.k8sclusterForm.value.credentials); |
| 394 | if (validJSONCredentails) { |
| 395 | this.k8sclusterForm.value.credentials = jsyaml.load(this.k8sclusterForm.value.credentials.toString(), { json: true }); |
| 396 | } else { |
| 397 | this.notifierService.notify('error', this.translateService.instant('INVALIDCONFIG')); |
| 398 | return; |
| 399 | } |
| 400 | const validJSONNets: boolean = this.sharedService.checkJson(this.k8sclusterForm.value.nets); |
| 401 | if (validJSONNets) { |
| 402 | this.k8sclusterForm.value.nets = jsyaml.load(this.k8sclusterForm.value.nets.toString(), { json: true }); |
| 403 | } else { |
| 404 | this.notifierService.notify('error', this.translateService.instant('INVALIDCONFIG')); |
| 405 | return; |
| 406 | } |
| bacigalupo | f633dbf | 2022-03-25 17:24:56 +0000 | [diff] [blame] | 407 | |
| 408 | this.deploymentMethodsSubmit = new Map<string, boolean>(); |
| 409 | /// Set deployment method Map |
| 410 | for (const methods of this.deploymentMethodsSelect) { |
| 411 | this.deploymentMethodsSubmit.set(methods.value, false); |
| 412 | } |
| 413 | |
| 414 | this.k8sclusterForm.value.deployment_methods.forEach((dm: string): void => { |
| 415 | this.deploymentMethodsSubmit.set(dm, true); |
| 416 | }); |
| 417 | // Transform Map to json object |
| 418 | const jsonDMObject: {} = {}; |
| 419 | this.deploymentMethodsSubmit.forEach((value: boolean, key: string): void => { |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame] | 420 | // eslint-disable-next-line security/detect-object-injection |
| bacigalupo | f633dbf | 2022-03-25 17:24:56 +0000 | [diff] [blame] | 421 | jsonDMObject[key] = value; |
| 422 | }); |
| 423 | |
| 424 | // Transform values to json |
| 425 | this.k8sclusterForm.value.deployment_methods = jsonDMObject; |
| 426 | |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 427 | this.k8sclusterForm.value.vim_account = this.vimAccountId; |
| 428 | |
| SANDHYA.JS | b772de0 | 2024-12-10 15:21:03 +0530 | [diff] [blame] | 429 | if (this.k8sclusterForm.value.description === '') { |
| 430 | delete this.k8sclusterForm.value.description; |
| 431 | } |
| 432 | |
| 433 | const apiURLHeader: APIURLHEADER = { |
| 434 | url: this.clusterUrl, |
| 435 | httpOptions: { headers: this.headers } |
| 436 | }; |
| 437 | |
| 438 | this.isLoadingResults = true; |
| 439 | this.restService.postResource(apiURLHeader, this.k8sclusterForm.value).subscribe((result: {}) => { |
| 440 | this.activeModal.close(modalData); |
| 441 | this.isLoadingResults = false; |
| 442 | this.notifierService.notify('success', this.k8sclusterForm.value.name + |
| 443 | this.translateService.instant('PAGE.K8S.REGISTEREDSUCCESSFULLY')); |
| 444 | }, (error: ERRORDATA) => { |
| 445 | this.restService.handleError(error, 'post'); |
| 446 | this.isLoadingResults = false; |
| 447 | }); |
| 448 | } |
| 449 | |
| 450 | /** New Register cluster flow @public */ |
| 451 | public registerCluster(): void { |
| 452 | this.submitted = true; |
| 453 | this.sharedService.cleanForm(this.k8sclusterForm); |
| 454 | if (this.k8sclusterForm.invalid) { |
| 455 | return; |
| 456 | } |
| 457 | const modalData: MODALCLOSERESPONSEDATA = { |
| 458 | message: 'Done' |
| 459 | }; |
| 460 | const validJSONCredentails: boolean = this.sharedService.checkJson(this.k8sclusterForm.value.credentials); |
| 461 | if (validJSONCredentails) { |
| 462 | this.k8sclusterForm.value.credentials = jsyaml.load(this.k8sclusterForm.value.credentials.toString(), { json: true }); |
| 463 | } else { |
| 464 | this.notifierService.notify('error', this.translateService.instant('INVALIDCONFIG')); |
| 465 | return; |
| 466 | } |
| 467 | |
| 468 | if (this.k8sclusterForm.value.description === '') { |
| 469 | delete this.k8sclusterForm.value.description; |
| 470 | } |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 471 | const apiURLHeader: APIURLHEADER = { |
| 472 | url: this.clusterUrl, |
| 473 | httpOptions: { headers: this.headers } |
| 474 | }; |
| 475 | |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 476 | this.isLoadingResults = true; |
| 477 | this.restService.postResource(apiURLHeader, this.k8sclusterForm.value).subscribe((result: {}) => { |
| 478 | this.activeModal.close(modalData); |
| 479 | this.isLoadingResults = false; |
| 480 | this.notifierService.notify('success', this.k8sclusterForm.value.name + |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 481 | this.translateService.instant('PAGE.K8S.REGISTEREDSUCCESSFULLY')); |
| 482 | }, (error: ERRORDATA) => { |
| 483 | this.restService.handleError(error, 'post'); |
| 484 | this.isLoadingResults = false; |
| 485 | }); |
| 486 | } |
| 487 | |
| SANDHYA.JS | 92379ec | 2025-06-13 17:29:35 +0530 | [diff] [blame^] | 488 | /** controlplane cluster flow @public */ |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 489 | public manageCluster(): void { |
| 490 | this.submitted = true; |
| 491 | this.sharedService.cleanForm(this.k8sclusterForm); |
| 492 | if (this.k8sclusterForm.invalid) { |
| 493 | return; |
| 494 | } |
| 495 | const modalData: MODALCLOSERESPONSEDATA = { |
| 496 | message: 'Done' |
| 497 | }; |
| 498 | const apiURLHeader: APIURLHEADER = { |
| SANDHYA.JS | 92379ec | 2025-06-13 17:29:35 +0530 | [diff] [blame^] | 499 | url: this.clusterUrl, |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 500 | httpOptions: { headers: this.headers } |
| 501 | }; |
| 502 | |
| 503 | this.isLoadingResults = true; |
| SANDHYA.JS | 92379ec | 2025-06-13 17:29:35 +0530 | [diff] [blame^] | 504 | const formData = this.k8sclusterForm.value; |
| 505 | if (this.k8sclusterForm.value.private_subnet.length <= 0) { |
| 506 | delete this.k8sclusterForm.value.private_subnet; |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 507 | } |
| SANDHYA.JS | 92379ec | 2025-06-13 17:29:35 +0530 | [diff] [blame^] | 508 | if (this.k8sclusterForm.value.public_subnet.length <= 0) { |
| 509 | delete this.k8sclusterForm.value.public_subnet; |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 510 | } |
| SANDHYA.JS | 92379ec | 2025-06-13 17:29:35 +0530 | [diff] [blame^] | 511 | if (this.vimType !== 'aws') { |
| 512 | this.payload = { |
| 513 | name: this.k8sclusterForm.value.name, |
| 514 | vim_account: this.k8sclusterForm.value.vim_account, |
| 515 | location: this.k8sclusterForm.value.location, |
| 516 | region_name: this.k8sclusterForm.value.region_name, |
| 517 | resource_group: this.k8sclusterForm.value.resource_group, |
| 518 | k8s_version: this.k8sclusterForm.value.k8s_version, |
| 519 | node_size: this.k8sclusterForm.value.node_size, |
| 520 | node_count: Number(this.k8sclusterForm.value.node_count), |
| 521 | description: this.k8sclusterForm.value.description, |
| 522 | bootstrap: Boolean(this.k8sclusterForm.value.bootstrap) |
| 523 | }; |
| 524 | if (this.k8sclusterForm.value.region_name === '') { |
| 525 | delete this.payload.region_name; |
| 526 | } |
| 527 | if (this.k8sclusterForm.value.resource_group === '') { |
| 528 | delete this.payload.resource_group; |
| 529 | } |
| 530 | if (this.k8sclusterForm.value.description === '') { |
| 531 | delete this.payload.description; |
| 532 | } |
| 533 | } else { |
| 534 | this.payload = Object.keys(formData).reduce((acc, key) => { |
| 535 | // eslint-disable-next-line security/detect-object-injection |
| 536 | if (formData[key] !== null && formData[key] !== undefined && formData[key] !== '') { |
| 537 | // eslint-disable-next-line security/detect-object-injection |
| 538 | acc[key] = formData[key]; |
| 539 | } |
| 540 | return acc; |
| 541 | }, {}); |
| 542 | } |
| 543 | this.restService.postResource(apiURLHeader, this.payload).subscribe((result: {}) => { |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 544 | this.activeModal.close(modalData); |
| 545 | this.isLoadingResults = false; |
| 546 | this.notifierService.notify('success', this.k8sclusterForm.value.name + |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 547 | this.translateService.instant('PAGE.K8S.CREATEDSUCCESSFULLY')); |
| 548 | }, (error: ERRORDATA) => { |
| 549 | this.restService.handleError(error, 'post'); |
| 550 | this.isLoadingResults = false; |
| 551 | }); |
| 552 | } |
| 553 | |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 554 | /** Update cluster @public */ |
| 555 | public updateCluster(): void { |
| 556 | this.submitted = true; |
| 557 | this.sharedService.cleanForm(this.k8sclusterForm); |
| 558 | if (this.k8sclusterForm.invalid) { |
| 559 | return; |
| 560 | } |
| 561 | const modalData: MODALCLOSERESPONSEDATA = { |
| 562 | message: 'Done' |
| 563 | }; |
| 564 | const apiURLHeader: APIURLHEADER = { |
| 565 | url: this.clusterUrl, |
| 566 | httpOptions: { headers: this.headers } |
| 567 | }; |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 568 | if (this.profileType === 'upgrade') { |
| 569 | this.payload = { |
| SANDHYA.JS | b772de0 | 2024-12-10 15:21:03 +0530 | [diff] [blame] | 570 | k8s_version: this.k8sclusterForm.value.k8sVersion |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 571 | }; |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 572 | } |
| SANDHYA.JS | b772de0 | 2024-12-10 15:21:03 +0530 | [diff] [blame] | 573 | this.isLoadingResults = true; |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 574 | this.restService.postResource(apiURLHeader, this.payload).subscribe((result: {}) => { |
| 575 | this.activeModal.close(modalData); |
| 576 | this.isLoadingResults = false; |
| 577 | this.notifierService.notify('success', |
| 578 | this.translateService.instant('PAGE.K8S.UPDATEDSUCCESSFULLY')); |
| 579 | }, (error: ERRORDATA) => { |
| 580 | this.restService.handleError(error, 'post'); |
| 581 | this.isLoadingResults = false; |
| 582 | }); |
| 583 | } |
| 584 | |
| SANDHYA.JS | 92379ec | 2025-06-13 17:29:35 +0530 | [diff] [blame^] | 585 | /** Edit cluster @public */ |
| SANDHYA.JS | a3ff32a | 2025-02-13 16:24:46 +0530 | [diff] [blame] | 586 | public editCluster(): void { |
| 587 | this.submitted = true; |
| 588 | this.sharedService.cleanForm(this.k8sclusterForm); |
| 589 | if (this.k8sclusterForm.invalid) { |
| 590 | return; |
| 591 | } |
| 592 | const modalData: MODALCLOSERESPONSEDATA = { |
| 593 | message: 'Done' |
| 594 | }; |
| 595 | const apiURLHeader: APIURLHEADER = { |
| 596 | url: this.clusterUrl, |
| 597 | httpOptions: { headers: this.headers } |
| 598 | }; |
| 599 | this.isLoadingResults = true; |
| 600 | if (this.k8sclusterForm.value.description === '') { |
| 601 | delete this.k8sclusterForm.value.description; |
| 602 | this.payload = { |
| 603 | name: this.k8sclusterForm.value.update |
| 604 | }; |
| 605 | } |
| 606 | if (this.k8sclusterForm.value.update === '') { |
| 607 | delete this.k8sclusterForm.value.update; |
| 608 | this.payload = { |
| 609 | description: this.k8sclusterForm.value.description |
| 610 | }; |
| 611 | } |
| 612 | if (this.k8sclusterForm.value.update !== '' && this.k8sclusterForm.value.description !== '') { |
| 613 | this.payload = { |
| 614 | name: this.k8sclusterForm.value.update, |
| 615 | description: this.k8sclusterForm.value.description |
| 616 | }; |
| 617 | } |
| 618 | this.restService.patchResource(apiURLHeader, this.payload).subscribe((result: {}) => { |
| 619 | this.activeModal.close(modalData); |
| 620 | this.isLoadingResults = false; |
| 621 | this.notifierService.notify('success', |
| 622 | this.translateService.instant('PAGE.K8S.UPDATEDSUCCESSFULLY')); |
| 623 | }, (error: ERRORDATA) => { |
| 624 | this.restService.handleError(error, 'post'); |
| 625 | this.isLoadingResults = false; |
| 626 | }); |
| 627 | } |
| 628 | |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 629 | /** Nets file process @private */ |
| 630 | public netsFile(files: FileList): void { |
| 631 | if (files && files.length === 1) { |
| 632 | this.sharedService.getFileString(files, 'json').then((fileContent: string): void => { |
| 633 | const getNetsJson: string = jsyaml.load(fileContent, { json: true }); |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 634 | this.k8sclusterForm.get('nets').setValue(JSON.stringify(getNetsJson)); |
| 635 | }).catch((err: string): void => { |
| 636 | if (err === 'typeError') { |
| 637 | this.notifierService.notify('error', this.translateService.instant('JSONFILETYPEERRROR')); |
| 638 | } else { |
| 639 | this.notifierService.notify('error', this.translateService.instant('ERROR')); |
| 640 | } |
| 641 | this.fileInputNetsLabel.nativeElement.innerText = this.translateService.instant('CHOOSEFILE'); |
| 642 | this.fileInputNets.nativeElement.value = null; |
| 643 | }); |
| 644 | } else if (files && files.length > 1) { |
| 645 | this.notifierService.notify('error', this.translateService.instant('DROPFILESVALIDATION')); |
| 646 | } |
| 647 | this.fileInputNetsLabel.nativeElement.innerText = files[0].name; |
| 648 | this.fileInputNets.nativeElement.value = null; |
| 649 | } |
| 650 | |
| 651 | /** credentials file process @private */ |
| 652 | public credentialsFile(files: FileList): void { |
| 653 | if (files && files.length === 1) { |
| 654 | this.sharedService.getFileString(files, 'yaml').then((fileContent: string): void => { |
| 655 | const getCredentialsJson: string = jsyaml.load(fileContent, { json: true }); |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 656 | this.k8sclusterForm.get('credentials').setValue(JSON.stringify(getCredentialsJson)); |
| 657 | }).catch((err: string): void => { |
| 658 | if (err === 'typeError') { |
| 659 | this.notifierService.notify('error', this.translateService.instant('YAMLFILETYPEERRROR')); |
| 660 | } else { |
| 661 | this.notifierService.notify('error', this.translateService.instant('ERROR')); |
| 662 | } |
| 663 | this.fileInputCredentialsLabel.nativeElement.innerText = this.translateService.instant('CHOOSEFILE'); |
| 664 | this.fileInputCredentials.nativeElement.value = null; |
| 665 | }); |
| 666 | } else if (files && files.length > 1) { |
| 667 | this.notifierService.notify('error', this.translateService.instant('DROPFILESVALIDATION')); |
| 668 | } |
| 669 | this.fileInputCredentialsLabel.nativeElement.innerText = files[0].name; |
| 670 | this.fileInputCredentials.nativeElement.value = null; |
| 671 | } |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 672 | |
| 673 | /** Used to get the AbstractControl of controlName passed @private */ |
| 674 | private getFormControl(controlName: string): AbstractControl { |
| 675 | // eslint-disable-next-line security/detect-object-injection |
| 676 | return this.k8sclusterForm.controls[controlName]; |
| 677 | } |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 678 | } |