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