]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/blob
f6b97fdda25e23502274613511d506dc65e8bd2b
[ceph.git] /
1 import {
2   AfterViewInit,
3   ChangeDetectorRef,
4   Component,
5   Inject,
6   OnInit,
7   Optional
8 } from '@angular/core';
9 import { FormControl, Validators } from '@angular/forms';
10 import { OperatorFunction, Observable, of } from 'rxjs';
11 import { debounceTime, distinctUntilChanged, switchMap, catchError } from 'rxjs/operators';
12 import { CephfsService } from '~/app/shared/api/cephfs.service';
13 import { DirectoryStoreService } from '~/app/shared/api/directory-store.service';
14 import { ActionLabelsI18n } from '~/app/shared/constants/app.constants';
15 import { Icons } from '~/app/shared/enum/icons.enum';
16 import { CdForm } from '~/app/shared/forms/cd-form';
17 import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
18 import { FinishedTask } from '~/app/shared/models/finished-task';
19 import { ModalCdsService } from '~/app/shared/services/modal-cds.service';
20 import { TaskWrapperService } from '~/app/shared/services/task-wrapper.service';
21 import { PERMISSION_NAMES } from '~/app/shared/models/cephfs.model';
22
23 const DEBOUNCE_TIMER = 300;
24
25 @Component({
26   selector: 'cd-cephfs-auth-modal',
27   templateUrl: './cephfs-auth-modal.component.html',
28   styleUrls: ['./cephfs-auth-modal.component.scss']
29 })
30 export class CephfsAuthModalComponent extends CdForm implements OnInit, AfterViewInit {
31   subvolumeGroup: string;
32   subvolume: string;
33   isDefaultSubvolumeGroup = false;
34   isSubvolume = false;
35   form: CdFormGroup;
36   action: string;
37   resource: string;
38   icons = Icons;
39   readonly defaultdir: string = '/';
40
41   clientPermissions = [
42     {
43       name: PERMISSION_NAMES.READ,
44       description: $localize`Read permission is the minimum givable access`
45     },
46     {
47       name: PERMISSION_NAMES.WRITE,
48       description: $localize`Permission to set layouts or quotas, write access needed`
49     },
50     {
51       name: PERMISSION_NAMES.QUOTA,
52       description: $localize`Permission to set layouts or quotas, write access needed`
53     },
54     {
55       name: PERMISSION_NAMES.SNAPSHOT,
56       description: $localize`Permission to create or delete snapshots, write access needed`
57     },
58     {
59       name: PERMISSION_NAMES.ROOTSQUASH,
60       description: $localize`Safety measure to prevent scenarios such as accidental sudo rm -rf /path`
61     }
62   ];
63
64   constructor(
65     private actionLabels: ActionLabelsI18n,
66     public directoryStore: DirectoryStoreService,
67     private cephfsService: CephfsService,
68     private taskWrapper: TaskWrapperService,
69     private modalService: ModalCdsService,
70     private changeDetectorRef: ChangeDetectorRef,
71
72     @Optional() @Inject('fsName') public fsName: string,
73     @Optional() @Inject('id') public id: number
74   ) {
75     super();
76     this.action = this.actionLabels.UPDATE;
77     this.resource = $localize`access`;
78   }
79
80   ngAfterViewInit(): void {
81     this.changeDetectorRef.detectChanges();
82   }
83
84   ngOnInit() {
85     this.directoryStore.loadDirectories(this.id, '/', 3);
86     this.createForm();
87     this.loadingReady();
88   }
89
90   createForm() {
91     this.form = new CdFormGroup({
92       fsName: new FormControl(
93         { value: this.fsName, disabled: true },
94         {
95           validators: [Validators.required]
96         }
97       ),
98       directory: new FormControl(
99         { value: this.defaultdir, disabled: false },
100         {
101           updateOn: 'blur',
102           validators: [Validators.required]
103         }
104       ),
105       userId: new FormControl(undefined, {
106         validators: [Validators.required]
107       }),
108       read: new FormControl(
109         { value: true, disabled: true },
110         {
111           validators: [Validators.required]
112         }
113       ),
114       write: new FormControl(undefined),
115       snapshot: new FormControl({ value: false, disabled: true }),
116       quota: new FormControl({ value: false, disabled: true }),
117       rootSquash: new FormControl(undefined)
118     });
119   }
120
121   search: OperatorFunction<string, readonly string[]> = (input: Observable<string>) =>
122     input.pipe(
123       debounceTime(DEBOUNCE_TIMER),
124       distinctUntilChanged(),
125       switchMap((term) =>
126         this.directoryStore.search(term, this.id).pipe(
127           catchError(() => {
128             return of([]);
129           })
130         )
131       )
132     );
133
134   onSubmit() {
135     const clientId: number = this.form.getValue('userId');
136     const caps: string[] = [this.form.getValue('directory'), this.transformPermissions()];
137     const rootSquash: boolean = this.form.getValue(PERMISSION_NAMES.ROOTSQUASH);
138     this.taskWrapper
139       .wrapTaskAroundCall({
140         task: new FinishedTask('cephfs/auth', {
141           clientId: clientId
142         }),
143         call: this.cephfsService.setAuth(this.fsName, clientId, caps, rootSquash)
144       })
145       .subscribe({
146         error: () => this.form.setErrors({ cdSubmitButton: true }),
147         complete: () => {
148           this.modalService.dismissAll();
149         }
150       });
151   }
152
153   transformPermissions(): string {
154     const write = this.form.getValue(PERMISSION_NAMES.WRITE);
155     const snapshot = this.form.getValue(PERMISSION_NAMES.SNAPSHOT);
156     const quota = this.form.getValue(PERMISSION_NAMES.QUOTA);
157     return `r${write ? 'w' : ''}${quota ? 'p' : ''}${snapshot ? 's' : ''}`;
158   }
159
160   toggleFormControl(_event?: boolean, permisson?: string) {
161     const snapshot = this.form.get(PERMISSION_NAMES.SNAPSHOT);
162     const quota = this.form.get(PERMISSION_NAMES.QUOTA);
163     if (_event && permisson == PERMISSION_NAMES.WRITE) {
164       snapshot.disabled ? snapshot.enable() : snapshot.disable();
165       quota.disabled ? quota.enable() : quota.disable();
166     } else if (!_event && permisson == PERMISSION_NAMES.WRITE) {
167       snapshot.setValue(false);
168       quota.setValue(false);
169       snapshot.disable();
170       quota.disable();
171     }
172   }
173 }