blob: c50831c8ca0eb12a833847991514488e738aa10a [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 k8sclustercomponent.ts.
20 */
21import { Component, Injector, OnDestroy, OnInit } from '@angular/core';
22import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
23import { TranslateService } from '@ngx-translate/core';
24import { CONFIGCONSTANT, ERRORDATA, MODALCLOSERESPONSEDATA } from 'CommonModel';
25import { DataService } from 'DataService';
26import { environment } from 'environment';
27import { K8sActionComponent } from 'K8sActionComponent';
28import { K8sAddClusterComponent } from 'K8sAddClusterComponent';
SANDHYA.JS26570112024-07-05 21:35:46 +053029import { K8SCLUSTERDATA, K8SCLUSTERDATADISPLAY, K8SCREATEDATADISPLAY } from 'K8sModel';
kumaran.m3b4814a2020-05-01 19:48:54 +053030import { LocalDataSource } from 'ng2-smart-table';
31import { RestService } from 'RestService';
SANDHYA.JSb772de02024-12-10 15:21:03 +053032import { forkJoin, Observable, Subscription } from 'rxjs';
33import { map } from 'rxjs/operators';
34import { isNullOrUndefined, SharedService } from 'SharedService';
kumaran.m3b4814a2020-05-01 19:48:54 +053035/**
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 */
45export 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.JS26570112024-07-05 21:35:46 +053070 /** operational State created data @public */
kumaran.m3b4814a2020-05-01 19:48:54 +053071 public operationalStateFirstStep: string = CONFIGCONSTANT.k8OperationalStateFirstStep;
72
SANDHYA.JS26570112024-07-05 21:35:46 +053073 /** operational State in creation data @public */
kumaran.m3b4814a2020-05-01 19:48:54 +053074 public operationalStateSecondStep: string = CONFIGCONSTANT.k8OperationalStateStateSecondStep;
75
SANDHYA.JS26570112024-07-05 21:35:46 +053076 /** operational State in deletion data @public */
kumaran.m3b4814a2020-05-01 19:48:54 +053077 public operationalStateThirdStep: string = CONFIGCONSTANT.k8OperationalStateThirdStep;
78
SANDHYA.JS92379ec2025-06-13 17:29:35 +053079 /** operational State failed creation data @public */
SANDHYA.JS26570112024-07-05 21:35:46 +053080 public operationalStateFourthStep: string = CONFIGCONSTANT.k8OperationalStateFourthStep;
81
82 /** operational State failed creation data @public */
83 public operationalStateFifthStep: string = CONFIGCONSTANT.k8OperationalStateFifthStep;
84
SANDHYA.JSb772de02024-12-10 15:21:03 +053085 /** 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.JS26570112024-07-05 21:35:46 +053094 /** cluster Type @public */
95 public isCluster: string = 'Registered';
96
97 /** cluster @public */
98 public clusterUrl: string;
99
kumaran.m3b4814a2020-05-01 19:48:54 +0530100 /** 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.JS26570112024-07-05 21:35:46 +0530132 sessionStorage.setItem('clusterType', this.isCluster);
kumaran.m3b4814a2020-05-01 19:48:54 +0530133 }
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.JSb772de02024-12-10 15:21:03 +0530141 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.JS26570112024-07-05 21:35:46 +0530167 state: {
SANDHYA.JS92379ec2025-06-13 17:29:35 +0530168 title: this.translateService.instant('STATE'), width: '15%', type: 'html',
kumaran.m3b4814a2020-05-01 19:48:54 +0530169 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.JS26570112024-07-05 21:35:46 +0530176 { value: this.operationalStateThirdStep, title: this.operationalStateThirdStep },
SANDHYA.JSb772de02024-12-10 15:21:03 +0530177 { value: this.operationalStateFourthStep, title: this.operationalStateFourthStep },
178 { value: this.operationalStateFifthStep, title: this.operationalStateFifthStep },
SANDHYA.JS92379ec2025-06-13 17:29:35 +0530179 { value: this.operationalStateSixthStep, title: this.operationalStateSixthStep }
kumaran.m3b4814a2020-05-01 19:48:54 +0530180 ]
181 }
182 },
183 valuePrepareFunction: (cell: K8SCLUSTERDATADISPLAY, row: K8SCLUSTERDATADISPLAY): string => {
SANDHYA.JS26570112024-07-05 21:35:46 +0530184 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}">
SANDHYA.JS92379ec2025-06-13 17:29:35 +0530190 <i class="fas fa-times-circle text-danger"></i>
SANDHYA.JS26570112024-07-05 21:35:46 +0530191 </span>`;
192 } else if (row.state === this.operationalStateFourthStep) {
193 return `<span class="icon-label" title="${row.state}">
SANDHYA.JS92379ec2025-06-13 17:29:35 +0530194 <i class="fas fa-ban text-danger"></i>
SANDHYA.JS26570112024-07-05 21:35:46 +0530195 </span>`;
196 } else if (row.state === this.operationalStateFifthStep) {
197 return `<span class="icon-label" title="${row.state}">
SANDHYA.JS92379ec2025-06-13 17:29:35 +0530198 <i class="fas fa-check-circle text-success"></i>
SANDHYA.JS26570112024-07-05 21:35:46 +0530199 </span>`;
SANDHYA.JSb772de02024-12-10 15:21:03 +0530200 } else if (row.state === this.operationalStateSixthStep) {
201 return `<span class="icon-label" title="${row.state}">
SANDHYA.JS92379ec2025-06-13 17:29:35 +0530202 <i class="fas fa-arrow-circle-down text-warning"></i>
SANDHYA.JSb772de02024-12-10 15:21:03 +0530203 </span>`;
kumaran.m3b4814a2020-05-01 19:48:54 +0530204 } else {
SANDHYA.JS92379ec2025-06-13 17:29:35 +0530205 return `<span class="icon-label" title="${row.state}">
206 <i class="fas fa-spinner text-warning"></i>
207 </span>`;
kumaran.m3b4814a2020-05-01 19:48:54 +0530208 }
209 }
210 },
kumaran.m3b4814a2020-05-01 19:48:54 +0530211 Actions: {
212 name: 'Action', width: '5%', filter: false, sort: false, title: this.translateService.instant('ACTIONS'), type: 'custom',
213 valuePrepareFunction: (cell: K8SCLUSTERDATADISPLAY, row: K8SCLUSTERDATADISPLAY): K8SCLUSTERDATADISPLAY => row,
214 renderComponent: K8sActionComponent
215 }
216 };
217 }
218
219 /** smart table Data Settings @public */
220 public generateSettings(): void {
221 this.settings = {
222 columns: this.columnList,
223 actions: { add: false, edit: false, delete: false, position: 'right' },
224 attr: this.sharedService.tableClassConfig(),
225 pager: this.sharedService.paginationPagerConfig(),
226 noDataMessage: this.translateService.instant('NODATAMSG')
227 };
228 }
229
230 /** smart table listing manipulation @public */
231 public onChange(perPageValue: number): void {
232 this.dataSource.setPaging(1, perPageValue, true);
233 }
234
235 /** smart table listing manipulation @public */
236 public onUserRowSelect(event: MessageEvent): void {
237 Object.assign(event.data, { page: 'k8-cluster' });
238 this.dataService.changeMessage(event.data);
239 }
240
241 /** Compose new K8s Cluster Accounts @public */
SANDHYA.JSb772de02024-12-10 15:21:03 +0530242 public addK8sCluster(type?: string): void {
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +0530243 // eslint-disable-next-line security/detect-non-literal-fs-filename
kumaran.m3b4814a2020-05-01 19:48:54 +0530244 const modalRef: NgbModalRef = this.modalService.open(K8sAddClusterComponent, { backdrop: 'static' });
SANDHYA.JS26570112024-07-05 21:35:46 +0530245
SANDHYA.JSb772de02024-12-10 15:21:03 +0530246 if (type === 'Register') {
SANDHYA.JS26570112024-07-05 21:35:46 +0530247 modalRef.componentInstance.profileType = 'Register';
SANDHYA.JS92379ec2025-06-13 17:29:35 +0530248 } else if (type === 'Manage') {
SANDHYA.JS26570112024-07-05 21:35:46 +0530249 modalRef.componentInstance.profileType = 'Manage';
SANDHYA.JS92379ec2025-06-13 17:29:35 +0530250 } else {
251 modalRef.componentInstance.profileType = 'Control';
SANDHYA.JS26570112024-07-05 21:35:46 +0530252 }
kumaran.m3b4814a2020-05-01 19:48:54 +0530253 modalRef.result.then((result: MODALCLOSERESPONSEDATA) => {
254 if (result) {
255 this.sharedService.callData();
256 }
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +0530257 }).catch((): void => {
258 // Catch Navigation Error
SANDHYA.JS26570112024-07-05 21:35:46 +0530259 });
kumaran.m3b4814a2020-05-01 19:48:54 +0530260 }
261
262 /**
263 * Lifecyle hook which get trigger on component destruction
264 */
265 public ngOnDestroy(): void {
266 this.generateDataSub.unsubscribe();
267 }
268
269 /** Generate nsData object from loop and return for the datasource @public */
270 public generateK8sclusterData(k8sClusterdata: K8SCLUSTERDATA): K8SCLUSTERDATADISPLAY {
271 return {
272 name: k8sClusterdata.name,
SANDHYA.JS92379ec2025-06-13 17:29:35 +0530273 state: !isNullOrUndefined(k8sClusterdata.resourceState) ? k8sClusterdata.resourceState : 'N/A',
kumaran.m3b4814a2020-05-01 19:48:54 +0530274 identifier: k8sClusterdata._id,
kumaran.m3b4814a2020-05-01 19:48:54 +0530275 version: k8sClusterdata.k8s_version,
276 created: this.sharedService.convertEpochTime(Number(k8sClusterdata._admin.created)),
277 modified: this.sharedService.convertEpochTime(Number(k8sClusterdata._admin.modified)),
SANDHYA.JSb772de02024-12-10 15:21:03 +0530278 pageType: 'cluster',
279 createdbyosm: !isNullOrUndefined(k8sClusterdata.created) ? (k8sClusterdata.created) : 'false',
280 bootstrap: !isNullOrUndefined(k8sClusterdata.bootstrap) ? (k8sClusterdata.bootstrap) : false,
281 key: (k8sClusterdata.key === 'registered') ? true : false,
SANDHYA.JS92379ec2025-06-13 17:29:35 +0530282 clusterMode: (k8sClusterdata.created === 'true') ? 'managed' : 'registered'
kumaran.m3b4814a2020-05-01 19:48:54 +0530283 };
284 }
285
286 /** Fetching the data from server to Load in the smarttable @protected */
287 protected generateData(): void {
288 this.isLoadingResults = true;
SANDHYA.JSa3ff32a2025-02-13 16:24:46 +0530289 this.restService.getResource(environment.K8SCREATECLUSTER_URL).subscribe((k8sClusterDatas: K8SCLUSTERDATA[]) => {
290 if (!isNullOrUndefined(k8sClusterDatas)) {
291 this.k8sClusterData = [];
292 k8sClusterDatas.forEach((clusterData: K8SCLUSTERDATA) => {
293 const k8sClusterDataObj: K8SCLUSTERDATADISPLAY = this.generateK8sclusterData(clusterData);
294 this.k8sClusterData.push(k8sClusterDataObj);
295 });
296 if (this.k8sClusterData.length > 0) {
297 this.checkDataClass = 'dataTables_present';
298 } else {
299 this.checkDataClass = 'dataTables_empty';
SANDHYA.JSb772de02024-12-10 15:21:03 +0530300 }
SANDHYA.JSa3ff32a2025-02-13 16:24:46 +0530301 this.dataSource.load(this.k8sClusterData).then((data: boolean) => {
302 this.isLoadingResults = false;
303 }).catch(() => {
304 this.isLoadingResults = false;
SANDHYA.JSb772de02024-12-10 15:21:03 +0530305 });
kumaran.m3b4814a2020-05-01 19:48:54 +0530306 }
SANDHYA.JSa3ff32a2025-02-13 16:24:46 +0530307 }, (error: ERRORDATA) => {
308 this.restService.handleError(error, 'get');
309 this.isLoadingResults = false;
kumaran.m3b4814a2020-05-01 19:48:54 +0530310 });
311 }
kumaran.m3b4814a2020-05-01 19:48:54 +0530312}