blob: 69fe54fd633d68e1cf990f16026b869d8f2c1a5f [file] [log] [blame]
kumaran.m3b4814a2020-05-01 19:48:54 +05301/*
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 */
21import { HttpHeaders } from '@angular/common/http';
SANDHYA.JS26570112024-07-05 21:35:46 +053022import { Component, ElementRef, Injector, Input, OnInit, ViewChild } from '@angular/core';
SANDHYA.JS92379ec2025-06-13 17:29:35 +053023import { AbstractControl, FormArray, FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
kumaran.m3b4814a2020-05-01 19:48:54 +053024import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
25import { TranslateService } from '@ngx-translate/core';
26import { NotifierService } from 'angular-notifier';
bacigalupof633dbf2022-03-25 17:24:56 +000027import { APIURLHEADER, ERRORDATA, MODALCLOSERESPONSEDATA, TYPESECTION } from 'CommonModel';
kumaran.m3b4814a2020-05-01 19:48:54 +053028import { environment } from 'environment';
29import * as jsyaml from 'js-yaml';
SANDHYA.JSa3ff32a2025-02-13 16:24:46 +053030import { K8SCLUSTERDATA, K8SPayload } from 'K8sModel';
kumaran.m3b4814a2020-05-01 19:48:54 +053031import { RestService } from 'RestService';
SANDHYA.JSb772de02024-12-10 15:21:03 +053032import { isNullOrUndefined, SharedService } from 'SharedService';
kumaran.m3b4814a2020-05-01 19:48:54 +053033import { 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 */
44export 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.JS92379ec2025-06-13 17:29:35 +053054 /** Contains selected vim */
55 public vimType: string;
56
SANDHYA.JS26570112024-07-05 21:35:46 +053057 /** Input contains Modal dialog component Instance @public */
58 @Input() public profileType: string;
59
60 /** Input contains Modal dialog component Instance @public */
61 @Input() public profileID: string;
62
bacigalupof633dbf2022-03-25 17:24:56 +000063 /** Contains all deployment methods */
64 public deploymentMethodsSelect: TYPESECTION[] = [];
65
66 /** Submited deployments methods format */
67 public deploymentMethodsSubmit: Map<string, boolean>;
68
69 /** Contains all deployment methods selected */
SANDHYA.JS26570112024-07-05 21:35:46 +053070 public selectedDeploymentMethods: string[] = ['helm-chart-v3', 'juju-bundle'];
bacigalupof633dbf2022-03-25 17:24:56 +000071
SANDHYA.JSa3ff32a2025-02-13 16:24:46 +053072 /** Contains all action types */
SANDHYA.JS52af4802025-05-22 17:00:15 +053073 public actionTypes: string[] = ['update', 'upgrade'];
SANDHYA.JSa3ff32a2025-02-13 16:24:46 +053074
kumaran.m3b4814a2020-05-01 19:48:54 +053075 /** Instance for active modal service @public */
76 public activeModal: NgbActiveModal;
77
78 /** Variable set for twoway bindng @public */
79 public vimAccountId: string;
80
SANDHYA.JS26570112024-07-05 21:35:46 +053081 /** contains url @public */
82 public clusterUrl: string;
83
kumaran.m3b4814a2020-05-01 19:48:54 +053084 /** Form submission Add */
85 public submitted: boolean = false;
86
SANDHYA.JS26570112024-07-05 21:35:46 +053087 /** contains payload */
88 public payload: K8SPayload;
89
SANDHYA.JSb772de02024-12-10 15:21:03 +053090 /** Check the checkbox status */
91 public isChecked: boolean = true;
92
kumaran.m3b4814a2020-05-01 19:48:54 +053093 /** Check the loading results @public */
94 public isLoadingResults: boolean = false;
95
96 /** Give the message for the loading @public */
97 public message: string = 'PLEASEWAIT';
98
99 /** Element ref for fileInputNets @public */
100 @ViewChild('fileInputNets', { static: true }) public fileInputNets: ElementRef;
101
102 /** Element ref for fileInputNetsLabel @public */
103 @ViewChild('fileInputNetsLabel', { static: true }) public fileInputNetsLabel: ElementRef;
104
105 /** Element ref for fileInputCredentials @public */
106 @ViewChild('fileInputCredentials', { static: true }) public fileInputCredentials: ElementRef;
107
108 /** Element ref for fileInputCredentialsLabel @public */
109 @ViewChild('fileInputCredentialsLabel', { static: true }) public fileInputCredentialsLabel: ElementRef;
110
111 /** FormBuilder instance added to the formBuilder @private */
112 private formBuilder: FormBuilder;
113
114 /** Utilizes rest service for any CRUD operations @private */
115 private restService: RestService;
116
117 /** Notifier service to popup notification @private */
118 private notifierService: NotifierService;
119
120 /** Contains tranlsate instance @private */
121 private translateService: TranslateService;
122
123 /** Controls the header form @private */
124 private headers: HttpHeaders;
125
126 /** Contains all methods related to shared @private */
127 private sharedService: SharedService;
128
129 constructor(injector: Injector) {
130 this.injector = injector;
131 this.restService = this.injector.get(RestService);
132 this.activeModal = this.injector.get(NgbActiveModal);
133 this.formBuilder = this.injector.get(FormBuilder);
134 this.notifierService = this.injector.get(NotifierService);
135 this.translateService = this.injector.get(TranslateService);
136 this.sharedService = this.injector.get(SharedService);
bacigalupof633dbf2022-03-25 17:24:56 +0000137 this.deploymentMethodsSelect = [
138 {
bacigalupof633dbf2022-03-25 17:24:56 +0000139 title: 'Helm v3',
140 value: 'helm-chart-v3'
141 },
142 {
143 title: 'Juju bundle',
144 value: 'juju-bundle'
145 }
146 ];
kumaran.m3b4814a2020-05-01 19:48:54 +0530147 }
148
149 public ngOnInit(): void {
150 /** On Initializing call the methods */
151 this.k8sclusterFormAction();
152 this.getDetailsvimAccount();
153 this.headers = new HttpHeaders({
154 Accept: 'application/json',
155 'Content-Type': 'application/json',
156 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0'
157 });
SANDHYA.JSa3ff32a2025-02-13 16:24:46 +0530158 this.actionTypes.forEach((type: string): void => {
159 if (type === this.profileType) {
160 this.k8sClusterDetail();
161 }
162 });
kumaran.m3b4814a2020-05-01 19:48:54 +0530163 }
164
165 /** On modal initializing forms @public */
166 public k8sclusterFormAction(): void {
167 this.k8sclusterForm = this.formBuilder.group({
168 name: ['', [Validators.required]],
SANDHYA.JS92379ec2025-06-13 17:29:35 +0530169 k8s_version: [''],
kumaran.m3b4814a2020-05-01 19:48:54 +0530170 vim_account: [null, [Validators.required]],
SANDHYA.JSb772de02024-12-10 15:21:03 +0530171 description: [''],
kumaran.m3b4814a2020-05-01 19:48:54 +0530172 nets: ['', [Validators.required]],
bacigalupof633dbf2022-03-25 17:24:56 +0000173 deployment_methods: ['', [Validators.required]],
SANDHYA.JS26570112024-07-05 21:35:46 +0530174 credentials: ['', [Validators.required]],
175 region_name: [''],
176 resource_group: [''],
177 node_count: ['', [Validators.required]],
SANDHYA.JSb772de02024-12-10 15:21:03 +0530178 node_size: ['', [Validators.required]],
179 bootstrap: [true],
180 k8sVersion: ['', [Validators.required]],
SANDHYA.JS92379ec2025-06-13 17:29:35 +0530181 iam_role: [''],
182 private_subnet: this.formBuilder.array([]),
183 public_subnet: this.formBuilder.array([]),
SANDHYA.JSa3ff32a2025-02-13 16:24:46 +0530184 update: ['']
kumaran.m3b4814a2020-05-01 19:48:54 +0530185 });
186 }
187
188 /** convenience getter for easy access to form fields */
189 get f(): FormGroup['controls'] { return this.k8sclusterForm.controls; }
190
SANDHYA.JS92379ec2025-06-13 17:29:35 +0530191 /** convenience getter for easy access to form array privatesubnet */
192 get chipsprivateArray(): FormArray {
193 return this.k8sclusterForm.get('private_subnet') as FormArray;
194 }
195
196 /** To add privatesubnet chips */
197 public addprivateChips() {
198 const input = document.getElementById('private_subnet') as HTMLInputElement;
199 const value = input.value.trim();
200
201 if (value) {
202 this.chipsprivateArray.push(new FormControl(value));
203 input.value = '';
204 }
205 }
206 /** convenience getter for easy access to form array publicsubnet */
207 get chipspublicArray(): FormArray {
208 return this.k8sclusterForm.get('public_subnet') as FormArray;
209 }
210
211 /** To add publicsubnet chips */
212 public addpublicChips() {
213 const input = document.getElementById('public_subnet') as HTMLInputElement;
214 const value = input.value.trim();
215
216 if (value) {
217 this.chipspublicArray.push(new FormControl(value));
218 input.value = '';
219 }
220 }
221
222 /** To remove privatesubnet chips */
223 public removeprivateChip(index: number) {
224 this.chipsprivateArray.removeAt(index);
225 }
226
227 /** To remove privatesubnet last chip */
228 public removeprivateLastChip(event: KeyboardEvent) {
229 const input = event.target as HTMLInputElement;
230 if (event.key === 'Backspace' && input.value === '' && this.chipsprivateArray.length > 0) {
231 this.chipsprivateArray.removeAt(this.chipsprivateArray.length - 1); // Remove last chip if input is empty
232 event.preventDefault();
233 }
234 }
235 /** To remove publicsubnet chips */
236 public removepublicChip(index: number) {
237 this.chipspublicArray.removeAt(index);
238 }
239
240 /** To remove publicsubnet last chip */
241 public removepublicLastChip(event: KeyboardEvent) {
242 const input = event.target as HTMLInputElement;
243 if (event.key === 'Backspace' && input.value === '' && this.chipspublicArray.length > 0) {
244 this.chipspublicArray.removeAt(this.chipspublicArray.length - 1); // Remove last chip if input is empty
245 event.preventDefault();
246 }
247 }
248
249
kumaran.m3b4814a2020-05-01 19:48:54 +0530250 /** Call the vimAccount details in the selection options @public */
251 public getDetailsvimAccount(): void {
252 this.isLoadingResults = true;
253 this.restService.getResource(environment.VIMACCOUNTS_URL).subscribe((vimAccounts: VimAccountDetails) => {
254 this.vimAccountSelect = vimAccounts;
255 this.isLoadingResults = false;
256 }, (error: ERRORDATA) => {
257 this.restService.handleError(error, 'get');
258 this.isLoadingResults = false;
259 });
260 }
261
SANDHYA.JSa3ff32a2025-02-13 16:24:46 +0530262 /** patch the form values for edit @public */
263 public k8sClusterDetail(): void {
264 this.isLoadingResults = true;
265 this.restService.getResource(environment.K8SCREATECLUSTER_URL + '/' + this.profileID).subscribe((k8sData: K8SCLUSTERDATA) => {
266 if (this.profileType === 'update') {
267 this.k8sclusterForm.patchValue({ update: k8sData.name, description: !isNullOrUndefined(k8sData.description) ? k8sData.description : '' });
268 } else if (this.profileType === 'upgrade') {
269 this.k8sclusterForm.patchValue({ k8sVersion: !isNullOrUndefined(k8sData.k8s_version) ? k8sData.k8s_version : '' });
SANDHYA.JSa3ff32a2025-02-13 16:24:46 +0530270 }
271 this.isLoadingResults = false;
272 }, (error: ERRORDATA) => {
273 this.restService.handleError(error, 'get');
274 this.isLoadingResults = false;
275 });
276 }
277
SANDHYA.JSb772de02024-12-10 15:21:03 +0530278 /** Call the event when checkbox is checked @public */
279 public getValue(event: Event): void {
280 this.isChecked = (event.target as HTMLInputElement).checked;
281 }
282
SANDHYA.JS26570112024-07-05 21:35:46 +0530283
284 /** Contain selected vimAccount details @public */
285 public getDetailsvim(event: VimAccountDetails): void {
286 this.vimAccountId = event._id;
SANDHYA.JS92379ec2025-06-13 17:29:35 +0530287 this.vimType = event.vim_type;
SANDHYA.JS26570112024-07-05 21:35:46 +0530288 }
289
kumaran.m3b4814a2020-05-01 19:48:54 +0530290 /** On modal submit k8sAddClusterSubmit will called @public */
291 public k8sAddClusterSubmit(): void {
SANDHYA.JS26570112024-07-05 21:35:46 +0530292 if (this.profileType === 'Manage') {
293 this.getFormControl('nets').disable();
294 this.getFormControl('credentials').disable();
295 this.getFormControl('deployment_methods').disable();
SANDHYA.JSb772de02024-12-10 15:21:03 +0530296 this.getFormControl('k8sVersion').disable();
SANDHYA.JSa3ff32a2025-02-13 16:24:46 +0530297 this.getFormControl('update').disable();
SANDHYA.JS92379ec2025-06-13 17:29:35 +0530298 this.clusterUrl = environment.K8SCREATECLUSTER_URL;
299 if (this.vimType === 'aws') {
300 this.getFormControl('resource_group').disable();
301 this.getFormControl('k8sVersion').disable();
302 this.getFormControl('node_size').disable();
303 this.getFormControl('node_count').disable();
304 this.getFormControl('nets').disable();
305 this.getFormControl('credentials').disable();
306 this.getFormControl('update').disable();
307 this.getFormControl('deployment_methods').disable();
308 } else if (this.vimType !== 'aws') {
309 this.getFormControl('nets').disable();
310 this.getFormControl('credentials').disable();
311 this.getFormControl('deployment_methods').disable();
312 this.getFormControl('k8sVersion').disable();
313 this.getFormControl('update').disable();
314 }
SANDHYA.JS26570112024-07-05 21:35:46 +0530315 this.manageCluster();
SANDHYA.JSb772de02024-12-10 15:21:03 +0530316 } else if (this.profileType === 'Register' && this.isChecked === true) {
SANDHYA.JS26570112024-07-05 21:35:46 +0530317 this.clusterUrl = environment.K8SCREATECLUSTER_URL + '/register';
318 this.getFormControl('region_name').disable();
319 this.getFormControl('resource_group').disable();
SANDHYA.JS26570112024-07-05 21:35:46 +0530320 this.getFormControl('k8s_version').disable();
321 this.getFormControl('node_size').disable();
SANDHYA.JSb772de02024-12-10 15:21:03 +0530322 this.getFormControl('node_count').disable();
323 this.getFormControl('nets').disable();
324 this.getFormControl('deployment_methods').disable();
325 this.getFormControl('k8sVersion').disable();
SANDHYA.JS92379ec2025-06-13 17:29:35 +0530326 this.getFormControl('iam_role').disable();
327 this.getFormControl('private_subnet').disable();
328 this.getFormControl('public_subnet').disable();
SANDHYA.JSa3ff32a2025-02-13 16:24:46 +0530329 this.getFormControl('update').disable();
SANDHYA.JSb772de02024-12-10 15:21:03 +0530330 this.registerCluster();
331 } if (this.isChecked === false && this.profileType === 'Register') {
332 this.clusterUrl = environment.K8SCLUSTER_URL;
333 this.getFormControl('bootstrap').disable();
334 this.getFormControl('region_name').disable();
335 this.getFormControl('resource_group').disable();
336 this.getFormControl('node_count').disable();
337 this.getFormControl('node_size').disable();
338 this.getFormControl('k8sVersion').disable();
SANDHYA.JS92379ec2025-06-13 17:29:35 +0530339 this.getFormControl('iam_role').disable();
340 this.getFormControl('private_subnet').disable();
341 this.getFormControl('public_subnet').disable();
SANDHYA.JSa3ff32a2025-02-13 16:24:46 +0530342 this.getFormControl('update').disable();
SANDHYA.JSb772de02024-12-10 15:21:03 +0530343 this.oldregisterCluster();
SANDHYA.JS52af4802025-05-22 17:00:15 +0530344 } else if (this.profileType === 'upgrade') {
345 this.clusterUrl = environment.K8SCREATECLUSTER_URL + '/' + this.profileID + '/' + 'upgrade';
SANDHYA.JSb772de02024-12-10 15:21:03 +0530346 this.getFormControl('region_name').disable();
347 this.getFormControl('resource_group').disable();
SANDHYA.JS26570112024-07-05 21:35:46 +0530348 this.getFormControl('nets').disable();
349 this.getFormControl('credentials').disable();
350 this.getFormControl('deployment_methods').disable();
351 this.getFormControl('name').disable();
352 this.getFormControl('vim_account').disable();
353 this.getFormControl('description').disable();
SANDHYA.JSb772de02024-12-10 15:21:03 +0530354 this.getFormControl('bootstrap').disable();
355 this.getFormControl('node_count').disable();
356 this.getFormControl('node_size').disable();
SANDHYA.JS92379ec2025-06-13 17:29:35 +0530357 this.getFormControl('iam_role').disable();
358 this.getFormControl('private_subnet').disable();
359 this.getFormControl('public_subnet').disable();
SANDHYA.JSa3ff32a2025-02-13 16:24:46 +0530360 this.getFormControl('update').disable();
SANDHYA.JS26570112024-07-05 21:35:46 +0530361 this.updateCluster();
SANDHYA.JSa3ff32a2025-02-13 16:24:46 +0530362 } else if (this.profileType === 'update') {
363 this.clusterUrl = environment.K8SCREATECLUSTER_URL + '/' + this.profileID;
364 this.getFormControl('bootstrap').disable();
365 this.getFormControl('region_name').disable();
366 this.getFormControl('resource_group').disable();
367 this.getFormControl('nets').disable();
368 this.getFormControl('credentials').disable();
369 this.getFormControl('deployment_methods').disable();
370 this.getFormControl('vim_account').disable();
371 this.getFormControl('node_count').disable();
372 this.getFormControl('node_size').disable();
373 this.getFormControl('k8s_version').disable();
SANDHYA.JSa3ff32a2025-02-13 16:24:46 +0530374 this.getFormControl('k8sVersion').disable();
SANDHYA.JS92379ec2025-06-13 17:29:35 +0530375 this.getFormControl('iam_role').disable();
376 this.getFormControl('private_subnet').disable();
377 this.getFormControl('public_subnet').disable();
SANDHYA.JSa3ff32a2025-02-13 16:24:46 +0530378 this.getFormControl('name').disable();
SANDHYA.JSa3ff32a2025-02-13 16:24:46 +0530379 this.editCluster();
SANDHYA.JS26570112024-07-05 21:35:46 +0530380 }
381 }
382
SANDHYA.JSb772de02024-12-10 15:21:03 +0530383 /** Old Register cluster flow @public */
384 public oldregisterCluster(): void {
kumaran.m3b4814a2020-05-01 19:48:54 +0530385 this.submitted = true;
386 this.sharedService.cleanForm(this.k8sclusterForm);
387 if (this.k8sclusterForm.invalid) {
388 return;
389 }
390 const modalData: MODALCLOSERESPONSEDATA = {
391 message: 'Done'
392 };
kumaran.m3b4814a2020-05-01 19:48:54 +0530393 const validJSONCredentails: boolean = this.sharedService.checkJson(this.k8sclusterForm.value.credentials);
394 if (validJSONCredentails) {
395 this.k8sclusterForm.value.credentials = jsyaml.load(this.k8sclusterForm.value.credentials.toString(), { json: true });
396 } else {
397 this.notifierService.notify('error', this.translateService.instant('INVALIDCONFIG'));
398 return;
399 }
400 const validJSONNets: boolean = this.sharedService.checkJson(this.k8sclusterForm.value.nets);
401 if (validJSONNets) {
402 this.k8sclusterForm.value.nets = jsyaml.load(this.k8sclusterForm.value.nets.toString(), { json: true });
403 } else {
404 this.notifierService.notify('error', this.translateService.instant('INVALIDCONFIG'));
405 return;
406 }
bacigalupof633dbf2022-03-25 17:24:56 +0000407
408 this.deploymentMethodsSubmit = new Map<string, boolean>();
409 /// Set deployment method Map
410 for (const methods of this.deploymentMethodsSelect) {
411 this.deploymentMethodsSubmit.set(methods.value, false);
412 }
413
414 this.k8sclusterForm.value.deployment_methods.forEach((dm: string): void => {
415 this.deploymentMethodsSubmit.set(dm, true);
416 });
417 // Transform Map to json object
418 const jsonDMObject: {} = {};
419 this.deploymentMethodsSubmit.forEach((value: boolean, key: string): void => {
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +0530420 // eslint-disable-next-line security/detect-object-injection
bacigalupof633dbf2022-03-25 17:24:56 +0000421 jsonDMObject[key] = value;
422 });
423
424 // Transform values to json
425 this.k8sclusterForm.value.deployment_methods = jsonDMObject;
426
SANDHYA.JS26570112024-07-05 21:35:46 +0530427 this.k8sclusterForm.value.vim_account = this.vimAccountId;
428
SANDHYA.JSb772de02024-12-10 15:21:03 +0530429 if (this.k8sclusterForm.value.description === '') {
430 delete this.k8sclusterForm.value.description;
431 }
432
433 const apiURLHeader: APIURLHEADER = {
434 url: this.clusterUrl,
435 httpOptions: { headers: this.headers }
436 };
437
438 this.isLoadingResults = true;
439 this.restService.postResource(apiURLHeader, this.k8sclusterForm.value).subscribe((result: {}) => {
440 this.activeModal.close(modalData);
441 this.isLoadingResults = false;
442 this.notifierService.notify('success', this.k8sclusterForm.value.name +
443 this.translateService.instant('PAGE.K8S.REGISTEREDSUCCESSFULLY'));
444 }, (error: ERRORDATA) => {
445 this.restService.handleError(error, 'post');
446 this.isLoadingResults = false;
447 });
448 }
449
450 /** New Register cluster flow @public */
451 public registerCluster(): void {
452 this.submitted = true;
453 this.sharedService.cleanForm(this.k8sclusterForm);
454 if (this.k8sclusterForm.invalid) {
455 return;
456 }
457 const modalData: MODALCLOSERESPONSEDATA = {
458 message: 'Done'
459 };
460 const validJSONCredentails: boolean = this.sharedService.checkJson(this.k8sclusterForm.value.credentials);
461 if (validJSONCredentails) {
462 this.k8sclusterForm.value.credentials = jsyaml.load(this.k8sclusterForm.value.credentials.toString(), { json: true });
463 } else {
464 this.notifierService.notify('error', this.translateService.instant('INVALIDCONFIG'));
465 return;
466 }
467
468 if (this.k8sclusterForm.value.description === '') {
469 delete this.k8sclusterForm.value.description;
470 }
SANDHYA.JS26570112024-07-05 21:35:46 +0530471 const apiURLHeader: APIURLHEADER = {
472 url: this.clusterUrl,
473 httpOptions: { headers: this.headers }
474 };
475
kumaran.m3b4814a2020-05-01 19:48:54 +0530476 this.isLoadingResults = true;
477 this.restService.postResource(apiURLHeader, this.k8sclusterForm.value).subscribe((result: {}) => {
478 this.activeModal.close(modalData);
479 this.isLoadingResults = false;
480 this.notifierService.notify('success', this.k8sclusterForm.value.name +
SANDHYA.JS26570112024-07-05 21:35:46 +0530481 this.translateService.instant('PAGE.K8S.REGISTEREDSUCCESSFULLY'));
482 }, (error: ERRORDATA) => {
483 this.restService.handleError(error, 'post');
484 this.isLoadingResults = false;
485 });
486 }
487
SANDHYA.JS92379ec2025-06-13 17:29:35 +0530488 /** controlplane cluster flow @public */
SANDHYA.JS26570112024-07-05 21:35:46 +0530489 public manageCluster(): void {
490 this.submitted = true;
491 this.sharedService.cleanForm(this.k8sclusterForm);
492 if (this.k8sclusterForm.invalid) {
493 return;
494 }
495 const modalData: MODALCLOSERESPONSEDATA = {
496 message: 'Done'
497 };
498 const apiURLHeader: APIURLHEADER = {
SANDHYA.JS92379ec2025-06-13 17:29:35 +0530499 url: this.clusterUrl,
SANDHYA.JS26570112024-07-05 21:35:46 +0530500 httpOptions: { headers: this.headers }
501 };
502
503 this.isLoadingResults = true;
SANDHYA.JS92379ec2025-06-13 17:29:35 +0530504 const formData = this.k8sclusterForm.value;
505 if (this.k8sclusterForm.value.private_subnet.length <= 0) {
506 delete this.k8sclusterForm.value.private_subnet;
SANDHYA.JS26570112024-07-05 21:35:46 +0530507 }
SANDHYA.JS92379ec2025-06-13 17:29:35 +0530508 if (this.k8sclusterForm.value.public_subnet.length <= 0) {
509 delete this.k8sclusterForm.value.public_subnet;
SANDHYA.JS26570112024-07-05 21:35:46 +0530510 }
SANDHYA.JS92379ec2025-06-13 17:29:35 +0530511 if (this.vimType !== 'aws') {
512 this.payload = {
513 name: this.k8sclusterForm.value.name,
514 vim_account: this.k8sclusterForm.value.vim_account,
515 location: this.k8sclusterForm.value.location,
516 region_name: this.k8sclusterForm.value.region_name,
517 resource_group: this.k8sclusterForm.value.resource_group,
518 k8s_version: this.k8sclusterForm.value.k8s_version,
519 node_size: this.k8sclusterForm.value.node_size,
520 node_count: Number(this.k8sclusterForm.value.node_count),
521 description: this.k8sclusterForm.value.description,
522 bootstrap: Boolean(this.k8sclusterForm.value.bootstrap)
523 };
524 if (this.k8sclusterForm.value.region_name === '') {
525 delete this.payload.region_name;
526 }
527 if (this.k8sclusterForm.value.resource_group === '') {
528 delete this.payload.resource_group;
529 }
530 if (this.k8sclusterForm.value.description === '') {
531 delete this.payload.description;
532 }
533 } else {
534 this.payload = Object.keys(formData).reduce((acc, key) => {
535 // eslint-disable-next-line security/detect-object-injection
536 if (formData[key] !== null && formData[key] !== undefined && formData[key] !== '') {
537 // eslint-disable-next-line security/detect-object-injection
538 acc[key] = formData[key];
539 }
540 return acc;
541 }, {});
542 }
543 this.restService.postResource(apiURLHeader, this.payload).subscribe((result: {}) => {
SANDHYA.JS26570112024-07-05 21:35:46 +0530544 this.activeModal.close(modalData);
545 this.isLoadingResults = false;
546 this.notifierService.notify('success', this.k8sclusterForm.value.name +
kumaran.m3b4814a2020-05-01 19:48:54 +0530547 this.translateService.instant('PAGE.K8S.CREATEDSUCCESSFULLY'));
548 }, (error: ERRORDATA) => {
549 this.restService.handleError(error, 'post');
550 this.isLoadingResults = false;
551 });
552 }
553
SANDHYA.JS26570112024-07-05 21:35:46 +0530554 /** Update cluster @public */
555 public updateCluster(): void {
556 this.submitted = true;
557 this.sharedService.cleanForm(this.k8sclusterForm);
558 if (this.k8sclusterForm.invalid) {
559 return;
560 }
561 const modalData: MODALCLOSERESPONSEDATA = {
562 message: 'Done'
563 };
564 const apiURLHeader: APIURLHEADER = {
565 url: this.clusterUrl,
566 httpOptions: { headers: this.headers }
567 };
SANDHYA.JS26570112024-07-05 21:35:46 +0530568 if (this.profileType === 'upgrade') {
569 this.payload = {
SANDHYA.JSb772de02024-12-10 15:21:03 +0530570 k8s_version: this.k8sclusterForm.value.k8sVersion
SANDHYA.JS26570112024-07-05 21:35:46 +0530571 };
SANDHYA.JS26570112024-07-05 21:35:46 +0530572 }
SANDHYA.JSb772de02024-12-10 15:21:03 +0530573 this.isLoadingResults = true;
SANDHYA.JS26570112024-07-05 21:35:46 +0530574 this.restService.postResource(apiURLHeader, this.payload).subscribe((result: {}) => {
575 this.activeModal.close(modalData);
576 this.isLoadingResults = false;
577 this.notifierService.notify('success',
578 this.translateService.instant('PAGE.K8S.UPDATEDSUCCESSFULLY'));
579 }, (error: ERRORDATA) => {
580 this.restService.handleError(error, 'post');
581 this.isLoadingResults = false;
582 });
583 }
584
SANDHYA.JS92379ec2025-06-13 17:29:35 +0530585 /** Edit cluster @public */
SANDHYA.JSa3ff32a2025-02-13 16:24:46 +0530586 public editCluster(): void {
587 this.submitted = true;
588 this.sharedService.cleanForm(this.k8sclusterForm);
589 if (this.k8sclusterForm.invalid) {
590 return;
591 }
592 const modalData: MODALCLOSERESPONSEDATA = {
593 message: 'Done'
594 };
595 const apiURLHeader: APIURLHEADER = {
596 url: this.clusterUrl,
597 httpOptions: { headers: this.headers }
598 };
599 this.isLoadingResults = true;
600 if (this.k8sclusterForm.value.description === '') {
601 delete this.k8sclusterForm.value.description;
602 this.payload = {
603 name: this.k8sclusterForm.value.update
604 };
605 }
606 if (this.k8sclusterForm.value.update === '') {
607 delete this.k8sclusterForm.value.update;
608 this.payload = {
609 description: this.k8sclusterForm.value.description
610 };
611 }
612 if (this.k8sclusterForm.value.update !== '' && this.k8sclusterForm.value.description !== '') {
613 this.payload = {
614 name: this.k8sclusterForm.value.update,
615 description: this.k8sclusterForm.value.description
616 };
617 }
618 this.restService.patchResource(apiURLHeader, this.payload).subscribe((result: {}) => {
619 this.activeModal.close(modalData);
620 this.isLoadingResults = false;
621 this.notifierService.notify('success',
622 this.translateService.instant('PAGE.K8S.UPDATEDSUCCESSFULLY'));
623 }, (error: ERRORDATA) => {
624 this.restService.handleError(error, 'post');
625 this.isLoadingResults = false;
626 });
627 }
628
kumaran.m3b4814a2020-05-01 19:48:54 +0530629 /** Nets file process @private */
630 public netsFile(files: FileList): void {
631 if (files && files.length === 1) {
632 this.sharedService.getFileString(files, 'json').then((fileContent: string): void => {
633 const getNetsJson: string = jsyaml.load(fileContent, { json: true });
kumaran.m3b4814a2020-05-01 19:48:54 +0530634 this.k8sclusterForm.get('nets').setValue(JSON.stringify(getNetsJson));
635 }).catch((err: string): void => {
636 if (err === 'typeError') {
637 this.notifierService.notify('error', this.translateService.instant('JSONFILETYPEERRROR'));
638 } else {
639 this.notifierService.notify('error', this.translateService.instant('ERROR'));
640 }
641 this.fileInputNetsLabel.nativeElement.innerText = this.translateService.instant('CHOOSEFILE');
642 this.fileInputNets.nativeElement.value = null;
643 });
644 } else if (files && files.length > 1) {
645 this.notifierService.notify('error', this.translateService.instant('DROPFILESVALIDATION'));
646 }
647 this.fileInputNetsLabel.nativeElement.innerText = files[0].name;
648 this.fileInputNets.nativeElement.value = null;
649 }
650
651 /** credentials file process @private */
652 public credentialsFile(files: FileList): void {
653 if (files && files.length === 1) {
654 this.sharedService.getFileString(files, 'yaml').then((fileContent: string): void => {
655 const getCredentialsJson: string = jsyaml.load(fileContent, { json: true });
kumaran.m3b4814a2020-05-01 19:48:54 +0530656 this.k8sclusterForm.get('credentials').setValue(JSON.stringify(getCredentialsJson));
657 }).catch((err: string): void => {
658 if (err === 'typeError') {
659 this.notifierService.notify('error', this.translateService.instant('YAMLFILETYPEERRROR'));
660 } else {
661 this.notifierService.notify('error', this.translateService.instant('ERROR'));
662 }
663 this.fileInputCredentialsLabel.nativeElement.innerText = this.translateService.instant('CHOOSEFILE');
664 this.fileInputCredentials.nativeElement.value = null;
665 });
666 } else if (files && files.length > 1) {
667 this.notifierService.notify('error', this.translateService.instant('DROPFILESVALIDATION'));
668 }
669 this.fileInputCredentialsLabel.nativeElement.innerText = files[0].name;
670 this.fileInputCredentials.nativeElement.value = null;
671 }
SANDHYA.JS26570112024-07-05 21:35:46 +0530672
673 /** Used to get the AbstractControl of controlName passed @private */
674 private getFormControl(controlName: string): AbstractControl {
675 // eslint-disable-next-line security/detect-object-injection
676 return this.k8sclusterForm.controls[controlName];
677 }
kumaran.m3b4814a2020-05-01 19:48:54 +0530678}