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 { AuthStorageService } from '~/app/shared/services/auth-storage.service';
15 import { Permissions } from '~/app/shared/models/permissions';
16 import { CdTableSelection } from '~/app/shared/models/cd-table-selection';
17 import { CdDatePipe } from '~/app/shared/pipes/cd-date.pipe';
18 import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
19 import { CriticalConfirmationModalComponent } from '~/app/shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
20 import { FinishedTask } from '~/app/shared/models/finished-task';
21 import { TaskWrapperService } from '~/app/shared/services/task-wrapper.service';
22 import { FormModalComponent } from '~/app/shared/components/form-modal/form-modal.component';
23 import { NotificationService } from '~/app/shared/services/notification.service';
24 import { NotificationType } from '~/app/shared/enum/notification-type.enum';
25 import moment from 'moment';
26 import { Validators } from '@angular/forms';
27 import { CdValidators } from '~/app/shared/forms/cd-validators';
28 import { ModalCdsService } from '~/app/shared/services/modal-cds.service';
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: ModalCdsService,
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(CephfsSubvolumeSnapshotsFormComponent, {
206 subVolumeName: this.activeSubVolumeName,
207 subVolumeGroupName: this.activeGroupName,
208 subVolumeGroups: this.subvolumeGroupList,
213 updateSelection(selection: CdTableSelection) {
214 this.selection = selection;
218 const snapshotName = this.selection.first().name;
219 const subVolumeName = this.activeSubVolumeName;
220 const subVolumeGroupName = this.activeGroupName;
221 const fsName = this.fsName;
222 this.modalRef = this.modalService.show(CriticalConfirmationModalComponent, {
223 actionDescription: this.actionLabels.REMOVE,
224 itemNames: [snapshotName],
225 itemDescription: 'Snapshot',
228 .wrapTaskAroundCall({
229 task: new FinishedTask('cephfs/subvolume/snapshot/delete', {
231 subVolumeName: subVolumeName,
232 subVolumeGroupName: subVolumeGroupName,
233 snapshotName: snapshotName
235 call: this.cephfsSubvolumeService.deleteSnapshot(
243 complete: () => this.modalService.dismissAll(),
244 error: () => this.modalRef.componentInstance.stopLoadingSpinner()
250 const cloneName = `clone_${moment().toISOString(true)}`;
251 const allGroups = Array.from(this.subvolumeGroupList).map((group) => {
252 return { value: group, text: group === '' ? '_nogroup' : group };
254 this.modalService.show(FormModalComponent, {
255 titleText: $localize`Create clone`,
261 label: $localize`Name`,
262 validators: [Validators.required, Validators.pattern(/^[.A-Za-z0-9_+:-]+$/)],
265 this.cephfsSubvolumeService.exists,
266 this.cephfsSubvolumeService,
275 pattern: $localize`Allowed characters are letters, numbers, '.', '-', '+', ':' or '_'`,
276 notUnique: $localize`A subvolume or clone with this name already exists.`
282 value: this.activeGroupName,
283 label: $localize`Group name`,
284 valueChangeListener: true,
285 dependsOn: 'cloneName',
291 submitButtonText: $localize`Create Clone`,
292 updateAsyncValidators: (value: any) =>
294 this.cephfsSubvolumeService.exists,
295 this.cephfsSubvolumeService,
301 onSubmit: (value: any) => {
302 this.cephfsSubvolumeService
303 .createSnapshotClone(
305 this.activeSubVolumeName,
306 this.selection.first().name,
308 this.activeGroupName,
312 this.notificationService.show(
313 NotificationType.success,
314 $localize`Created Clone "${value.cloneName}" successfully.`