1 import { Component, Input, OnChanges, OnInit, SimpleChanges } from '@angular/core';
2 import { BehaviorSubject, Observable, forkJoin, of } from 'rxjs';
3 import { catchError, shareReplay, switchMap, tap } from 'rxjs/operators';
4 import { CephfsSubvolumeGroupService } from '~/app/shared/api/cephfs-subvolume-group.service';
5 import { CephfsSubvolumeService } from '~/app/shared/api/cephfs-subvolume.service';
6 import { ActionLabelsI18n } from '~/app/shared/constants/app.constants';
7 import { CellTemplate } from '~/app/shared/enum/cell-template.enum';
8 import { Icons } from '~/app/shared/enum/icons.enum';
9 import { CdTableAction } from '~/app/shared/models/cd-table-action';
10 import { CdTableColumn } from '~/app/shared/models/cd-table-column';
11 import { CdTableFetchDataContext } from '~/app/shared/models/cd-table-fetch-data-context';
12 import { CephfsSubvolume, SubvolumeSnapshot } from '~/app/shared/models/cephfs-subvolume.model';
13 import { CephfsSubvolumeSnapshotsFormComponent } from './cephfs-subvolume-snapshots-form/cephfs-subvolume-snapshots-form.component';
14 import { ModalService } from '~/app/shared/services/modal.service';
15 import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
16 import { Permissions } from '~/app/shared/models/permissions';
17 import { CdTableSelection } from '~/app/shared/models/cd-table-selection';
18 import { CdDatePipe } from '~/app/shared/pipes/cd-date.pipe';
19 import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
20 import { CriticalConfirmationModalComponent } from '~/app/shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
21 import { FinishedTask } from '~/app/shared/models/finished-task';
22 import { TaskWrapperService } from '~/app/shared/services/task-wrapper.service';
23 import { FormModalComponent } from '~/app/shared/components/form-modal/form-modal.component';
24 import { NotificationService } from '~/app/shared/services/notification.service';
25 import { NotificationType } from '~/app/shared/enum/notification-type.enum';
26 import moment from 'moment';
27 import { Validators } from '@angular/forms';
28 import { CdValidators } from '~/app/shared/forms/cd-validators';
31 selector: 'cd-cephfs-subvolume-snapshots-list',
32 templateUrl: './cephfs-subvolume-snapshots-list.component.html',
33 styleUrls: ['./cephfs-subvolume-snapshots-list.component.scss']
35 export class CephfsSubvolumeSnapshotsListComponent implements OnInit, OnChanges {
36 @Input() fsName: string;
38 context: CdTableFetchDataContext;
39 columns: CdTableColumn[] = [];
40 tableActions: CdTableAction[];
41 selection = new CdTableSelection();
42 permissions: Permissions;
43 modalRef: NgbModalRef;
45 subVolumes$: Observable<CephfsSubvolume[]>;
46 snapshots$: Observable<any[]>;
47 snapshotSubject = new BehaviorSubject<SubvolumeSnapshot[]>([]);
48 subVolumeSubject = new BehaviorSubject<CephfsSubvolume[]>([]);
50 subvolumeGroupList: string[] = [];
51 subVolumesList: string[];
54 activeSubVolumeName = '';
56 isSubVolumesAvailable = false;
59 observables: any = [];
62 private cephfsSubvolumeGroupService: CephfsSubvolumeGroupService,
63 private cephfsSubvolumeService: CephfsSubvolumeService,
64 private actionLabels: ActionLabelsI18n,
65 private modalService: ModalService,
66 private authStorageService: AuthStorageService,
67 private cdDatePipe: CdDatePipe,
68 private taskWrapper: TaskWrapperService,
69 private notificationService: NotificationService
71 this.permissions = this.authStorageService.getPermissions();
77 name: $localize`Name`,
82 name: $localize`Created`,
83 prop: 'info.created_at',
88 name: $localize`Pending Clones`,
89 prop: 'info.has_pending_clones',
91 cellTransformation: CellTemplate.badge,
92 customTemplateConfig: {
94 no: { class: 'badge-success' },
95 yes: { class: 'badge-info' }
101 this.tableActions = [
103 name: this.actionLabels.CREATE,
104 permission: 'create',
106 click: () => this.openModal()
109 name: this.actionLabels.CLONE,
110 permission: 'create',
112 disable: () => !this.selection.hasSingleSelection,
113 click: () => this.cloneModal()
116 name: this.actionLabels.REMOVE,
117 permission: 'delete',
119 disable: () => !this.selection.hasSingleSelection,
120 click: () => this.deleteSnapshot()
124 this.cephfsSubvolumeGroupService
127 switchMap((groups) => {
128 // manually adding the group '_nogroup' to the list.
129 groups.unshift({ name: '' });
131 const observables = groups.map((group) =>
132 this.cephfsSubvolumeService.existsInFs(this.fsName, group.name).pipe(
133 switchMap((resp) => {
135 this.subvolumeGroupList.push(group.name);
137 return of(resp); // Emit the response
142 return forkJoin(observables);
146 if (this.subvolumeGroupList.length) {
147 this.isSubVolumesAvailable = true;
149 this.isLoading = false;
153 ngOnChanges(changes: SimpleChanges): void {
154 if (changes.fsName) {
155 this.subVolumeSubject.next([]);
159 selectSubVolumeGroup(subVolumeGroupName: string) {
160 this.activeGroupName = subVolumeGroupName;
161 this.getSubVolumes();
164 selectSubVolume(subVolumeName: string) {
165 this.activeSubVolumeName = subVolumeName;
166 this.getSubVolumesSnapshot();
170 this.subVolumes$ = this.subVolumeSubject.pipe(
172 this.cephfsSubvolumeService.get(this.fsName, this.activeGroupName, false).pipe(
174 this.subVolumesList = resp.map((subVolume) => subVolume.name);
175 this.activeSubVolumeName = resp[0].name;
176 this.getSubVolumesSnapshot();
183 getSubVolumesSnapshot() {
184 this.snapshots$ = this.snapshotSubject.pipe(
186 this.cephfsSubvolumeService
187 .getSnapshots(this.fsName, this.activeSubVolumeName, this.activeGroupName)
190 this.context.error();
200 this.snapshotSubject.next([]);
203 openModal(edit = false) {
204 this.modalService.show(
205 CephfsSubvolumeSnapshotsFormComponent,
208 subVolumeName: this.activeSubVolumeName,
209 subVolumeGroupName: this.activeGroupName,
210 subVolumeGroups: this.subvolumeGroupList,
217 updateSelection(selection: CdTableSelection) {
218 this.selection = selection;
222 const snapshotName = this.selection.first().name;
223 const subVolumeName = this.activeSubVolumeName;
224 const subVolumeGroupName = this.activeGroupName;
225 const fsName = this.fsName;
226 this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
227 actionDescription: this.actionLabels.REMOVE,
228 itemNames: [snapshotName],
229 itemDescription: 'Snapshot',
232 .wrapTaskAroundCall({
233 task: new FinishedTask('cephfs/subvolume/snapshot/delete', {
235 subVolumeName: subVolumeName,
236 subVolumeGroupName: subVolumeGroupName,
237 snapshotName: snapshotName
239 call: this.cephfsSubvolumeService.deleteSnapshot(
247 complete: () => this.modalRef.close(),
248 error: () => this.modalRef.componentInstance.stopLoadingSpinner()
254 const cloneName = `clone_${moment().toISOString(true)}`;
255 const allGroups = Array.from(this.subvolumeGroupList).map((group) => {
256 return { value: group, text: group === '' ? '_nogroup' : group };
258 this.modalService.show(FormModalComponent, {
259 titleText: $localize`Create clone`,
265 label: $localize`Name`,
266 validators: [Validators.required, Validators.pattern(/^[.A-Za-z0-9_+:-]+$/)],
269 this.cephfsSubvolumeService.exists,
270 this.cephfsSubvolumeService,
279 pattern: $localize`Allowed characters are letters, numbers, '.', '-', '+', ':' or '_'`,
280 notUnique: $localize`A subvolume or clone with this name already exists.`
286 value: this.activeGroupName,
287 label: $localize`Group name`,
288 valueChangeListener: true,
289 dependsOn: 'cloneName',
295 submitButtonText: $localize`Create Clone`,
296 updateAsyncValidators: (value: any) =>
298 this.cephfsSubvolumeService.exists,
299 this.cephfsSubvolumeService,
305 onSubmit: (value: any) => {
306 this.cephfsSubvolumeService
307 .createSnapshotClone(
309 this.activeSubVolumeName,
310 this.selection.first().name,
312 this.activeGroupName,
316 this.notificationService.show(
317 NotificationType.success,
318 $localize`Created Clone "${value.cloneName}" successfully.`