1 import { Component, OnDestroy, OnInit } from '@angular/core';
2 import { FormControl, FormGroup, ValidatorFn, Validators } from '@angular/forms';
4 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
5 import * as _ from 'lodash';
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 activeModal: NgbActiveModal,
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) => {
66 const pools = data.content_data.pools;
67 this.pools = pools.reduce((acc: any[], pool: Pool) => {
70 mirror_mode: pool['mirror_mode']
75 const poolsControl = this.importBootstrapForm.get('pools') as FormGroup;
76 _.each(this.pools, (pool) => {
77 const poolName = pool['name'];
78 const mirroring_disabled = pool['mirror_mode'] === 'disabled';
79 const control = poolsControl.controls[poolName];
81 if (mirroring_disabled && control.disabled) {
83 } else if (!mirroring_disabled && control.enabled) {
85 control.setValue(true);
88 poolsControl.addControl(
90 new FormControl({ value: !mirroring_disabled, disabled: !mirroring_disabled })
99 this.subs.unsubscribe();
103 validatePools(): ValidatorFn {
104 return (poolsControl: FormGroup): { [key: string]: any } => {
105 let checkedCount = 0;
106 _.each(poolsControl.controls, (control) => {
107 if (control.value === true) {
112 if (checkedCount > 0) {
116 return { requirePool: true };
120 validateToken(): ValidatorFn {
121 return (token: FormControl): { [key: string]: any } => {
123 if (JSON.parse(atob(token.value))) {
127 return { invalidToken: true };
132 const bootstrapPoolNames: string[] = [];
133 const poolNames: string[] = [];
134 const poolsControl = this.importBootstrapForm.get('pools') as FormGroup;
135 _.each(poolsControl.controls, (control, poolName) => {
136 if (control.value === true) {
137 bootstrapPoolNames.push(poolName);
138 if (!control.disabled) {
139 poolNames.push(poolName);
144 const poolModeRequest = {
148 let apiActionsObs: Observable<any> = concat(
149 this.rbdMirroringService.setSiteName(this.importBootstrapForm.getValue('siteName')),
151 poolNames.map((poolName) => this.rbdMirroringService.updatePool(poolName, poolModeRequest))
155 apiActionsObs = bootstrapPoolNames
156 .reduce((obs, poolName) => {
159 this.rbdMirroringService.importBootstrapToken(
161 this.importBootstrapForm.getValue('direction'),
162 this.importBootstrapForm.getValue('token')
168 const finishHandler = () => {
169 this.rbdMirroringService.refresh();
170 this.importBootstrapForm.setErrors({ cdSubmitButton: true });
173 const taskObs = this.taskWrapper.wrapTaskAroundCall({
174 task: new FinishedTask('rbd/mirroring/bootstrap/import', {}),
178 error: finishHandler,
181 this.activeModal.close();