blob: 919dda10e0c1317d2e8c8c5718dd58c4413c1ea2 [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
SANDHYA.JS26570112024-07-05 21:35:46 +053010 Unless required by applicable law or agreed to in writing, software
kumaran.m3b4814a2020-05-01 19:48:54 +053011 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 K8 Action Component
20 */
SANDHYA.JSb5347152024-10-15 11:41:45 +053021import { HttpHeaders } from '@angular/common/http';
22import { ChangeDetectorRef, Component, Injector } from '@angular/core';
kumaran.m3b4814a2020-05-01 19:48:54 +053023import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
24import { TranslateService } from '@ngx-translate/core';
SANDHYA.JSb5347152024-10-15 11:41:45 +053025import { NotifierService } from 'angular-notifier';
26import { ERRORDATA, GETAPIURLHEADER, MODALCLOSERESPONSEDATA } from 'CommonModel';
kumaran.m3b4814a2020-05-01 19:48:54 +053027import { DeleteComponent } from 'DeleteComponent';
SANDHYA.JSb5347152024-10-15 11:41:45 +053028import { environment } from 'environment';
SANDHYA.JS26570112024-07-05 21:35:46 +053029import { K8sAddClusterComponent } from 'K8sAddClusterComponent';
30import { K8sAttachProfileComponent } from 'K8sAttachProfileComponent';
31import { K8sInfraConfigAddComponent } from 'K8sInfraConfigAddComponent';
32import { INFRACONFIGPAYLOAD, K8SCLUSTERDATADISPLAY, K8SPayload, K8SREPODATADISPLAY } from 'K8sModel';
33import { KSUAddComponent } from 'KSUAddComponent';
SANDHYA.JSb5347152024-10-15 11:41:45 +053034import { RestService } from 'RestService';
35import { isNullOrUndefined, SharedService } from 'SharedService';
kumaran.m3b4814a2020-05-01 19:48:54 +053036import { ShowInfoComponent } from 'ShowInfoComponent';
37/**
38 * Creating component
39 * @Component takes K8sActionComponent.html as template url
40 */
41@Component({
42 selector: 'app-k8s-action',
43 templateUrl: './K8sActionComponent.html',
44 styleUrls: ['./K8sActionComponent.scss']
45})
46/** Exporting a class @exports K8sActionComponent */
SANDHYA.JS26570112024-07-05 21:35:46 +053047export class K8sActionComponent {
kumaran.m3b4814a2020-05-01 19:48:54 +053048 /** To inject services @public */
49 public injector: Injector;
50
51 /** To get the value from the Users action via valuePrepareFunction default Property of ng-smarttable @public */
SANDHYA.JS26570112024-07-05 21:35:46 +053052 public value: K8SCLUSTERDATADISPLAY | K8SREPODATADISPLAY | INFRACONFIGPAYLOAD;
kumaran.m3b4814a2020-05-01 19:48:54 +053053
54 /** handle translate @public */
55 public translateService: TranslateService;
56
SANDHYA.JS26570112024-07-05 21:35:46 +053057 /** Contains K8s Type @public */
kumaran.m3b4814a2020-05-01 19:48:54 +053058 public getK8sType: string;
59
SANDHYA.JS26570112024-07-05 21:35:46 +053060 /** Check register page @public */
61 public checkRegister = false;
62
63 /** Contains state @public */
64 public state: string;
65
66 /** Check profile or not @public */
67 public isProfile = false;
68
69 /** Check ksu or not @public */
70 public isKSU = false;
71
72 /** Check cluster or not @public */
73 public isCluster = false;
74
SANDHYA.JSb5347152024-10-15 11:41:45 +053075 /** Check the loading results for loader status @public */
76 public isLoadingDownloadResult: boolean = false;
77
kumaran.m3b4814a2020-05-01 19:48:54 +053078 /** Instance of the modal service @private */
79 private modalService: NgbModal;
80
81 /** Contains all methods related to shared @private */
82 private sharedService: SharedService;
83
84 /** Contains instance ID @private */
85 private instanceID: string;
86
SANDHYA.JSb5347152024-10-15 11:41:45 +053087 /** Utilizes rest service for any CRUD operations @private */
88 private restService: RestService;
89
90 /** Notifier service to popup notification @private */
91 private notifierService: NotifierService;
92
93 /** Detect changes for the User Input */
94 private cd: ChangeDetectorRef;
95
96 /** Set timeout @private */
97 // eslint-disable-next-line @typescript-eslint/no-magic-numbers
98 private timeOut: number = 1000;
99
100 /** Controls the header form @private */
101 private headers: HttpHeaders;
102
kumaran.m3b4814a2020-05-01 19:48:54 +0530103 constructor(injector: Injector) {
104 this.injector = injector;
105 this.modalService = this.injector.get(NgbModal);
SANDHYA.JSb5347152024-10-15 11:41:45 +0530106 this.notifierService = this.injector.get(NotifierService);
kumaran.m3b4814a2020-05-01 19:48:54 +0530107 this.sharedService = this.injector.get(SharedService);
108 this.translateService = this.injector.get(TranslateService);
SANDHYA.JSb5347152024-10-15 11:41:45 +0530109 this.restService = this.injector.get(RestService);
110 this.cd = this.injector.get(ChangeDetectorRef);
kumaran.m3b4814a2020-05-01 19:48:54 +0530111 }
112
113 /**
114 * Lifecyle Hooks the trigger before component is instantiate
115 */
116 public ngOnInit(): void {
SANDHYA.JSb5347152024-10-15 11:41:45 +0530117 this.headers = new HttpHeaders({
118 Accept: 'application/zip, application/json',
119 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0'
120 });
kumaran.m3b4814a2020-05-01 19:48:54 +0530121 this.instanceID = this.value.identifier;
122 this.getK8sType = this.value.pageType;
SANDHYA.JS26570112024-07-05 21:35:46 +0530123 this.state = this.value.state;
SANDHYA.JSb772de02024-12-10 15:21:03 +0530124 if ((this.value.createdbyosm) === 'true') {
SANDHYA.JS26570112024-07-05 21:35:46 +0530125 this.isCluster = true;
SANDHYA.JSb772de02024-12-10 15:21:03 +0530126 } else if ((this.value.createdbyosm) === 'false') {
127 this.isCluster = false;
SANDHYA.JS26570112024-07-05 21:35:46 +0530128 }
129 if (this.getK8sType === 'k8-ksu') {
130 this.isKSU = true;
SANDHYA.JSb772de02024-12-10 15:21:03 +0530131 } else {
132 this.isKSU = false;
133 }
134
135 if (this.getK8sType === 'infra-config' || this.getK8sType === 'infra-controller' || this.getK8sType === 'app-profile' || this.getK8sType === 'resource-profile') {
SANDHYA.JS52af4802025-05-22 17:00:15 +0530136 this.isProfile = true;
SANDHYA.JSb772de02024-12-10 15:21:03 +0530137 } else {
SANDHYA.JS26570112024-07-05 21:35:46 +0530138 this.isProfile = false;
139 }
kumaran.m3b4814a2020-05-01 19:48:54 +0530140 }
141
142 /** Delete User Account @public */
SANDHYA.JS52af4802025-05-22 17:00:15 +0530143 public deleteK8s(forceAction: boolean): void {
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +0530144 // eslint-disable-next-line security/detect-non-literal-fs-filename
kumaran.m3b4814a2020-05-01 19:48:54 +0530145 const modalRef: NgbModalRef = this.modalService.open(DeleteComponent, { backdrop: 'static' });
SANDHYA.JS52af4802025-05-22 17:00:15 +0530146 modalRef.componentInstance.params = {
147 forceDeleteType: forceAction
148 };
kumaran.m3b4814a2020-05-01 19:48:54 +0530149 modalRef.result.then((result: MODALCLOSERESPONSEDATA) => {
150 if (result) {
151 this.sharedService.callData();
152 }
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +0530153 }).catch((): void => {
154 // Catch Navigation Error
SANDHYA.JS26570112024-07-05 21:35:46 +0530155 });
kumaran.m3b4814a2020-05-01 19:48:54 +0530156 }
157
158 /** Shows information using modalservice @public */
159 public infoK8s(pageType: string): void {
160 let pageName: string = '';
161 let title: string = '';
162 if (pageType === 'repo') {
163 pageName = 'k8s-repo';
164 title = 'PAGE.K8S.K8SREPODETAILS';
165 } else {
166 pageName = 'k8s-cluster';
167 title = 'PAGE.K8S.K8SCLUSTERDETAILS';
168 }
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +0530169 // eslint-disable-next-line security/detect-non-literal-fs-filename
kumaran.m3b4814a2020-05-01 19:48:54 +0530170 this.modalService.open(ShowInfoComponent, { backdrop: 'static' }).componentInstance.params = {
171 id: this.instanceID,
172 page: pageName,
SANDHYA.JSb772de02024-12-10 15:21:03 +0530173 titleName: title,
174 createdbyosm: this.value.createdbyosm,
175 bootstrap: this.value.bootstrap,
176 key: this.value.key
kumaran.m3b4814a2020-05-01 19:48:54 +0530177 };
178 }
SANDHYA.JS26570112024-07-05 21:35:46 +0530179 /** Edit profile @public */
180 public editProfile(editType: string): void {
181 // eslint-disable-next-line security/detect-non-literal-fs-filename
182 const modalRef: NgbModalRef = this.modalService.open(K8sInfraConfigAddComponent, { backdrop: 'static' });
183 modalRef.componentInstance.profileID = this.value.identifier;
184 modalRef.componentInstance.profileType = editType;
185 modalRef.componentInstance.profileName = this.value.name;
186 modalRef.componentInstance.profileDescription = this.value.description;
187 modalRef.result.then((result: MODALCLOSERESPONSEDATA) => {
188 if (result) {
189 this.sharedService.callData();
190 }
191 }).catch((): void => {
192 // Catch Navigation Error
193 });
194 }
195
196 /** Edit cluster @public */
197 public editCluster(editType: string): void {
198 // eslint-disable-next-line security/detect-non-literal-fs-filename
199 const modalRef: NgbModalRef = this.modalService.open(K8sAddClusterComponent, { backdrop: 'static' });
200 modalRef.componentInstance.profileID = this.value.identifier;
201 modalRef.componentInstance.profileType = editType;
202 modalRef.result.then((result: MODALCLOSERESPONSEDATA) => {
203 if (result) {
204 this.sharedService.callData();
205 }
206 }).catch((): void => {
207 // Catch Navigation Error
208 });
209 }
210
211
212 /** Edit profile under cluster @public */
213 public editClusterProfile(editType: string): void {
214 // eslint-disable-next-line security/detect-non-literal-fs-filename
215 const modalRef: NgbModalRef = this.modalService.open(K8sAttachProfileComponent, { backdrop: 'static' });
216 modalRef.componentInstance.profileID = this.value.identifier;
217 modalRef.componentInstance.profileType = editType;
218 modalRef.result.then((result: MODALCLOSERESPONSEDATA) => {
219 if (result) {
220 this.sharedService.callData();
221 }
222 }).catch((): void => {
223 // Catch Navigation Error
224 });
225 }
226
227 /** Move KSU @public */
228 public moveKsu(): void {
229 // eslint-disable-next-line security/detect-non-literal-fs-filename
230 const modalRef: NgbModalRef = this.modalService.open(KSUAddComponent, { backdrop: 'static' });
231 modalRef.componentInstance.profileID = this.value.identifier;
232 modalRef.componentInstance.profileType = 'move';
233 modalRef.result.then((result: MODALCLOSERESPONSEDATA) => {
234 if (result) {
235 this.sharedService.callData();
236 }
237 }).catch((): void => {
238 // Catch Navigation Error
239 });
240 }
241
242 /** Edit KSU @public */
243 public editKsu(): void {
244 // eslint-disable-next-line security/detect-non-literal-fs-filename
245 const modalRef: NgbModalRef = this.modalService.open(KSUAddComponent, { backdrop: 'static' });
246 modalRef.componentInstance.profileID = this.value.identifier;
247 modalRef.componentInstance.profileType = 'edit';
248 modalRef.result.then((result: MODALCLOSERESPONSEDATA) => {
249 if (result) {
250 this.sharedService.callData();
251 }
252 }).catch((): void => {
253 // Catch Navigation Error
254 });
255 }
256
SANDHYA.JSb5347152024-10-15 11:41:45 +0530257 /** Download credentials @public */
258 public getCredentials(): void {
259 this.isLoadingDownloadResult = true;
260 const httpOptions: GETAPIURLHEADER = {
261 headers: this.headers,
262 responseType: 'blob'
263 };
264 this.restService.getResource(environment.K8SCREATECLUSTER_URL + '/' + this.instanceID + '/get_creds')
265 .subscribe((res: { op_id: string }) => {
266 if (!isNullOrUndefined(res.op_id)) {
267 this.restService.getResource(environment.K8SCREATECLUSTER_URL + '/' + this.instanceID + '/get_creds_file' + '/' + res.op_id, httpOptions)
268 .subscribe((response: Blob) => {
269 this.isLoadingDownloadResult = true;
270 if (!isNullOrUndefined(response)) {
271 this.isLoadingDownloadResult = false;
272 const binaryData: Blob[] = [];
273 binaryData.push(response);
274 this.sharedService.downloadFiles(this.value.name, binaryData, response.type);
275 this.isLoadingDownloadResult = false;
276 this.changeDetactionforDownload();
277 }
278 }, (error: ERRORDATA) => {
279 this.isLoadingDownloadResult = false;
280 this.notifierService.notify('error', this.translateService.instant('ERROR'));
281 this.changeDetactionforDownload();
282 if (typeof error.error === 'object') {
283 error.error.text().then((data: string): void => {
284 error.error = JSON.parse(data);
285 this.restService.handleError(error, 'getBlob');
286 });
287 }
288 });
289 }
290 }, (error: ERRORDATA) => {
291 this.isLoadingDownloadResult = false;
292 this.restService.handleError(error, 'get');
293 });
294 }
295
SANDHYA.JS26570112024-07-05 21:35:46 +0530296 /** Clone KSU @public */
297 public cloneKsu(): void {
298 // eslint-disable-next-line security/detect-non-literal-fs-filename
299 const modalRef: NgbModalRef = this.modalService.open(KSUAddComponent, { backdrop: 'static' });
300 modalRef.componentInstance.profileID = this.value.identifier;
301 modalRef.componentInstance.profileType = 'clone';
302 modalRef.result.then((result: MODALCLOSERESPONSEDATA) => {
303 if (result) {
304 this.sharedService.callData();
305 }
306 }).catch((): void => {
307 // Catch Navigation Error
308 });
309 }
SANDHYA.JSb5347152024-10-15 11:41:45 +0530310
311 /** Change the detaction @public */
312 public changeDetactionforDownload(): void {
313 setTimeout(() => {
314 this.cd.detectChanges();
315 }, this.timeOut);
316 }
kumaran.m3b4814a2020-05-01 19:48:54 +0530317}