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