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 { 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';
25 selector: 'cd-rbd-namespace-form-modal',
26 templateUrl: './rbd-namespace-form-modal.component.html',
27 styleUrls: ['./rbd-namespace-form-modal.component.scss']
29 export class RbdNamespaceFormModalComponent implements OnInit {
30 poolPermission: Permission;
31 pools: Array<Pool> = null;
35 namespaceForm: CdFormGroup;
39 public onSubmit: Subject<void>;
42 public activeModal: NgbActiveModal,
43 public actionLabels: ActionLabelsI18n,
44 private authStorageService: AuthStorageService,
45 private notificationService: NotificationService,
46 private poolService: PoolService,
47 private rbdService: RbdService
49 this.poolPermission = this.authStorageService.getPermissions().pool;
54 this.namespaceForm = new CdFormGroup(
56 pool: new FormControl(''),
57 namespace: new FormControl('')
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 };
72 poolCtrl.setErrors(poolErrors);
73 let namespaceErrors = null;
74 if (!namespaceCtrl.value) {
75 namespaceErrors = { required: true };
77 namespaceCtrl.setErrors(namespaceErrors);
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);
101 this.onSubmit = new Subject();
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') {
112 if (this.pools.length === 1) {
113 const poolName = this.pools[0]['pool_name'];
114 this.namespaceForm.get('pool').setValue(poolName);
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 = {
130 .createNamespace(pool, namespace)
133 this.notificationService.show(
134 NotificationType.success,
135 $localize`Created namespace '${pool}/${namespace}'`
137 this.activeModal.close();
138 this.onSubmit.next();
141 this.namespaceForm.setErrors({ cdSubmitButton: true });