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';
23 import { ModalCdsService } from '~/app/shared/services/modal-cds.service';
26 selector: 'cd-cephfs-subvolume-group',
27 templateUrl: './cephfs-subvolume-group.component.html',
28 styleUrls: ['./cephfs-subvolume-group.component.scss']
30 export class CephfsSubvolumeGroupComponent implements OnInit, OnChanges {
31 @ViewChild('quotaUsageTpl', { static: true })
34 @ViewChild('typeTpl', { static: true })
37 @ViewChild('modeToHumanReadableTpl', { static: true })
38 modeToHumanReadableTpl: any;
40 @ViewChild('nameTpl', { static: true })
43 @ViewChild('quotaSizeTpl', { static: true })
48 @Input() pools: any[];
50 columns: CdTableColumn[];
51 tableActions: CdTableAction[];
52 context: CdTableFetchDataContext;
53 selection = new CdTableSelection();
55 permissions: Permissions;
57 subvolumeGroup$: Observable<CephfsSubvolumeGroup[]>;
58 subject = new BehaviorSubject<CephfsSubvolumeGroup[]>([]);
60 modalRef: NgbModalRef;
63 private cephfsSubvolumeGroup: CephfsSubvolumeGroupService,
64 private actionLabels: ActionLabelsI18n,
65 private modalService: ModalService,
66 private authStorageService: AuthStorageService,
67 private taskWrapper: TaskWrapperService,
68 private cdsModalService: ModalCdsService
70 this.permissions = this.authStorageService.getPermissions();
76 name: $localize`Name`,
79 cellTransformation: CellTemplate.bold
82 name: $localize`Data Pool`,
83 prop: 'info.data_pool',
85 cellTransformation: CellTemplate.badge,
86 customTemplateConfig: {
87 class: 'badge-background-primary'
91 name: $localize`Usage`,
92 prop: 'info.bytes_pcent',
94 cellTemplate: this.quotaUsageTpl,
95 cellClass: 'text-right'
98 name: $localize`Mode`,
101 cellTemplate: this.modeToHumanReadableTpl
104 name: $localize`Created`,
105 prop: 'info.created_at',
107 cellTransformation: CellTemplate.timeAgo
111 this.tableActions = [
113 name: this.actionLabels.CREATE,
114 permission: 'create',
116 click: () => this.openModal(),
117 canBePrimary: (selection: CdTableSelection) => !selection.hasSelection
120 name: this.actionLabels.EDIT,
121 permission: 'update',
123 click: () => this.openModal(true)
126 name: this.actionLabels.NFS_EXPORT,
127 permission: 'create',
128 icon: Icons.nfsExport,
129 routerLink: () => ['/cephfs/nfs/create', this.fsName, this.selection?.first()?.name],
130 disable: () => !this.selection.hasSingleSelection
133 name: this.actionLabels.REMOVE,
134 permission: 'delete',
136 click: () => this.removeSubVolumeModal()
140 this.subvolumeGroup$ = this.subject.pipe(
142 this.cephfsSubvolumeGroup.get(this.fsName).pipe(
144 this.context.error();
154 this.subject.next([]);
157 ngOnChanges(changes: SimpleChanges) {
158 if (changes.fsName) {
159 this.subject.next([]);
163 updateSelection(selection: CdTableSelection) {
164 this.selection = selection;
167 openModal(edit = false) {
168 this.modalService.show(
169 CephfsSubvolumegroupFormComponent,
172 subvolumegroupName: this.selection?.first()?.name,
180 removeSubVolumeModal() {
181 const name = this.selection.first().name;
182 this.cdsModalService.show(CriticalConfirmationModalComponent, {
183 itemDescription: 'subvolume group',
185 actionDescription: 'remove',
186 submitActionObservable: () =>
187 this.taskWrapper.wrapTaskAroundCall({
188 task: new FinishedTask('cephfs/subvolume/group/remove', { subvolumegroupName: name }),
189 call: this.cephfsSubvolumeGroup.remove(this.fsName, name)