blob: 95c8e8816e6b1b38864c0e55c35c199a6b587ad2 [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.JS26570112024-07-05 21:35:46 +053079 /** 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.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: {
168 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 },
179 { value: this.operationalStateSixthStep, title: this.operationalStateFifthStep }
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}">
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.JSb772de02024-12-10 15:21:03 +0530204 } 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.m3b4814a2020-05-01 19:48:54 +0530208 } else {
SANDHYA.JS26570112024-07-05 21:35:46 +0530209 return `<span>${row.state}</span>`;
kumaran.m3b4814a2020-05-01 19:48:54 +0530210 }
211 }
212 },
213 created: { title: this.translateService.instant('CREATED'), width: '15%' },
kumaran.m3b4814a2020-05-01 19:48:54 +0530214 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.JSb772de02024-12-10 15:21:03 +0530245 public addK8sCluster(type?: string): void {
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +0530246 // eslint-disable-next-line security/detect-non-literal-fs-filename
kumaran.m3b4814a2020-05-01 19:48:54 +0530247 const modalRef: NgbModalRef = this.modalService.open(K8sAddClusterComponent, { backdrop: 'static' });
SANDHYA.JS26570112024-07-05 21:35:46 +0530248
SANDHYA.JSb772de02024-12-10 15:21:03 +0530249 if (type === 'Register') {
SANDHYA.JS26570112024-07-05 21:35:46 +0530250 modalRef.componentInstance.profileType = 'Register';
251 } else {
252 modalRef.componentInstance.profileType = 'Manage';
253 }
kumaran.m3b4814a2020-05-01 19:48:54 +0530254 modalRef.result.then((result: MODALCLOSERESPONSEDATA) => {
255 if (result) {
256 this.sharedService.callData();
257 }
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +0530258 }).catch((): void => {
259 // Catch Navigation Error
SANDHYA.JS26570112024-07-05 21:35:46 +0530260 });
kumaran.m3b4814a2020-05-01 19:48:54 +0530261 }
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.JSb772de02024-12-10 15:21:03 +0530274 state: !isNullOrUndefined(k8sClusterdata.state) ? k8sClusterdata.state : 'N/A',
kumaran.m3b4814a2020-05-01 19:48:54 +0530275 identifier: k8sClusterdata._id,
SANDHYA.JSb772de02024-12-10 15:21:03 +0530276 operationalState: !isNullOrUndefined(k8sClusterdata._admin.operationalState) ? k8sClusterdata._admin.operationalState : 'N/A',
kumaran.m3b4814a2020-05-01 19:48:54 +0530277 version: k8sClusterdata.k8s_version,
278 created: this.sharedService.convertEpochTime(Number(k8sClusterdata._admin.created)),
279 modified: this.sharedService.convertEpochTime(Number(k8sClusterdata._admin.modified)),
SANDHYA.JSb772de02024-12-10 15:21:03 +0530280 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.m3b4814a2020-05-01 19:48:54 +0530285 };
286 }
287
288 /** Fetching the data from server to Load in the smarttable @protected */
289 protected generateData(): void {
290 this.isLoadingResults = true;
SANDHYA.JSb772de02024-12-10 15:21:03 +0530291 const tempURL: Observable<K8SCLUSTERDATA[]>[] = [];
292 const apiUrl1: string = environment.K8SCLUSTER_URL;
293 const apiUrl2: string = environment.K8SCREATECLUSTER_URL;
294 const response1$: Observable<K8SCLUSTERDATA[]> = this.restService.getResource(apiUrl1).pipe(
295 map((data: unknown) =>
296 data as K8SCLUSTERDATA[]
297 )
298 );
299 const response2$: Observable<K8SCLUSTERDATA[]> = this.restService.getResource(apiUrl2).pipe(
300 map((data: unknown) =>
301 data as K8SCLUSTERDATA[]
302 )
303 );
304 tempURL.push(response1$, response2$);
305 forkJoin(tempURL).subscribe(
306 ([response1, response2]) => {
307 if (!isNullOrUndefined(response1) && !isNullOrUndefined(response2)) {
308 const combinedResponse: K8SCLUSTERDATA[] = [...response1, ...response2];
309 const uniqueResponse = this.removeDuplicates(combinedResponse);
310 this.k8sClusterData = [];
311 uniqueResponse.forEach((clusterData: K8SCLUSTERDATA) => {
312 const k8sClusterDataObj: K8SCLUSTERDATADISPLAY = this.generateK8sclusterData(clusterData);
313 this.k8sClusterData.push(k8sClusterDataObj);
314 });
315 if (this.k8sClusterData.length > 0) {
316 this.checkDataClass = 'dataTables_present';
317 } else {
318 this.checkDataClass = 'dataTables_empty';
319 }
320 this.dataSource.load(this.k8sClusterData).then((data: boolean) => {
321 this.isLoadingResults = false;
322 }).catch(() => {
323 this.isLoadingResults = false;
324 });
325 }
326 }, (error: ERRORDATA) => {
327 this.restService.handleError(error, 'get');
328 this.isLoadingResults = false;
kumaran.m3b4814a2020-05-01 19:48:54 +0530329 });
SANDHYA.JSb772de02024-12-10 15:21:03 +0530330 }
331
332 /** Removes duplicates based on 'name' and merges them if necessary */
333 private removeDuplicates(response: K8SCLUSTERDATA[]): K8SCLUSTERDATA[] {
334 const seenIds = new Set();
335 const uniqueResponse: K8SCLUSTERDATA[] = [];
336 response.forEach((obj) => {
337 if (!seenIds.has(obj.name)) {
338 uniqueResponse.push(obj);
339 seenIds.add(obj.name);
kumaran.m3b4814a2020-05-01 19:48:54 +0530340 } else {
SANDHYA.JSb772de02024-12-10 15:21:03 +0530341 const existingObj = uniqueResponse.find((o) => o.name === obj.name);
342 Object.keys(obj).forEach((key) => {
343 // eslint-disable-next-line security/detect-object-injection
344 if (existingObj[key] !== obj[key]) {
345 // eslint-disable-next-line security/detect-object-injection
346 existingObj[key] = obj[key];
347 }
348 });
kumaran.m3b4814a2020-05-01 19:48:54 +0530349 }
kumaran.m3b4814a2020-05-01 19:48:54 +0530350 });
SANDHYA.JSb772de02024-12-10 15:21:03 +0530351 return uniqueResponse;
kumaran.m3b4814a2020-05-01 19:48:54 +0530352 }
kumaran.m3b4814a2020-05-01 19:48:54 +0530353}