1 import { Component, OnInit } from '@angular/core';
8 } from '@angular/forms';
10 import { I18n } from '@ngx-translate/i18n-polyfill';
11 import { BsModalRef } from 'ngx-bootstrap/modal';
12 import { Subject } from 'rxjs';
14 import { PoolService } from '../../../shared/api/pool.service';
15 import { RbdService } from '../../../shared/api/rbd.service';
16 import { NotificationType } from '../../../shared/enum/notification-type.enum';
17 import { CdFormGroup } from '../../../shared/forms/cd-form-group';
18 import { FinishedTask } from '../../../shared/models/finished-task';
19 import { Permission } from '../../../shared/models/permissions';
20 import { AuthStorageService } from '../../../shared/services/auth-storage.service';
21 import { NotificationService } from '../../../shared/services/notification.service';
22 import { Pool } from '../../pool/pool';
25 selector: 'cd-rbd-namespace-form',
26 templateUrl: './rbd-namespace-form.component.html',
27 styleUrls: ['./rbd-namespace-form.component.scss']
29 export class RbdNamespaceFormComponent implements OnInit {
30 poolPermission: Permission;
31 pools: Array<Pool> = null;
35 namespaceForm: CdFormGroup;
39 public onSubmit: Subject<void>;
42 public modalRef: BsModalRef,
43 private authStorageService: AuthStorageService,
44 private notificationService: NotificationService,
45 private poolService: PoolService,
46 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 this.i18n(`Created namespace '{{pool}}/{{namespace}}'`, {
140 this.modalRef.hide();
141 this.onSubmit.next();
144 this.namespaceForm.setErrors({ cdSubmitButton: true });