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: any) => {
63 const pools = data.content_data.pools;
64 this.pools = pools.reduce((acc: any[], pool: Pool) => {
67 mirror_mode: pool['mirror_mode']
72 const poolsControl = this.createBootstrapForm.get('pools') as FormGroup;
73 _.each(this.pools, (pool) => {
74 const poolName = pool['name'];
75 const mirroring_disabled = pool['mirror_mode'] === 'disabled';
76 const control = poolsControl.controls[poolName];
78 if (mirroring_disabled && control.disabled) {
80 } else if (!mirroring_disabled && control.enabled) {
82 control.setValue(true);
85 poolsControl.addControl(
87 new FormControl({ value: !mirroring_disabled, disabled: !mirroring_disabled })
96 this.subs.unsubscribe();
100 validatePools(): ValidatorFn {
101 return (poolsControl: FormGroup): { [key: string]: any } => {
102 let checkedCount = 0;
103 _.each(poolsControl.controls, (control) => {
104 if (control.value === true) {
109 if (checkedCount > 0) {
113 return { requirePool: true };
118 this.createBootstrapForm.get('token').setValue('');
120 let bootstrapPoolName = '';
121 const poolNames: string[] = [];
122 const poolsControl = this.createBootstrapForm.get('pools') as FormGroup;
123 _.each(poolsControl.controls, (control, poolName) => {
124 if (control.value === true) {
125 bootstrapPoolName = poolName;
126 if (!control.disabled) {
127 poolNames.push(poolName);
132 const poolModeRequest = {
136 const apiActionsObs = concat(
137 this.rbdMirroringService.setSiteName(this.createBootstrapForm.getValue('siteName')),
139 poolNames.map((poolName) => this.rbdMirroringService.updatePool(poolName, poolModeRequest))
141 this.rbdMirroringService
142 .createBootstrapToken(bootstrapPoolName)
143 .pipe(tap((data: any) => this.createBootstrapForm.get('token').setValue(data['token'])))
146 const finishHandler = () => {
147 this.rbdMirroringService.refresh();
148 this.createBootstrapForm.setErrors({ cdSubmitButton: true });
151 const taskObs = this.taskWrapper.wrapTaskAroundCall({
152 task: new FinishedTask('rbd/mirroring/bootstrap/create', {}),
155 taskObs.subscribe(undefined, finishHandler, finishHandler);