]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
584caa88442f4f3bfc69960982ad124c1fd0d0e2
[ceph.git] /
1 import { Component, OnInit } from '@angular/core';
2 import {
3   AbstractControl,
4   AsyncValidatorFn,
5   UntypedFormControl,
6   ValidationErrors,
7   ValidatorFn
8 } from '@angular/forms';
9
10 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
11 import { Subject } from 'rxjs';
12
13 import { Pool } from '~/app/ceph/pool/pool';
14 import { PoolService } from '~/app/shared/api/pool.service';
15 import { RbdService } from '~/app/shared/api/rbd.service';
16 import { ActionLabelsI18n } from '~/app/shared/constants/app.constants';
17 import { NotificationType } from '~/app/shared/enum/notification-type.enum';
18 import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
19 import { FinishedTask } from '~/app/shared/models/finished-task';
20 import { Permission } from '~/app/shared/models/permissions';
21 import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
22 import { NotificationService } from '~/app/shared/services/notification.service';
23
24 @Component({
25   selector: 'cd-rbd-namespace-form-modal',
26   templateUrl: './rbd-namespace-form-modal.component.html',
27   styleUrls: ['./rbd-namespace-form-modal.component.scss']
28 })
29 export class RbdNamespaceFormModalComponent implements OnInit {
30   poolPermission: Permission;
31   pools: Array<Pool> = null;
32   pool: string;
33   namespace: string;
34
35   namespaceForm: CdFormGroup;
36
37   editing = false;
38
39   public onSubmit: Subject<void>;
40
41   constructor(
42     public activeModal: NgbActiveModal,
43     public actionLabels: ActionLabelsI18n,
44     private authStorageService: AuthStorageService,
45     private notificationService: NotificationService,
46     private poolService: PoolService,
47     private rbdService: RbdService
48   ) {
49     this.poolPermission = this.authStorageService.getPermissions().pool;
50     this.createForm();
51   }
52
53   createForm() {
54     this.namespaceForm = new CdFormGroup(
55       {
56         pool: new UntypedFormControl(''),
57         namespace: new UntypedFormControl('')
58       },
59       this.validator(),
60       this.asyncValidator()
61     );
62   }
63
64   validator(): ValidatorFn {
65     return (control: AbstractControl) => {
66       const poolCtrl = control.get('pool');
67       const namespaceCtrl = control.get('namespace');
68       let poolErrors = null;
69       if (!poolCtrl.value) {
70         poolErrors = { required: true };
71       }
72       poolCtrl.setErrors(poolErrors);
73       let namespaceErrors = null;
74       if (!namespaceCtrl.value) {
75         namespaceErrors = { required: true };
76       }
77       namespaceCtrl.setErrors(namespaceErrors);
78       return null;
79     };
80   }
81
82   asyncValidator(): AsyncValidatorFn {
83     return (control: AbstractControl): Promise<ValidationErrors | null> => {
84       return new Promise((resolve) => {
85         const poolCtrl = control.get('pool');
86         const namespaceCtrl = control.get('namespace');
87         this.rbdService.listNamespaces(poolCtrl.value).subscribe((namespaces: any[]) => {
88           if (namespaces.some((ns) => ns.namespace === namespaceCtrl.value)) {
89             const error = { namespaceExists: true };
90             namespaceCtrl.setErrors(error);
91             resolve(error);
92           } else {
93             resolve(null);
94           }
95         });
96       });
97     };
98   }
99
100   ngOnInit() {
101     this.onSubmit = new Subject();
102
103     if (this.poolPermission.read) {
104       this.poolService.list(['pool_name', 'type', 'application_metadata']).then((resp) => {
105         const pools: Pool[] = [];
106         for (const pool of resp) {
107           if (this.rbdService.isRBDPool(pool) && pool.type === 'replicated') {
108             pools.push(pool);
109           }
110         }
111         this.pools = pools;
112         if (this.pools.length === 1) {
113           const poolName = this.pools[0]['pool_name'];
114           this.namespaceForm.get('pool').setValue(poolName);
115         }
116       });
117     }
118   }
119
120   submit() {
121     const pool = this.namespaceForm.getValue('pool');
122     const namespace = this.namespaceForm.getValue('namespace');
123     const finishedTask = new FinishedTask();
124     finishedTask.name = 'rbd/namespace/create';
125     finishedTask.metadata = {
126       pool: pool,
127       namespace: namespace
128     };
129     this.rbdService
130       .createNamespace(pool, namespace)
131       .toPromise()
132       .then(() => {
133         this.notificationService.show(
134           NotificationType.success,
135           $localize`Created namespace '${pool}/${namespace}'`
136         );
137         this.activeModal.close();
138         this.onSubmit.next();
139       })
140       .catch(() => {
141         this.namespaceForm.setErrors({ cdSubmitButton: true });
142       });
143   }
144 }