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[] = [];
59 nfsSquash: any[] = [];
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`;
99 this.nfsAccessType = this.nfsService.nfsAccessType;
100 this.nfsSquash = Object.keys(this.nfsService.nfsSquash);
102 const promises: Observable<any>[] = [
103 this.nfsService.listClusters(),
104 this.nfsService.fsals(),
105 this.nfsService.filesystems()
108 if (this.router.url.startsWith('/nfs/edit')) {
113 this.action = this.actionLabels.EDIT;
114 this.route.params.subscribe((params: { cluster_id: string; export_id: string }) => {
115 this.cluster_id = decodeURIComponent(params.cluster_id);
116 this.export_id = decodeURIComponent(params.export_id);
117 promises.push(this.nfsService.get(this.cluster_id, this.export_id));
119 this.getData(promises);
121 this.nfsForm.get('cluster_id').disable();
123 this.action = this.actionLabels.CREATE;
124 this.getData(promises);
128 getData(promises: Observable<any>[]) {
129 forkJoin(promises).subscribe((data: any[]) => {
130 this.resolveClusters(data[0]);
131 this.resolveFsals(data[1]);
132 this.resolveFilesystems(data[2]);
134 this.resolveModel(data[3]);
142 this.nfsForm = new CdFormGroup({
143 cluster_id: new UntypedFormControl('', {
144 validators: [Validators.required]
146 fsal: new CdFormGroup({
147 name: new UntypedFormControl('', {
148 validators: [Validators.required]
150 fs_name: new UntypedFormControl('', {
152 CdValidators.requiredIf({
158 path: new UntypedFormControl('/'),
159 protocolNfsv4: new UntypedFormControl(true),
160 pseudo: new UntypedFormControl('', {
162 CdValidators.requiredIf({ protocolNfsv4: true }),
163 Validators.pattern('^/[^><|&()]*$')
166 access_type: new UntypedFormControl('RW'),
167 squash: new UntypedFormControl(this.nfsSquash[0]),
168 transportUDP: new UntypedFormControl(true, {
170 CdValidators.requiredIf({ transportTCP: false }, (value: boolean) => {
175 transportTCP: new UntypedFormControl(true, {
177 CdValidators.requiredIf({ transportUDP: false }, (value: boolean) => {
182 clients: this.formBuilder.array([]),
183 security_label: new UntypedFormControl(false),
184 sec_label_xattr: new UntypedFormControl(
186 CdValidators.requiredIf({ security_label: true, 'fsal.name': 'CEPH' })
191 resolveModel(res: any) {
192 if (res.fsal.name === 'CEPH') {
193 res.sec_label_xattr = res.fsal.sec_label_xattr;
196 res.protocolNfsv4 = res.protocols.indexOf(4) !== -1;
197 delete res.protocols;
199 res.transportTCP = res.transports.indexOf('TCP') !== -1;
200 res.transportUDP = res.transports.indexOf('UDP') !== -1;
201 delete res.transports;
203 Object.entries(this.nfsService.nfsSquash).forEach(([key, value]) => {
204 if (value.includes(res.squash)) {
209 res.clients.forEach((client: any) => {
211 client.addresses.forEach((address: string) => {
212 addressStr += address + ', ';
214 if (addressStr.length >= 2) {
215 addressStr = addressStr.substring(0, addressStr.length - 2);
217 client.addresses = addressStr;
220 this.nfsForm.patchValue(res);
221 this.setPathValidation();
222 this.clients = res.clients;
225 resolveClusters(clusters: string[]) {
226 this.allClusters = [];
227 for (const cluster of clusters) {
228 this.allClusters.push({ cluster_id: cluster });
230 if (!this.isEdit && this.allClusters.length > 0) {
231 this.nfsForm.get('cluster_id').setValue(this.allClusters[0].cluster_id);
235 resolveFsals(res: string[]) {
236 res.forEach((fsal) => {
237 const fsalItem = this.nfsService.nfsFsal.find((currentFsalItem) => {
238 return fsal === currentFsalItem.value;
241 if (_.isObjectLike(fsalItem)) {
242 this.allFsals.push(fsalItem);
245 if (!this.isEdit && this.allFsals.length > 0) {
246 this.nfsForm.patchValue({
248 name: this.allFsals[0].value
254 resolveFilesystems(filesystems: any[]) {
255 this.allFsNames = filesystems;
256 if (!this.isEdit && filesystems.length > 0) {
257 this.nfsForm.patchValue({
259 fs_name: filesystems[0].name
265 fsalChangeHandler() {
266 this.setPathValidation();
267 const fsalValue = this.nfsForm.getValue('name');
268 const checkAvailability =
270 ? this.rgwSiteService.get('realms').pipe(
271 mergeMap((realms: string[]) =>
274 : this.rgwSiteService.isDefaultRealm().pipe(
275 mergeMap((isDefaultRealm) => {
276 if (!isDefaultRealm) {
277 throw new Error('Selected realm is not the default.');
284 : this.nfsService.filesystems();
286 checkAvailability.subscribe({
288 this.setFsalAvailability(fsalValue, true);
290 this.nfsForm.patchValue({
291 path: fsalValue === 'RGW' ? '' : '/',
292 pseudo: this.generatePseudo(),
293 access_type: this.updateAccessType()
297 this.cdRef.detectChanges();
300 this.setFsalAvailability(fsalValue, false, error);
301 this.nfsForm.get('name').setValue('');
306 private setFsalAvailability(fsalValue: string, available: boolean, errorMessage: string = '') {
307 this.allFsals = this.allFsals.map((fsalItem: NfsFSAbstractionLayer) => {
308 if (fsalItem.value === fsalValue) {
309 fsalItem.disabled = !available;
311 this.fsalAvailabilityError = fsalItem.disabled
312 ? $localize`${fsalItem.descr} backend is not available. ${errorMessage}`
319 accessTypeChangeHandler() {
320 const name = this.nfsForm.getValue('name');
321 const accessType = this.nfsForm.getValue('access_type');
322 this.defaultAccessType[name] = accessType;
325 setPathValidation() {
326 const path = this.nfsForm.get('path');
327 path.setValidators([Validators.required]);
328 if (this.nfsForm.getValue('name') === 'RGW') {
329 path.setAsyncValidators([CdValidators.bucketExistence(true, this.rgwBucketService)]);
331 path.setAsyncValidators([this.pathExistence(true)]);
339 getAccessTypeHelp(accessType: string) {
340 const accessTypeItem = this.nfsAccessType.find((currentAccessTypeItem) => {
341 if (accessType === currentAccessTypeItem.value) {
342 return currentAccessTypeItem;
345 return _.isObjectLike(accessTypeItem) ? accessTypeItem.help : '';
350 _.isString(this.nfsForm.getValue('cluster_id')) &&
351 _.isString(this.nfsForm.getValue('path'))
353 return this.nfsForm.getValue('cluster_id') + ':' + this.nfsForm.getValue('path');
358 private getPathTypeahead(path: any) {
359 if (!_.isString(path) || path === '/') {
363 const fsName = this.nfsForm.getValue('fsal').fs_name;
364 return this.nfsService.lsDir(fsName, path).pipe(
365 map((result: Directory) =>
366 result.paths.filter((dirName: string) => dirName.toLowerCase().includes(path)).slice(0, 15)
368 catchError(() => of([$localize`Error while retrieving paths.`]))
372 pathChangeHandler() {
374 this.nfsForm.patchValue({
375 pseudo: this.generatePseudo()
380 private getBucketTypeahead(path: string): Observable<any> {
381 if (_.isString(path) && path !== '/' && path !== '') {
382 return this.rgwBucketService.list().pipe(
385 .filter((bucketName: string) => bucketName.toLowerCase().includes(path))
388 catchError(() => of([$localize`Error while retrieving bucket names.`]))
395 private generatePseudo() {
396 let newPseudo = this.nfsForm.getValue('pseudo');
397 if (this.nfsForm.get('pseudo') && !this.nfsForm.get('pseudo').dirty) {
398 newPseudo = undefined;
399 if (this.nfsForm.getValue('fsal') === 'CEPH') {
400 newPseudo = '/cephfs';
401 if (_.isString(this.nfsForm.getValue('path'))) {
402 newPseudo += this.nfsForm.getValue('path');
409 private updateAccessType() {
410 const name = this.nfsForm.getValue('name');
411 let accessType = this.defaultAccessType[name];
421 let action: Observable<any>;
422 const requestModel = this.buildRequest();
425 action = this.taskWrapper.wrapTaskAroundCall({
426 task: new FinishedTask('nfs/edit', {
427 cluster_id: this.cluster_id,
428 export_id: _.parseInt(this.export_id)
430 call: this.nfsService.update(this.cluster_id, _.parseInt(this.export_id), requestModel)
434 action = this.taskWrapper.wrapTaskAroundCall({
435 task: new FinishedTask('nfs/create', {
436 path: requestModel.path,
437 fsal: requestModel.fsal,
438 cluster_id: requestModel.cluster_id
440 call: this.nfsService.create(requestModel)
445 error: (errorResponse: CdHttpErrorResponse) => this.setFormErrors(errorResponse),
446 complete: () => this.router.navigate(['/nfs'])
450 private setFormErrors(errorResponse: CdHttpErrorResponse) {
452 errorResponse.error.detail &&
453 errorResponse.error.detail
455 .includes(`Pseudo ${this.nfsForm.getValue('pseudo')} is already in use`)
457 this.nfsForm.get('pseudo').setErrors({ pseudoAlreadyExists: true });
459 this.nfsForm.setErrors({ cdSubmitButton: true });
462 private buildRequest() {
463 const requestModel: any = _.cloneDeep(this.nfsForm.value);
466 requestModel.export_id = _.parseInt(this.export_id);
469 if (requestModel.fsal.name === 'RGW') {
470 delete requestModel.fsal.fs_name;
473 requestModel.protocols = [];
474 if (requestModel.protocolNfsv4) {
475 requestModel.protocols.push(4);
477 requestModel.pseudo = null;
479 delete requestModel.protocolNfsv4;
481 requestModel.transports = [];
482 if (requestModel.transportTCP) {
483 requestModel.transports.push('TCP');
485 delete requestModel.transportTCP;
486 if (requestModel.transportUDP) {
487 requestModel.transports.push('UDP');
489 delete requestModel.transportUDP;
491 requestModel.clients.forEach((client: any) => {
492 if (_.isString(client.addresses)) {
493 client.addresses = _(client.addresses)
496 .filter((address) => address !== '')
499 client.addresses = [];
502 if (!client.squash) {
503 client.squash = requestModel.squash;
506 if (!client.access_type) {
507 client.access_type = requestModel.access_type;
511 if (requestModel.security_label === false || requestModel.fsal.name === 'RGW') {
512 requestModel.fsal.sec_label_xattr = null;
514 requestModel.fsal.sec_label_xattr = requestModel.sec_label_xattr;
516 delete requestModel.sec_label_xattr;
521 private pathExistence(requiredExistenceResult: boolean): AsyncValidatorFn {
522 return (control: AbstractControl): Observable<ValidationErrors | null> => {
523 if (control.pristine || !control.value) {
524 return of({ required: true });
526 const fsName = this.nfsForm.getValue('fsal').fs_name;
527 return this.nfsService.lsDir(fsName, control.value).pipe(
528 map((directory: Directory) =>
529 directory.paths.includes(control.value) === requiredExistenceResult
531 : { pathNameNotAllowed: true }
533 catchError(() => of({ pathNameNotAllowed: true }))