blob: bbd574f5375b34ad02852a790428964e00fa4bf6 [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') {
136 this.isProfile = true;
137 } 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.JS26570112024-07-05 21:35:46 +0530143 public deleteK8s(): 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' });
146 modalRef.result.then((result: MODALCLOSERESPONSEDATA) => {
147 if (result) {
148 this.sharedService.callData();
149 }
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +0530150 }).catch((): void => {
151 // Catch Navigation Error
SANDHYA.JS26570112024-07-05 21:35:46 +0530152 });
kumaran.m3b4814a2020-05-01 19:48:54 +0530153 }
154
155 /** Shows information using modalservice @public */
156 public infoK8s(pageType: string): void {
157 let pageName: string = '';
158 let title: string = '';
159 if (pageType === 'repo') {
160 pageName = 'k8s-repo';
161 title = 'PAGE.K8S.K8SREPODETAILS';
162 } else {
163 pageName = 'k8s-cluster';
164 title = 'PAGE.K8S.K8SCLUSTERDETAILS';
165 }
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +0530166 // eslint-disable-next-line security/detect-non-literal-fs-filename
kumaran.m3b4814a2020-05-01 19:48:54 +0530167 this.modalService.open(ShowInfoComponent, { backdrop: 'static' }).componentInstance.params = {
168 id: this.instanceID,
169 page: pageName,
SANDHYA.JSb772de02024-12-10 15:21:03 +0530170 titleName: title,
171 createdbyosm: this.value.createdbyosm,
172 bootstrap: this.value.bootstrap,
173 key: this.value.key
kumaran.m3b4814a2020-05-01 19:48:54 +0530174 };
175 }
SANDHYA.JS26570112024-07-05 21:35:46 +0530176 /** Edit profile @public */
177 public editProfile(editType: string): void {
178 // eslint-disable-next-line security/detect-non-literal-fs-filename
179 const modalRef: NgbModalRef = this.modalService.open(K8sInfraConfigAddComponent, { backdrop: 'static' });
180 modalRef.componentInstance.profileID = this.value.identifier;
181 modalRef.componentInstance.profileType = editType;
182 modalRef.componentInstance.profileName = this.value.name;
183 modalRef.componentInstance.profileDescription = this.value.description;
184 modalRef.result.then((result: MODALCLOSERESPONSEDATA) => {
185 if (result) {
186 this.sharedService.callData();
187 }
188 }).catch((): void => {
189 // Catch Navigation Error
190 });
191 }
192
193 /** Edit cluster @public */
194 public editCluster(editType: string): void {
195 // eslint-disable-next-line security/detect-non-literal-fs-filename
196 const modalRef: NgbModalRef = this.modalService.open(K8sAddClusterComponent, { backdrop: 'static' });
197 modalRef.componentInstance.profileID = this.value.identifier;
198 modalRef.componentInstance.profileType = editType;
199 modalRef.result.then((result: MODALCLOSERESPONSEDATA) => {
200 if (result) {
201 this.sharedService.callData();
202 }
203 }).catch((): void => {
204 // Catch Navigation Error
205 });
206 }
207
208
209 /** Edit profile under cluster @public */
210 public editClusterProfile(editType: string): void {
211 // eslint-disable-next-line security/detect-non-literal-fs-filename
212 const modalRef: NgbModalRef = this.modalService.open(K8sAttachProfileComponent, { backdrop: 'static' });
213 modalRef.componentInstance.profileID = this.value.identifier;
214 modalRef.componentInstance.profileType = editType;
215 modalRef.result.then((result: MODALCLOSERESPONSEDATA) => {
216 if (result) {
217 this.sharedService.callData();
218 }
219 }).catch((): void => {
220 // Catch Navigation Error
221 });
222 }
223
224 /** Move KSU @public */
225 public moveKsu(): void {
226 // eslint-disable-next-line security/detect-non-literal-fs-filename
227 const modalRef: NgbModalRef = this.modalService.open(KSUAddComponent, { backdrop: 'static' });
228 modalRef.componentInstance.profileID = this.value.identifier;
229 modalRef.componentInstance.profileType = 'move';
230 modalRef.result.then((result: MODALCLOSERESPONSEDATA) => {
231 if (result) {
232 this.sharedService.callData();
233 }
234 }).catch((): void => {
235 // Catch Navigation Error
236 });
237 }
238
239 /** Edit KSU @public */
240 public editKsu(): void {
241 // eslint-disable-next-line security/detect-non-literal-fs-filename
242 const modalRef: NgbModalRef = this.modalService.open(KSUAddComponent, { backdrop: 'static' });
243 modalRef.componentInstance.profileID = this.value.identifier;
244 modalRef.componentInstance.profileType = 'edit';
245 modalRef.result.then((result: MODALCLOSERESPONSEDATA) => {
246 if (result) {
247 this.sharedService.callData();
248 }
249 }).catch((): void => {
250 // Catch Navigation Error
251 });
252 }
253
SANDHYA.JSb5347152024-10-15 11:41:45 +0530254 /** Download credentials @public */
255 public getCredentials(): void {
256 this.isLoadingDownloadResult = true;
257 const httpOptions: GETAPIURLHEADER = {
258 headers: this.headers,
259 responseType: 'blob'
260 };
261 this.restService.getResource(environment.K8SCREATECLUSTER_URL + '/' + this.instanceID + '/get_creds')
262 .subscribe((res: { op_id: string }) => {
263 if (!isNullOrUndefined(res.op_id)) {
264 this.restService.getResource(environment.K8SCREATECLUSTER_URL + '/' + this.instanceID + '/get_creds_file' + '/' + res.op_id, httpOptions)
265 .subscribe((response: Blob) => {
266 this.isLoadingDownloadResult = true;
267 if (!isNullOrUndefined(response)) {
268 this.isLoadingDownloadResult = false;
269 const binaryData: Blob[] = [];
270 binaryData.push(response);
271 this.sharedService.downloadFiles(this.value.name, binaryData, response.type);
272 this.isLoadingDownloadResult = false;
273 this.changeDetactionforDownload();
274 }
275 }, (error: ERRORDATA) => {
276 this.isLoadingDownloadResult = false;
277 this.notifierService.notify('error', this.translateService.instant('ERROR'));
278 this.changeDetactionforDownload();
279 if (typeof error.error === 'object') {
280 error.error.text().then((data: string): void => {
281 error.error = JSON.parse(data);
282 this.restService.handleError(error, 'getBlob');
283 });
284 }
285 });
286 }
287 }, (error: ERRORDATA) => {
288 this.isLoadingDownloadResult = false;
289 this.restService.handleError(error, 'get');
290 });
291 }
292
SANDHYA.JS26570112024-07-05 21:35:46 +0530293 /** Clone KSU @public */
294 public cloneKsu(): void {
295 // eslint-disable-next-line security/detect-non-literal-fs-filename
296 const modalRef: NgbModalRef = this.modalService.open(KSUAddComponent, { backdrop: 'static' });
297 modalRef.componentInstance.profileID = this.value.identifier;
298 modalRef.componentInstance.profileType = 'clone';
299 modalRef.result.then((result: MODALCLOSERESPONSEDATA) => {
300 if (result) {
301 this.sharedService.callData();
302 }
303 }).catch((): void => {
304 // Catch Navigation Error
305 });
306 }
SANDHYA.JSb5347152024-10-15 11:41:45 +0530307
308 /** Change the detaction @public */
309 public changeDetactionforDownload(): void {
310 setTimeout(() => {
311 this.cd.detectChanges();
312 }, this.timeOut);
313 }
kumaran.m3b4814a2020-05-01 19:48:54 +0530314}