blob: 34ceeab12755898f148e66ae619b16cac5ce98d9 [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';
23import { AbstractControl, FormBuilder, 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.JSc08e1ec2025-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.JS26570112024-07-05 21:35:46 +053054 /** 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
bacigalupof633dbf2022-03-25 17:24:56 +000060 /** 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.JS26570112024-07-05 21:35:46 +053067 public selectedDeploymentMethods: string[] = ['helm-chart-v3', 'juju-bundle'];
bacigalupof633dbf2022-03-25 17:24:56 +000068
SANDHYA.JSc08e1ec2025-02-13 16:24:46 +053069 /** Contains all action types */
SANDHYA.JSec5e5cd2025-05-22 17:00:15 +053070 public actionTypes: string[] = ['update', 'upgrade'];
SANDHYA.JSc08e1ec2025-02-13 16:24:46 +053071
kumaran.m3b4814a2020-05-01 19:48:54 +053072 /** Instance for active modal service @public */
73 public activeModal: NgbActiveModal;
74
75 /** Variable set for twoway bindng @public */
76 public vimAccountId: string;
77
SANDHYA.JS26570112024-07-05 21:35:46 +053078 /** contains url @public */
79 public clusterUrl: string;
80
kumaran.m3b4814a2020-05-01 19:48:54 +053081 /** Form submission Add */
82 public submitted: boolean = false;
83
SANDHYA.JS26570112024-07-05 21:35:46 +053084 /** contains payload */
85 public payload: K8SPayload;
86
SANDHYA.JSb772de02024-12-10 15:21:03 +053087 /** Check the checkbox status */
88 public isChecked: boolean = true;
89
kumaran.m3b4814a2020-05-01 19:48:54 +053090 /** 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);
bacigalupof633dbf2022-03-25 17:24:56 +0000134 this.deploymentMethodsSelect = [
135 {
bacigalupof633dbf2022-03-25 17:24:56 +0000136 title: 'Helm v3',
137 value: 'helm-chart-v3'
138 },
139 {
140 title: 'Juju bundle',
141 value: 'juju-bundle'
142 }
143 ];
kumaran.m3b4814a2020-05-01 19:48:54 +0530144 }
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.JSc08e1ec2025-02-13 16:24:46 +0530155 this.actionTypes.forEach((type: string): void => {
156 if (type === this.profileType) {
157 this.k8sClusterDetail();
158 }
159 });
kumaran.m3b4814a2020-05-01 19:48:54 +0530160 }
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.JSb772de02024-12-10 15:21:03 +0530168 description: [''],
kumaran.m3b4814a2020-05-01 19:48:54 +0530169 nets: ['', [Validators.required]],
bacigalupof633dbf2022-03-25 17:24:56 +0000170 deployment_methods: ['', [Validators.required]],
SANDHYA.JS26570112024-07-05 21:35:46 +0530171 credentials: ['', [Validators.required]],
172 region_name: [''],
173 resource_group: [''],
174 node_count: ['', [Validators.required]],
SANDHYA.JSb772de02024-12-10 15:21:03 +0530175 node_size: ['', [Validators.required]],
176 bootstrap: [true],
177 k8sVersion: ['', [Validators.required]],
SANDHYA.JSc08e1ec2025-02-13 16:24:46 +0530178 update: ['']
kumaran.m3b4814a2020-05-01 19:48:54 +0530179 });
180 }
181
182 /** convenience getter for easy access to form fields */
183 get f(): FormGroup['controls'] { return this.k8sclusterForm.controls; }
184
185 /** Call the vimAccount details in the selection options @public */
186 public getDetailsvimAccount(): void {
187 this.isLoadingResults = true;
188 this.restService.getResource(environment.VIMACCOUNTS_URL).subscribe((vimAccounts: VimAccountDetails) => {
189 this.vimAccountSelect = vimAccounts;
190 this.isLoadingResults = false;
191 }, (error: ERRORDATA) => {
192 this.restService.handleError(error, 'get');
193 this.isLoadingResults = false;
194 });
195 }
196
SANDHYA.JSc08e1ec2025-02-13 16:24:46 +0530197 /** patch the form values for edit @public */
198 public k8sClusterDetail(): void {
199 this.isLoadingResults = true;
200 this.restService.getResource(environment.K8SCREATECLUSTER_URL + '/' + this.profileID).subscribe((k8sData: K8SCLUSTERDATA) => {
201 if (this.profileType === 'update') {
202 this.k8sclusterForm.patchValue({ update: k8sData.name, description: !isNullOrUndefined(k8sData.description) ? k8sData.description : '' });
203 } else if (this.profileType === 'upgrade') {
204 this.k8sclusterForm.patchValue({ k8sVersion: !isNullOrUndefined(k8sData.k8s_version) ? k8sData.k8s_version : '' });
SANDHYA.JSc08e1ec2025-02-13 16:24:46 +0530205 }
206 this.isLoadingResults = false;
207 }, (error: ERRORDATA) => {
208 this.restService.handleError(error, 'get');
209 this.isLoadingResults = false;
210 });
211 }
212
SANDHYA.JSb772de02024-12-10 15:21:03 +0530213 /** Call the event when checkbox is checked @public */
214 public getValue(event: Event): void {
215 this.isChecked = (event.target as HTMLInputElement).checked;
216 }
217
SANDHYA.JS26570112024-07-05 21:35:46 +0530218
219 /** Contain selected vimAccount details @public */
220 public getDetailsvim(event: VimAccountDetails): void {
221 this.vimAccountId = event._id;
222 }
223
kumaran.m3b4814a2020-05-01 19:48:54 +0530224 /** On modal submit k8sAddClusterSubmit will called @public */
225 public k8sAddClusterSubmit(): void {
SANDHYA.JS26570112024-07-05 21:35:46 +0530226 if (this.profileType === 'Manage') {
227 this.getFormControl('nets').disable();
228 this.getFormControl('credentials').disable();
229 this.getFormControl('deployment_methods').disable();
SANDHYA.JSb772de02024-12-10 15:21:03 +0530230 this.getFormControl('k8sVersion').disable();
SANDHYA.JSc08e1ec2025-02-13 16:24:46 +0530231 this.getFormControl('update').disable();
SANDHYA.JS26570112024-07-05 21:35:46 +0530232 this.manageCluster();
SANDHYA.JSb772de02024-12-10 15:21:03 +0530233 } else if (this.profileType === 'Register' && this.isChecked === true) {
SANDHYA.JS26570112024-07-05 21:35:46 +0530234 this.clusterUrl = environment.K8SCREATECLUSTER_URL + '/register';
235 this.getFormControl('region_name').disable();
236 this.getFormControl('resource_group').disable();
SANDHYA.JS26570112024-07-05 21:35:46 +0530237 this.getFormControl('k8s_version').disable();
238 this.getFormControl('node_size').disable();
SANDHYA.JSb772de02024-12-10 15:21:03 +0530239 this.getFormControl('node_count').disable();
240 this.getFormControl('nets').disable();
241 this.getFormControl('deployment_methods').disable();
242 this.getFormControl('k8sVersion').disable();
SANDHYA.JSc08e1ec2025-02-13 16:24:46 +0530243 this.getFormControl('update').disable();
SANDHYA.JSb772de02024-12-10 15:21:03 +0530244 this.registerCluster();
245 } if (this.isChecked === false && this.profileType === 'Register') {
246 this.clusterUrl = environment.K8SCLUSTER_URL;
247 this.getFormControl('bootstrap').disable();
248 this.getFormControl('region_name').disable();
249 this.getFormControl('resource_group').disable();
250 this.getFormControl('node_count').disable();
251 this.getFormControl('node_size').disable();
252 this.getFormControl('k8sVersion').disable();
SANDHYA.JSc08e1ec2025-02-13 16:24:46 +0530253 this.getFormControl('update').disable();
SANDHYA.JSb772de02024-12-10 15:21:03 +0530254 this.oldregisterCluster();
SANDHYA.JSec5e5cd2025-05-22 17:00:15 +0530255 } else if (this.profileType === 'upgrade') {
256 this.clusterUrl = environment.K8SCREATECLUSTER_URL + '/' + this.profileID + '/' + 'upgrade';
SANDHYA.JSb772de02024-12-10 15:21:03 +0530257 this.getFormControl('region_name').disable();
258 this.getFormControl('resource_group').disable();
SANDHYA.JS26570112024-07-05 21:35:46 +0530259 this.getFormControl('nets').disable();
260 this.getFormControl('credentials').disable();
261 this.getFormControl('deployment_methods').disable();
262 this.getFormControl('name').disable();
263 this.getFormControl('vim_account').disable();
264 this.getFormControl('description').disable();
SANDHYA.JSb772de02024-12-10 15:21:03 +0530265 this.getFormControl('bootstrap').disable();
266 this.getFormControl('node_count').disable();
267 this.getFormControl('node_size').disable();
268 this.getFormControl('k8s_version').disable();
SANDHYA.JSc08e1ec2025-02-13 16:24:46 +0530269 this.getFormControl('update').disable();
SANDHYA.JS26570112024-07-05 21:35:46 +0530270 this.updateCluster();
SANDHYA.JSc08e1ec2025-02-13 16:24:46 +0530271 } else if (this.profileType === 'update') {
272 this.clusterUrl = environment.K8SCREATECLUSTER_URL + '/' + this.profileID;
273 this.getFormControl('bootstrap').disable();
274 this.getFormControl('region_name').disable();
275 this.getFormControl('resource_group').disable();
276 this.getFormControl('nets').disable();
277 this.getFormControl('credentials').disable();
278 this.getFormControl('deployment_methods').disable();
279 this.getFormControl('vim_account').disable();
280 this.getFormControl('node_count').disable();
281 this.getFormControl('node_size').disable();
282 this.getFormControl('k8s_version').disable();
SANDHYA.JSc08e1ec2025-02-13 16:24:46 +0530283 this.getFormControl('k8sVersion').disable();
SANDHYA.JSc08e1ec2025-02-13 16:24:46 +0530284 this.getFormControl('name').disable();
285
286 this.editCluster();
SANDHYA.JS26570112024-07-05 21:35:46 +0530287 }
288 }
289
SANDHYA.JSb772de02024-12-10 15:21:03 +0530290 /** Old Register cluster flow @public */
291 public oldregisterCluster(): void {
kumaran.m3b4814a2020-05-01 19:48:54 +0530292 this.submitted = true;
293 this.sharedService.cleanForm(this.k8sclusterForm);
294 if (this.k8sclusterForm.invalid) {
295 return;
296 }
297 const modalData: MODALCLOSERESPONSEDATA = {
298 message: 'Done'
299 };
kumaran.m3b4814a2020-05-01 19:48:54 +0530300 const validJSONCredentails: boolean = this.sharedService.checkJson(this.k8sclusterForm.value.credentials);
301 if (validJSONCredentails) {
302 this.k8sclusterForm.value.credentials = jsyaml.load(this.k8sclusterForm.value.credentials.toString(), { json: true });
303 } else {
304 this.notifierService.notify('error', this.translateService.instant('INVALIDCONFIG'));
305 return;
306 }
307 const validJSONNets: boolean = this.sharedService.checkJson(this.k8sclusterForm.value.nets);
308 if (validJSONNets) {
309 this.k8sclusterForm.value.nets = jsyaml.load(this.k8sclusterForm.value.nets.toString(), { json: true });
310 } else {
311 this.notifierService.notify('error', this.translateService.instant('INVALIDCONFIG'));
312 return;
313 }
bacigalupof633dbf2022-03-25 17:24:56 +0000314
315 this.deploymentMethodsSubmit = new Map<string, boolean>();
316 /// Set deployment method Map
317 for (const methods of this.deploymentMethodsSelect) {
318 this.deploymentMethodsSubmit.set(methods.value, false);
319 }
320
321 this.k8sclusterForm.value.deployment_methods.forEach((dm: string): void => {
322 this.deploymentMethodsSubmit.set(dm, true);
323 });
324 // Transform Map to json object
325 const jsonDMObject: {} = {};
326 this.deploymentMethodsSubmit.forEach((value: boolean, key: string): void => {
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +0530327 // eslint-disable-next-line security/detect-object-injection
bacigalupof633dbf2022-03-25 17:24:56 +0000328 jsonDMObject[key] = value;
329 });
330
331 // Transform values to json
332 this.k8sclusterForm.value.deployment_methods = jsonDMObject;
333
SANDHYA.JS26570112024-07-05 21:35:46 +0530334 this.k8sclusterForm.value.vim_account = this.vimAccountId;
335
SANDHYA.JSb772de02024-12-10 15:21:03 +0530336 if (this.k8sclusterForm.value.description === '') {
337 delete this.k8sclusterForm.value.description;
338 }
339
340 const apiURLHeader: APIURLHEADER = {
341 url: this.clusterUrl,
342 httpOptions: { headers: this.headers }
343 };
344
345 this.isLoadingResults = true;
346 this.restService.postResource(apiURLHeader, this.k8sclusterForm.value).subscribe((result: {}) => {
347 this.activeModal.close(modalData);
348 this.isLoadingResults = false;
349 this.notifierService.notify('success', this.k8sclusterForm.value.name +
350 this.translateService.instant('PAGE.K8S.REGISTEREDSUCCESSFULLY'));
351 }, (error: ERRORDATA) => {
352 this.restService.handleError(error, 'post');
353 this.isLoadingResults = false;
354 });
355 }
356
357 /** New Register cluster flow @public */
358 public registerCluster(): void {
359 this.submitted = true;
360 this.sharedService.cleanForm(this.k8sclusterForm);
361 if (this.k8sclusterForm.invalid) {
362 return;
363 }
364 const modalData: MODALCLOSERESPONSEDATA = {
365 message: 'Done'
366 };
367 const validJSONCredentails: boolean = this.sharedService.checkJson(this.k8sclusterForm.value.credentials);
368 if (validJSONCredentails) {
369 this.k8sclusterForm.value.credentials = jsyaml.load(this.k8sclusterForm.value.credentials.toString(), { json: true });
370 } else {
371 this.notifierService.notify('error', this.translateService.instant('INVALIDCONFIG'));
372 return;
373 }
374
375 if (this.k8sclusterForm.value.description === '') {
376 delete this.k8sclusterForm.value.description;
377 }
378
SANDHYA.JS26570112024-07-05 21:35:46 +0530379 const apiURLHeader: APIURLHEADER = {
380 url: this.clusterUrl,
381 httpOptions: { headers: this.headers }
382 };
383
kumaran.m3b4814a2020-05-01 19:48:54 +0530384 this.isLoadingResults = true;
385 this.restService.postResource(apiURLHeader, this.k8sclusterForm.value).subscribe((result: {}) => {
386 this.activeModal.close(modalData);
387 this.isLoadingResults = false;
388 this.notifierService.notify('success', this.k8sclusterForm.value.name +
SANDHYA.JS26570112024-07-05 21:35:46 +0530389 this.translateService.instant('PAGE.K8S.REGISTEREDSUCCESSFULLY'));
390 }, (error: ERRORDATA) => {
391 this.restService.handleError(error, 'post');
392 this.isLoadingResults = false;
393 });
394 }
395
396 /** Manage cluster @public */
397 public manageCluster(): void {
398 this.submitted = true;
399 this.sharedService.cleanForm(this.k8sclusterForm);
400 if (this.k8sclusterForm.invalid) {
401 return;
402 }
403 const modalData: MODALCLOSERESPONSEDATA = {
404 message: 'Done'
405 };
406 const apiURLHeader: APIURLHEADER = {
407 url: environment.K8SCREATECLUSTER_URL,
408 httpOptions: { headers: this.headers }
409 };
410
411 this.isLoadingResults = true;
412 const payload: K8SPayload = {
413 name: this.k8sclusterForm.value.name,
414 vim_account: this.k8sclusterForm.value.vim_account,
415 location: this.k8sclusterForm.value.location,
416 region_name: this.k8sclusterForm.value.region_name,
417 resource_group: this.k8sclusterForm.value.resource_group,
418 k8s_version: this.k8sclusterForm.value.k8s_version,
419 node_size: this.k8sclusterForm.value.node_size,
420 node_count: Number(this.k8sclusterForm.value.node_count),
SANDHYA.JSb772de02024-12-10 15:21:03 +0530421 description: this.k8sclusterForm.value.description,
422 bootstrap: Boolean(this.k8sclusterForm.value.bootstrap)
SANDHYA.JS26570112024-07-05 21:35:46 +0530423 };
424 if (this.k8sclusterForm.value.region_name === '') {
425 delete payload.region_name;
426 } else if (this.k8sclusterForm.value.resource_group === '') {
427 delete payload.resource_group;
SANDHYA.JSb772de02024-12-10 15:21:03 +0530428 } else if (this.k8sclusterForm.value.description === '') {
429 delete payload.description;
SANDHYA.JS26570112024-07-05 21:35:46 +0530430 }
SANDHYA.JSb772de02024-12-10 15:21:03 +0530431 if (this.k8sclusterForm.value.region_name === '' && this.k8sclusterForm.value.resource_group === '' && this.k8sclusterForm.value.description === '') {
SANDHYA.JS26570112024-07-05 21:35:46 +0530432 delete payload.region_name;
433 delete payload.resource_group;
SANDHYA.JSb772de02024-12-10 15:21:03 +0530434 delete payload.description;
SANDHYA.JS26570112024-07-05 21:35:46 +0530435 }
436 this.restService.postResource(apiURLHeader, payload).subscribe((result: {}) => {
437 this.activeModal.close(modalData);
438 this.isLoadingResults = false;
439 this.notifierService.notify('success', this.k8sclusterForm.value.name +
kumaran.m3b4814a2020-05-01 19:48:54 +0530440 this.translateService.instant('PAGE.K8S.CREATEDSUCCESSFULLY'));
441 }, (error: ERRORDATA) => {
442 this.restService.handleError(error, 'post');
443 this.isLoadingResults = false;
444 });
445 }
446
SANDHYA.JS26570112024-07-05 21:35:46 +0530447 /** Update cluster @public */
448 public updateCluster(): void {
449 this.submitted = true;
450 this.sharedService.cleanForm(this.k8sclusterForm);
451 if (this.k8sclusterForm.invalid) {
452 return;
453 }
454 const modalData: MODALCLOSERESPONSEDATA = {
455 message: 'Done'
456 };
457 const apiURLHeader: APIURLHEADER = {
458 url: this.clusterUrl,
459 httpOptions: { headers: this.headers }
460 };
SANDHYA.JS26570112024-07-05 21:35:46 +0530461 if (this.profileType === 'upgrade') {
462 this.payload = {
SANDHYA.JSb772de02024-12-10 15:21:03 +0530463 k8s_version: this.k8sclusterForm.value.k8sVersion
SANDHYA.JS26570112024-07-05 21:35:46 +0530464 };
SANDHYA.JS26570112024-07-05 21:35:46 +0530465 }
SANDHYA.JSb772de02024-12-10 15:21:03 +0530466 this.isLoadingResults = true;
SANDHYA.JS26570112024-07-05 21:35:46 +0530467 this.restService.postResource(apiURLHeader, this.payload).subscribe((result: {}) => {
468 this.activeModal.close(modalData);
469 this.isLoadingResults = false;
470 this.notifierService.notify('success',
471 this.translateService.instant('PAGE.K8S.UPDATEDSUCCESSFULLY'));
472 }, (error: ERRORDATA) => {
473 this.restService.handleError(error, 'post');
474 this.isLoadingResults = false;
475 });
476 }
477
SANDHYA.JSc08e1ec2025-02-13 16:24:46 +0530478
479 /** Update cluster @public */
480 public editCluster(): void {
481 this.submitted = true;
482 this.sharedService.cleanForm(this.k8sclusterForm);
483 if (this.k8sclusterForm.invalid) {
484 return;
485 }
486 const modalData: MODALCLOSERESPONSEDATA = {
487 message: 'Done'
488 };
489 const apiURLHeader: APIURLHEADER = {
490 url: this.clusterUrl,
491 httpOptions: { headers: this.headers }
492 };
493 this.isLoadingResults = true;
494 if (this.k8sclusterForm.value.description === '') {
495 delete this.k8sclusterForm.value.description;
496 this.payload = {
497 name: this.k8sclusterForm.value.update
498 };
499 }
500 if (this.k8sclusterForm.value.update === '') {
501 delete this.k8sclusterForm.value.update;
502 this.payload = {
503 description: this.k8sclusterForm.value.description
504 };
505 }
506 if (this.k8sclusterForm.value.update !== '' && this.k8sclusterForm.value.description !== '') {
507 this.payload = {
508 name: this.k8sclusterForm.value.update,
509 description: this.k8sclusterForm.value.description
510 };
511 }
512 this.restService.patchResource(apiURLHeader, this.payload).subscribe((result: {}) => {
513 this.activeModal.close(modalData);
514 this.isLoadingResults = false;
515 this.notifierService.notify('success',
516 this.translateService.instant('PAGE.K8S.UPDATEDSUCCESSFULLY'));
517 }, (error: ERRORDATA) => {
518 this.restService.handleError(error, 'post');
519 this.isLoadingResults = false;
520 });
521 }
522
kumaran.m3b4814a2020-05-01 19:48:54 +0530523 /** Nets file process @private */
524 public netsFile(files: FileList): void {
525 if (files && files.length === 1) {
526 this.sharedService.getFileString(files, 'json').then((fileContent: string): void => {
527 const getNetsJson: string = jsyaml.load(fileContent, { json: true });
kumaran.m3b4814a2020-05-01 19:48:54 +0530528 this.k8sclusterForm.get('nets').setValue(JSON.stringify(getNetsJson));
529 }).catch((err: string): void => {
530 if (err === 'typeError') {
531 this.notifierService.notify('error', this.translateService.instant('JSONFILETYPEERRROR'));
532 } else {
533 this.notifierService.notify('error', this.translateService.instant('ERROR'));
534 }
535 this.fileInputNetsLabel.nativeElement.innerText = this.translateService.instant('CHOOSEFILE');
536 this.fileInputNets.nativeElement.value = null;
537 });
538 } else if (files && files.length > 1) {
539 this.notifierService.notify('error', this.translateService.instant('DROPFILESVALIDATION'));
540 }
541 this.fileInputNetsLabel.nativeElement.innerText = files[0].name;
542 this.fileInputNets.nativeElement.value = null;
543 }
544
545 /** credentials file process @private */
546 public credentialsFile(files: FileList): void {
547 if (files && files.length === 1) {
548 this.sharedService.getFileString(files, 'yaml').then((fileContent: string): void => {
549 const getCredentialsJson: string = jsyaml.load(fileContent, { json: true });
kumaran.m3b4814a2020-05-01 19:48:54 +0530550 this.k8sclusterForm.get('credentials').setValue(JSON.stringify(getCredentialsJson));
551 }).catch((err: string): void => {
552 if (err === 'typeError') {
553 this.notifierService.notify('error', this.translateService.instant('YAMLFILETYPEERRROR'));
554 } else {
555 this.notifierService.notify('error', this.translateService.instant('ERROR'));
556 }
557 this.fileInputCredentialsLabel.nativeElement.innerText = this.translateService.instant('CHOOSEFILE');
558 this.fileInputCredentials.nativeElement.value = null;
559 });
560 } else if (files && files.length > 1) {
561 this.notifierService.notify('error', this.translateService.instant('DROPFILESVALIDATION'));
562 }
563 this.fileInputCredentialsLabel.nativeElement.innerText = files[0].name;
564 this.fileInputCredentials.nativeElement.value = null;
565 }
SANDHYA.JS26570112024-07-05 21:35:46 +0530566
567 /** Used to get the AbstractControl of controlName passed @private */
568 private getFormControl(controlName: string): AbstractControl {
569 // eslint-disable-next-line security/detect-object-injection
570 return this.k8sclusterForm.controls[controlName];
571 }
kumaran.m3b4814a2020-05-01 19:48:54 +0530572}