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';
6 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
7 import { Subscription } from 'rxjs';
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';
17 selector: 'cd-pool-edit-mode-modal',
18 templateUrl: './pool-edit-mode-modal.component.html',
19 styleUrls: ['./pool-edit-mode-modal.component.scss']
21 export class PoolEditModeModalComponent implements OnInit, OnDestroy {
26 editModeForm: CdFormGroup;
28 containerClass: 'theme-default'
32 response: PoolEditModeResponseModel;
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` }
42 public activeModal: NgbActiveModal,
43 public actionLabels: ActionLabelsI18n,
44 private rbdMirroringService: RbdMirroringService,
45 private taskWrapper: TaskWrapperService,
46 private route: ActivatedRoute,
47 private location: Location
53 this.editModeForm = new CdFormGroup({
54 mirrorMode: new UntypedFormControl('', {
55 validators: [Validators.required, this.validateMode.bind(this)]
61 this.route.params.subscribe((params: { pool_name: string }) => {
62 this.poolName = params.pool_name;
64 this.pattern = `${this.poolName}`;
65 this.rbdMirroringService.getPool(this.poolName).subscribe((resp: PoolEditModeResponseModel) => {
66 this.setResponse(resp);
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;
78 this.subs.unsubscribe();
81 validateMode(control: AbstractControl) {
82 if (control.value === 'disabled' && this.peerExists) {
83 return { cannotDisable: { value: control.value } };
88 setResponse(response: PoolEditModeResponseModel) {
89 this.editModeForm.get('mirrorMode').setValue(response.mirror_mode);
93 const request = new PoolEditModeResponseModel();
94 request.mirror_mode = this.editModeForm.getValue('mirrorMode');
96 const action = this.taskWrapper.wrapTaskAroundCall({
97 task: new FinishedTask('rbd/mirroring/pool/edit', {
98 pool_name: this.poolName
100 call: this.rbdMirroringService.updatePool(this.poolName, request)
104 error: () => this.editModeForm.setErrors({ cdSubmitButton: true }),
106 this.rbdMirroringService.refresh();
107 this.location.back();