]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
fb92f27fa704a017d05a43525a325a8e380472bf
[ceph.git] /
1 import { Component, OnDestroy, OnInit } from '@angular/core';
2 import { FormControl, FormGroup, ValidatorFn, Validators } from '@angular/forms';
3
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';
8
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';
14
15 @Component({
16   selector: 'cd-bootstrap-create-modal',
17   templateUrl: './bootstrap-create-modal.component.html',
18   styleUrls: ['./bootstrap-create-modal.component.scss']
19 })
20 export class BootstrapCreateModalComponent implements OnDestroy, OnInit {
21   siteName: string;
22   pools: any[] = [];
23   token: string;
24
25   subs: Subscription;
26
27   createBootstrapForm: CdFormGroup;
28
29   constructor(
30     public modalRef: BsModalRef,
31     private rbdMirroringService: RbdMirroringService,
32     private taskWrapper: TaskWrapperService
33   ) {
34     this.createForm();
35   }
36
37   createForm() {
38     this.createBootstrapForm = new CdFormGroup({
39       siteName: new FormControl('', {
40         validators: [Validators.required]
41       }),
42       pools: new FormGroup(
43         {},
44         {
45           validators: [this.validatePools()]
46         }
47       ),
48       token: new FormControl('', {})
49     });
50   }
51
52   ngOnInit() {
53     this.createBootstrapForm.get('siteName').setValue(this.siteName);
54     this.rbdMirroringService.getSiteName().subscribe((response: any) => {
55       this.createBootstrapForm.get('siteName').setValue(response.site_name);
56     });
57
58     this.subs = this.rbdMirroringService.subscribeSummary((data) => {
59       const pools = data.content_data.pools;
60       this.pools = pools.reduce((acc: any[], pool: Pool) => {
61         acc.push({
62           name: pool['name'],
63           mirror_mode: pool['mirror_mode']
64         });
65         return acc;
66       }, []);
67
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];
73         if (control) {
74           if (mirroring_disabled && control.disabled) {
75             control.enable();
76           } else if (!mirroring_disabled && control.enabled) {
77             control.disable();
78             control.setValue(true);
79           }
80         } else {
81           poolsControl.addControl(
82             poolName,
83             new FormControl({ value: !mirroring_disabled, disabled: !mirroring_disabled })
84           );
85         }
86       });
87     });
88   }
89
90   ngOnDestroy() {
91     if (this.subs) {
92       this.subs.unsubscribe();
93     }
94   }
95
96   validatePools(): ValidatorFn {
97     return (poolsControl: FormGroup): { [key: string]: any } => {
98       let checkedCount = 0;
99       _.each(poolsControl.controls, (control) => {
100         if (control.value === true) {
101           ++checkedCount;
102         }
103       });
104
105       if (checkedCount > 0) {
106         return null;
107       }
108
109       return { requirePool: true };
110     };
111   }
112
113   generate() {
114     this.createBootstrapForm.get('token').setValue('');
115
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);
124         }
125       }
126     });
127
128     const poolModeRequest = {
129       mirror_mode: 'image'
130     };
131
132     const apiActionsObs = concat(
133       this.rbdMirroringService.setSiteName(this.createBootstrapForm.getValue('siteName')),
134       forkJoin(
135         poolNames.map((poolName) => this.rbdMirroringService.updatePool(poolName, poolModeRequest))
136       ),
137       this.rbdMirroringService
138         .createBootstrapToken(bootstrapPoolName)
139         .pipe(tap((data: any) => this.createBootstrapForm.get('token').setValue(data['token'])))
140     ).pipe(last());
141
142     const finishHandler = () => {
143       this.rbdMirroringService.refresh();
144       this.createBootstrapForm.setErrors({ cdSubmitButton: true });
145     };
146
147     const taskObs = this.taskWrapper.wrapTaskAroundCall({
148       task: new FinishedTask('rbd/mirroring/bootstrap/create', {}),
149       call: apiActionsObs
150     });
151     taskObs.subscribe({ error: finishHandler, complete: finishHandler });
152   }
153 }