1 import { Component, OnInit } from '@angular/core';
8 } from '@angular/forms';
10 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
11 import { Subject } from 'rxjs';
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';
24 selector: 'cd-rbd-namespace-form-modal',
25 templateUrl: './rbd-namespace-form-modal.component.html',
26 styleUrls: ['./rbd-namespace-form-modal.component.scss']
28 export class RbdNamespaceFormModalComponent implements OnInit {
29 poolPermission: Permission;
30 pools: Array<Pool> = null;
34 namespaceForm: CdFormGroup;
38 public onSubmit: Subject<void>;
41 public activeModal: NgbActiveModal,
42 private authStorageService: AuthStorageService,
43 private notificationService: NotificationService,
44 private poolService: PoolService,
45 private rbdService: RbdService
47 this.poolPermission = this.authStorageService.getPermissions().pool;
52 this.namespaceForm = new CdFormGroup(
54 pool: new FormControl(''),
55 namespace: new FormControl('')
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 };
70 poolCtrl.setErrors(poolErrors);
71 let namespaceErrors = null;
72 if (!namespaceCtrl.value) {
73 namespaceErrors = { required: true };
75 namespaceCtrl.setErrors(namespaceErrors);
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);
99 this.onSubmit = new Subject();
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') {
110 if (this.pools.length === 1) {
111 const poolName = this.pools[0]['pool_name'];
112 this.namespaceForm.get('pool').setValue(poolName);
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 = {
128 .createNamespace(pool, namespace)
131 this.notificationService.show(
132 NotificationType.success,
133 $localize`Created namespace '${pool}/${namespace}'`
135 this.activeModal.close();
136 this.onSubmit.next();
139 this.namespaceForm.setErrors({ cdSubmitButton: true });