blob: ee98ed533794de20f1e5ba596290b156c15eddb3 [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';
32import { Subscription } from 'rxjs';
33import { SharedService } from 'SharedService';
34/**
35 * Creating Component
36 * @Component takes K8sClusterComponent.html as template url
37 */
38@Component({
39 selector: 'app-k8scluster',
40 templateUrl: './K8sClusterComponent.html',
41 styleUrls: ['./K8sClusterComponent.scss']
42})
43/** Exporting a class @exports K8sClusterComponent */
44export class K8sClusterComponent implements OnInit, OnDestroy {
45 /** To inject services @public */
46 public injector: Injector;
47
48 /** handle translate @public */
49 public translateService: TranslateService;
50
51 /** Data of smarttable populate through LocalDataSource @public */
52 public dataSource: LocalDataSource = new LocalDataSource();
53
54 /** Columns list of the smart table @public */
55 public columnList: object = {};
56
57 /** Settings for smarttable to populate the table with columns @public */
58 public settings: object = {};
59
60 /** Check the loading results @public */
61 public isLoadingResults: boolean = true;
62
63 /** Give the message for the loading @public */
64 public message: string = 'PLEASEWAIT';
65
66 /** Class for empty and present data @public */
67 public checkDataClass: string;
68
SANDHYA.JS26570112024-07-05 21:35:46 +053069 /** operational State created data @public */
kumaran.m3b4814a2020-05-01 19:48:54 +053070 public operationalStateFirstStep: string = CONFIGCONSTANT.k8OperationalStateFirstStep;
71
SANDHYA.JS26570112024-07-05 21:35:46 +053072 /** operational State in creation data @public */
kumaran.m3b4814a2020-05-01 19:48:54 +053073 public operationalStateSecondStep: string = CONFIGCONSTANT.k8OperationalStateStateSecondStep;
74
SANDHYA.JS26570112024-07-05 21:35:46 +053075 /** operational State in deletion data @public */
kumaran.m3b4814a2020-05-01 19:48:54 +053076 public operationalStateThirdStep: string = CONFIGCONSTANT.k8OperationalStateThirdStep;
77
SANDHYA.JS26570112024-07-05 21:35:46 +053078 /** operational State failed deletion data @public */
79 public operationalStateFourthStep: string = CONFIGCONSTANT.k8OperationalStateFourthStep;
80
81 /** operational State failed creation data @public */
82 public operationalStateFifthStep: string = CONFIGCONSTANT.k8OperationalStateFifthStep;
83
84 /** cluster Type @public */
85 public isCluster: string = 'Registered';
86
87 /** cluster @public */
88 public clusterUrl: string;
89
kumaran.m3b4814a2020-05-01 19:48:54 +053090 /** Instance of the rest service @private */
91 private restService: RestService;
92
93 /** dataService to pass the data from one component to another @private */
94 private dataService: DataService;
95
96 /** Formation of appropriate Data for LocalDatasource @private */
97 private k8sClusterData: {}[] = [];
98
99 /** Contains all methods related to shared @private */
100 private sharedService: SharedService;
101
102 /** Instance of the modal service @private */
103 private modalService: NgbModal;
104
105 /** Instance of subscriptions @private */
106 private generateDataSub: Subscription;
107
108 constructor(injector: Injector) {
109 this.injector = injector;
110 this.restService = this.injector.get(RestService);
111 this.dataService = this.injector.get(DataService);
112 this.sharedService = this.injector.get(SharedService);
113 this.translateService = this.injector.get(TranslateService);
114 this.modalService = this.injector.get(NgbModal);
115 }
116 /** Lifecyle Hooks the trigger before component is instantiate @public */
117 public ngOnInit(): void {
118 this.generateColumns();
119 this.generateSettings();
120 this.generateData();
121 this.generateDataSub = this.sharedService.dataEvent.subscribe(() => { this.generateData(); });
SANDHYA.JS26570112024-07-05 21:35:46 +0530122 sessionStorage.setItem('clusterType', this.isCluster);
kumaran.m3b4814a2020-05-01 19:48:54 +0530123 }
124
125 /** smart table Header Colums @public */
126 public generateColumns(): void {
127 this.columnList = {
128 name: { title: this.translateService.instant('NAME'), width: '20%', sortDirection: 'asc' },
129 identifier: { title: this.translateService.instant('IDENTIFIER'), width: '20%' },
130 version: { title: this.translateService.instant('K8VERSION'), width: '10%' },
SANDHYA.JS26570112024-07-05 21:35:46 +0530131 state: {
132 title: this.translateService.instant('STATE'), width: '15%', type: 'html',
kumaran.m3b4814a2020-05-01 19:48:54 +0530133 filter: {
134 type: 'list',
135 config: {
136 selectText: 'Select',
137 list: [
138 { value: this.operationalStateFirstStep, title: this.operationalStateFirstStep },
139 { value: this.operationalStateSecondStep, title: this.operationalStateSecondStep },
SANDHYA.JS26570112024-07-05 21:35:46 +0530140 { value: this.operationalStateThirdStep, title: this.operationalStateThirdStep },
141 { value: this.operationalStateThirdStep, title: this.operationalStateFourthStep },
142 { value: this.operationalStateThirdStep, title: this.operationalStateFifthStep }
kumaran.m3b4814a2020-05-01 19:48:54 +0530143 ]
144 }
145 },
146 valuePrepareFunction: (cell: K8SCLUSTERDATADISPLAY, row: K8SCLUSTERDATADISPLAY): string => {
SANDHYA.JS26570112024-07-05 21:35:46 +0530147 if (row.state === this.operationalStateFirstStep) {
148 return `<span class="icon-label" title="${row.state}">
149 <i class="fas fa-clock text-success"></i>
150 </span>`;
151 } else if (row.state === this.operationalStateSecondStep) {
152 return `<span class="icon-label" title="${row.state}">
153 <i class="fas fa-spinner text-warning"></i>
154 </span>`;
155 } else if (row.state === this.operationalStateThirdStep) {
156 return `<span class="icon-label" title="${row.state}">
157 <i class="fas fa-spinner text-danger"></i>
158 </span>`;
159 } else if (row.state === this.operationalStateFourthStep) {
160 return `<span class="icon-label" title="${row.state}">
161 <i class="fas fa-times-circle text-danger"></i>
162 </span>`;
163 } else if (row.state === this.operationalStateFifthStep) {
164 return `<span class="icon-label" title="${row.state}">
165 <i class="fas fa-times-circle text-warning"></i>
166 </span>`;
kumaran.m3b4814a2020-05-01 19:48:54 +0530167 } else {
SANDHYA.JS26570112024-07-05 21:35:46 +0530168 return `<span>${row.state}</span>`;
kumaran.m3b4814a2020-05-01 19:48:54 +0530169 }
170 }
171 },
172 created: { title: this.translateService.instant('CREATED'), width: '15%' },
173 modified: { title: this.translateService.instant('MODIFIED'), width: '15%' },
174 Actions: {
175 name: 'Action', width: '5%', filter: false, sort: false, title: this.translateService.instant('ACTIONS'), type: 'custom',
176 valuePrepareFunction: (cell: K8SCLUSTERDATADISPLAY, row: K8SCLUSTERDATADISPLAY): K8SCLUSTERDATADISPLAY => row,
177 renderComponent: K8sActionComponent
178 }
179 };
180 }
181
182 /** smart table Data Settings @public */
183 public generateSettings(): void {
184 this.settings = {
185 columns: this.columnList,
186 actions: { add: false, edit: false, delete: false, position: 'right' },
187 attr: this.sharedService.tableClassConfig(),
188 pager: this.sharedService.paginationPagerConfig(),
189 noDataMessage: this.translateService.instant('NODATAMSG')
190 };
191 }
192
193 /** smart table listing manipulation @public */
194 public onChange(perPageValue: number): void {
195 this.dataSource.setPaging(1, perPageValue, true);
196 }
197
198 /** smart table listing manipulation @public */
199 public onUserRowSelect(event: MessageEvent): void {
200 Object.assign(event.data, { page: 'k8-cluster' });
201 this.dataService.changeMessage(event.data);
202 }
203
204 /** Compose new K8s Cluster Accounts @public */
205 public addK8sCluster(): void {
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +0530206 // eslint-disable-next-line security/detect-non-literal-fs-filename
kumaran.m3b4814a2020-05-01 19:48:54 +0530207 const modalRef: NgbModalRef = this.modalService.open(K8sAddClusterComponent, { backdrop: 'static' });
SANDHYA.JS26570112024-07-05 21:35:46 +0530208
209 if (this.isCluster === 'Registered') {
210 modalRef.componentInstance.profileType = 'Register';
211 } else {
212 modalRef.componentInstance.profileType = 'Manage';
213 }
kumaran.m3b4814a2020-05-01 19:48:54 +0530214 modalRef.result.then((result: MODALCLOSERESPONSEDATA) => {
215 if (result) {
216 this.sharedService.callData();
217 }
SANDHYA.JS0a34dfa2023-04-25 23:59:41 +0530218 }).catch((): void => {
219 // Catch Navigation Error
SANDHYA.JS26570112024-07-05 21:35:46 +0530220 });
kumaran.m3b4814a2020-05-01 19:48:54 +0530221 }
222
223 /**
224 * Lifecyle hook which get trigger on component destruction
225 */
226 public ngOnDestroy(): void {
227 this.generateDataSub.unsubscribe();
228 }
229
230 /** Generate nsData object from loop and return for the datasource @public */
231 public generateK8sclusterData(k8sClusterdata: K8SCLUSTERDATA): K8SCLUSTERDATADISPLAY {
232 return {
233 name: k8sClusterdata.name,
SANDHYA.JS26570112024-07-05 21:35:46 +0530234 state: k8sClusterdata.state,
kumaran.m3b4814a2020-05-01 19:48:54 +0530235 identifier: k8sClusterdata._id,
236 operationalState: k8sClusterdata._admin.operationalState,
237 version: k8sClusterdata.k8s_version,
238 created: this.sharedService.convertEpochTime(Number(k8sClusterdata._admin.created)),
239 modified: this.sharedService.convertEpochTime(Number(k8sClusterdata._admin.modified)),
240 pageType: 'cluster'
241 };
242 }
243
SANDHYA.JS26570112024-07-05 21:35:46 +0530244 /** Change event @public */
245 public onChangeEvent(value: string): void {
246 if (value === 'Managed') {
247 this.isCluster = 'Managed';
248 } else {
249 this.isCluster = 'Registered';
250 }
251 sessionStorage.setItem('clusterType', value);
252 this.generateColumns();
253 this.generateSettings();
254 this.generateData();
255 }
256
kumaran.m3b4814a2020-05-01 19:48:54 +0530257 /** Fetching the data from server to Load in the smarttable @protected */
258 protected generateData(): void {
259 this.isLoadingResults = true;
SANDHYA.JS26570112024-07-05 21:35:46 +0530260 if (this.isCluster === 'Registered') {
261 this.clusterUrl = environment.K8SCREATECLUSTER_URL + '?created=false';
262 } else {
263 this.clusterUrl = environment.K8SCREATECLUSTER_URL + '?created=true';
264 }
265 this.restService.getResource(this.clusterUrl).subscribe((k8sClusterDatas: K8SCLUSTERDATA[]) => {
kumaran.m3b4814a2020-05-01 19:48:54 +0530266 this.k8sClusterData = [];
267 k8sClusterDatas.forEach((k8sClusterdata: K8SCLUSTERDATA) => {
268 const k8sClusterDataObj: K8SCLUSTERDATADISPLAY = this.generateK8sclusterData(k8sClusterdata);
269 this.k8sClusterData.push(k8sClusterDataObj);
270 });
271 if (this.k8sClusterData.length > 0) {
272 this.checkDataClass = 'dataTables_present';
273 } else {
274 this.checkDataClass = 'dataTables_empty';
275 }
276 this.dataSource.load(this.k8sClusterData).then((data: boolean) => {
277 this.isLoadingResults = false;
278 }).catch(() => {
279 this.isLoadingResults = false;
280 });
281 }, (error: ERRORDATA) => {
282 this.restService.handleError(error, 'get');
283 this.isLoadingResults = false;
284 });
285 }
kumaran.m3b4814a2020-05-01 19:48:54 +0530286}