1 import { ChangeDetectorRef, Component, OnInit, ViewChild } from '@angular/core';
2 import { FormControl, Validators } from '@angular/forms';
3 import { ActivatedRoute, Router } from '@angular/router';
5 import { I18n } from '@ngx-translate/i18n-polyfill';
6 import * as _ from 'lodash';
7 import { forkJoin, Observable, of } from 'rxjs';
8 import { map, mergeMap } from 'rxjs/operators';
10 import { NfsService } from '../../../shared/api/nfs.service';
11 import { RgwUserService } from '../../../shared/api/rgw-user.service';
12 import { SelectMessages } from '../../../shared/components/select/select-messages.model';
13 import { SelectOption } from '../../../shared/components/select/select-option.model';
14 import { ActionLabelsI18n } from '../../../shared/constants/app.constants';
15 import { Icons } from '../../../shared/enum/icons.enum';
16 import { CdFormBuilder } from '../../../shared/forms/cd-form-builder';
17 import { CdFormGroup } from '../../../shared/forms/cd-form-group';
18 import { CdValidators } from '../../../shared/forms/cd-validators';
19 import { FinishedTask } from '../../../shared/models/finished-task';
20 import { Permission } from '../../../shared/models/permissions';
21 import { AuthStorageService } from '../../../shared/services/auth-storage.service';
22 import { TaskWrapperService } from '../../../shared/services/task-wrapper.service';
23 import { NFSClusterType } from '../nfs-cluster-type.enum';
24 import { NfsFormClientComponent } from '../nfs-form-client/nfs-form-client.component';
27 selector: 'cd-nfs-form',
28 templateUrl: './nfs-form.component.html',
29 styleUrls: ['./nfs-form.component.scss']
31 export class NfsFormComponent implements OnInit {
32 @ViewChild('nfsClients', { static: true })
33 nfsClients: NfsFormClientComponent;
35 permission: Permission;
39 cluster_id: string = null;
40 clusterType: string = null;
41 export_id: string = null;
43 isNewDirectory = false;
45 isDefaultCluster = false;
47 allClusters: { cluster_id: string; cluster_type: string }[] = null;
52 allRgwUsers: any[] = [];
53 allCephxClients: any[] = null;
54 allFsNames: any[] = null;
56 defaultAccessType = { RGW: 'RO' };
57 nfsAccessType: any[] = this.nfsService.nfsAccessType;
58 nfsSquash: any[] = this.nfsService.nfsSquash;
63 daemonsSelections: SelectOption[] = [];
64 daemonsMessages = new SelectMessages(
65 { noOptions: this.i18n('There are no daemons available.') },
69 pathDataSource: Observable<any> = Observable.create((observer: any) => {
70 observer.next(this.nfsForm.getValue('path'));
72 mergeMap((token: string) => this.getPathTypeahead(token)),
73 map((val: any) => val.paths)
76 bucketDataSource: Observable<any> = Observable.create((observer: any) => {
77 observer.next(this.nfsForm.getValue('path'));
78 }).pipe(mergeMap((token: string) => this.getBucketTypeahead(token)));
81 private authStorageService: AuthStorageService,
82 private nfsService: NfsService,
83 private route: ActivatedRoute,
84 private router: Router,
85 private rgwUserService: RgwUserService,
86 private formBuilder: CdFormBuilder,
87 private taskWrapper: TaskWrapperService,
88 private cdRef: ChangeDetectorRef,
90 public actionLabels: ActionLabelsI18n
92 this.permission = this.authStorageService.getPermissions().pool;
93 this.resource = this.i18n('NFS export');
98 const promises: Observable<any>[] = [
99 this.nfsService.daemon(),
100 this.nfsService.fsals(),
101 this.nfsService.clients(),
102 this.nfsService.filesystems()
105 if (this.router.url.startsWith('/nfs/edit')) {
110 this.action = this.actionLabels.EDIT;
111 this.route.params.subscribe((params: { cluster_id: string; export_id: string }) => {
112 this.cluster_id = decodeURIComponent(params.cluster_id);
113 this.export_id = decodeURIComponent(params.export_id);
114 promises.push(this.nfsService.get(this.cluster_id, this.export_id));
116 this.getData(promises);
118 this.nfsForm.get('cluster_id').disable();
120 this.action = this.actionLabels.CREATE;
121 this.getData(promises);
125 getData(promises: Observable<any>[]) {
126 forkJoin(promises).subscribe((data: any[]) => {
127 this.resolveDaemons(data[0]);
128 this.resolvefsals(data[1]);
129 this.resolveClients(data[2]);
130 this.resolveFilesystems(data[3]);
132 this.resolveModel(data[4]);
138 this.nfsForm = new CdFormGroup({
139 cluster_id: new FormControl('', {
140 validators: [Validators.required]
142 daemons: new FormControl([]),
143 fsal: new CdFormGroup({
144 name: new FormControl('', {
145 validators: [Validators.required]
147 user_id: new FormControl('', {
149 CdValidators.requiredIf({
154 fs_name: new FormControl('', {
156 CdValidators.requiredIf({
161 rgw_user_id: new FormControl('', {
163 CdValidators.requiredIf({
169 path: new FormControl(''),
170 protocolNfsv3: new FormControl(true, {
172 CdValidators.requiredIf({ protocolNfsv4: false }, (value: boolean) => {
177 protocolNfsv4: new FormControl(true, {
179 CdValidators.requiredIf({ protocolNfsv3: false }, (value: boolean) => {
184 tag: new FormControl(''),
185 pseudo: new FormControl('', {
187 CdValidators.requiredIf({ protocolNfsv4: true }),
188 Validators.pattern('^/[^><|&()]*$')
191 access_type: new FormControl('RW', {
192 validators: [Validators.required]
194 squash: new FormControl('', {
195 validators: [Validators.required]
197 transportUDP: new FormControl(true, {
199 CdValidators.requiredIf({ transportTCP: false }, (value: boolean) => {
204 transportTCP: new FormControl(true, {
206 CdValidators.requiredIf({ transportUDP: false }, (value: boolean) => {
211 clients: this.formBuilder.array([]),
212 security_label: new FormControl(false),
213 sec_label_xattr: new FormControl(
215 CdValidators.requiredIf({ security_label: true, 'fsal.name': 'CEPH' })
220 resolveModel(res: any) {
221 if (res.fsal.name === 'CEPH') {
222 res.sec_label_xattr = res.fsal.sec_label_xattr;
225 if (this.clusterType === NFSClusterType.user) {
226 this.daemonsSelections = _.map(
227 this.allDaemons[res.cluster_id],
228 (daemon) => new SelectOption(res.daemons.indexOf(daemon) !== -1, daemon, '')
230 this.daemonsSelections = [...this.daemonsSelections];
233 res.protocolNfsv3 = res.protocols.indexOf(3) !== -1;
234 res.protocolNfsv4 = res.protocols.indexOf(4) !== -1;
235 delete res.protocols;
237 res.transportTCP = res.transports.indexOf('TCP') !== -1;
238 res.transportUDP = res.transports.indexOf('UDP') !== -1;
239 delete res.transports;
241 res.clients.forEach((client: any) => {
243 client.addresses.forEach((address: string) => {
244 addressStr += address + ', ';
246 if (addressStr.length >= 2) {
247 addressStr = addressStr.substring(0, addressStr.length - 2);
249 client.addresses = addressStr;
252 this.nfsForm.patchValue(res);
253 this.setPathValidation();
254 this.nfsClients.resolveModel(res.clients);
257 resolveDaemons(daemons: Record<string, any>) {
258 daemons = _.sortBy(daemons, ['daemon_id']);
259 const clusters = _.groupBy(daemons, 'cluster_id');
261 this.allClusters = [];
262 _.forIn(clusters, (cluster, cluster_id) => {
263 this.allClusters.push({ cluster_id: cluster_id, cluster_type: cluster[0].cluster_type });
264 this.allDaemons[cluster_id] = [];
267 _.forEach(daemons, (daemon) => {
268 this.allDaemons[daemon.cluster_id].push(daemon.daemon_id);
272 const cluster = _.find(this.allClusters, { cluster_id: this.cluster_id });
273 this.clusterType = cluster ? cluster.cluster_type : null;
276 const hasOneCluster = _.isArray(this.allClusters) && this.allClusters.length === 1;
277 this.isDefaultCluster = hasOneCluster && this.allClusters[0].cluster_id === '_default_';
279 this.nfsForm.patchValue({
280 cluster_id: this.allClusters[0].cluster_id
282 this.onClusterChange();
286 resolvefsals(res: string[]) {
287 res.forEach((fsal) => {
288 const fsalItem = this.nfsService.nfsFsal.find((currentFsalItem) => {
289 return fsal === currentFsalItem.value;
292 if (_.isObjectLike(fsalItem)) {
293 this.allFsals.push(fsalItem);
294 if (fsalItem.value === 'RGW') {
295 this.rgwUserService.list().subscribe((result: any) => {
296 result.forEach((user: Record<string, any>) => {
297 if (user.suspended === 0 && user.keys.length > 0) {
298 this.allRgwUsers.push(user.user_id);
306 if (this.allFsals.length === 1 && _.isUndefined(this.nfsForm.getValue('fsal'))) {
307 this.nfsForm.patchValue({
308 fsal: this.allFsals[0]
313 resolveClients(clients: any[]) {
314 this.allCephxClients = clients;
317 resolveFilesystems(filesystems: any[]) {
318 this.allFsNames = filesystems;
319 if (filesystems.length === 1) {
320 this.nfsForm.patchValue({
322 fs_name: filesystems[0].name
328 fsalChangeHandler() {
329 this.nfsForm.patchValue({
330 tag: this._generateTag(),
331 pseudo: this._generatePseudo(),
332 access_type: this._updateAccessType()
335 this.setPathValidation();
337 this.cdRef.detectChanges();
340 accessTypeChangeHandler() {
341 const name = this.nfsForm.getValue('name');
342 const accessType = this.nfsForm.getValue('access_type');
343 this.defaultAccessType[name] = accessType;
346 setPathValidation() {
347 if (this.nfsForm.getValue('name') === 'RGW') {
350 .setValidators([Validators.required, Validators.pattern('^(/|[^/><|&()#?]+)$')]);
354 .setValidators([Validators.required, Validators.pattern('^/[^><|&()?]*$')]);
358 rgwUserIdChangeHandler() {
359 this.nfsForm.patchValue({
360 pseudo: this._generatePseudo()
364 getAccessTypeHelp(accessType: string) {
365 const accessTypeItem = this.nfsAccessType.find((currentAccessTypeItem) => {
366 if (accessType === currentAccessTypeItem.value) {
367 return currentAccessTypeItem;
370 return _.isObjectLike(accessTypeItem) ? accessTypeItem.help : '';
375 _.isString(this.nfsForm.getValue('cluster_id')) &&
376 _.isString(this.nfsForm.getValue('path'))
378 return this.nfsForm.getValue('cluster_id') + ':' + this.nfsForm.getValue('path');
383 getPathTypeahead(path: any) {
384 if (!_.isString(path) || path === '/') {
388 const fsName = this.nfsForm.getValue('fsal').fs_name;
389 return this.nfsService.lsDir(fsName, path);
392 pathChangeHandler() {
393 this.nfsForm.patchValue({
394 pseudo: this._generatePseudo()
397 const path = this.nfsForm.getValue('path');
398 this.getPathTypeahead(path).subscribe((res: any) => {
399 this.isNewDirectory = path !== '/' && res.paths.indexOf(path) === -1;
403 bucketChangeHandler() {
404 this.nfsForm.patchValue({
405 tag: this._generateTag(),
406 pseudo: this._generatePseudo()
409 const bucket = this.nfsForm.getValue('path');
410 this.getBucketTypeahead(bucket).subscribe((res: any) => {
411 this.isNewBucket = bucket !== '' && res.indexOf(bucket) === -1;
415 getBucketTypeahead(path: string): Observable<any> {
416 const rgwUserId = this.nfsForm.getValue('rgw_user_id');
418 if (_.isString(rgwUserId) && _.isString(path) && path !== '/' && path !== '') {
419 return this.nfsService.buckets(rgwUserId);
426 let newTag = this.nfsForm.getValue('tag');
427 if (!this.nfsForm.get('tag').dirty) {
429 if (this.nfsForm.getValue('fsal') === 'RGW') {
430 newTag = this.nfsForm.getValue('path');
437 let newPseudo = this.nfsForm.getValue('pseudo');
438 if (this.nfsForm.get('pseudo') && !this.nfsForm.get('pseudo').dirty) {
439 newPseudo = undefined;
440 if (this.nfsForm.getValue('fsal') === 'CEPH') {
441 newPseudo = '/cephfs';
442 if (_.isString(this.nfsForm.getValue('path'))) {
443 newPseudo += this.nfsForm.getValue('path');
445 } else if (this.nfsForm.getValue('fsal') === 'RGW') {
446 if (_.isString(this.nfsForm.getValue('rgw_user_id'))) {
447 newPseudo = '/' + this.nfsForm.getValue('rgw_user_id');
448 if (_.isString(this.nfsForm.getValue('path'))) {
449 newPseudo += '/' + this.nfsForm.getValue('path');
457 _updateAccessType() {
458 const name = this.nfsForm.getValue('name');
459 let accessType = this.defaultAccessType[name];
469 const cluster_id = this.nfsForm.getValue('cluster_id');
470 const cluster = _.find(this.allClusters, { cluster_id: cluster_id });
471 this.clusterType = cluster ? cluster.cluster_type : null;
472 if (this.clusterType === NFSClusterType.user) {
473 this.daemonsSelections = _.map(
474 this.allDaemons[cluster_id],
475 (daemon) => new SelectOption(false, daemon, '')
477 this.daemonsSelections = [...this.daemonsSelections];
479 this.daemonsSelections = [];
481 this.nfsForm.patchValue({ daemons: [] });
484 removeDaemon(index: number, daemon: string) {
485 this.daemonsSelections.forEach((value) => {
486 if (value.name === daemon) {
487 value.selected = false;
491 const daemons = this.nfsForm.get('daemons');
492 daemons.value.splice(index, 1);
493 daemons.setValue(daemons.value);
498 onDaemonSelection() {
499 this.nfsForm.get('daemons').setValue(this.nfsForm.getValue('daemons'));
502 onToggleAllDaemonsSelection() {
503 const cluster_id = this.nfsForm.getValue('cluster_id');
505 this.nfsForm.getValue('daemons').length === 0 ? this.allDaemons[cluster_id] : [];
506 this.nfsForm.patchValue({ daemons: daemons });
510 let action: Observable<any>;
511 const requestModel = this._buildRequest();
514 action = this.taskWrapper.wrapTaskAroundCall({
515 task: new FinishedTask('nfs/edit', {
516 cluster_id: this.cluster_id,
517 export_id: this.export_id
519 call: this.nfsService.update(this.cluster_id, this.export_id, requestModel)
523 action = this.taskWrapper.wrapTaskAroundCall({
524 task: new FinishedTask('nfs/create', {
525 path: requestModel.path,
526 fsal: requestModel.fsal,
527 cluster_id: requestModel.cluster_id
529 call: this.nfsService.create(requestModel)
535 () => this.nfsForm.setErrors({ cdSubmitButton: true }),
536 () => this.router.navigate(['/nfs'])
541 const requestModel: any = _.cloneDeep(this.nfsForm.value);
543 if (_.isUndefined(requestModel.tag) || requestModel.tag === '') {
544 requestModel.tag = null;
548 requestModel.export_id = this.export_id;
551 if (requestModel.fsal.name === 'CEPH') {
552 delete requestModel.fsal.rgw_user_id;
554 delete requestModel.fsal.fs_name;
555 delete requestModel.fsal.user_id;
558 requestModel.protocols = [];
559 if (requestModel.protocolNfsv3) {
560 requestModel.protocols.push(3);
562 requestModel.tag = null;
564 delete requestModel.protocolNfsv3;
565 if (requestModel.protocolNfsv4) {
566 requestModel.protocols.push(4);
568 requestModel.pseudo = null;
570 delete requestModel.protocolNfsv4;
572 requestModel.transports = [];
573 if (requestModel.transportTCP) {
574 requestModel.transports.push('TCP');
576 delete requestModel.transportTCP;
577 if (requestModel.transportUDP) {
578 requestModel.transports.push('UDP');
580 delete requestModel.transportUDP;
582 requestModel.clients.forEach((client: any) => {
583 if (_.isString(client.addresses)) {
584 client.addresses = _(client.addresses)
587 .filter((address) => address !== '')
590 client.addresses = [];
593 if (!client.squash) {
594 client.squash = requestModel.squash;
597 if (!client.access_type) {
598 client.access_type = requestModel.access_type;
602 if (requestModel.security_label === false || requestModel.fsal.name === 'RGW') {
603 requestModel.fsal.sec_label_xattr = null;
605 requestModel.fsal.sec_label_xattr = requestModel.sec_label_xattr;
607 delete requestModel.sec_label_xattr;