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