]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
64054aabb0838a6101ee3b391a3eb0fdc50fec3b
[ceph.git] /
1 import { Component, OnDestroy, OnInit } from '@angular/core';
2 import { AbstractControl, FormControl, Validators } from '@angular/forms';
3
4 import { I18n } from '@ngx-translate/i18n-polyfill';
5 import { BsModalRef } from 'ngx-bootstrap/modal';
6 import { Subscription } from 'rxjs';
7
8 import { RbdMirroringService } from '../../../../shared/api/rbd-mirroring.service';
9 import { CdFormGroup } from '../../../../shared/forms/cd-form-group';
10 import { FinishedTask } from '../../../../shared/models/finished-task';
11 import { TaskWrapperService } from '../../../../shared/services/task-wrapper.service';
12 import { PoolEditModeResponseModel } from './pool-edit-mode-response.model';
13
14 @Component({
15   selector: 'cd-pool-edit-mode-modal',
16   templateUrl: './pool-edit-mode-modal.component.html',
17   styleUrls: ['./pool-edit-mode-modal.component.scss']
18 })
19 export class PoolEditModeModalComponent implements OnInit, OnDestroy {
20   poolName: string;
21
22   subs: Subscription;
23
24   editModeForm: CdFormGroup;
25   bsConfig = {
26     containerClass: 'theme-default'
27   };
28   pattern: string;
29
30   response: PoolEditModeResponseModel;
31   peerExists = false;
32
33   mirrorModes: Array<{ id: string; name: string }> = [
34     { id: 'disabled', name: this.i18n('Disabled') },
35     { id: 'pool', name: this.i18n('Pool') },
36     { id: 'image', name: this.i18n('Image') }
37   ];
38
39   constructor(
40     public modalRef: BsModalRef,
41     private i18n: I18n,
42     private rbdMirroringService: RbdMirroringService,
43     private taskWrapper: TaskWrapperService
44   ) {
45     this.createForm();
46   }
47
48   createForm() {
49     this.editModeForm = new CdFormGroup({
50       mirrorMode: new FormControl('', {
51         validators: [Validators.required, this.validateMode.bind(this)]
52       })
53     });
54   }
55
56   ngOnInit() {
57     this.pattern = `${this.poolName}`;
58     this.rbdMirroringService.getPool(this.poolName).subscribe((resp: PoolEditModeResponseModel) => {
59       this.setResponse(resp);
60     });
61
62     this.subs = this.rbdMirroringService.subscribeSummary((data) => {
63       this.peerExists = false;
64       const poolData = data.content_data.pools;
65       const pool = poolData.find((o: any) => this.poolName === o['name']);
66       this.peerExists = pool && pool['peer_uuids'].length;
67     });
68   }
69
70   ngOnDestroy(): void {
71     this.subs.unsubscribe();
72   }
73
74   validateMode(control: AbstractControl) {
75     if (control.value === 'disabled' && this.peerExists) {
76       return { cannotDisable: { value: control.value } };
77     }
78     return null;
79   }
80
81   setResponse(response: PoolEditModeResponseModel) {
82     this.editModeForm.get('mirrorMode').setValue(response.mirror_mode);
83   }
84
85   update() {
86     const request = new PoolEditModeResponseModel();
87     request.mirror_mode = this.editModeForm.getValue('mirrorMode');
88
89     const action = this.taskWrapper.wrapTaskAroundCall({
90       task: new FinishedTask('rbd/mirroring/pool/edit', {
91         pool_name: this.poolName
92       }),
93       call: this.rbdMirroringService.updatePool(this.poolName, request)
94     });
95
96     action.subscribe(
97       undefined,
98       () => this.editModeForm.setErrors({ cdSubmitButton: true }),
99       () => {
100         this.rbdMirroringService.refresh();
101         this.modalRef.hide();
102       }
103     );
104   }
105 }