9 } from '@angular/core';
10 import { UntypedFormControl, UntypedFormGroup, ValidatorFn, Validators } from '@angular/forms';
12 import { BaseModal } from 'carbon-components-angular';
13 import _ from 'lodash';
14 import { concat, forkJoin, Subscription } from 'rxjs';
15 import { last, tap } from 'rxjs/operators';
17 import { Pool } from '~/app/ceph/pool/pool';
18 import { RbdMirroringService } from '~/app/shared/api/rbd-mirroring.service';
19 import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
20 import { FinishedTask } from '~/app/shared/models/finished-task';
21 import { TaskWrapperService } from '~/app/shared/services/task-wrapper.service';
24 selector: 'cd-bootstrap-create-modal',
25 templateUrl: './bootstrap-create-modal.component.html',
26 styleUrls: ['./bootstrap-create-modal.component.scss']
28 export class BootstrapCreateModalComponent
30 implements OnDestroy, OnInit, AfterViewInit {
36 createBootstrapForm: CdFormGroup;
39 private rbdMirroringService: RbdMirroringService,
40 private taskWrapper: TaskWrapperService,
41 private changeDetectorRef: ChangeDetectorRef,
43 @Inject('siteName') @Optional() public siteName?: string
49 ngAfterViewInit(): void {
50 this.changeDetectorRef.detectChanges();
54 this.createBootstrapForm = new CdFormGroup({
55 siteName: new UntypedFormControl('', {
56 validators: [Validators.required]
58 pools: new UntypedFormGroup(
61 validators: [this.validatePools()]
64 token: new UntypedFormControl('', {})
69 this.createBootstrapForm.get('siteName').setValue(this.siteName);
70 this.rbdMirroringService.getSiteName().subscribe((response: any) => {
71 this.createBootstrapForm.get('siteName').setValue(response.site_name);
74 this.subs = this.rbdMirroringService.subscribeSummary((data) => {
75 const pools = data.content_data.pools;
76 this.pools = pools.reduce((acc: any[], pool: Pool) => {
79 mirror_mode: pool['mirror_mode']
84 const poolsControl = this.createBootstrapForm.get('pools') as UntypedFormGroup;
85 _.each(this.pools, (pool) => {
86 const poolName = pool['name'];
87 const mirroring_disabled = pool['mirror_mode'] === 'disabled';
88 const control = poolsControl.controls[poolName];
90 if (mirroring_disabled && control.disabled) {
92 } else if (!mirroring_disabled && control.enabled) {
94 control.setValue(true);
97 poolsControl.addControl(
99 new UntypedFormControl({ value: !mirroring_disabled, disabled: !mirroring_disabled })
108 this.subs.unsubscribe();
112 validatePools(): ValidatorFn {
113 return (poolsControl: UntypedFormGroup): { [key: string]: any } => {
114 let checkedCount = 0;
115 _.each(poolsControl.controls, (control) => {
116 if (control.value === true) {
121 if (checkedCount > 0) {
125 return { requirePool: true };
130 this.createBootstrapForm.get('token').setValue('');
132 let bootstrapPoolName = '';
133 const poolNames: string[] = [];
134 const poolsControl = this.createBootstrapForm.get('pools') as UntypedFormGroup;
135 _.each(poolsControl.controls, (control, poolName) => {
136 if (control.value === true) {
137 bootstrapPoolName = poolName;
138 if (!control.disabled) {
139 poolNames.push(poolName);
144 const poolModeRequest = {
148 const apiActionsObs = concat(
149 this.rbdMirroringService.setSiteName(this.createBootstrapForm.getValue('siteName')),
151 poolNames.map((poolName) => this.rbdMirroringService.updatePool(poolName, poolModeRequest))
153 this.rbdMirroringService
154 .createBootstrapToken(bootstrapPoolName)
155 .pipe(tap((data: any) => this.createBootstrapForm.get('token').setValue(data['token'])))
158 const finishHandler = () => {
159 this.rbdMirroringService.refresh();
160 this.createBootstrapForm.setErrors({ cdSubmitButton: true });
163 const taskObs = this.taskWrapper.wrapTaskAroundCall({
164 task: new FinishedTask('rbd/mirroring/bootstrap/create', {}),
167 taskObs.subscribe({ error: finishHandler, complete: finishHandler });