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';
21 selector: 'cd-cephfs-subvolume-snapshots-list',
22 templateUrl: './cephfs-subvolume-snapshots-list.component.html',
23 styleUrls: ['./cephfs-subvolume-snapshots-list.component.scss']
25 export class CephfsSubvolumeSnapshotsListComponent implements OnInit, OnChanges {
26 @Input() fsName: string;
28 context: CdTableFetchDataContext;
29 columns: CdTableColumn[] = [];
30 tableActions: CdTableAction[];
31 selection = new CdTableSelection();
32 permissions: Permissions;
34 subVolumes$: Observable<CephfsSubvolume[]>;
35 snapshots$: Observable<any[]>;
36 snapshotSubject = new BehaviorSubject<SubvolumeSnapshot[]>([]);
37 subVolumeSubject = new BehaviorSubject<CephfsSubvolume[]>([]);
39 subvolumeGroupList: string[] = [];
40 subVolumesList: string[];
43 activeSubVolumeName = '';
45 isSubVolumesAvailable = false;
48 observables: any = [];
51 private cephfsSubvolumeGroupService: CephfsSubvolumeGroupService,
52 private cephfsSubvolumeService: CephfsSubvolumeService,
53 private actionLabels: ActionLabelsI18n,
54 private modalService: ModalService,
55 private authStorageService: AuthStorageService,
56 private cdDatePipe: CdDatePipe
58 this.permissions = this.authStorageService.getPermissions();
64 name: $localize`Name`,
69 name: $localize`Created`,
70 prop: 'info.created_at',
75 name: $localize`Pending Clones`,
76 prop: 'info.has_pending_clones',
78 cellTransformation: CellTemplate.badge,
79 customTemplateConfig: {
81 no: { class: 'badge-success' },
82 yes: { class: 'badge-info' }
90 name: this.actionLabels.CREATE,
93 click: () => this.openModal()
97 this.cephfsSubvolumeGroupService
100 switchMap((groups) => {
101 // manually adding the group '_nogroup' to the list.
102 groups.unshift({ name: '' });
104 const observables = groups.map((group) =>
105 this.cephfsSubvolumeService.existsInFs(this.fsName, group.name).pipe(
106 switchMap((resp) => {
108 this.subvolumeGroupList.push(group.name);
110 return of(resp); // Emit the response
115 return forkJoin(observables);
119 if (this.subvolumeGroupList.length) {
120 this.isSubVolumesAvailable = true;
122 this.isLoading = false;
126 ngOnChanges(changes: SimpleChanges): void {
127 if (changes.fsName) {
128 this.subVolumeSubject.next([]);
132 selectSubVolumeGroup(subVolumeGroupName: string) {
133 this.activeGroupName = subVolumeGroupName;
134 this.getSubVolumes();
137 selectSubVolume(subVolumeName: string) {
138 this.activeSubVolumeName = subVolumeName;
139 this.getSubVolumesSnapshot();
143 this.subVolumes$ = this.subVolumeSubject.pipe(
145 this.cephfsSubvolumeService.get(this.fsName, this.activeGroupName, false).pipe(
147 this.subVolumesList = resp.map((subVolume) => subVolume.name);
148 this.activeSubVolumeName = resp[0].name;
149 this.getSubVolumesSnapshot();
156 getSubVolumesSnapshot() {
157 this.snapshots$ = this.snapshotSubject.pipe(
159 this.cephfsSubvolumeService
160 .getSnapshots(this.fsName, this.activeSubVolumeName, this.activeGroupName)
163 this.context.error();
173 this.snapshotSubject.next([]);
176 openModal(edit = false) {
177 this.modalService.show(
178 CephfsSubvolumeSnapshotsFormComponent,
181 subVolumeName: this.activeSubVolumeName,
182 subVolumeGroupName: this.activeGroupName,
183 subVolumeGroups: this.subvolumeGroupList,
190 updateSelection(selection: CdTableSelection) {
191 this.selection = selection;