1 import { Component, OnDestroy, OnInit, TemplateRef, ViewChild } from '@angular/core';
3 import { I18n } from '@ngx-translate/i18n-polyfill';
4 import * as _ from 'lodash';
5 import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal';
6 import { Subscription } from 'rxjs';
8 import { NfsService } from '../../../shared/api/nfs.service';
9 import { ListWithDetails } from '../../../shared/classes/list-with-details.class';
10 import { CriticalConfirmationModalComponent } from '../../../shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
11 import { ActionLabelsI18n } from '../../../shared/constants/app.constants';
12 import { TableComponent } from '../../../shared/datatable/table/table.component';
13 import { CellTemplate } from '../../../shared/enum/cell-template.enum';
14 import { Icons } from '../../../shared/enum/icons.enum';
15 import { ViewCacheStatus } from '../../../shared/enum/view-cache-status.enum';
16 import { CdTableAction } from '../../../shared/models/cd-table-action';
17 import { CdTableColumn } from '../../../shared/models/cd-table-column';
18 import { CdTableSelection } from '../../../shared/models/cd-table-selection';
19 import { FinishedTask } from '../../../shared/models/finished-task';
20 import { Permission } from '../../../shared/models/permissions';
21 import { Task } from '../../../shared/models/task';
22 import { AuthStorageService } from '../../../shared/services/auth-storage.service';
23 import { TaskListService } from '../../../shared/services/task-list.service';
24 import { TaskWrapperService } from '../../../shared/services/task-wrapper.service';
27 selector: 'cd-nfs-list',
28 templateUrl: './nfs-list.component.html',
29 styleUrls: ['./nfs-list.component.scss'],
30 providers: [TaskListService]
32 export class NfsListComponent extends ListWithDetails implements OnInit, OnDestroy {
33 @ViewChild('nfsState', { static: false })
34 nfsState: TemplateRef<any>;
35 @ViewChild('nfsFsal', { static: true })
36 nfsFsal: TemplateRef<any>;
38 @ViewChild('table', { static: true })
39 table: TableComponent;
41 columns: CdTableColumn[];
42 permission: Permission;
43 selection = new CdTableSelection();
44 summaryDataSubscription: Subscription;
47 tableActions: CdTableAction[];
48 isDefaultCluster = false;
53 'nfs/create': (metadata: any) => {
55 path: metadata['path'],
56 cluster_id: metadata['cluster_id'],
57 fsal: metadata['fsal']
63 private authStorageService: AuthStorageService,
65 private modalService: BsModalService,
66 private nfsService: NfsService,
67 private taskListService: TaskListService,
68 private taskWrapper: TaskWrapperService,
69 public actionLabels: ActionLabelsI18n
72 this.permission = this.authStorageService.getPermissions().nfs;
73 const getNfsUri = () =>
74 this.selection.first() &&
75 `${encodeURI(this.selection.first().cluster_id)}/${encodeURI(
76 this.selection.first().export_id
79 const createAction: CdTableAction = {
82 routerLink: () => '/nfs/create',
83 canBePrimary: (selection: CdTableSelection) => !selection.hasSingleSelection,
84 name: this.actionLabels.CREATE
87 const editAction: CdTableAction = {
90 routerLink: () => `/nfs/edit/${getNfsUri()}`,
91 name: this.actionLabels.EDIT
94 const deleteAction: CdTableAction = {
97 click: () => this.deleteNfsModal(),
98 name: this.actionLabels.DELETE
101 this.tableActions = [createAction, editAction, deleteAction];
107 name: this.i18n('Path'),
110 cellTransformation: CellTemplate.executing
113 name: this.i18n('Pseudo'),
118 name: this.i18n('Cluster'),
123 name: this.i18n('Daemons'),
128 name: this.i18n('Storage Backend'),
131 cellTemplate: this.nfsFsal
134 name: this.i18n('Access Type'),
140 this.nfsService.daemon().subscribe(
142 const clusters = _(daemons)
143 .map((daemon) => daemon.cluster_id)
147 this.isDefaultCluster = clusters.length === 1 && clusters[0] === '_default_';
148 this.columns[2].isHidden = this.isDefaultCluster;
150 this.table.updateColumns();
153 this.taskListService.init(
154 () => this.nfsService.list(),
155 (resp) => this.prepareResponse(resp),
156 (exports) => (this.exports = exports),
157 () => this.onFetchError(),
170 if (this.summaryDataSubscription) {
171 this.summaryDataSubscription.unsubscribe();
175 prepareResponse(resp: any): any[] {
176 let result: any[] = [];
177 resp.forEach((nfs: any) => {
178 nfs.id = `${nfs.cluster_id}:${nfs.export_id}`;
179 nfs.state = 'LOADING';
180 result = result.concat(nfs);
187 this.table.reset(); // Disable loading indicator.
188 this.viewCacheStatus = { status: ViewCacheStatus.ValueException };
191 itemFilter(entry: any, task: Task) {
193 entry.cluster_id === task.metadata['cluster_id'] &&
194 entry.export_id === task.metadata['export_id']
198 taskFilter(task: Task) {
199 return ['nfs/create', 'nfs/delete', 'nfs/edit'].includes(task.name);
202 updateSelection(selection: CdTableSelection) {
203 this.selection = selection;
207 const cluster_id = this.selection.first().cluster_id;
208 const export_id = this.selection.first().export_id;
210 this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
212 itemDescription: this.i18n('NFS export'),
213 itemNames: [`${cluster_id}:${export_id}`],
214 submitActionObservable: () =>
215 this.taskWrapper.wrapTaskAroundCall({
216 task: new FinishedTask('nfs/delete', {
217 cluster_id: cluster_id,
220 call: this.nfsService.delete(cluster_id, export_id)