| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +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 | |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 10 | Unless required by applicable law or agreed to in writing, software |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 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 K8 Action Component |
| 20 | */ |
| SANDHYA.JS | b534715 | 2024-10-15 11:41:45 +0530 | [diff] [blame^] | 21 | import { HttpHeaders } from '@angular/common/http'; |
| 22 | import { ChangeDetectorRef, Component, Injector } from '@angular/core'; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 23 | import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap'; |
| 24 | import { TranslateService } from '@ngx-translate/core'; |
| SANDHYA.JS | b534715 | 2024-10-15 11:41:45 +0530 | [diff] [blame^] | 25 | import { NotifierService } from 'angular-notifier'; |
| 26 | import { ERRORDATA, GETAPIURLHEADER, MODALCLOSERESPONSEDATA } from 'CommonModel'; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 27 | import { DeleteComponent } from 'DeleteComponent'; |
| SANDHYA.JS | b534715 | 2024-10-15 11:41:45 +0530 | [diff] [blame^] | 28 | import { environment } from 'environment'; |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 29 | import { K8sAddClusterComponent } from 'K8sAddClusterComponent'; |
| 30 | import { K8sAttachProfileComponent } from 'K8sAttachProfileComponent'; |
| 31 | import { K8sInfraConfigAddComponent } from 'K8sInfraConfigAddComponent'; |
| 32 | import { INFRACONFIGPAYLOAD, K8SCLUSTERDATADISPLAY, K8SPayload, K8SREPODATADISPLAY } from 'K8sModel'; |
| 33 | import { KSUAddComponent } from 'KSUAddComponent'; |
| SANDHYA.JS | b534715 | 2024-10-15 11:41:45 +0530 | [diff] [blame^] | 34 | import { RestService } from 'RestService'; |
| 35 | import { isNullOrUndefined, SharedService } from 'SharedService'; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 36 | import { 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.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 47 | export class K8sActionComponent { |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 48 | /** 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.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 52 | public value: K8SCLUSTERDATADISPLAY | K8SREPODATADISPLAY | INFRACONFIGPAYLOAD; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 53 | |
| 54 | /** handle translate @public */ |
| 55 | public translateService: TranslateService; |
| 56 | |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 57 | /** Contains K8s Type @public */ |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 58 | public getK8sType: string; |
| 59 | |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 60 | /** 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.JS | b534715 | 2024-10-15 11:41:45 +0530 | [diff] [blame^] | 75 | /** Check the loading results for loader status @public */ |
| 76 | public isLoadingDownloadResult: boolean = false; |
| 77 | |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 78 | /** 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.JS | b534715 | 2024-10-15 11:41:45 +0530 | [diff] [blame^] | 87 | /** 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.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 103 | constructor(injector: Injector) { |
| 104 | this.injector = injector; |
| 105 | this.modalService = this.injector.get(NgbModal); |
| SANDHYA.JS | b534715 | 2024-10-15 11:41:45 +0530 | [diff] [blame^] | 106 | this.notifierService = this.injector.get(NotifierService); |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 107 | this.sharedService = this.injector.get(SharedService); |
| 108 | this.translateService = this.injector.get(TranslateService); |
| SANDHYA.JS | b534715 | 2024-10-15 11:41:45 +0530 | [diff] [blame^] | 109 | this.restService = this.injector.get(RestService); |
| 110 | this.cd = this.injector.get(ChangeDetectorRef); |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Lifecyle Hooks the trigger before component is instantiate |
| 115 | */ |
| 116 | public ngOnInit(): void { |
| SANDHYA.JS | b534715 | 2024-10-15 11:41:45 +0530 | [diff] [blame^] | 117 | this.headers = new HttpHeaders({ |
| 118 | Accept: 'application/zip, application/json', |
| 119 | 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0' |
| 120 | }); |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 121 | this.instanceID = this.value.identifier; |
| 122 | this.getK8sType = this.value.pageType; |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 123 | this.state = this.value.state; |
| 124 | if (sessionStorage.getItem('clusterType') === 'Registered') { |
| 125 | this.checkRegister = true; |
| 126 | } |
| 127 | if (this.getK8sType === 'infra-config' || this.getK8sType === 'infra-controller' || this.getK8sType === 'app-profile' || this.getK8sType === 'resource-profile') { |
| 128 | this.isCluster = false; |
| 129 | this.isProfile = true; |
| 130 | this.isKSU = false; |
| 131 | } else if (sessionStorage.getItem('clusterType') === 'Managed') { |
| 132 | this.isCluster = true; |
| 133 | this.isProfile = false; |
| 134 | this.isKSU = false; |
| 135 | } |
| 136 | if (this.getK8sType === 'k8-ksu') { |
| 137 | this.isKSU = true; |
| 138 | this.isCluster = false; |
| 139 | this.isProfile = false; |
| 140 | } |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /** Delete User Account @public */ |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 144 | public deleteK8s(): void { |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame] | 145 | // eslint-disable-next-line security/detect-non-literal-fs-filename |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 146 | const modalRef: NgbModalRef = this.modalService.open(DeleteComponent, { backdrop: 'static' }); |
| 147 | modalRef.result.then((result: MODALCLOSERESPONSEDATA) => { |
| 148 | if (result) { |
| 149 | this.sharedService.callData(); |
| 150 | } |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame] | 151 | }).catch((): void => { |
| 152 | // Catch Navigation Error |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 153 | }); |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /** Shows information using modalservice @public */ |
| 157 | public infoK8s(pageType: string): void { |
| 158 | let pageName: string = ''; |
| 159 | let title: string = ''; |
| 160 | if (pageType === 'repo') { |
| 161 | pageName = 'k8s-repo'; |
| 162 | title = 'PAGE.K8S.K8SREPODETAILS'; |
| 163 | } else { |
| 164 | pageName = 'k8s-cluster'; |
| 165 | title = 'PAGE.K8S.K8SCLUSTERDETAILS'; |
| 166 | } |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame] | 167 | // eslint-disable-next-line security/detect-non-literal-fs-filename |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 168 | this.modalService.open(ShowInfoComponent, { backdrop: 'static' }).componentInstance.params = { |
| 169 | id: this.instanceID, |
| 170 | page: pageName, |
| 171 | titleName: title |
| 172 | }; |
| 173 | } |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 174 | /** Edit profile @public */ |
| 175 | public editProfile(editType: string): void { |
| 176 | // eslint-disable-next-line security/detect-non-literal-fs-filename |
| 177 | const modalRef: NgbModalRef = this.modalService.open(K8sInfraConfigAddComponent, { backdrop: 'static' }); |
| 178 | modalRef.componentInstance.profileID = this.value.identifier; |
| 179 | modalRef.componentInstance.profileType = editType; |
| 180 | modalRef.componentInstance.profileName = this.value.name; |
| 181 | modalRef.componentInstance.profileDescription = this.value.description; |
| 182 | modalRef.result.then((result: MODALCLOSERESPONSEDATA) => { |
| 183 | if (result) { |
| 184 | this.sharedService.callData(); |
| 185 | } |
| 186 | }).catch((): void => { |
| 187 | // Catch Navigation Error |
| 188 | }); |
| 189 | } |
| 190 | |
| 191 | /** Edit cluster @public */ |
| 192 | public editCluster(editType: string): void { |
| 193 | // eslint-disable-next-line security/detect-non-literal-fs-filename |
| 194 | const modalRef: NgbModalRef = this.modalService.open(K8sAddClusterComponent, { backdrop: 'static' }); |
| 195 | modalRef.componentInstance.profileID = this.value.identifier; |
| 196 | modalRef.componentInstance.profileType = editType; |
| 197 | modalRef.result.then((result: MODALCLOSERESPONSEDATA) => { |
| 198 | if (result) { |
| 199 | this.sharedService.callData(); |
| 200 | } |
| 201 | }).catch((): void => { |
| 202 | // Catch Navigation Error |
| 203 | }); |
| 204 | } |
| 205 | |
| 206 | |
| 207 | /** Edit profile under cluster @public */ |
| 208 | public editClusterProfile(editType: string): void { |
| 209 | // eslint-disable-next-line security/detect-non-literal-fs-filename |
| 210 | const modalRef: NgbModalRef = this.modalService.open(K8sAttachProfileComponent, { backdrop: 'static' }); |
| 211 | modalRef.componentInstance.profileID = this.value.identifier; |
| 212 | modalRef.componentInstance.profileType = editType; |
| 213 | modalRef.result.then((result: MODALCLOSERESPONSEDATA) => { |
| 214 | if (result) { |
| 215 | this.sharedService.callData(); |
| 216 | } |
| 217 | }).catch((): void => { |
| 218 | // Catch Navigation Error |
| 219 | }); |
| 220 | } |
| 221 | |
| 222 | /** Move KSU @public */ |
| 223 | public moveKsu(): void { |
| 224 | // eslint-disable-next-line security/detect-non-literal-fs-filename |
| 225 | const modalRef: NgbModalRef = this.modalService.open(KSUAddComponent, { backdrop: 'static' }); |
| 226 | modalRef.componentInstance.profileID = this.value.identifier; |
| 227 | modalRef.componentInstance.profileType = 'move'; |
| 228 | modalRef.result.then((result: MODALCLOSERESPONSEDATA) => { |
| 229 | if (result) { |
| 230 | this.sharedService.callData(); |
| 231 | } |
| 232 | }).catch((): void => { |
| 233 | // Catch Navigation Error |
| 234 | }); |
| 235 | } |
| 236 | |
| 237 | /** Edit KSU @public */ |
| 238 | public editKsu(): void { |
| 239 | // eslint-disable-next-line security/detect-non-literal-fs-filename |
| 240 | const modalRef: NgbModalRef = this.modalService.open(KSUAddComponent, { backdrop: 'static' }); |
| 241 | modalRef.componentInstance.profileID = this.value.identifier; |
| 242 | modalRef.componentInstance.profileType = 'edit'; |
| 243 | modalRef.result.then((result: MODALCLOSERESPONSEDATA) => { |
| 244 | if (result) { |
| 245 | this.sharedService.callData(); |
| 246 | } |
| 247 | }).catch((): void => { |
| 248 | // Catch Navigation Error |
| 249 | }); |
| 250 | } |
| 251 | |
| SANDHYA.JS | b534715 | 2024-10-15 11:41:45 +0530 | [diff] [blame^] | 252 | /** Download credentials @public */ |
| 253 | public getCredentials(): void { |
| 254 | this.isLoadingDownloadResult = true; |
| 255 | const httpOptions: GETAPIURLHEADER = { |
| 256 | headers: this.headers, |
| 257 | responseType: 'blob' |
| 258 | }; |
| 259 | this.restService.getResource(environment.K8SCREATECLUSTER_URL + '/' + this.instanceID + '/get_creds') |
| 260 | .subscribe((res: { op_id: string }) => { |
| 261 | if (!isNullOrUndefined(res.op_id)) { |
| 262 | this.restService.getResource(environment.K8SCREATECLUSTER_URL + '/' + this.instanceID + '/get_creds_file' + '/' + res.op_id, httpOptions) |
| 263 | .subscribe((response: Blob) => { |
| 264 | this.isLoadingDownloadResult = true; |
| 265 | if (!isNullOrUndefined(response)) { |
| 266 | this.isLoadingDownloadResult = false; |
| 267 | const binaryData: Blob[] = []; |
| 268 | binaryData.push(response); |
| 269 | this.sharedService.downloadFiles(this.value.name, binaryData, response.type); |
| 270 | this.isLoadingDownloadResult = false; |
| 271 | this.changeDetactionforDownload(); |
| 272 | } |
| 273 | }, (error: ERRORDATA) => { |
| 274 | this.isLoadingDownloadResult = false; |
| 275 | this.notifierService.notify('error', this.translateService.instant('ERROR')); |
| 276 | this.changeDetactionforDownload(); |
| 277 | if (typeof error.error === 'object') { |
| 278 | error.error.text().then((data: string): void => { |
| 279 | error.error = JSON.parse(data); |
| 280 | this.restService.handleError(error, 'getBlob'); |
| 281 | }); |
| 282 | } |
| 283 | }); |
| 284 | } |
| 285 | }, (error: ERRORDATA) => { |
| 286 | this.isLoadingDownloadResult = false; |
| 287 | this.restService.handleError(error, 'get'); |
| 288 | }); |
| 289 | } |
| 290 | |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 291 | /** Clone KSU @public */ |
| 292 | public cloneKsu(): void { |
| 293 | // eslint-disable-next-line security/detect-non-literal-fs-filename |
| 294 | const modalRef: NgbModalRef = this.modalService.open(KSUAddComponent, { backdrop: 'static' }); |
| 295 | modalRef.componentInstance.profileID = this.value.identifier; |
| 296 | modalRef.componentInstance.profileType = 'clone'; |
| 297 | modalRef.result.then((result: MODALCLOSERESPONSEDATA) => { |
| 298 | if (result) { |
| 299 | this.sharedService.callData(); |
| 300 | } |
| 301 | }).catch((): void => { |
| 302 | // Catch Navigation Error |
| 303 | }); |
| 304 | } |
| SANDHYA.JS | b534715 | 2024-10-15 11:41:45 +0530 | [diff] [blame^] | 305 | |
| 306 | /** Change the detaction @public */ |
| 307 | public changeDetactionforDownload(): void { |
| 308 | setTimeout(() => { |
| 309 | this.cd.detectChanges(); |
| 310 | }, this.timeOut); |
| 311 | } |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 312 | } |