| 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 | |
| 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 k8sclustercomponent.ts. |
| 20 | */ |
| 21 | import { Component, Injector, OnDestroy, OnInit } from '@angular/core'; |
| 22 | import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap'; |
| 23 | import { TranslateService } from '@ngx-translate/core'; |
| 24 | import { CONFIGCONSTANT, ERRORDATA, MODALCLOSERESPONSEDATA } from 'CommonModel'; |
| 25 | import { DataService } from 'DataService'; |
| 26 | import { environment } from 'environment'; |
| 27 | import { K8sActionComponent } from 'K8sActionComponent'; |
| 28 | import { K8sAddClusterComponent } from 'K8sAddClusterComponent'; |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 29 | import { K8SCLUSTERDATA, K8SCLUSTERDATADISPLAY, K8SCREATEDATADISPLAY } from 'K8sModel'; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 30 | import { LocalDataSource } from 'ng2-smart-table'; |
| 31 | import { RestService } from 'RestService'; |
| SANDHYA.JS | b772de0 | 2024-12-10 15:21:03 +0530 | [diff] [blame] | 32 | import { forkJoin, Observable, Subscription } from 'rxjs'; |
| 33 | import { map } from 'rxjs/operators'; |
| 34 | import { isNullOrUndefined, SharedService } from 'SharedService'; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 35 | /** |
| 36 | * Creating Component |
| 37 | * @Component takes K8sClusterComponent.html as template url |
| 38 | */ |
| 39 | @Component({ |
| 40 | selector: 'app-k8scluster', |
| 41 | templateUrl: './K8sClusterComponent.html', |
| 42 | styleUrls: ['./K8sClusterComponent.scss'] |
| 43 | }) |
| 44 | /** Exporting a class @exports K8sClusterComponent */ |
| 45 | export class K8sClusterComponent implements OnInit, OnDestroy { |
| 46 | /** To inject services @public */ |
| 47 | public injector: Injector; |
| 48 | |
| 49 | /** handle translate @public */ |
| 50 | public translateService: TranslateService; |
| 51 | |
| 52 | /** Data of smarttable populate through LocalDataSource @public */ |
| 53 | public dataSource: LocalDataSource = new LocalDataSource(); |
| 54 | |
| 55 | /** Columns list of the smart table @public */ |
| 56 | public columnList: object = {}; |
| 57 | |
| 58 | /** Settings for smarttable to populate the table with columns @public */ |
| 59 | public settings: object = {}; |
| 60 | |
| 61 | /** Check the loading results @public */ |
| 62 | public isLoadingResults: boolean = true; |
| 63 | |
| 64 | /** Give the message for the loading @public */ |
| 65 | public message: string = 'PLEASEWAIT'; |
| 66 | |
| 67 | /** Class for empty and present data @public */ |
| 68 | public checkDataClass: string; |
| 69 | |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 70 | /** operational State created data @public */ |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 71 | public operationalStateFirstStep: string = CONFIGCONSTANT.k8OperationalStateFirstStep; |
| 72 | |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 73 | /** operational State in creation data @public */ |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 74 | public operationalStateSecondStep: string = CONFIGCONSTANT.k8OperationalStateStateSecondStep; |
| 75 | |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 76 | /** operational State in deletion data @public */ |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 77 | public operationalStateThirdStep: string = CONFIGCONSTANT.k8OperationalStateThirdStep; |
| 78 | |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 79 | /** operational State failed deletion data @public */ |
| 80 | public operationalStateFourthStep: string = CONFIGCONSTANT.k8OperationalStateFourthStep; |
| 81 | |
| 82 | /** operational State failed creation data @public */ |
| 83 | public operationalStateFifthStep: string = CONFIGCONSTANT.k8OperationalStateFifthStep; |
| 84 | |
| SANDHYA.JS | b772de0 | 2024-12-10 15:21:03 +0530 | [diff] [blame] | 85 | /** operational State failed creation data @public */ |
| 86 | public operationalStateSixthStep: string = CONFIGCONSTANT.k8OperationalStateSixthStep; |
| 87 | |
| 88 | /** Cluster Mode Managed data @public */ |
| 89 | public clusterModeFirstStep: string = CONFIGCONSTANT.clusterModeFirstStep; |
| 90 | |
| 91 | /** Cluster Mode Regsitered data @public */ |
| 92 | public clusterModeSecondStep: string = CONFIGCONSTANT.clusterModeSecondStep; |
| 93 | |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 94 | /** cluster Type @public */ |
| 95 | public isCluster: string = 'Registered'; |
| 96 | |
| 97 | /** cluster @public */ |
| 98 | public clusterUrl: string; |
| 99 | |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 100 | /** Instance of the rest service @private */ |
| 101 | private restService: RestService; |
| 102 | |
| 103 | /** dataService to pass the data from one component to another @private */ |
| 104 | private dataService: DataService; |
| 105 | |
| 106 | /** Formation of appropriate Data for LocalDatasource @private */ |
| 107 | private k8sClusterData: {}[] = []; |
| 108 | |
| 109 | /** Contains all methods related to shared @private */ |
| 110 | private sharedService: SharedService; |
| 111 | |
| 112 | /** Instance of the modal service @private */ |
| 113 | private modalService: NgbModal; |
| 114 | |
| 115 | /** Instance of subscriptions @private */ |
| 116 | private generateDataSub: Subscription; |
| 117 | |
| 118 | constructor(injector: Injector) { |
| 119 | this.injector = injector; |
| 120 | this.restService = this.injector.get(RestService); |
| 121 | this.dataService = this.injector.get(DataService); |
| 122 | this.sharedService = this.injector.get(SharedService); |
| 123 | this.translateService = this.injector.get(TranslateService); |
| 124 | this.modalService = this.injector.get(NgbModal); |
| 125 | } |
| 126 | /** Lifecyle Hooks the trigger before component is instantiate @public */ |
| 127 | public ngOnInit(): void { |
| 128 | this.generateColumns(); |
| 129 | this.generateSettings(); |
| 130 | this.generateData(); |
| 131 | this.generateDataSub = this.sharedService.dataEvent.subscribe(() => { this.generateData(); }); |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 132 | sessionStorage.setItem('clusterType', this.isCluster); |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /** smart table Header Colums @public */ |
| 136 | public generateColumns(): void { |
| 137 | this.columnList = { |
| 138 | name: { title: this.translateService.instant('NAME'), width: '20%', sortDirection: 'asc' }, |
| 139 | identifier: { title: this.translateService.instant('IDENTIFIER'), width: '20%' }, |
| 140 | version: { title: this.translateService.instant('K8VERSION'), width: '10%' }, |
| SANDHYA.JS | b772de0 | 2024-12-10 15:21:03 +0530 | [diff] [blame] | 141 | clusterMode: { |
| 142 | title: this.translateService.instant('Cluster Mode'), width: '15%', type: 'html', |
| 143 | filter: { |
| 144 | type: 'list', |
| 145 | config: { |
| 146 | selectText: 'Select', |
| 147 | list: [ |
| 148 | { value: this.clusterModeFirstStep, title: this.clusterModeFirstStep }, |
| 149 | { value: this.clusterModeSecondStep, title: this.clusterModeSecondStep } |
| 150 | ] |
| 151 | } |
| 152 | }, |
| 153 | valuePrepareFunction: (cell: K8SCLUSTERDATADISPLAY, row: K8SCLUSTERDATADISPLAY): string => { |
| 154 | if (row.clusterMode === this.clusterModeFirstStep) { |
| 155 | return `<span class="icon-label" title="${row.clusterMode}"> |
| 156 | <i class="fas fa-cloud-upload-alt text-info"></i> |
| 157 | </span>`; |
| 158 | } else if (row.clusterMode === this.clusterModeSecondStep) { |
| 159 | return `<span class="icon-label" title="${row.clusterMode}"> |
| 160 | <i class="fas fa-clipboard-check text-info"></i> |
| 161 | </span>`; |
| 162 | } else { |
| 163 | return `<span>${row.clusterMode}</span>`; |
| 164 | } |
| 165 | } |
| 166 | }, |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 167 | state: { |
| SANDHYA.JS | a3ff32a | 2025-02-13 16:24:46 +0530 | [diff] [blame] | 168 | title: this.translateService.instant('GITSTATE'), width: '15%', type: 'html', |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 169 | filter: { |
| 170 | type: 'list', |
| 171 | config: { |
| 172 | selectText: 'Select', |
| 173 | list: [ |
| 174 | { value: this.operationalStateFirstStep, title: this.operationalStateFirstStep }, |
| 175 | { value: this.operationalStateSecondStep, title: this.operationalStateSecondStep }, |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 176 | { value: this.operationalStateThirdStep, title: this.operationalStateThirdStep }, |
| SANDHYA.JS | b772de0 | 2024-12-10 15:21:03 +0530 | [diff] [blame] | 177 | { value: this.operationalStateFourthStep, title: this.operationalStateFourthStep }, |
| 178 | { value: this.operationalStateFifthStep, title: this.operationalStateFifthStep }, |
| 179 | { value: this.operationalStateSixthStep, title: this.operationalStateFifthStep } |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 180 | ] |
| 181 | } |
| 182 | }, |
| 183 | valuePrepareFunction: (cell: K8SCLUSTERDATADISPLAY, row: K8SCLUSTERDATADISPLAY): string => { |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 184 | if (row.state === this.operationalStateFirstStep) { |
| 185 | return `<span class="icon-label" title="${row.state}"> |
| 186 | <i class="fas fa-clock text-success"></i> |
| 187 | </span>`; |
| 188 | } else if (row.state === this.operationalStateSecondStep) { |
| 189 | return `<span class="icon-label" title="${row.state}"> |
| 190 | <i class="fas fa-spinner text-warning"></i> |
| 191 | </span>`; |
| 192 | } else if (row.state === this.operationalStateThirdStep) { |
| 193 | return `<span class="icon-label" title="${row.state}"> |
| 194 | <i class="fas fa-spinner text-danger"></i> |
| 195 | </span>`; |
| 196 | } else if (row.state === this.operationalStateFourthStep) { |
| 197 | return `<span class="icon-label" title="${row.state}"> |
| 198 | <i class="fas fa-times-circle text-danger"></i> |
| 199 | </span>`; |
| 200 | } else if (row.state === this.operationalStateFifthStep) { |
| 201 | return `<span class="icon-label" title="${row.state}"> |
| 202 | <i class="fas fa-times-circle text-warning"></i> |
| 203 | </span>`; |
| SANDHYA.JS | b772de0 | 2024-12-10 15:21:03 +0530 | [diff] [blame] | 204 | } else if (row.state === this.operationalStateSixthStep) { |
| 205 | return `<span class="icon-label" title="${row.state}"> |
| 206 | <i class="fas fa-ban text-danger"></i> |
| 207 | </span>`; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 208 | } else { |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 209 | return `<span>${row.state}</span>`; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | }, |
| 213 | created: { title: this.translateService.instant('CREATED'), width: '15%' }, |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 214 | Actions: { |
| 215 | name: 'Action', width: '5%', filter: false, sort: false, title: this.translateService.instant('ACTIONS'), type: 'custom', |
| 216 | valuePrepareFunction: (cell: K8SCLUSTERDATADISPLAY, row: K8SCLUSTERDATADISPLAY): K8SCLUSTERDATADISPLAY => row, |
| 217 | renderComponent: K8sActionComponent |
| 218 | } |
| 219 | }; |
| 220 | } |
| 221 | |
| 222 | /** smart table Data Settings @public */ |
| 223 | public generateSettings(): void { |
| 224 | this.settings = { |
| 225 | columns: this.columnList, |
| 226 | actions: { add: false, edit: false, delete: false, position: 'right' }, |
| 227 | attr: this.sharedService.tableClassConfig(), |
| 228 | pager: this.sharedService.paginationPagerConfig(), |
| 229 | noDataMessage: this.translateService.instant('NODATAMSG') |
| 230 | }; |
| 231 | } |
| 232 | |
| 233 | /** smart table listing manipulation @public */ |
| 234 | public onChange(perPageValue: number): void { |
| 235 | this.dataSource.setPaging(1, perPageValue, true); |
| 236 | } |
| 237 | |
| 238 | /** smart table listing manipulation @public */ |
| 239 | public onUserRowSelect(event: MessageEvent): void { |
| 240 | Object.assign(event.data, { page: 'k8-cluster' }); |
| 241 | this.dataService.changeMessage(event.data); |
| 242 | } |
| 243 | |
| 244 | /** Compose new K8s Cluster Accounts @public */ |
| SANDHYA.JS | b772de0 | 2024-12-10 15:21:03 +0530 | [diff] [blame] | 245 | public addK8sCluster(type?: string): void { |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame] | 246 | // eslint-disable-next-line security/detect-non-literal-fs-filename |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 247 | const modalRef: NgbModalRef = this.modalService.open(K8sAddClusterComponent, { backdrop: 'static' }); |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 248 | |
| SANDHYA.JS | b772de0 | 2024-12-10 15:21:03 +0530 | [diff] [blame] | 249 | if (type === 'Register') { |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 250 | modalRef.componentInstance.profileType = 'Register'; |
| 251 | } else { |
| 252 | modalRef.componentInstance.profileType = 'Manage'; |
| 253 | } |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 254 | modalRef.result.then((result: MODALCLOSERESPONSEDATA) => { |
| 255 | if (result) { |
| 256 | this.sharedService.callData(); |
| 257 | } |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame] | 258 | }).catch((): void => { |
| 259 | // Catch Navigation Error |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 260 | }); |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Lifecyle hook which get trigger on component destruction |
| 265 | */ |
| 266 | public ngOnDestroy(): void { |
| 267 | this.generateDataSub.unsubscribe(); |
| 268 | } |
| 269 | |
| 270 | /** Generate nsData object from loop and return for the datasource @public */ |
| 271 | public generateK8sclusterData(k8sClusterdata: K8SCLUSTERDATA): K8SCLUSTERDATADISPLAY { |
| 272 | return { |
| 273 | name: k8sClusterdata.name, |
| SANDHYA.JS | b772de0 | 2024-12-10 15:21:03 +0530 | [diff] [blame] | 274 | state: !isNullOrUndefined(k8sClusterdata.state) ? k8sClusterdata.state : 'N/A', |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 275 | identifier: k8sClusterdata._id, |
| SANDHYA.JS | b772de0 | 2024-12-10 15:21:03 +0530 | [diff] [blame] | 276 | operationalState: !isNullOrUndefined(k8sClusterdata._admin.operationalState) ? k8sClusterdata._admin.operationalState : 'N/A', |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 277 | version: k8sClusterdata.k8s_version, |
| 278 | created: this.sharedService.convertEpochTime(Number(k8sClusterdata._admin.created)), |
| 279 | modified: this.sharedService.convertEpochTime(Number(k8sClusterdata._admin.modified)), |
| SANDHYA.JS | b772de0 | 2024-12-10 15:21:03 +0530 | [diff] [blame] | 280 | pageType: 'cluster', |
| 281 | createdbyosm: !isNullOrUndefined(k8sClusterdata.created) ? (k8sClusterdata.created) : 'false', |
| 282 | bootstrap: !isNullOrUndefined(k8sClusterdata.bootstrap) ? (k8sClusterdata.bootstrap) : false, |
| 283 | key: (k8sClusterdata.key === 'registered') ? true : false, |
| 284 | clusterMode: (k8sClusterdata.created === 'true') ? 'MANAGED' : 'REGISTERED' |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 285 | }; |
| 286 | } |
| 287 | |
| 288 | /** Fetching the data from server to Load in the smarttable @protected */ |
| 289 | protected generateData(): void { |
| 290 | this.isLoadingResults = true; |
| SANDHYA.JS | a3ff32a | 2025-02-13 16:24:46 +0530 | [diff] [blame] | 291 | this.restService.getResource(environment.K8SCREATECLUSTER_URL).subscribe((k8sClusterDatas: K8SCLUSTERDATA[]) => { |
| 292 | if (!isNullOrUndefined(k8sClusterDatas)) { |
| 293 | this.k8sClusterData = []; |
| 294 | k8sClusterDatas.forEach((clusterData: K8SCLUSTERDATA) => { |
| 295 | const k8sClusterDataObj: K8SCLUSTERDATADISPLAY = this.generateK8sclusterData(clusterData); |
| 296 | this.k8sClusterData.push(k8sClusterDataObj); |
| 297 | }); |
| 298 | if (this.k8sClusterData.length > 0) { |
| 299 | this.checkDataClass = 'dataTables_present'; |
| 300 | } else { |
| 301 | this.checkDataClass = 'dataTables_empty'; |
| SANDHYA.JS | b772de0 | 2024-12-10 15:21:03 +0530 | [diff] [blame] | 302 | } |
| SANDHYA.JS | a3ff32a | 2025-02-13 16:24:46 +0530 | [diff] [blame] | 303 | this.dataSource.load(this.k8sClusterData).then((data: boolean) => { |
| 304 | this.isLoadingResults = false; |
| 305 | }).catch(() => { |
| 306 | this.isLoadingResults = false; |
| SANDHYA.JS | b772de0 | 2024-12-10 15:21:03 +0530 | [diff] [blame] | 307 | }); |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 308 | } |
| SANDHYA.JS | a3ff32a | 2025-02-13 16:24:46 +0530 | [diff] [blame] | 309 | }, (error: ERRORDATA) => { |
| 310 | this.restService.handleError(error, 'get'); |
| 311 | this.isLoadingResults = false; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 312 | }); |
| 313 | } |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 314 | } |