]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/mirroring/pool-edit-mode-modal/pool-edit-mode-modal.component.ts
137e787174d7a0e545382746cf5fbf9cf182afcc
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / ceph / block / mirroring / pool-edit-mode-modal / pool-edit-mode-modal.component.ts
1 import { Component, OnDestroy, OnInit } from '@angular/core';
2 import { AbstractControl, FormControl, Validators } from '@angular/forms';
3
4 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
5 import { Subscription } from 'rxjs';
6
7 import { RbdMirroringService } from '~/app/shared/api/rbd-mirroring.service';
8 import { ActionLabelsI18n } from '~/app/shared/constants/app.constants';
9 import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
10 import { FinishedTask } from '~/app/shared/models/finished-task';
11 import { TaskWrapperService } from '~/app/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: $localize`Disabled` },
35 { id: 'pool', name: $localize`Pool` },
36 { id: 'image', name: $localize`Image` }
37 ];
38
39 constructor(
40 public activeModal: NgbActiveModal,
41 public actionLabels: ActionLabelsI18n,
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 error: () => this.editModeForm.setErrors({ cdSubmitButton: true }),
98 complete: () => {
99 this.rbdMirroringService.refresh();
100 this.activeModal.close();
101 }
102 });
103 }
104 }