1 import { Component, OnDestroy, OnInit } from '@angular/core';
2 import { FormControl, FormGroup, ValidatorFn, Validators } from '@angular/forms';
4 import * as _ from 'lodash';
5 import { BsModalRef } from 'ngx-bootstrap/modal';
6 import { concat, forkJoin, Subscription } from 'rxjs';
7 import { last, tap } from 'rxjs/operators';
9 import { RbdMirroringService } from '../../../../shared/api/rbd-mirroring.service';
10 import { CdFormGroup } from '../../../../shared/forms/cd-form-group';
11 import { FinishedTask } from '../../../../shared/models/finished-task';
12 import { TaskWrapperService } from '../../../../shared/services/task-wrapper.service';
13 import { Pool } from '../../../pool/pool';
16 selector: 'cd-bootstrap-create-modal',
17 templateUrl: './bootstrap-create-modal.component.html',
18 styleUrls: ['./bootstrap-create-modal.component.scss']
20 export class BootstrapCreateModalComponent implements OnDestroy, OnInit {
27 createBootstrapForm: CdFormGroup;
30 public modalRef: BsModalRef,
31 private rbdMirroringService: RbdMirroringService,
32 private taskWrapper: TaskWrapperService
38 this.createBootstrapForm = new CdFormGroup({
39 siteName: new FormControl('', {
40 validators: [Validators.required]
45 validators: [this.validatePools()]
48 token: new FormControl('', {})
53 this.createBootstrapForm.get('siteName').setValue(this.siteName);
54 this.rbdMirroringService.getSiteName().subscribe((response: any) => {
55 this.createBootstrapForm.get('siteName').setValue(response.site_name);
58 this.subs = this.rbdMirroringService.subscribeSummary((data) => {
59 const pools = data.content_data.pools;
60 this.pools = pools.reduce((acc: any[], pool: Pool) => {
63 mirror_mode: pool['mirror_mode']
68 const poolsControl = this.createBootstrapForm.get('pools') as FormGroup;
69 _.each(this.pools, (pool) => {
70 const poolName = pool['name'];
71 const mirroring_disabled = pool['mirror_mode'] === 'disabled';
72 const control = poolsControl.controls[poolName];
74 if (mirroring_disabled && control.disabled) {
76 } else if (!mirroring_disabled && control.enabled) {
78 control.setValue(true);
81 poolsControl.addControl(
83 new FormControl({ value: !mirroring_disabled, disabled: !mirroring_disabled })
92 this.subs.unsubscribe();
96 validatePools(): ValidatorFn {
97 return (poolsControl: FormGroup): { [key: string]: any } => {
99 _.each(poolsControl.controls, (control) => {
100 if (control.value === true) {
105 if (checkedCount > 0) {
109 return { requirePool: true };
114 this.createBootstrapForm.get('token').setValue('');
116 let bootstrapPoolName = '';
117 const poolNames: string[] = [];
118 const poolsControl = this.createBootstrapForm.get('pools') as FormGroup;
119 _.each(poolsControl.controls, (control, poolName) => {
120 if (control.value === true) {
121 bootstrapPoolName = poolName;
122 if (!control.disabled) {
123 poolNames.push(poolName);
128 const poolModeRequest = {
132 const apiActionsObs = concat(
133 this.rbdMirroringService.setSiteName(this.createBootstrapForm.getValue('siteName')),
135 poolNames.map((poolName) => this.rbdMirroringService.updatePool(poolName, poolModeRequest))
137 this.rbdMirroringService
138 .createBootstrapToken(bootstrapPoolName)
139 .pipe(tap((data: any) => this.createBootstrapForm.get('token').setValue(data['token'])))
142 const finishHandler = () => {
143 this.rbdMirroringService.refresh();
144 this.createBootstrapForm.setErrors({ cdSubmitButton: true });
147 const taskObs = this.taskWrapper.wrapTaskAroundCall({
148 task: new FinishedTask('rbd/mirroring/bootstrap/create', {}),
151 taskObs.subscribe(undefined, finishHandler, finishHandler);