1 import { Component, Inject, OnDestroy, OnInit, Optional } from '@angular/core';
2 import { UntypedFormControl, UntypedFormGroup, ValidatorFn, Validators } from '@angular/forms';
4 import { BaseModal } from 'carbon-components-angular';
5 import _ from 'lodash';
6 import { concat, forkJoin, Subscription } from 'rxjs';
7 import { last, tap } from 'rxjs/operators';
9 import { Pool } from '~/app/ceph/pool/pool';
10 import { RbdMirroringService } from '~/app/shared/api/rbd-mirroring.service';
11 import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
12 import { FinishedTask } from '~/app/shared/models/finished-task';
13 import { TaskWrapperService } from '~/app/shared/services/task-wrapper.service';
16 selector: 'cd-bootstrap-create-modal',
17 templateUrl: './bootstrap-create-modal.component.html',
18 styleUrls: ['./bootstrap-create-modal.component.scss']
20 export class BootstrapCreateModalComponent extends BaseModal implements OnDestroy, OnInit {
26 createBootstrapForm: CdFormGroup;
29 private rbdMirroringService: RbdMirroringService,
30 private taskWrapper: TaskWrapperService,
32 @Inject('siteName') @Optional() public siteName?: string
39 this.createBootstrapForm = new CdFormGroup({
40 siteName: new UntypedFormControl('', {
41 validators: [Validators.required]
43 pools: new UntypedFormGroup(
46 validators: [this.validatePools()]
49 token: new UntypedFormControl('', {})
54 this.createBootstrapForm.get('siteName').setValue(this.siteName);
55 this.rbdMirroringService.getSiteName().subscribe((response: any) => {
56 this.createBootstrapForm.get('siteName').setValue(response.site_name);
59 this.subs = this.rbdMirroringService.subscribeSummary((data) => {
60 const pools = data.content_data.pools;
61 this.pools = pools.reduce((acc: any[], pool: Pool) => {
64 mirror_mode: pool['mirror_mode']
69 const poolsControl = this.createBootstrapForm.get('pools') as UntypedFormGroup;
70 _.each(this.pools, (pool) => {
71 const poolName = pool['name'];
72 const mirroring_disabled = pool['mirror_mode'] === 'disabled';
73 const control = poolsControl.controls[poolName];
75 if (mirroring_disabled && control.disabled) {
77 } else if (!mirroring_disabled && control.enabled) {
79 control.setValue(true);
82 poolsControl.addControl(
84 new UntypedFormControl({ value: !mirroring_disabled, disabled: !mirroring_disabled })
93 this.subs.unsubscribe();
97 validatePools(): ValidatorFn {
98 return (poolsControl: UntypedFormGroup): { [key: string]: any } => {
100 _.each(poolsControl.controls, (control) => {
101 if (control.value === true) {
106 if (checkedCount > 0) {
110 return { requirePool: true };
115 this.createBootstrapForm.get('token').setValue('');
117 let bootstrapPoolName = '';
118 const poolNames: string[] = [];
119 const poolsControl = this.createBootstrapForm.get('pools') as UntypedFormGroup;
120 _.each(poolsControl.controls, (control, poolName) => {
121 if (control.value === true) {
122 bootstrapPoolName = poolName;
123 if (!control.disabled) {
124 poolNames.push(poolName);
129 const poolModeRequest = {
133 const apiActionsObs = concat(
134 this.rbdMirroringService.setSiteName(this.createBootstrapForm.getValue('siteName')),
136 poolNames.map((poolName) => this.rbdMirroringService.updatePool(poolName, poolModeRequest))
138 this.rbdMirroringService
139 .createBootstrapToken(bootstrapPoolName)
140 .pipe(tap((data: any) => this.createBootstrapForm.get('token').setValue(data['token'])))
143 const finishHandler = () => {
144 this.rbdMirroringService.refresh();
145 this.createBootstrapForm.setErrors({ cdSubmitButton: true });
148 const taskObs = this.taskWrapper.wrapTaskAroundCall({
149 task: new FinishedTask('rbd/mirroring/bootstrap/create', {}),
152 taskObs.subscribe({ error: finishHandler, complete: finishHandler });