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