1 import { Component, Input, OnChanges, OnInit, TemplateRef, ViewChild } from '@angular/core';
2 import { Observable, ReplaySubject, of } from 'rxjs';
3 import { catchError, shareReplay, switchMap } from 'rxjs/operators';
4 import { CephfsSubvolumeService } from '~/app/shared/api/cephfs-subvolume.service';
5 import { ActionLabelsI18n } from '~/app/shared/constants/app.constants';
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 { CephfsSubvolume } from '~/app/shared/models/cephfs-subvolume.model';
13 import { ModalService } from '~/app/shared/services/modal.service';
14 import { CephfsSubvolumeFormComponent } from '../cephfs-subvolume-form/cephfs-subvolume-form.component';
15 import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
16 import { Permissions } from '~/app/shared/models/permissions';
17 import { TaskWrapperService } from '~/app/shared/services/task-wrapper.service';
18 import { FinishedTask } from '~/app/shared/models/finished-task';
19 import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
20 import { FormControl } from '@angular/forms';
21 import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
22 import { CdForm } from '~/app/shared/forms/cd-form';
23 import { CriticalConfirmationModalComponent } from '~/app/shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
26 selector: 'cd-cephfs-subvolume-list',
27 templateUrl: './cephfs-subvolume-list.component.html',
28 styleUrls: ['./cephfs-subvolume-list.component.scss']
30 export class CephfsSubvolumeListComponent extends CdForm 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 })
46 @ViewChild('removeTmpl', { static: true })
47 removeTmpl: TemplateRef<any>;
49 @Input() fsName: string;
50 @Input() pools: any[];
52 columns: CdTableColumn[] = [];
53 tableActions: CdTableAction[];
54 context: CdTableFetchDataContext;
55 selection = new CdTableSelection();
56 removeForm: CdFormGroup;
58 permissions: Permissions;
59 modalRef: NgbModalRef;
60 errorMessage: string = '';
61 selectedName: string = '';
63 subVolumes$: Observable<CephfsSubvolume[]>;
64 subject = new ReplaySubject<CephfsSubvolume[]>();
67 private cephfsSubVolume: CephfsSubvolumeService,
68 private actionLabels: ActionLabelsI18n,
69 private modalService: ModalService,
70 private authStorageService: AuthStorageService,
71 private taskWrapper: TaskWrapperService
74 this.permissions = this.authStorageService.getPermissions();
80 name: $localize`Name`,
83 cellTemplate: this.nameTpl
86 name: $localize`Data Pool`,
87 prop: 'info.data_pool',
89 cellTransformation: CellTemplate.badge,
90 customTemplateConfig: {
91 class: 'badge-background-primary'
95 name: $localize`Usage`,
96 prop: 'info.bytes_pcent',
98 cellTemplate: this.quotaUsageTpl,
99 cellClass: 'text-right'
102 name: $localize`Path`,
105 cellTransformation: CellTemplate.path
108 name: $localize`Mode`,
111 cellTemplate: this.modeToHumanReadableTpl
114 name: $localize`Created`,
115 prop: 'info.created_at',
117 cellTransformation: CellTemplate.timeAgo
121 this.tableActions = [
123 name: this.actionLabels.CREATE,
124 permission: 'create',
127 this.modalService.show(
128 CephfsSubvolumeFormComponent,
137 name: this.actionLabels.EDIT,
138 permission: 'update',
140 click: () => this.openModal(true)
143 name: this.actionLabels.REMOVE,
144 permission: 'delete',
146 click: () => this.removeSubVolumeModal()
150 this.subVolumes$ = this.subject.pipe(
152 this.cephfsSubVolume.get(this.fsName).pipe(
154 this.context.error();
171 updateSelection(selection: CdTableSelection) {
172 this.selection = selection;
175 openModal(edit = false) {
176 this.modalService.show(
177 CephfsSubvolumeFormComponent,
180 subVolumeName: this.selection?.first()?.name,
188 removeSubVolumeModal() {
189 this.removeForm = new CdFormGroup({
190 retainSnapshots: new FormControl(false)
192 this.errorMessage = '';
193 this.selectedName = this.selection.first().name;
194 this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
195 actionDescription: 'Remove',
196 itemNames: [this.selectedName],
197 itemDescription: 'Subvolume',
198 childFormGroup: this.removeForm,
199 childFormGroupTemplate: this.removeTmpl,
202 .wrapTaskAroundCall({
203 task: new FinishedTask('cephfs/subvolume/remove', { subVolumeName: this.selectedName }),
204 call: this.cephfsSubVolume.remove(
207 this.removeForm.getValue('retainSnapshots')
211 complete: () => this.modalRef.close(),
213 this.modalRef.componentInstance.stopLoadingSpinner();
214 this.errorMessage = error.error.detail;