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';
22 const DEBOUNCE_TIMER = 300;
25 selector: 'cd-cephfs-auth-modal',
26 templateUrl: './cephfs-auth-modal.component.html',
27 styleUrls: ['./cephfs-auth-modal.component.scss']
29 export class CephfsAuthModalComponent extends CdForm implements OnInit, AfterViewInit {
30 subvolumeGroup: string;
32 isDefaultSubvolumeGroup = false;
42 description: $localize`Read permission is the minimum givable access`
46 description: $localize`Permission to set layouts or quotas, write access needed`
50 description: $localize`Permission to set layouts or quotas, write access needed`
54 description: $localize`Permission to create or delete snapshots, write access needed`
58 description: $localize`Safety measure to prevent scenarios such as accidental sudo rm -rf /path`
63 private actionLabels: ActionLabelsI18n,
64 public directoryStore: DirectoryStoreService,
65 private cephfsService: CephfsService,
66 private taskWrapper: TaskWrapperService,
67 private modalService: ModalCdsService,
68 private changeDetectorRef: ChangeDetectorRef,
70 @Optional() @Inject('fsName') public fsName: string,
71 @Optional() @Inject('id') public id: number
74 this.action = this.actionLabels.UPDATE;
75 this.resource = $localize`access`;
78 ngAfterViewInit(): void {
79 this.changeDetectorRef.detectChanges();
83 this.directoryStore.loadDirectories(this.id, '/', 3);
89 this.form = new CdFormGroup({
90 fsName: new FormControl(
91 { value: this.fsName, disabled: true },
93 validators: [Validators.required]
96 directory: new FormControl(undefined, {
98 validators: [Validators.required]
100 userId: new FormControl(undefined, {
101 validators: [Validators.required]
103 read: new FormControl(
104 { value: true, disabled: true },
106 validators: [Validators.required]
109 write: new FormControl(undefined),
110 snapshot: new FormControl({ value: false, disabled: true }),
111 quota: new FormControl({ value: false, disabled: true }),
112 rootSquash: new FormControl(undefined)
116 search: OperatorFunction<string, readonly string[]> = (input: Observable<string>) =>
118 debounceTime(DEBOUNCE_TIMER),
119 distinctUntilChanged(),
121 this.directoryStore.search(term, this.id).pipe(
130 const clientId: number = this.form.getValue('userId');
131 const caps: string[] = [this.form.getValue('directory'), this.transformPermissions()];
132 const rootSquash: boolean = this.form.getValue('rootSquash');
134 .wrapTaskAroundCall({
135 task: new FinishedTask('cephfs/auth', {
138 call: this.cephfsService.setAuth(this.fsName, clientId, caps, rootSquash)
141 error: () => this.form.setErrors({ cdSubmitButton: true }),
143 this.modalService.dismissAll();
148 transformPermissions(): string {
149 const write = this.form.getValue('write');
150 const snapshot = this.form.getValue('snapshot');
151 const quota = this.form.getValue('quota');
152 return `r${write ? 'w' : ''}${quota ? 'p' : ''}${snapshot ? 's' : ''}`;
155 toggleFormControl() {
156 const snapshot = this.form.get('snapshot');
157 const quota = this.form.get('quota');
158 snapshot.disabled ? snapshot.enable() : snapshot.disable();
159 quota.disabled ? quota.enable() : quota.disable();