| 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 */ |
| 70 | public actionTypes: string[] = ['update', 'upgrade', 'horizontal', 'vertical']; |
| 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]], |
| 178 | nodeCount: ['', [Validators.required]], |
| SANDHYA.JS | c08e1ec | 2025-02-13 16:24:46 +0530 | [diff] [blame^] | 179 | nodeSize: ['', [Validators.required]], |
| 180 | update: [''] |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 181 | }); |
| 182 | } |
| 183 | |
| 184 | /** convenience getter for easy access to form fields */ |
| 185 | get f(): FormGroup['controls'] { return this.k8sclusterForm.controls; } |
| 186 | |
| 187 | /** Call the vimAccount details in the selection options @public */ |
| 188 | public getDetailsvimAccount(): void { |
| 189 | this.isLoadingResults = true; |
| 190 | this.restService.getResource(environment.VIMACCOUNTS_URL).subscribe((vimAccounts: VimAccountDetails) => { |
| 191 | this.vimAccountSelect = vimAccounts; |
| 192 | this.isLoadingResults = false; |
| 193 | }, (error: ERRORDATA) => { |
| 194 | this.restService.handleError(error, 'get'); |
| 195 | this.isLoadingResults = false; |
| 196 | }); |
| 197 | } |
| 198 | |
| SANDHYA.JS | c08e1ec | 2025-02-13 16:24:46 +0530 | [diff] [blame^] | 199 | /** patch the form values for edit @public */ |
| 200 | public k8sClusterDetail(): void { |
| 201 | this.isLoadingResults = true; |
| 202 | this.restService.getResource(environment.K8SCREATECLUSTER_URL + '/' + this.profileID).subscribe((k8sData: K8SCLUSTERDATA) => { |
| 203 | if (this.profileType === 'update') { |
| 204 | this.k8sclusterForm.patchValue({ update: k8sData.name, description: !isNullOrUndefined(k8sData.description) ? k8sData.description : '' }); |
| 205 | } else if (this.profileType === 'upgrade') { |
| 206 | this.k8sclusterForm.patchValue({ k8sVersion: !isNullOrUndefined(k8sData.k8s_version) ? k8sData.k8s_version : '' }); |
| 207 | } else if (this.profileType === 'horizontal') { |
| 208 | this.k8sclusterForm.patchValue({ nodeCount: !isNullOrUndefined(k8sData.node_count) ? k8sData.node_count : '' }); |
| 209 | } else if (this.profileType === 'vertical') { |
| 210 | this.k8sclusterForm.patchValue({ nodeSize: !isNullOrUndefined(k8sData.node_size) ? k8sData.node_size : '' }); |
| 211 | } |
| 212 | this.isLoadingResults = false; |
| 213 | }, (error: ERRORDATA) => { |
| 214 | this.restService.handleError(error, 'get'); |
| 215 | this.isLoadingResults = false; |
| 216 | }); |
| 217 | } |
| 218 | |
| SANDHYA.JS | b772de0 | 2024-12-10 15:21:03 +0530 | [diff] [blame] | 219 | /** Call the event when checkbox is checked @public */ |
| 220 | public getValue(event: Event): void { |
| 221 | this.isChecked = (event.target as HTMLInputElement).checked; |
| 222 | } |
| 223 | |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 224 | |
| 225 | /** Contain selected vimAccount details @public */ |
| 226 | public getDetailsvim(event: VimAccountDetails): void { |
| 227 | this.vimAccountId = event._id; |
| 228 | } |
| 229 | |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 230 | /** On modal submit k8sAddClusterSubmit will called @public */ |
| 231 | public k8sAddClusterSubmit(): void { |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 232 | if (this.profileType === 'Manage') { |
| 233 | this.getFormControl('nets').disable(); |
| 234 | this.getFormControl('credentials').disable(); |
| 235 | this.getFormControl('deployment_methods').disable(); |
| SANDHYA.JS | b772de0 | 2024-12-10 15:21:03 +0530 | [diff] [blame] | 236 | this.getFormControl('k8sVersion').disable(); |
| 237 | this.getFormControl('nodeSize').disable(); |
| 238 | this.getFormControl('nodeCount').disable(); |
| SANDHYA.JS | c08e1ec | 2025-02-13 16:24:46 +0530 | [diff] [blame^] | 239 | this.getFormControl('update').disable(); |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 240 | this.manageCluster(); |
| SANDHYA.JS | b772de0 | 2024-12-10 15:21:03 +0530 | [diff] [blame] | 241 | } else if (this.profileType === 'Register' && this.isChecked === true) { |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 242 | this.clusterUrl = environment.K8SCREATECLUSTER_URL + '/register'; |
| 243 | this.getFormControl('region_name').disable(); |
| 244 | this.getFormControl('resource_group').disable(); |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 245 | this.getFormControl('k8s_version').disable(); |
| 246 | this.getFormControl('node_size').disable(); |
| SANDHYA.JS | b772de0 | 2024-12-10 15:21:03 +0530 | [diff] [blame] | 247 | this.getFormControl('node_count').disable(); |
| 248 | this.getFormControl('nets').disable(); |
| 249 | this.getFormControl('deployment_methods').disable(); |
| 250 | this.getFormControl('k8sVersion').disable(); |
| 251 | this.getFormControl('nodeSize').disable(); |
| 252 | this.getFormControl('nodeCount').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.registerCluster(); |
| 255 | } if (this.isChecked === false && this.profileType === 'Register') { |
| 256 | this.clusterUrl = environment.K8SCLUSTER_URL; |
| 257 | this.getFormControl('bootstrap').disable(); |
| 258 | this.getFormControl('region_name').disable(); |
| 259 | this.getFormControl('resource_group').disable(); |
| 260 | this.getFormControl('node_count').disable(); |
| 261 | this.getFormControl('node_size').disable(); |
| 262 | this.getFormControl('k8sVersion').disable(); |
| 263 | this.getFormControl('nodeSize').disable(); |
| 264 | this.getFormControl('nodeCount').disable(); |
| SANDHYA.JS | c08e1ec | 2025-02-13 16:24:46 +0530 | [diff] [blame^] | 265 | this.getFormControl('update').disable(); |
| SANDHYA.JS | b772de0 | 2024-12-10 15:21:03 +0530 | [diff] [blame] | 266 | this.oldregisterCluster(); |
| 267 | } else if (this.profileType === 'upgrade' || this.profileType === 'horizontal' || this.profileType === 'vertical') { |
| 268 | this.clusterUrl = environment.K8SCREATECLUSTER_URL + '/' + this.profileID + '/' + 'update'; |
| 269 | this.getFormControl('region_name').disable(); |
| 270 | this.getFormControl('resource_group').disable(); |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 271 | this.getFormControl('nets').disable(); |
| 272 | this.getFormControl('credentials').disable(); |
| 273 | this.getFormControl('deployment_methods').disable(); |
| 274 | this.getFormControl('name').disable(); |
| 275 | this.getFormControl('vim_account').disable(); |
| 276 | this.getFormControl('description').disable(); |
| SANDHYA.JS | b772de0 | 2024-12-10 15:21:03 +0530 | [diff] [blame] | 277 | this.getFormControl('bootstrap').disable(); |
| 278 | this.getFormControl('node_count').disable(); |
| 279 | this.getFormControl('node_size').disable(); |
| 280 | this.getFormControl('k8s_version').disable(); |
| SANDHYA.JS | c08e1ec | 2025-02-13 16:24:46 +0530 | [diff] [blame^] | 281 | this.getFormControl('update').disable(); |
| SANDHYA.JS | b772de0 | 2024-12-10 15:21:03 +0530 | [diff] [blame] | 282 | if (this.profileType === 'upgrade') { |
| 283 | this.getFormControl('nodeCount').disable(); |
| 284 | this.getFormControl('nodeSize').disable(); |
| 285 | } else if (this.profileType === 'vertical') { |
| 286 | this.getFormControl('nodeCount').disable(); |
| 287 | this.getFormControl('k8sVersion').disable(); |
| 288 | } else if (this.profileType === 'horizontal') { |
| 289 | this.getFormControl('nodeSize').disable(); |
| 290 | this.getFormControl('k8sVersion').disable(); |
| 291 | } |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 292 | this.updateCluster(); |
| SANDHYA.JS | c08e1ec | 2025-02-13 16:24:46 +0530 | [diff] [blame^] | 293 | } else if (this.profileType === 'update') { |
| 294 | this.clusterUrl = environment.K8SCREATECLUSTER_URL + '/' + this.profileID; |
| 295 | this.getFormControl('bootstrap').disable(); |
| 296 | this.getFormControl('region_name').disable(); |
| 297 | this.getFormControl('resource_group').disable(); |
| 298 | this.getFormControl('nets').disable(); |
| 299 | this.getFormControl('credentials').disable(); |
| 300 | this.getFormControl('deployment_methods').disable(); |
| 301 | this.getFormControl('vim_account').disable(); |
| 302 | this.getFormControl('node_count').disable(); |
| 303 | this.getFormControl('node_size').disable(); |
| 304 | this.getFormControl('k8s_version').disable(); |
| 305 | this.getFormControl('nodeCount').disable(); |
| 306 | this.getFormControl('k8sVersion').disable(); |
| 307 | this.getFormControl('nodeSize').disable(); |
| 308 | this.getFormControl('name').disable(); |
| 309 | |
| 310 | this.editCluster(); |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
| SANDHYA.JS | b772de0 | 2024-12-10 15:21:03 +0530 | [diff] [blame] | 314 | /** Old Register cluster flow @public */ |
| 315 | public oldregisterCluster(): void { |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 316 | this.submitted = true; |
| 317 | this.sharedService.cleanForm(this.k8sclusterForm); |
| 318 | if (this.k8sclusterForm.invalid) { |
| 319 | return; |
| 320 | } |
| 321 | const modalData: MODALCLOSERESPONSEDATA = { |
| 322 | message: 'Done' |
| 323 | }; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 324 | const validJSONCredentails: boolean = this.sharedService.checkJson(this.k8sclusterForm.value.credentials); |
| 325 | if (validJSONCredentails) { |
| 326 | this.k8sclusterForm.value.credentials = jsyaml.load(this.k8sclusterForm.value.credentials.toString(), { json: true }); |
| 327 | } else { |
| 328 | this.notifierService.notify('error', this.translateService.instant('INVALIDCONFIG')); |
| 329 | return; |
| 330 | } |
| 331 | const validJSONNets: boolean = this.sharedService.checkJson(this.k8sclusterForm.value.nets); |
| 332 | if (validJSONNets) { |
| 333 | this.k8sclusterForm.value.nets = jsyaml.load(this.k8sclusterForm.value.nets.toString(), { json: true }); |
| 334 | } else { |
| 335 | this.notifierService.notify('error', this.translateService.instant('INVALIDCONFIG')); |
| 336 | return; |
| 337 | } |
| bacigalupo | f633dbf | 2022-03-25 17:24:56 +0000 | [diff] [blame] | 338 | |
| 339 | this.deploymentMethodsSubmit = new Map<string, boolean>(); |
| 340 | /// Set deployment method Map |
| 341 | for (const methods of this.deploymentMethodsSelect) { |
| 342 | this.deploymentMethodsSubmit.set(methods.value, false); |
| 343 | } |
| 344 | |
| 345 | this.k8sclusterForm.value.deployment_methods.forEach((dm: string): void => { |
| 346 | this.deploymentMethodsSubmit.set(dm, true); |
| 347 | }); |
| 348 | // Transform Map to json object |
| 349 | const jsonDMObject: {} = {}; |
| 350 | this.deploymentMethodsSubmit.forEach((value: boolean, key: string): void => { |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame] | 351 | // eslint-disable-next-line security/detect-object-injection |
| bacigalupo | f633dbf | 2022-03-25 17:24:56 +0000 | [diff] [blame] | 352 | jsonDMObject[key] = value; |
| 353 | }); |
| 354 | |
| 355 | // Transform values to json |
| 356 | this.k8sclusterForm.value.deployment_methods = jsonDMObject; |
| 357 | |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 358 | this.k8sclusterForm.value.vim_account = this.vimAccountId; |
| 359 | |
| SANDHYA.JS | b772de0 | 2024-12-10 15:21:03 +0530 | [diff] [blame] | 360 | if (this.k8sclusterForm.value.description === '') { |
| 361 | delete this.k8sclusterForm.value.description; |
| 362 | } |
| 363 | |
| 364 | const apiURLHeader: APIURLHEADER = { |
| 365 | url: this.clusterUrl, |
| 366 | httpOptions: { headers: this.headers } |
| 367 | }; |
| 368 | |
| 369 | this.isLoadingResults = true; |
| 370 | this.restService.postResource(apiURLHeader, this.k8sclusterForm.value).subscribe((result: {}) => { |
| 371 | this.activeModal.close(modalData); |
| 372 | this.isLoadingResults = false; |
| 373 | this.notifierService.notify('success', this.k8sclusterForm.value.name + |
| 374 | this.translateService.instant('PAGE.K8S.REGISTEREDSUCCESSFULLY')); |
| 375 | }, (error: ERRORDATA) => { |
| 376 | this.restService.handleError(error, 'post'); |
| 377 | this.isLoadingResults = false; |
| 378 | }); |
| 379 | } |
| 380 | |
| 381 | /** New Register cluster flow @public */ |
| 382 | public registerCluster(): void { |
| 383 | this.submitted = true; |
| 384 | this.sharedService.cleanForm(this.k8sclusterForm); |
| 385 | if (this.k8sclusterForm.invalid) { |
| 386 | return; |
| 387 | } |
| 388 | const modalData: MODALCLOSERESPONSEDATA = { |
| 389 | message: 'Done' |
| 390 | }; |
| 391 | const validJSONCredentails: boolean = this.sharedService.checkJson(this.k8sclusterForm.value.credentials); |
| 392 | if (validJSONCredentails) { |
| 393 | this.k8sclusterForm.value.credentials = jsyaml.load(this.k8sclusterForm.value.credentials.toString(), { json: true }); |
| 394 | } else { |
| 395 | this.notifierService.notify('error', this.translateService.instant('INVALIDCONFIG')); |
| 396 | return; |
| 397 | } |
| 398 | |
| 399 | if (this.k8sclusterForm.value.description === '') { |
| 400 | delete this.k8sclusterForm.value.description; |
| 401 | } |
| 402 | |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 403 | const apiURLHeader: APIURLHEADER = { |
| 404 | url: this.clusterUrl, |
| 405 | httpOptions: { headers: this.headers } |
| 406 | }; |
| 407 | |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 408 | this.isLoadingResults = true; |
| 409 | this.restService.postResource(apiURLHeader, this.k8sclusterForm.value).subscribe((result: {}) => { |
| 410 | this.activeModal.close(modalData); |
| 411 | this.isLoadingResults = false; |
| 412 | this.notifierService.notify('success', this.k8sclusterForm.value.name + |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 413 | this.translateService.instant('PAGE.K8S.REGISTEREDSUCCESSFULLY')); |
| 414 | }, (error: ERRORDATA) => { |
| 415 | this.restService.handleError(error, 'post'); |
| 416 | this.isLoadingResults = false; |
| 417 | }); |
| 418 | } |
| 419 | |
| 420 | /** Manage cluster @public */ |
| 421 | public manageCluster(): 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: environment.K8SCREATECLUSTER_URL, |
| 432 | httpOptions: { headers: this.headers } |
| 433 | }; |
| 434 | |
| 435 | this.isLoadingResults = true; |
| 436 | const payload: K8SPayload = { |
| 437 | name: this.k8sclusterForm.value.name, |
| 438 | vim_account: this.k8sclusterForm.value.vim_account, |
| 439 | location: this.k8sclusterForm.value.location, |
| 440 | region_name: this.k8sclusterForm.value.region_name, |
| 441 | resource_group: this.k8sclusterForm.value.resource_group, |
| 442 | k8s_version: this.k8sclusterForm.value.k8s_version, |
| 443 | node_size: this.k8sclusterForm.value.node_size, |
| 444 | node_count: Number(this.k8sclusterForm.value.node_count), |
| SANDHYA.JS | b772de0 | 2024-12-10 15:21:03 +0530 | [diff] [blame] | 445 | description: this.k8sclusterForm.value.description, |
| 446 | bootstrap: Boolean(this.k8sclusterForm.value.bootstrap) |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 447 | }; |
| 448 | if (this.k8sclusterForm.value.region_name === '') { |
| 449 | delete payload.region_name; |
| 450 | } else if (this.k8sclusterForm.value.resource_group === '') { |
| 451 | delete payload.resource_group; |
| SANDHYA.JS | b772de0 | 2024-12-10 15:21:03 +0530 | [diff] [blame] | 452 | } else if (this.k8sclusterForm.value.description === '') { |
| 453 | delete payload.description; |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 454 | } |
| SANDHYA.JS | b772de0 | 2024-12-10 15:21:03 +0530 | [diff] [blame] | 455 | 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] | 456 | delete payload.region_name; |
| 457 | delete payload.resource_group; |
| SANDHYA.JS | b772de0 | 2024-12-10 15:21:03 +0530 | [diff] [blame] | 458 | delete payload.description; |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 459 | } |
| 460 | this.restService.postResource(apiURLHeader, payload).subscribe((result: {}) => { |
| 461 | this.activeModal.close(modalData); |
| 462 | this.isLoadingResults = false; |
| 463 | this.notifierService.notify('success', this.k8sclusterForm.value.name + |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 464 | this.translateService.instant('PAGE.K8S.CREATEDSUCCESSFULLY')); |
| 465 | }, (error: ERRORDATA) => { |
| 466 | this.restService.handleError(error, 'post'); |
| 467 | this.isLoadingResults = false; |
| 468 | }); |
| 469 | } |
| 470 | |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 471 | /** Update cluster @public */ |
| 472 | public updateCluster(): void { |
| 473 | this.submitted = true; |
| 474 | this.sharedService.cleanForm(this.k8sclusterForm); |
| 475 | if (this.k8sclusterForm.invalid) { |
| 476 | return; |
| 477 | } |
| 478 | const modalData: MODALCLOSERESPONSEDATA = { |
| 479 | message: 'Done' |
| 480 | }; |
| 481 | const apiURLHeader: APIURLHEADER = { |
| 482 | url: this.clusterUrl, |
| 483 | httpOptions: { headers: this.headers } |
| 484 | }; |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 485 | if (this.profileType === 'upgrade') { |
| 486 | this.payload = { |
| SANDHYA.JS | b772de0 | 2024-12-10 15:21:03 +0530 | [diff] [blame] | 487 | k8s_version: this.k8sclusterForm.value.k8sVersion |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 488 | }; |
| SANDHYA.JS | b772de0 | 2024-12-10 15:21:03 +0530 | [diff] [blame] | 489 | } else if (this.profileType === 'vertical') { |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 490 | this.payload = { |
| SANDHYA.JS | b772de0 | 2024-12-10 15:21:03 +0530 | [diff] [blame] | 491 | node_size: (this.k8sclusterForm.value.nodeSize) |
| 492 | }; |
| 493 | } else if (this.profileType === 'horizontal') { |
| 494 | this.payload = { |
| 495 | node_count: Number(this.k8sclusterForm.value.nodeCount) |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 496 | }; |
| 497 | } |
| SANDHYA.JS | b772de0 | 2024-12-10 15:21:03 +0530 | [diff] [blame] | 498 | this.isLoadingResults = true; |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 499 | this.restService.postResource(apiURLHeader, this.payload).subscribe((result: {}) => { |
| 500 | this.activeModal.close(modalData); |
| 501 | this.isLoadingResults = false; |
| 502 | this.notifierService.notify('success', |
| 503 | this.translateService.instant('PAGE.K8S.UPDATEDSUCCESSFULLY')); |
| 504 | }, (error: ERRORDATA) => { |
| 505 | this.restService.handleError(error, 'post'); |
| 506 | this.isLoadingResults = false; |
| 507 | }); |
| 508 | } |
| 509 | |
| SANDHYA.JS | c08e1ec | 2025-02-13 16:24:46 +0530 | [diff] [blame^] | 510 | |
| 511 | /** Update cluster @public */ |
| 512 | public editCluster(): void { |
| 513 | this.submitted = true; |
| 514 | this.sharedService.cleanForm(this.k8sclusterForm); |
| 515 | if (this.k8sclusterForm.invalid) { |
| 516 | return; |
| 517 | } |
| 518 | const modalData: MODALCLOSERESPONSEDATA = { |
| 519 | message: 'Done' |
| 520 | }; |
| 521 | const apiURLHeader: APIURLHEADER = { |
| 522 | url: this.clusterUrl, |
| 523 | httpOptions: { headers: this.headers } |
| 524 | }; |
| 525 | this.isLoadingResults = true; |
| 526 | if (this.k8sclusterForm.value.description === '') { |
| 527 | delete this.k8sclusterForm.value.description; |
| 528 | this.payload = { |
| 529 | name: this.k8sclusterForm.value.update |
| 530 | }; |
| 531 | } |
| 532 | if (this.k8sclusterForm.value.update === '') { |
| 533 | delete this.k8sclusterForm.value.update; |
| 534 | this.payload = { |
| 535 | description: this.k8sclusterForm.value.description |
| 536 | }; |
| 537 | } |
| 538 | if (this.k8sclusterForm.value.update !== '' && this.k8sclusterForm.value.description !== '') { |
| 539 | this.payload = { |
| 540 | name: this.k8sclusterForm.value.update, |
| 541 | description: this.k8sclusterForm.value.description |
| 542 | }; |
| 543 | } |
| 544 | this.restService.patchResource(apiURLHeader, this.payload).subscribe((result: {}) => { |
| 545 | this.activeModal.close(modalData); |
| 546 | this.isLoadingResults = false; |
| 547 | this.notifierService.notify('success', |
| 548 | this.translateService.instant('PAGE.K8S.UPDATEDSUCCESSFULLY')); |
| 549 | }, (error: ERRORDATA) => { |
| 550 | this.restService.handleError(error, 'post'); |
| 551 | this.isLoadingResults = false; |
| 552 | }); |
| 553 | } |
| 554 | |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 555 | /** Nets file process @private */ |
| 556 | public netsFile(files: FileList): void { |
| 557 | if (files && files.length === 1) { |
| 558 | this.sharedService.getFileString(files, 'json').then((fileContent: string): void => { |
| 559 | const getNetsJson: string = jsyaml.load(fileContent, { json: true }); |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 560 | this.k8sclusterForm.get('nets').setValue(JSON.stringify(getNetsJson)); |
| 561 | }).catch((err: string): void => { |
| 562 | if (err === 'typeError') { |
| 563 | this.notifierService.notify('error', this.translateService.instant('JSONFILETYPEERRROR')); |
| 564 | } else { |
| 565 | this.notifierService.notify('error', this.translateService.instant('ERROR')); |
| 566 | } |
| 567 | this.fileInputNetsLabel.nativeElement.innerText = this.translateService.instant('CHOOSEFILE'); |
| 568 | this.fileInputNets.nativeElement.value = null; |
| 569 | }); |
| 570 | } else if (files && files.length > 1) { |
| 571 | this.notifierService.notify('error', this.translateService.instant('DROPFILESVALIDATION')); |
| 572 | } |
| 573 | this.fileInputNetsLabel.nativeElement.innerText = files[0].name; |
| 574 | this.fileInputNets.nativeElement.value = null; |
| 575 | } |
| 576 | |
| 577 | /** credentials file process @private */ |
| 578 | public credentialsFile(files: FileList): void { |
| 579 | if (files && files.length === 1) { |
| 580 | this.sharedService.getFileString(files, 'yaml').then((fileContent: string): void => { |
| 581 | const getCredentialsJson: string = jsyaml.load(fileContent, { json: true }); |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 582 | this.k8sclusterForm.get('credentials').setValue(JSON.stringify(getCredentialsJson)); |
| 583 | }).catch((err: string): void => { |
| 584 | if (err === 'typeError') { |
| 585 | this.notifierService.notify('error', this.translateService.instant('YAMLFILETYPEERRROR')); |
| 586 | } else { |
| 587 | this.notifierService.notify('error', this.translateService.instant('ERROR')); |
| 588 | } |
| 589 | this.fileInputCredentialsLabel.nativeElement.innerText = this.translateService.instant('CHOOSEFILE'); |
| 590 | this.fileInputCredentials.nativeElement.value = null; |
| 591 | }); |
| 592 | } else if (files && files.length > 1) { |
| 593 | this.notifierService.notify('error', this.translateService.instant('DROPFILESVALIDATION')); |
| 594 | } |
| 595 | this.fileInputCredentialsLabel.nativeElement.innerText = files[0].name; |
| 596 | this.fileInputCredentials.nativeElement.value = null; |
| 597 | } |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 598 | |
| 599 | /** Used to get the AbstractControl of controlName passed @private */ |
| 600 | private getFormControl(controlName: string): AbstractControl { |
| 601 | // eslint-disable-next-line security/detect-object-injection |
| 602 | return this.k8sclusterForm.controls[controlName]; |
| 603 | } |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 604 | } |