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 _ from 'lodash';
6 import { concat, forkJoin, Observable, Subscription } from 'rxjs';
7 import { last } from 'rxjs/operators';
9 import { Pool } from '~/app/ceph/pool/pool';
10 import { RbdMirroringService } from '~/app/shared/api/rbd-mirroring.service';
11 import { ActionLabelsI18n } from '~/app/shared/constants/app.constants';
12 import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
13 import { FinishedTask } from '~/app/shared/models/finished-task';
14 import { TaskWrapperService } from '~/app/shared/services/task-wrapper.service';
17 selector: 'cd-bootstrap-import-modal',
18 templateUrl: './bootstrap-import-modal.component.html',
19 styleUrls: ['./bootstrap-import-modal.component.scss']
21 export class BootstrapImportModalComponent implements OnInit, OnDestroy {
28 importBootstrapForm: CdFormGroup;
30 directions: Array<any> = [
31 { key: 'rx-tx', desc: 'Bidirectional' },
32 { key: 'rx', desc: 'Unidirectional (receive-only)' }
36 public activeModal: NgbActiveModal,
37 public actionLabels: ActionLabelsI18n,
38 private rbdMirroringService: RbdMirroringService,
39 private taskWrapper: TaskWrapperService
45 this.importBootstrapForm = new CdFormGroup({
46 siteName: new FormControl('', {
47 validators: [Validators.required]
49 direction: new FormControl('rx-tx', {}),
53 validators: [this.validatePools()]
56 token: new FormControl('', {
57 validators: [Validators.required, this.validateToken()]
63 this.rbdMirroringService.getSiteName().subscribe((response: any) => {
64 this.importBootstrapForm.get('siteName').setValue(response.site_name);
67 this.subs = this.rbdMirroringService.subscribeSummary((data) => {
68 const pools = data.content_data.pools;
69 this.pools = pools.reduce((acc: any[], pool: Pool) => {
72 mirror_mode: pool['mirror_mode']
77 const poolsControl = this.importBootstrapForm.get('pools') as FormGroup;
78 _.each(this.pools, (pool) => {
79 const poolName = pool['name'];
80 const mirroring_disabled = pool['mirror_mode'] === 'disabled';
81 const control = poolsControl.controls[poolName];
83 if (mirroring_disabled && control.disabled) {
85 } else if (!mirroring_disabled && control.enabled) {
87 control.setValue(true);
90 poolsControl.addControl(
92 new FormControl({ value: !mirroring_disabled, disabled: !mirroring_disabled })
101 this.subs.unsubscribe();
105 validatePools(): ValidatorFn {
106 return (poolsControl: FormGroup): { [key: string]: any } => {
107 let checkedCount = 0;
108 _.each(poolsControl.controls, (control) => {
109 if (control.value === true) {
114 if (checkedCount > 0) {
118 return { requirePool: true };
122 validateToken(): ValidatorFn {
123 return (token: FormControl): { [key: string]: any } => {
125 if (JSON.parse(atob(token.value))) {
129 return { invalidToken: true };
134 const bootstrapPoolNames: string[] = [];
135 const poolNames: string[] = [];
136 const poolsControl = this.importBootstrapForm.get('pools') as FormGroup;
137 _.each(poolsControl.controls, (control, poolName) => {
138 if (control.value === true) {
139 bootstrapPoolNames.push(poolName);
140 if (!control.disabled) {
141 poolNames.push(poolName);
146 const poolModeRequest = {
150 let apiActionsObs: Observable<any> = concat(
151 this.rbdMirroringService.setSiteName(this.importBootstrapForm.getValue('siteName')),
153 poolNames.map((poolName) => this.rbdMirroringService.updatePool(poolName, poolModeRequest))
157 apiActionsObs = bootstrapPoolNames
158 .reduce((obs, poolName) => {
161 this.rbdMirroringService.importBootstrapToken(
163 this.importBootstrapForm.getValue('direction'),
164 this.importBootstrapForm.getValue('token')
170 const finishHandler = () => {
171 this.rbdMirroringService.refresh();
172 this.importBootstrapForm.setErrors({ cdSubmitButton: true });
175 const taskObs = this.taskWrapper.wrapTaskAroundCall({
176 task: new FinishedTask('rbd/mirroring/bootstrap/import', {}),
180 error: finishHandler,
183 this.activeModal.close();