1 import { Component, TemplateRef, ViewChild } from '@angular/core';
2 import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
3 import { MultiClusterService } from '~/app/shared/api/multi-cluster.service';
4 import { ActionLabelsI18n } from '~/app/shared/constants/app.constants';
5 import { Icons } from '~/app/shared/enum/icons.enum';
6 import { CdTableAction } from '~/app/shared/models/cd-table-action';
7 import { CdTableColumn } from '~/app/shared/models/cd-table-column';
8 import { CdTableSelection } from '~/app/shared/models/cd-table-selection';
9 import { ModalService } from '~/app/shared/services/modal.service';
10 import { MultiClusterFormComponent } from '../multi-cluster-form/multi-cluster-form.component';
11 import { TableComponent } from '~/app/shared/datatable/table/table.component';
12 import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
13 import { Permissions } from '~/app/shared/models/permissions';
14 import { CriticalConfirmationModalComponent } from '~/app/shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
15 import { NotificationService } from '~/app/shared/services/notification.service';
16 import { NotificationType } from '~/app/shared/enum/notification-type.enum';
17 import { CellTemplate } from '~/app/shared/enum/cell-template.enum';
18 import { MultiCluster } from '~/app/shared/models/multi-cluster';
19 import { SummaryService } from '~/app/shared/services/summary.service';
20 import { Router } from '@angular/router';
21 import { CookiesService } from '~/app/shared/services/cookie.service';
24 selector: 'cd-multi-cluster-list',
25 templateUrl: './multi-cluster-list.component.html',
26 styleUrls: ['./multi-cluster-list.component.scss']
28 export class MultiClusterListComponent {
29 @ViewChild(TableComponent)
30 table: TableComponent;
31 @ViewChild('urlTpl', { static: true })
32 public urlTpl: TemplateRef<any>;
34 permissions: Permissions;
35 tableActions: CdTableAction[];
36 clusterTokenStatus: object = {};
37 columns: Array<CdTableColumn> = [];
39 selection = new CdTableSelection();
40 bsModalRef: NgbModalRef;
41 clustersTokenMap: Map<string, string> = new Map<string, string>();
43 modalRef: NgbModalRef;
46 private multiClusterService: MultiClusterService,
47 private router: Router,
48 private summaryService: SummaryService,
49 public actionLabels: ActionLabelsI18n,
50 private notificationService: NotificationService,
51 private authStorageService: AuthStorageService,
52 private modalService: ModalService,
53 private cookieService: CookiesService
59 name: this.actionLabels.CONNECT,
60 click: () => this.openRemoteClusterInfoModal('connect')
65 name: this.actionLabels.EDIT,
66 disable: (selection: CdTableSelection) => this.getDisable('edit', selection),
67 click: () => this.openRemoteClusterInfoModal('edit')
72 name: this.actionLabels.RECONNECT,
73 disable: (selection: CdTableSelection) => this.getDisable('reconnect', selection),
74 click: () => this.openRemoteClusterInfoModal('reconnect')
79 name: this.actionLabels.DISCONNECT,
80 disable: (selection: CdTableSelection) => this.getDisable('disconnect', selection),
81 click: () => this.openDeleteClusterModal()
84 this.permissions = this.authStorageService.getPermissions();
88 this.multiClusterService.subscribe((resp: object) => {
89 if (resp && resp['config']) {
90 const clusterDetailsArray = Object.values(resp['config']).flat();
91 this.data = clusterDetailsArray;
92 this.checkClusterConnectionStatus();
98 prop: 'cluster_alias',
99 name: $localize`Alias`,
103 prop: 'cluster_connection_status',
104 name: $localize`Connection`,
106 cellTransformation: CellTemplate.badge,
107 customTemplateConfig: {
109 1: { value: 'DISCONNECTED', class: 'badge-danger' },
110 0: { value: 'CONNECTED', class: 'badge-success' },
111 2: { value: 'CHECKING..', class: 'badge-info' }
117 name: $localize`FSID`,
122 name: $localize`URL`,
124 cellTemplate: this.urlTpl
128 name: $localize`User`,
133 this.multiClusterService.subscribeClusterTokenStatus((resp: object) => {
134 this.clusterTokenStatus = resp;
135 this.checkClusterConnectionStatus();
139 checkClusterConnectionStatus() {
140 if (this.clusterTokenStatus && this.data) {
141 this.data.forEach((cluster: MultiCluster) => {
142 const clusterStatus = this.clusterTokenStatus[cluster.name];
144 if (clusterStatus !== undefined) {
145 cluster.cluster_connection_status = clusterStatus.status;
147 cluster.cluster_connection_status = 2;
150 if (cluster.cluster_alias === 'local-cluster') {
151 cluster.cluster_connection_status = 0;
157 openRemoteClusterInfoModal(action: string) {
158 const initialState = {
159 clustersData: this.data,
161 cluster: this.selection.first()
163 this.bsModalRef = this.modalService.show(MultiClusterFormComponent, initialState, {
166 this.bsModalRef.componentInstance.submitAction.subscribe(() => {
167 this.multiClusterService.refresh();
168 this.summaryService.refresh();
169 const currentRoute = this.router.url.split('?')[0];
170 if (currentRoute.includes('dashboard')) {
171 this.router.navigateByUrl('/pool', { skipLocationChange: true }).then(() => {
172 this.router.navigate([currentRoute]);
175 this.router.navigateByUrl('/', { skipLocationChange: true }).then(() => {
176 this.router.navigate([currentRoute]);
182 updateSelection(selection: CdTableSelection) {
183 this.selection = selection;
186 openDeleteClusterModal() {
187 const cluster = this.selection.first();
188 this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
189 actionDescription: $localize`Disconnect`,
190 itemDescription: $localize`Cluster`,
191 itemNames: [cluster['cluster_alias'] + ' - ' + cluster['user']],
193 this.multiClusterService.deleteCluster(cluster['name'], cluster['user']).subscribe(() => {
194 this.cookieService.deleteToken(`${cluster['name']}-${cluster['user']}`);
195 this.modalRef.close();
196 this.notificationService.show(
197 NotificationType.success,
198 $localize`Disconnected cluster '${cluster['cluster_alias']}'`
204 getDisable(action: string, selection: CdTableSelection): string | boolean {
205 if (!selection.hasSelection) {
206 return $localize`Please select one or more clusters to ${action}`;
208 if (selection.hasSingleSelection) {
209 const cluster = selection.first();
210 if (cluster['cluster_alias'] === 'local-cluster') {
211 return $localize`Cannot ${action} local cluster`;