1 import { ChangeDetectorRef, Component, OnInit, ViewChild } from '@angular/core';
8 } from '@angular/forms';
9 import { ActivatedRoute, Router } from '@angular/router';
11 import _ from 'lodash';
12 import { forkJoin, Observable, of } from 'rxjs';
13 import { catchError, debounceTime, distinctUntilChanged, map, mergeMap } from 'rxjs/operators';
15 import { NfsFSAbstractionLayer } from '~/app/ceph/nfs/models/nfs.fsal';
16 import { Directory, NfsService } from '~/app/shared/api/nfs.service';
17 import { RgwBucketService } from '~/app/shared/api/rgw-bucket.service';
18 import { RgwSiteService } from '~/app/shared/api/rgw-site.service';
19 import { ActionLabelsI18n } from '~/app/shared/constants/app.constants';
20 import { Icons } from '~/app/shared/enum/icons.enum';
21 import { CdForm } from '~/app/shared/forms/cd-form';
22 import { CdFormBuilder } from '~/app/shared/forms/cd-form-builder';
23 import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
24 import { CdValidators } from '~/app/shared/forms/cd-validators';
25 import { FinishedTask } from '~/app/shared/models/finished-task';
26 import { Permission } from '~/app/shared/models/permissions';
27 import { CdHttpErrorResponse } from '~/app/shared/services/api-interceptor.service';
28 import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
29 import { TaskWrapperService } from '~/app/shared/services/task-wrapper.service';
30 import { NfsFormClientComponent } from '../nfs-form-client/nfs-form-client.component';
33 selector: 'cd-nfs-form',
34 templateUrl: './nfs-form.component.html',
35 styleUrls: ['./nfs-form.component.scss']
37 export class NfsFormComponent extends CdForm implements OnInit {
38 @ViewChild('nfsClients', { static: true })
39 nfsClients: NfsFormClientComponent;
43 permission: Permission;
47 cluster_id: string = null;
48 export_id: string = null;
50 allClusters: { cluster_id: string }[] = null;
54 allFsNames: any[] = null;
55 fsalAvailabilityError: string = null;
57 defaultAccessType = { RGW: 'RO' };
58 nfsAccessType: any[] = this.nfsService.nfsAccessType;
59 nfsSquash: any[] = Object.keys(this.nfsService.nfsSquash);
64 pathDataSource = (text$: Observable<string>) => {
67 distinctUntilChanged(),
68 mergeMap((token: string) => this.getPathTypeahead(token)),
69 map((val: string[]) => val)
73 bucketDataSource = (text$: Observable<string>) => {
76 distinctUntilChanged(),
77 mergeMap((token: string) => this.getBucketTypeahead(token))
82 private authStorageService: AuthStorageService,
83 private nfsService: NfsService,
84 private route: ActivatedRoute,
85 private router: Router,
86 private rgwBucketService: RgwBucketService,
87 private rgwSiteService: RgwSiteService,
88 private formBuilder: CdFormBuilder,
89 private taskWrapper: TaskWrapperService,
90 private cdRef: ChangeDetectorRef,
91 public actionLabels: ActionLabelsI18n
94 this.permission = this.authStorageService.getPermissions().pool;
95 this.resource = $localize`NFS export`;
100 const promises: Observable<any>[] = [
101 this.nfsService.listClusters(),
102 this.nfsService.fsals(),
103 this.nfsService.filesystems()
106 if (this.router.url.startsWith('/nfs/edit')) {
111 this.action = this.actionLabels.EDIT;
112 this.route.params.subscribe((params: { cluster_id: string; export_id: string }) => {
113 this.cluster_id = decodeURIComponent(params.cluster_id);
114 this.export_id = decodeURIComponent(params.export_id);
115 promises.push(this.nfsService.get(this.cluster_id, this.export_id));
117 this.getData(promises);
119 this.nfsForm.get('cluster_id').disable();
121 this.action = this.actionLabels.CREATE;
122 this.getData(promises);
126 getData(promises: Observable<any>[]) {
127 forkJoin(promises).subscribe((data: any[]) => {
128 this.resolveClusters(data[0]);
129 this.resolveFsals(data[1]);
130 this.resolveFilesystems(data[2]);
132 this.resolveModel(data[3]);
140 this.nfsForm = new CdFormGroup({
141 cluster_id: new FormControl('', {
142 validators: [Validators.required]
144 fsal: new CdFormGroup({
145 name: new FormControl('', {
146 validators: [Validators.required]
148 fs_name: new FormControl('', {
150 CdValidators.requiredIf({
156 path: new FormControl('/'),
157 protocolNfsv4: new FormControl(true),
158 pseudo: new FormControl('', {
160 CdValidators.requiredIf({ protocolNfsv4: true }),
161 Validators.pattern('^/[^><|&()]*$')
164 access_type: new FormControl('RW'),
165 squash: new FormControl(this.nfsSquash[0]),
166 transportUDP: new FormControl(true, {
168 CdValidators.requiredIf({ transportTCP: false }, (value: boolean) => {
173 transportTCP: new FormControl(true, {
175 CdValidators.requiredIf({ transportUDP: false }, (value: boolean) => {
180 clients: this.formBuilder.array([]),
181 security_label: new FormControl(false),
182 sec_label_xattr: new FormControl(
184 CdValidators.requiredIf({ security_label: true, 'fsal.name': 'CEPH' })
189 resolveModel(res: any) {
190 if (res.fsal.name === 'CEPH') {
191 res.sec_label_xattr = res.fsal.sec_label_xattr;
194 res.protocolNfsv4 = res.protocols.indexOf(4) !== -1;
195 delete res.protocols;
197 res.transportTCP = res.transports.indexOf('TCP') !== -1;
198 res.transportUDP = res.transports.indexOf('UDP') !== -1;
199 delete res.transports;
201 Object.entries(this.nfsService.nfsSquash).forEach(([key, value]) => {
202 if (value.includes(res.squash)) {
207 res.clients.forEach((client: any) => {
209 client.addresses.forEach((address: string) => {
210 addressStr += address + ', ';
212 if (addressStr.length >= 2) {
213 addressStr = addressStr.substring(0, addressStr.length - 2);
215 client.addresses = addressStr;
218 this.nfsForm.patchValue(res);
219 this.setPathValidation();
220 this.clients = res.clients;
223 resolveClusters(clusters: string[]) {
224 this.allClusters = [];
225 for (const cluster of clusters) {
226 this.allClusters.push({ cluster_id: cluster });
228 if (!this.isEdit && this.allClusters.length > 0) {
229 this.nfsForm.get('cluster_id').setValue(this.allClusters[0].cluster_id);
233 resolveFsals(res: string[]) {
234 res.forEach((fsal) => {
235 const fsalItem = this.nfsService.nfsFsal.find((currentFsalItem) => {
236 return fsal === currentFsalItem.value;
239 if (_.isObjectLike(fsalItem)) {
240 this.allFsals.push(fsalItem);
243 if (!this.isEdit && this.allFsals.length > 0) {
244 this.nfsForm.patchValue({
246 name: this.allFsals[0].value
252 resolveFilesystems(filesystems: any[]) {
253 this.allFsNames = filesystems;
254 if (!this.isEdit && filesystems.length > 0) {
255 this.nfsForm.patchValue({
257 fs_name: filesystems[0].name
263 fsalChangeHandler() {
264 this.setPathValidation();
265 const fsalValue = this.nfsForm.getValue('name');
266 const checkAvailability =
268 ? this.rgwSiteService.get('realms').pipe(
269 mergeMap((realms: string[]) =>
272 : this.rgwSiteService.isDefaultRealm().pipe(
273 mergeMap((isDefaultRealm) => {
274 if (!isDefaultRealm) {
275 throw new Error('Selected realm is not the default.');
282 : this.nfsService.filesystems();
284 checkAvailability.subscribe({
286 this.setFsalAvailability(fsalValue, true);
288 this.nfsForm.patchValue({
289 path: fsalValue === 'RGW' ? '' : '/',
290 pseudo: this.generatePseudo(),
291 access_type: this.updateAccessType()
295 this.cdRef.detectChanges();
298 this.setFsalAvailability(fsalValue, false, error);
299 this.nfsForm.get('name').setValue('');
304 private setFsalAvailability(fsalValue: string, available: boolean, errorMessage: string = '') {
305 this.allFsals = this.allFsals.map((fsalItem: NfsFSAbstractionLayer) => {
306 if (fsalItem.value === fsalValue) {
307 fsalItem.disabled = !available;
309 this.fsalAvailabilityError = fsalItem.disabled
310 ? $localize`${fsalItem.descr} backend is not available. ${errorMessage}`
317 accessTypeChangeHandler() {
318 const name = this.nfsForm.getValue('name');
319 const accessType = this.nfsForm.getValue('access_type');
320 this.defaultAccessType[name] = accessType;
323 setPathValidation() {
324 const path = this.nfsForm.get('path');
325 path.setValidators([Validators.required]);
326 if (this.nfsForm.getValue('name') === 'RGW') {
327 path.setAsyncValidators([CdValidators.bucketExistence(true, this.rgwBucketService)]);
329 path.setAsyncValidators([this.pathExistence(true)]);
337 getAccessTypeHelp(accessType: string) {
338 const accessTypeItem = this.nfsAccessType.find((currentAccessTypeItem) => {
339 if (accessType === currentAccessTypeItem.value) {
340 return currentAccessTypeItem;
343 return _.isObjectLike(accessTypeItem) ? accessTypeItem.help : '';
348 _.isString(this.nfsForm.getValue('cluster_id')) &&
349 _.isString(this.nfsForm.getValue('path'))
351 return this.nfsForm.getValue('cluster_id') + ':' + this.nfsForm.getValue('path');
356 private getPathTypeahead(path: any) {
357 if (!_.isString(path) || path === '/') {
361 const fsName = this.nfsForm.getValue('fsal').fs_name;
362 return this.nfsService.lsDir(fsName, path).pipe(
363 map((result: Directory) =>
364 result.paths.filter((dirName: string) => dirName.toLowerCase().includes(path)).slice(0, 15)
366 catchError(() => of([$localize`Error while retrieving paths.`]))
370 pathChangeHandler() {
372 this.nfsForm.patchValue({
373 pseudo: this.generatePseudo()
378 private getBucketTypeahead(path: string): Observable<any> {
379 if (_.isString(path) && path !== '/' && path !== '') {
380 return this.rgwBucketService.list().pipe(
383 .filter((bucketName: string) => bucketName.toLowerCase().includes(path))
386 catchError(() => of([$localize`Error while retrieving bucket names.`]))
393 private generatePseudo() {
394 let newPseudo = this.nfsForm.getValue('pseudo');
395 if (this.nfsForm.get('pseudo') && !this.nfsForm.get('pseudo').dirty) {
396 newPseudo = undefined;
397 if (this.nfsForm.getValue('fsal') === 'CEPH') {
398 newPseudo = '/cephfs';
399 if (_.isString(this.nfsForm.getValue('path'))) {
400 newPseudo += this.nfsForm.getValue('path');
407 private updateAccessType() {
408 const name = this.nfsForm.getValue('name');
409 let accessType = this.defaultAccessType[name];
419 let action: Observable<any>;
420 const requestModel = this.buildRequest();
423 action = this.taskWrapper.wrapTaskAroundCall({
424 task: new FinishedTask('nfs/edit', {
425 cluster_id: this.cluster_id,
426 export_id: _.parseInt(this.export_id)
428 call: this.nfsService.update(this.cluster_id, _.parseInt(this.export_id), requestModel)
432 action = this.taskWrapper.wrapTaskAroundCall({
433 task: new FinishedTask('nfs/create', {
434 path: requestModel.path,
435 fsal: requestModel.fsal,
436 cluster_id: requestModel.cluster_id
438 call: this.nfsService.create(requestModel)
443 error: (errorResponse: CdHttpErrorResponse) => this.setFormErrors(errorResponse),
444 complete: () => this.router.navigate(['/nfs'])
448 private setFormErrors(errorResponse: CdHttpErrorResponse) {
450 errorResponse.error.detail &&
451 errorResponse.error.detail
453 .includes(`Pseudo ${this.nfsForm.getValue('pseudo')} is already in use`)
455 this.nfsForm.get('pseudo').setErrors({ pseudoAlreadyExists: true });
457 this.nfsForm.setErrors({ cdSubmitButton: true });
460 private buildRequest() {
461 const requestModel: any = _.cloneDeep(this.nfsForm.value);
464 requestModel.export_id = _.parseInt(this.export_id);
467 if (requestModel.fsal.name === 'RGW') {
468 delete requestModel.fsal.fs_name;
471 requestModel.protocols = [];
472 if (requestModel.protocolNfsv4) {
473 requestModel.protocols.push(4);
475 requestModel.pseudo = null;
477 delete requestModel.protocolNfsv4;
479 requestModel.transports = [];
480 if (requestModel.transportTCP) {
481 requestModel.transports.push('TCP');
483 delete requestModel.transportTCP;
484 if (requestModel.transportUDP) {
485 requestModel.transports.push('UDP');
487 delete requestModel.transportUDP;
489 requestModel.clients.forEach((client: any) => {
490 if (_.isString(client.addresses)) {
491 client.addresses = _(client.addresses)
494 .filter((address) => address !== '')
497 client.addresses = [];
500 if (!client.squash) {
501 client.squash = requestModel.squash;
504 if (!client.access_type) {
505 client.access_type = requestModel.access_type;
509 if (requestModel.security_label === false || requestModel.fsal.name === 'RGW') {
510 requestModel.fsal.sec_label_xattr = null;
512 requestModel.fsal.sec_label_xattr = requestModel.sec_label_xattr;
514 delete requestModel.sec_label_xattr;
519 private pathExistence(requiredExistenceResult: boolean): AsyncValidatorFn {
520 return (control: AbstractControl): Observable<ValidationErrors | null> => {
521 if (control.pristine || !control.value) {
522 return of({ required: true });
524 const fsName = this.nfsForm.getValue('fsal').fs_name;
525 return this.nfsService.lsDir(fsName, control.value).pipe(
526 map((directory: Directory) =>
527 directory.paths.includes(control.value) === requiredExistenceResult
529 : { pathNameNotAllowed: true }
531 catchError(() => of({ pathNameNotAllowed: true }))