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 { CdFormBuilder } from '../../../shared/forms/cd-form-builder';
16 import { CdFormGroup } from '../../../shared/forms/cd-form-group';
17 import { CdValidators } from '../../../shared/forms/cd-validators';
18 import { FinishedTask } from '../../../shared/models/finished-task';
19 import { Permission } from '../../../shared/models/permissions';
20 import { CephReleaseNamePipe } from '../../../shared/pipes/ceph-release-name.pipe';
21 import { AuthStorageService } from '../../../shared/services/auth-storage.service';
22 import { SummaryService } from '../../../shared/services/summary.service';
23 import { TaskWrapperService } from '../../../shared/services/task-wrapper.service';
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')
33 nfsClients: NfsFormClientComponent;
35 permission: Permission;
42 isNewDirectory = false;
44 isDefaultCluster = false;
46 allClusters: string[] = null;
50 allRgwUsers: any[] = [];
51 allCephxClients: any[] = null;
52 allFsNames: any[] = null;
54 defaultAccessType = { RGW: 'RO' };
55 nfsAccessType: any[] = this.nfsService.nfsAccessType;
56 nfsSquash: any[] = this.nfsService.nfsSquash;
62 daemonsSelections: SelectOption[] = [];
63 daemonsMessages = new SelectMessages(
64 { noOptions: this.i18n('There are no daemons available.') },
68 pathDataSource: Observable<any> = Observable.create((observer: any) => {
69 observer.next(this.nfsForm.getValue('path'));
71 mergeMap((token: string) => this.getPathTypeahead(token)),
72 map((val: any) => val.paths)
75 bucketDataSource: Observable<any> = Observable.create((observer: any) => {
76 observer.next(this.nfsForm.getValue('path'));
77 }).pipe(mergeMap((token: string) => this.getBucketTypeahead(token)));
80 private authStorageService: AuthStorageService,
81 private nfsService: NfsService,
82 private route: ActivatedRoute,
83 private router: Router,
84 private rgwUserService: RgwUserService,
85 private formBuilder: CdFormBuilder,
86 private summaryservice: SummaryService,
87 private cephReleaseNamePipe: CephReleaseNamePipe,
88 private taskWrapper: TaskWrapperService,
89 private cdRef: ChangeDetectorRef,
91 public actionLabels: ActionLabelsI18n
93 this.permission = this.authStorageService.getPermissions().pool;
94 this.resource = this.i18n('NFS export');
99 const promises: any[] = [
100 this.nfsService.daemon(),
101 this.nfsService.fsals(),
102 this.nfsService.clients(),
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);
120 this.action = this.actionLabels.CREATE;
121 this.getData(promises);
124 const summary = this.summaryservice.getCurrentSummary();
125 const releaseName = this.cephReleaseNamePipe.transform(summary.version);
126 this.docsUrl = `http://docs.ceph.com/docs/${releaseName}/radosgw/nfs/`;
130 forkJoin(promises).subscribe((data: any[]) => {
131 this.resolveDaemons(data[0]);
132 this.resolvefsals(data[1]);
133 this.resolveClients(data[2]);
134 this.resolveFilesystems(data[3]);
136 this.resolveModel(data[4]);
142 this.nfsForm = new CdFormGroup({
143 cluster_id: new FormControl('', {
144 validators: [Validators.required]
146 daemons: new FormControl([]),
147 fsal: new CdFormGroup({
148 name: new FormControl('', {
149 validators: [Validators.required]
151 user_id: new FormControl('', {
153 CdValidators.requiredIf({
158 fs_name: new FormControl('', {
160 CdValidators.requiredIf({
165 rgw_user_id: new FormControl('', {
167 CdValidators.requiredIf({
173 path: new FormControl(''),
174 protocolNfsv3: new FormControl(true, {
176 CdValidators.requiredIf({ protocolNfsv4: false }, (value) => {
181 protocolNfsv4: new FormControl(true, {
183 CdValidators.requiredIf({ protocolNfsv3: false }, (value) => {
188 tag: new FormControl(''),
189 pseudo: new FormControl('', {
191 CdValidators.requiredIf({ protocolNfsv4: true }),
192 Validators.pattern('^/[^><|&()]*$')
195 access_type: new FormControl('RW', {
196 validators: [Validators.required]
198 squash: new FormControl('', {
199 validators: [Validators.required]
201 transportUDP: new FormControl(true, {
203 CdValidators.requiredIf({ transportTCP: false }, (value) => {
208 transportTCP: new FormControl(true, {
210 CdValidators.requiredIf({ transportUDP: false }, (value) => {
215 clients: this.formBuilder.array([]),
216 security_label: new FormControl(false),
217 sec_label_xattr: new FormControl(
219 CdValidators.requiredIf({ security_label: true, 'fsal.name': 'CEPH' })
225 if (res.fsal.name === 'CEPH') {
226 res.sec_label_xattr = res.fsal.sec_label_xattr;
229 this.daemonsSelections = _.map(
230 this.allDaemons[res.cluster_id],
231 (daemon) => new SelectOption(res.daemons.indexOf(daemon) !== -1, daemon, '')
233 this.daemonsSelections = [...this.daemonsSelections];
235 res.protocolNfsv3 = res.protocols.indexOf(3) !== -1;
236 res.protocolNfsv4 = res.protocols.indexOf(4) !== -1;
237 delete res.protocols;
239 res.transportTCP = res.transports.indexOf('TCP') !== -1;
240 res.transportUDP = res.transports.indexOf('UDP') !== -1;
241 delete res.transports;
243 res.clients.forEach((client) => {
245 client.addresses.forEach((address) => {
246 addressStr += address + ', ';
248 if (addressStr.length >= 2) {
249 addressStr = addressStr.substring(0, addressStr.length - 2);
251 client.addresses = addressStr;
254 this.nfsForm.patchValue(res);
255 this.setPathValidation();
256 this.nfsClients.resolveModel(res.clients);
259 resolveDaemons(daemons) {
260 daemons = _.sortBy(daemons, ['daemon_id']);
262 this.allClusters = _(daemons)
263 .map((daemon) => daemon.cluster_id)
267 _.forEach(this.allClusters, (cluster) => {
268 this.allDaemons[cluster] = [];
271 _.forEach(daemons, (daemon) => {
272 this.allDaemons[daemon.cluster_id].push(daemon.daemon_id);
275 const hasOneCluster = _.isArray(this.allClusters) && this.allClusters.length === 1;
276 this.isDefaultCluster = hasOneCluster && this.allClusters[0] === '_default_';
278 this.nfsForm.patchValue({
279 cluster_id: this.allClusters[0]
281 this.onClusterChange();
285 resolvefsals(res: string[]) {
286 res.forEach((fsal) => {
287 const fsalItem = this.nfsService.nfsFsal.find((currentFsalItem) => {
288 return fsal === currentFsalItem.value;
291 if (_.isObjectLike(fsalItem)) {
292 this.allFsals.push(fsalItem);
293 if (fsalItem.value === 'RGW') {
294 this.rgwUserService.list().subscribe((result: any) => {
295 result.forEach((user) => {
296 if (user.suspended === 0 && user.keys.length > 0) {
297 this.allRgwUsers.push(user.user_id);
305 if (this.allFsals.length === 1 && _.isUndefined(this.nfsForm.getValue('fsal'))) {
306 this.nfsForm.patchValue({
307 fsal: this.allFsals[0]
312 resolveClients(clients) {
313 this.allCephxClients = clients;
316 resolveFilesystems(filesystems) {
317 this.allFsNames = filesystems;
318 if (filesystems.length === 1) {
319 this.nfsForm.patchValue({
321 fs_name: filesystems[0].name
327 fsalChangeHandler() {
328 this.nfsForm.patchValue({
329 tag: this._generateTag(),
330 pseudo: this._generatePseudo(),
331 access_type: this._updateAccessType()
334 this.setPathValidation();
336 this.cdRef.detectChanges();
339 accessTypeChangeHandler() {
340 const name = this.nfsForm.getValue('name');
341 const accessType = this.nfsForm.getValue('access_type');
342 this.defaultAccessType[name] = accessType;
345 setPathValidation() {
346 if (this.nfsForm.getValue('name') === 'RGW') {
349 .setValidators([Validators.required, Validators.pattern('^(/|[^/><|&()#?]+)$')]);
353 .setValidators([Validators.required, Validators.pattern('^/[^><|&()?]*$')]);
357 rgwUserIdChangeHandler() {
358 this.nfsForm.patchValue({
359 pseudo: this._generatePseudo()
363 getAccessTypeHelp(accessType) {
364 const accessTypeItem = this.nfsAccessType.find((currentAccessTypeItem) => {
365 if (accessType === currentAccessTypeItem.value) {
366 return currentAccessTypeItem;
369 return _.isObjectLike(accessTypeItem) ? accessTypeItem.help : '';
374 _.isString(this.nfsForm.getValue('cluster_id')) &&
375 _.isString(this.nfsForm.getValue('path'))
377 return this.nfsForm.getValue('cluster_id') + ':' + this.nfsForm.getValue('path');
382 getPathTypeahead(path) {
383 if (!_.isString(path) || path === '/') {
387 return this.nfsService.lsDir(path);
390 pathChangeHandler() {
391 this.nfsForm.patchValue({
392 pseudo: this._generatePseudo()
395 const path = this.nfsForm.getValue('path');
396 this.getPathTypeahead(path).subscribe((res: any) => {
397 this.isNewDirectory = path !== '/' && res.paths.indexOf(path) === -1;
401 bucketChangeHandler() {
402 this.nfsForm.patchValue({
403 tag: this._generateTag(),
404 pseudo: this._generatePseudo()
407 const bucket = this.nfsForm.getValue('path');
408 this.getBucketTypeahead(bucket).subscribe((res: any) => {
409 this.isNewBucket = bucket !== '' && res.indexOf(bucket) === -1;
413 getBucketTypeahead(path: string): Observable<any> {
414 const rgwUserId = this.nfsForm.getValue('rgw_user_id');
416 if (_.isString(rgwUserId) && _.isString(path) && path !== '/' && path !== '') {
417 return this.nfsService.buckets(rgwUserId);
424 let newTag = this.nfsForm.getValue('tag');
425 if (!this.nfsForm.get('tag').dirty) {
427 if (this.nfsForm.getValue('fsal') === 'RGW') {
428 newTag = this.nfsForm.getValue('path');
435 let newPseudo = this.nfsForm.getValue('pseudo');
436 if (this.nfsForm.get('pseudo') && !this.nfsForm.get('pseudo').dirty) {
437 newPseudo = undefined;
438 if (this.nfsForm.getValue('fsal') === 'CEPH') {
439 newPseudo = '/cephfs';
440 if (_.isString(this.nfsForm.getValue('path'))) {
441 newPseudo += this.nfsForm.getValue('path');
443 } else if (this.nfsForm.getValue('fsal') === 'RGW') {
444 if (_.isString(this.nfsForm.getValue('rgw_user_id'))) {
445 newPseudo = '/' + this.nfsForm.getValue('rgw_user_id');
446 if (_.isString(this.nfsForm.getValue('path'))) {
447 newPseudo += '/' + this.nfsForm.getValue('path');
455 _updateAccessType() {
456 const name = this.nfsForm.getValue('name');
457 let accessType = this.defaultAccessType[name];
467 const cluster_id = this.nfsForm.getValue('cluster_id');
468 this.daemonsSelections = _.map(
469 this.allDaemons[cluster_id],
470 (daemon) => new SelectOption(false, daemon, '')
472 this.daemonsSelections = [...this.daemonsSelections];
473 this.nfsForm.patchValue({ daemons: [] });
476 removeDaemon(index, daemon) {
477 this.daemonsSelections.forEach((value) => {
478 if (value.name === daemon) {
479 value.selected = false;
483 const daemons = this.nfsForm.get('daemons');
484 daemons.value.splice(index, 1);
485 daemons.setValue(daemons.value);
490 onDaemonSelection() {
491 this.nfsForm.get('daemons').setValue(this.nfsForm.getValue('daemons'));
495 let action: Observable<any>;
496 const requestModel = this._buildRequest();
499 action = this.taskWrapper.wrapTaskAroundCall({
500 task: new FinishedTask('nfs/edit', {
501 cluster_id: this.cluster_id,
502 export_id: this.export_id
504 call: this.nfsService.update(this.cluster_id, this.export_id, requestModel)
508 action = this.taskWrapper.wrapTaskAroundCall({
509 task: new FinishedTask('nfs/create', {
510 path: requestModel.path,
511 fsal: requestModel.fsal,
512 cluster_id: requestModel.cluster_id
514 call: this.nfsService.create(requestModel)
520 () => this.nfsForm.setErrors({ cdSubmitButton: true }),
521 () => this.router.navigate(['/nfs'])
526 const requestModel: any = _.cloneDeep(this.nfsForm.value);
528 if (_.isUndefined(requestModel.tag) || requestModel.tag === '') {
529 requestModel.tag = null;
533 requestModel.export_id = this.export_id;
536 if (requestModel.fsal.name === 'CEPH') {
537 delete requestModel.fsal.rgw_user_id;
539 delete requestModel.fsal.fs_name;
540 delete requestModel.fsal.user_id;
543 requestModel.protocols = [];
544 if (requestModel.protocolNfsv3) {
545 requestModel.protocols.push(3);
547 requestModel.tag = null;
549 delete requestModel.protocolNfsv3;
550 if (requestModel.protocolNfsv4) {
551 requestModel.protocols.push(4);
553 requestModel.pseudo = null;
555 delete requestModel.protocolNfsv4;
557 requestModel.transports = [];
558 if (requestModel.transportTCP) {
559 requestModel.transports.push('TCP');
561 delete requestModel.transportTCP;
562 if (requestModel.transportUDP) {
563 requestModel.transports.push('UDP');
565 delete requestModel.transportUDP;
567 requestModel.clients.forEach((client) => {
568 if (_.isString(client.addresses)) {
569 client.addresses = _(client.addresses)
572 .filter((address) => address !== '')
575 client.addresses = [];
578 if (!client.squash) {
579 client.squash = requestModel.squash;
582 if (!client.access_type) {
583 client.access_type = requestModel.access_type;
587 if (requestModel.security_label === false || requestModel.fsal.name === 'RGW') {
588 requestModel.fsal.sec_label_xattr = null;
590 requestModel.fsal.sec_label_xattr = requestModel.sec_label_xattr;
592 delete requestModel.sec_label_xattr;