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, Observable, Subscription } from 'rxjs';
7 import { last } 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-import-modal',
17 templateUrl: './bootstrap-import-modal.component.html',
18 styleUrls: ['./bootstrap-import-modal.component.scss']
20 export class BootstrapImportModalComponent implements OnInit, OnDestroy {
27 importBootstrapForm: CdFormGroup;
29 directions: Array<any> = [
30 { key: 'rx-tx', desc: 'Bidirectional' },
31 { key: 'rx', desc: 'Unidirectional (receive-only)' }
35 public modalRef: BsModalRef,
36 private rbdMirroringService: RbdMirroringService,
37 private taskWrapper: TaskWrapperService
43 this.importBootstrapForm = new CdFormGroup({
44 siteName: new FormControl('', {
45 validators: [Validators.required]
47 direction: new FormControl('rx-tx', {}),
51 validators: [this.validatePools()]
54 token: new FormControl('', {
55 validators: [Validators.required, this.validateToken()]
61 this.rbdMirroringService.getSiteName().subscribe((response: any) => {
62 this.importBootstrapForm.get('siteName').setValue(response.site_name);
65 this.subs = this.rbdMirroringService.subscribeSummary((data: any) => {
70 const pools = data.content_data.pools;
71 this.pools = pools.reduce((acc: any[], pool: Pool) => {
74 mirror_mode: pool['mirror_mode']
79 const poolsControl = this.importBootstrapForm.get('pools') as FormGroup;
80 _.each(this.pools, (pool) => {
81 const poolName = pool['name'];
82 const mirroring_disabled = pool['mirror_mode'] === 'disabled';
83 const control = poolsControl.controls[poolName];
85 if (mirroring_disabled && control.disabled) {
87 } else if (!mirroring_disabled && control.enabled) {
89 control.setValue(true);
92 poolsControl.addControl(
94 new FormControl({ value: !mirroring_disabled, disabled: !mirroring_disabled })
103 this.subs.unsubscribe();
107 validatePools(): ValidatorFn {
108 return (poolsControl: FormGroup): { [key: string]: any } => {
109 let checkedCount = 0;
110 _.each(poolsControl.controls, (control) => {
111 if (control.value === true) {
116 if (checkedCount > 0) {
120 return { requirePool: true };
124 validateToken(): ValidatorFn {
125 return (token: FormControl): { [key: string]: any } => {
127 if (JSON.parse(atob(token.value))) {
131 return { invalidToken: true };
136 const bootstrapPoolNames: string[] = [];
137 const poolNames: string[] = [];
138 const poolsControl = this.importBootstrapForm.get('pools') as FormGroup;
139 _.each(poolsControl.controls, (control, poolName) => {
140 if (control.value === true) {
141 bootstrapPoolNames.push(poolName);
142 if (!control.disabled) {
143 poolNames.push(poolName);
148 const poolModeRequest = {
152 let apiActionsObs: Observable<any> = concat(
153 this.rbdMirroringService.setSiteName(this.importBootstrapForm.getValue('siteName')),
155 poolNames.map((poolName) => this.rbdMirroringService.updatePool(poolName, poolModeRequest))
159 apiActionsObs = bootstrapPoolNames
160 .reduce((obs, poolName) => {
163 this.rbdMirroringService.importBootstrapToken(
165 this.importBootstrapForm.getValue('direction'),
166 this.importBootstrapForm.getValue('token')
172 const finishHandler = () => {
173 this.rbdMirroringService.refresh();
174 this.importBootstrapForm.setErrors({ cdSubmitButton: true });
177 const taskObs = this.taskWrapper.wrapTaskAroundCall({
178 task: new FinishedTask('rbd/mirroring/bootstrap/import', {}),
181 taskObs.subscribe(undefined, finishHandler, () => {
183 this.modalRef.hide();