1 import { Component, Input, OnChanges, OnInit, SimpleChanges, ViewChild } from '@angular/core';
2 import { BehaviorSubject, Observable, of } from 'rxjs';
3 import { catchError, shareReplay, switchMap } from 'rxjs/operators';
5 import { CephfsSubvolumeGroupService } from '~/app/shared/api/cephfs-subvolume-group.service';
6 import { CellTemplate } from '~/app/shared/enum/cell-template.enum';
7 import { Icons } from '~/app/shared/enum/icons.enum';
8 import { CdTableAction } from '~/app/shared/models/cd-table-action';
9 import { CdTableColumn } from '~/app/shared/models/cd-table-column';
10 import { CdTableFetchDataContext } from '~/app/shared/models/cd-table-fetch-data-context';
11 import { CdTableSelection } from '~/app/shared/models/cd-table-selection';
12 import { CephfsSubvolumegroupFormComponent } from '../cephfs-subvolumegroup-form/cephfs-subvolumegroup-form.component';
13 import { ActionLabelsI18n } from '~/app/shared/constants/app.constants';
14 import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
15 import { ModalService } from '~/app/shared/services/modal.service';
16 import { Permissions } from '~/app/shared/models/permissions';
17 import { CriticalConfirmationModalComponent } from '~/app/shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
18 import { FinishedTask } from '~/app/shared/models/finished-task';
19 import { TaskWrapperService } from '~/app/shared/services/task-wrapper.service';
20 import { CephfsSubvolumeGroup } from '~/app/shared/models/cephfs-subvolume-group.model';
21 import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
22 import _ from 'lodash';
25 selector: 'cd-cephfs-subvolume-group',
26 templateUrl: './cephfs-subvolume-group.component.html',
27 styleUrls: ['./cephfs-subvolume-group.component.scss']
29 export class CephfsSubvolumeGroupComponent implements OnInit, OnChanges {
30 @ViewChild('quotaUsageTpl', { static: true })
33 @ViewChild('typeTpl', { static: true })
36 @ViewChild('modeToHumanReadableTpl', { static: true })
37 modeToHumanReadableTpl: any;
39 @ViewChild('nameTpl', { static: true })
42 @ViewChild('quotaSizeTpl', { static: true })
47 @Input() pools: any[];
49 columns: CdTableColumn[];
50 tableActions: CdTableAction[];
51 context: CdTableFetchDataContext;
52 selection = new CdTableSelection();
54 permissions: Permissions;
56 subvolumeGroup$: Observable<CephfsSubvolumeGroup[]>;
57 subject = new BehaviorSubject<CephfsSubvolumeGroup[]>([]);
59 modalRef: NgbModalRef;
62 private cephfsSubvolumeGroup: CephfsSubvolumeGroupService,
63 private actionLabels: ActionLabelsI18n,
64 private modalService: ModalService,
65 private authStorageService: AuthStorageService,
66 private taskWrapper: TaskWrapperService
68 this.permissions = this.authStorageService.getPermissions();
74 name: $localize`Name`,
77 cellTransformation: CellTemplate.bold
80 name: $localize`Data Pool`,
81 prop: 'info.data_pool',
83 cellTransformation: CellTemplate.badge,
84 customTemplateConfig: {
85 class: 'badge-background-primary'
89 name: $localize`Usage`,
90 prop: 'info.bytes_pcent',
92 cellTemplate: this.quotaUsageTpl,
93 cellClass: 'text-right'
96 name: $localize`Mode`,
99 cellTemplate: this.modeToHumanReadableTpl
102 name: $localize`Created`,
103 prop: 'info.created_at',
105 cellTransformation: CellTemplate.timeAgo
109 this.tableActions = [
111 name: this.actionLabels.CREATE,
112 permission: 'create',
114 click: () => this.openModal(),
115 canBePrimary: (selection: CdTableSelection) => !selection.hasSelection
118 name: this.actionLabels.EDIT,
119 permission: 'update',
121 click: () => this.openModal(true)
124 name: this.actionLabels.NFS_EXPORT,
125 permission: 'create',
126 icon: Icons.nfsExport,
127 routerLink: () => ['/cephfs/nfs/create', this.fsName, this.selection?.first()?.name],
128 disable: () => !this.selection.hasSingleSelection
131 name: this.actionLabels.REMOVE,
132 permission: 'delete',
134 click: () => this.removeSubVolumeModal()
138 this.subvolumeGroup$ = this.subject.pipe(
140 this.cephfsSubvolumeGroup.get(this.fsName).pipe(
142 this.context.error();
152 this.subject.next([]);
155 ngOnChanges(changes: SimpleChanges) {
156 if (changes.fsName) {
157 this.subject.next([]);
161 updateSelection(selection: CdTableSelection) {
162 this.selection = selection;
165 openModal(edit = false) {
166 this.modalService.show(
167 CephfsSubvolumegroupFormComponent,
170 subvolumegroupName: this.selection?.first()?.name,
178 removeSubVolumeModal() {
179 const name = this.selection.first().name;
180 this.modalService.show(CriticalConfirmationModalComponent, {
181 itemDescription: 'subvolume group',
183 actionDescription: 'remove',
184 submitActionObservable: () =>
185 this.taskWrapper.wrapTaskAroundCall({
186 task: new FinishedTask('cephfs/subvolume/group/remove', { subvolumegroupName: name }),
187 call: this.cephfsSubvolumeGroup.remove(this.fsName, name)