| SANDHYA.JS | 92379ec | 2025-06-13 17:29:35 +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: SANDHYA JS (sandhya.j@tataelxsi.co.in) |
| 17 | */ |
| 18 | /** |
| 19 | * @file K8sAddClusterComponent.ts. |
| 20 | */ |
| 21 | import { HttpHeaders } from '@angular/common/http'; |
| 22 | import { Component, Injector, Input, OnInit } from '@angular/core'; |
| 23 | import { AbstractControl, FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms'; |
| 24 | import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap'; |
| 25 | import { TranslateService } from '@ngx-translate/core'; |
| 26 | import { NotifierService } from 'angular-notifier'; |
| 27 | import { APIURLHEADER, ERRORDATA, MODALCLOSERESPONSEDATA, TYPESECTION } from 'CommonModel'; |
| 28 | import { environment } from 'environment'; |
| 29 | import { K8SCLUSTERDATA, K8SPayload } from 'K8sModel'; |
| 30 | import { RestService } from 'RestService'; |
| 31 | import { isNullOrUndefined, SharedService } from 'SharedService'; |
| 32 | /** |
| 33 | * Creating Component |
| 34 | * @Component takes NodeAddComponent.html as template url |
| 35 | */ |
| 36 | @Component({ |
| 37 | selector: 'app-node-add', |
| 38 | templateUrl: './NodeAddComponent.html', |
| 39 | styleUrls: ['./NodeAddComponent.scss'] |
| 40 | }) |
| 41 | /** Exporting a class @exports NodeAddComponent */ |
| 42 | export class NodeAddComponent implements OnInit { |
| 43 | /** To inject services @public */ |
| 44 | public injector: Injector; |
| 45 | |
| 46 | /** FormGroup instance added to the form @ html @public */ |
| 47 | public nodeForm: FormGroup; |
| 48 | |
| 49 | /** Contains all vim account collections */ |
| 50 | public clusterItems: K8SCLUSTERDATA; |
| 51 | |
| 52 | /** Input contains Modal dialog component Instance @public */ |
| 53 | @Input() public profileType: string; |
| 54 | |
| 55 | /** Input contains Modal dialog component Instance @public */ |
| 56 | @Input() public profileID: string; |
| 57 | |
| 58 | /** Input contains Modal dialog component Instance @public */ |
| 59 | @Input() public clusterId: string; |
| 60 | |
| 61 | /** Instance for active modal service @public */ |
| 62 | public activeModal: NgbActiveModal; |
| 63 | |
| 64 | /** Variable set for twoway bindng @public */ |
| 65 | public vimAccountId: string; |
| 66 | |
| 67 | /** contains url @public */ |
| 68 | public clusterUrl: string; |
| 69 | |
| 70 | /** contains privatesubnet items @public */ |
| 71 | privateItems: [] = []; |
| 72 | |
| 73 | /** contains publicsubnet items @public */ |
| 74 | publicItems: [] = []; |
| 75 | |
| 76 | /** contains privatesubnet items from api @public */ |
| 77 | privatesubnetItems: {}; |
| 78 | |
| 79 | /** contains publicsubnet itams from api @public */ |
| 80 | publicsubnetItems: {}; |
| 81 | |
| 82 | /** contains cluster ID @public */ |
| 83 | public cluster_name: string; |
| 84 | |
| 85 | /** contains scaling action paylaod@public */ |
| 86 | public scalingPayload = {}; |
| 87 | |
| 88 | /** Form submission Add */ |
| 89 | public submitted: boolean = false; |
| 90 | |
| 91 | /** contains payload */ |
| 92 | public payload: K8SPayload; |
| 93 | |
| 94 | /** Check the loading results @public */ |
| 95 | public isLoadingResults: boolean = false; |
| 96 | |
| 97 | /** Give the message for the loading @public */ |
| 98 | public message: string = 'PLEASEWAIT'; |
| 99 | |
| 100 | /** FormBuilder instance added to the formBuilder @private */ |
| 101 | private formBuilder: FormBuilder; |
| 102 | |
| 103 | /** Utilizes rest service for any CRUD operations @private */ |
| 104 | private restService: RestService; |
| 105 | |
| 106 | /** Notifier service to popup notification @private */ |
| 107 | private notifierService: NotifierService; |
| 108 | |
| 109 | /** Contains tranlsate instance @private */ |
| 110 | private translateService: TranslateService; |
| 111 | |
| 112 | /** Controls the header form @private */ |
| 113 | private headers: HttpHeaders; |
| 114 | |
| 115 | /** Contains all methods related to shared @private */ |
| 116 | private sharedService: SharedService; |
| 117 | |
| 118 | constructor(injector: Injector) { |
| 119 | this.injector = injector; |
| 120 | this.restService = this.injector.get(RestService); |
| 121 | this.activeModal = this.injector.get(NgbActiveModal); |
| 122 | this.formBuilder = this.injector.get(FormBuilder); |
| 123 | this.notifierService = this.injector.get(NotifierService); |
| 124 | this.translateService = this.injector.get(TranslateService); |
| 125 | this.sharedService = this.injector.get(SharedService); |
| 126 | } |
| 127 | |
| 128 | public ngOnInit(): void { |
| 129 | /** On Initializing call the methods */ |
| 130 | this.nodeFormAction(); |
| 131 | this.getDetailsvimAccount(); |
| 132 | this.headers = new HttpHeaders({ |
| 133 | Accept: 'application/json', |
| 134 | 'Content-Type': 'application/json', |
| 135 | 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0' |
| 136 | }); |
| 137 | if (this.profileType === 'card_node') { |
| 138 | this.cluster_name = this.clusterId; |
| 139 | this.clusterChange(); |
| 140 | } else if (this.profileType === 'k8s-scale' || this.profileType === 'edit-node') { |
| 141 | this.cluster_name = this.clusterId; |
| 142 | this.getDetails(); |
| 143 | } |
| 144 | else { |
| 145 | this.cluster_name = null; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /** On modal initializing forms @public */ |
| 150 | public nodeFormAction(): void { |
| 151 | this.nodeForm = this.formBuilder.group({ |
| 152 | name: ['', [Validators.required]], |
| 153 | description: [''], |
| 154 | node_count: ['', [Validators.required]], |
| 155 | node_size: ['', [Validators.required]], |
| 156 | nodeCount: [''], |
| 157 | role: [''], |
| 158 | public_subnet: [[]], |
| 159 | private_subnet: [[]], |
| 160 | cluster_id: ['', [Validators.required]], |
| 161 | edit_name: [''] |
| 162 | }); |
| 163 | } |
| 164 | |
| 165 | /** convenience getter for easy access to form fields */ |
| 166 | get f(): FormGroup['controls'] { return this.nodeForm.controls; } |
| 167 | |
| 168 | |
| 169 | /** Call the vimAccount details in the selection options @public */ |
| 170 | public getDetailsvimAccount(): void { |
| 171 | this.isLoadingResults = true; |
| 172 | this.restService.getResource(environment.K8SCREATECLUSTER_URL).subscribe((vimAccounts: K8SCLUSTERDATA) => { |
| 173 | this.clusterItems = vimAccounts; |
| 174 | this.isLoadingResults = false; |
| 175 | }, (error: ERRORDATA) => { |
| 176 | this.restService.handleError(error, 'get'); |
| 177 | this.isLoadingResults = false; |
| 178 | }); |
| 179 | } |
| 180 | |
| 181 | /** To patch value for edit @public */ |
| 182 | public getDetails(): void { |
| 183 | this.isLoadingResults = true; |
| 184 | this.restService.getResource(environment.K8SCREATECLUSTER_URL + '/' + this.cluster_name + '/nodegroup' + '/' + this.profileID).subscribe((vimAccounts: K8SCLUSTERDATA) => { |
| 185 | if (this.profileType === 'edit-node') { |
| 186 | this.nodeForm.patchValue({ edit_name: vimAccounts.name, description: vimAccounts.description }); |
| 187 | } else if (this.profileType === 'k8s-scale') { |
| 188 | this.nodeForm.patchValue({ nodeCount: vimAccounts.node_count }); |
| 189 | } |
| 190 | this.isLoadingResults = false; |
| 191 | }, (error: ERRORDATA) => { |
| 192 | this.restService.handleError(error, 'get'); |
| 193 | this.isLoadingResults = false; |
| 194 | }); |
| 195 | } |
| 196 | |
| 197 | /** To get privatesubnet and publicsubnet details @public */ |
| 198 | public clusterChange(): void { |
| 199 | this.isLoadingResults = true; |
| 200 | this.restService.getResource(environment.K8SCREATECLUSTER_URL + '/' + this.cluster_name).subscribe((vimAccounts: K8SCLUSTERDATA) => { |
| 201 | if (!isNullOrUndefined(vimAccounts)) { |
| 202 | this.privatesubnetItems = vimAccounts.private_subnet; |
| 203 | this.publicsubnetItems = vimAccounts.public_subnet; |
| 204 | } |
| 205 | this.isLoadingResults = false; |
| 206 | }, (error: ERRORDATA) => { |
| 207 | this.restService.handleError(error, 'get'); |
| 208 | this.isLoadingResults = false; |
| 209 | }); |
| 210 | } |
| 211 | |
| 212 | /** On modal submit nodeSubmit will called @public */ |
| 213 | public nodeSubmit(): void { |
| 214 | if (this.profileType === 'card_node') { |
| 215 | this.getFormControl('nodeCount').disable(); |
| 216 | this.getFormControl('cluster_id').disable(); |
| 217 | this.getFormControl('edit_name').disable(); |
| 218 | this.clusterUrl = environment.K8SCREATECLUSTER_URL + '/' + this.cluster_name + '/nodegroup'; |
| 219 | this.createNode(); |
| 220 | } else if (this.profileType === 'k8s-scale') { |
| 221 | this.getFormControl('cluster_id').disable(); |
| 222 | this.getFormControl('name').disable(); |
| 223 | this.getFormControl('description').disable(); |
| 224 | this.getFormControl('private_subnet').disable(); |
| 225 | this.getFormControl('public_subnet').disable(); |
| 226 | this.getFormControl('role').disable(); |
| 227 | this.getFormControl('node_count').disable(); |
| 228 | this.getFormControl('node_size').disable(); |
| 229 | this.getFormControl('edit_name').disable(); |
| 230 | this.clusterUrl = environment.K8SCREATECLUSTER_URL + '/' + this.cluster_name + '/nodegroup' + '/' + this.profileID + '/scale'; |
| 231 | this.scaling(); |
| 232 | } else if (this.profileType === 'edit-node') { |
| 233 | this.getFormControl('cluster_id').disable(); |
| 234 | this.getFormControl('nodeCount').disable(); |
| 235 | this.getFormControl('private_subnet').disable(); |
| 236 | this.getFormControl('public_subnet').disable(); |
| 237 | this.getFormControl('role').disable(); |
| 238 | this.getFormControl('node_count').disable(); |
| 239 | this.getFormControl('node_size').disable(); |
| 240 | this.getFormControl('name').disable(); |
| 241 | this.clusterUrl = environment.K8SCREATECLUSTER_URL + '/' + this.cluster_name + '/nodegroup' + '/' + this.profileID; |
| 242 | this.update(); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | /** Create Node @public */ |
| 247 | public createNode(): void { |
| 248 | this.submitted = true; |
| 249 | this.sharedService.cleanForm(this.nodeForm); |
| 250 | if (this.nodeForm.invalid) { |
| 251 | return; |
| 252 | } |
| 253 | const modalData: MODALCLOSERESPONSEDATA = { |
| 254 | message: 'Done' |
| 255 | }; |
| 256 | const apiURLHeader: APIURLHEADER = { |
| 257 | url: this.clusterUrl, |
| 258 | httpOptions: { headers: this.headers } |
| 259 | }; |
| 260 | this.isLoadingResults = true; |
| 261 | const formData = this.nodeForm.value; |
| 262 | const payload = Object.keys(formData).reduce((acc, key) => { |
| 263 | const value = formData[key]; |
| 264 | if (key === 'node_count' && value !== null && value !== undefined && value !== '') { |
| 265 | acc[key] = Number(value); |
| 266 | } else if (value !== null && value !== undefined && value !== '' && value.length !== 0) { |
| 267 | acc[key] = value; |
| 268 | } |
| 269 | return acc; |
| 270 | }, {}); |
| 271 | this.restService.postResource(apiURLHeader, payload).subscribe((result: {}) => { |
| 272 | this.activeModal.close(modalData); |
| 273 | this.isLoadingResults = false; |
| 274 | this.notifierService.notify('success', this.nodeForm.value.name + |
| 275 | this.translateService.instant('PAGE.K8S.NODECREATEDSUCCESSFULLY')); |
| 276 | }, (error: ERRORDATA) => { |
| 277 | this.restService.handleError(error, 'post'); |
| 278 | this.isLoadingResults = false; |
| 279 | }); |
| 280 | } |
| 281 | /** Node Scaling @public */ |
| 282 | public scaling(): void { |
| 283 | this.submitted = true; |
| 284 | this.sharedService.cleanForm(this.nodeForm); |
| 285 | if (this.nodeForm.invalid) { |
| 286 | return; |
| 287 | } |
| 288 | const modalData: MODALCLOSERESPONSEDATA = { |
| 289 | message: 'Done' |
| 290 | }; |
| 291 | const apiURLHeader: APIURLHEADER = { |
| 292 | url: this.clusterUrl, |
| 293 | httpOptions: { headers: this.headers } |
| 294 | }; |
| 295 | this.isLoadingResults = true; |
| 296 | if (this.profileType === 'k8s-scale') { |
| 297 | this.scalingPayload = { |
| 298 | node_count: Number(this.nodeForm.value.nodeCount) |
| 299 | }; |
| 300 | } |
| 301 | this.restService.postResource(apiURLHeader, this.scalingPayload).subscribe((result: {}) => { |
| 302 | this.activeModal.close(modalData); |
| 303 | this.isLoadingResults = false; |
| 304 | this.notifierService.notify('success', |
| 305 | this.translateService.instant('PAGE.K8S.NODEUPDATEDSUCCESSFULLY')); |
| 306 | }, (error: ERRORDATA) => { |
| 307 | this.restService.handleError(error, 'post'); |
| 308 | this.isLoadingResults = false; |
| 309 | }); |
| 310 | } |
| 311 | /** Node update @public */ |
| 312 | public update(): void { |
| 313 | this.submitted = true; |
| 314 | this.sharedService.cleanForm(this.nodeForm); |
| 315 | if (this.nodeForm.invalid) { |
| 316 | return; |
| 317 | } |
| 318 | const modalData: MODALCLOSERESPONSEDATA = { |
| 319 | message: 'Done' |
| 320 | }; |
| 321 | const apiURLHeader: APIURLHEADER = { |
| 322 | url: this.clusterUrl, |
| 323 | httpOptions: { headers: this.headers } |
| 324 | }; |
| 325 | this.isLoadingResults = true; |
| 326 | if (this.profileType === 'edit-node') { |
| 327 | if (this.nodeForm.value.description === '') { |
| 328 | this.scalingPayload = { |
| 329 | name: this.nodeForm.value.edit_name |
| 330 | }; |
| 331 | delete this.nodeForm.value.description; |
| 332 | } else if (this.nodeForm.value.name === '') { |
| 333 | this.scalingPayload = { |
| 334 | description: this.nodeForm.value.description |
| 335 | }; |
| 336 | delete this.nodeForm.value.name; |
| 337 | } else { |
| 338 | this.scalingPayload = { |
| 339 | name: this.nodeForm.value.edit_name, |
| 340 | description: this.nodeForm.value.description |
| 341 | }; |
| 342 | } |
| 343 | } |
| 344 | this.restService.patchResource(apiURLHeader, this.scalingPayload).subscribe((result: {}) => { |
| 345 | this.activeModal.close(modalData); |
| 346 | this.isLoadingResults = false; |
| 347 | this.notifierService.notify('success', |
| 348 | this.translateService.instant('PAGE.K8S.NODEUPDATEDSUCCESSFULLY')); |
| 349 | }, (error: ERRORDATA) => { |
| 350 | this.restService.handleError(error, 'post'); |
| 351 | this.isLoadingResults = false; |
| 352 | }); |
| 353 | } |
| 354 | /** Used to get the AbstractControl of controlName passed @private */ |
| 355 | private getFormControl(controlName: string): AbstractControl { |
| 356 | // eslint-disable-next-line security/detect-object-injection |
| 357 | return this.nodeForm.controls[controlName]; |
| 358 | } |
| 359 | } |