1 import { Component, OnInit } from '@angular/core';
2 import { UntypedFormControl, Validators } from '@angular/forms';
3 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
4 import { RgwDaemonService } from '~/app/shared/api/rgw-daemon.service';
5 import { ActionLabelsI18n } from '~/app/shared/constants/app.constants';
6 import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
7 import { NotificationService } from '~/app/shared/services/notification.service';
8 import { catchError, switchMap } from 'rxjs/operators';
9 import { RgwZonegroupService } from '~/app/shared/api/rgw-zonegroup.service';
10 import { RgwDaemon } from '../models/rgw-daemon';
11 import { FlowType, RgwZonegroup } from '../models/rgw-multisite';
12 import { of } from 'rxjs';
13 import { SelectOption } from '~/app/shared/components/select/select-option.model';
14 import _ from 'lodash';
15 import { Icons } from '~/app/shared/enum/icons.enum';
16 import { RgwMultisiteService } from '~/app/shared/api/rgw-multisite.service';
17 import { NotificationType } from '~/app/shared/enum/notification-type.enum';
18 import { ZoneData } from '../models/rgw-multisite-zone-selector';
21 selector: 'cd-rgw-multisite-sync-flow-modal',
22 templateUrl: './rgw-multisite-sync-flow-modal.component.html',
23 styleUrls: ['./rgw-multisite-sync-flow-modal.component.scss']
25 export class RgwMultisiteSyncFlowModalComponent implements OnInit {
27 editing: boolean = false;
29 groupExpandedRow: any;
31 syncPolicyDirectionalFlowForm: CdFormGroup;
32 syncPolicySymmetricalFlowForm: CdFormGroup;
33 syncPolicyPipeForm: CdFormGroup;
34 currentFormGroupContext: CdFormGroup;
37 zones = new ZoneData(false, 'Filter Zones');
38 sourceZones = new ZoneData(false, 'Filter Zones');
39 destinationZones = new ZoneData(true, 'Filter or Add Zones');
42 public activeModal: NgbActiveModal,
43 public actionLabels: ActionLabelsI18n,
44 public notificationService: NotificationService,
45 private rgwDaemonService: RgwDaemonService,
46 private rgwZonegroupService: RgwZonegroupService,
47 private rgwMultisiteService: RgwMultisiteService
51 if (this.action === 'edit') {
54 if (this.groupType === FlowType.symmetrical) {
55 this.createSymmetricalFlowForm();
56 this.currentFormGroupContext = _.cloneDeep(this.syncPolicySymmetricalFlowForm);
57 } else if (this.groupType === FlowType.directional) {
58 this.createDirectionalFlowForm();
59 this.currentFormGroupContext = _.cloneDeep(this.syncPolicyDirectionalFlowForm);
61 this.currentFormGroupContext.get('bucket_name').disable();
63 this.currentFormGroupContext.patchValue({
64 flow_id: this.flowSelectedRow.id,
65 bucket_name: this.groupExpandedRow.bucket || ''
69 this.rgwDaemonService.selectedDaemon$
71 switchMap((daemon: RgwDaemon) => {
73 const zonegroupObj = new RgwZonegroup();
74 zonegroupObj.name = daemon?.zonegroup_name;
75 return this.rgwZonegroupService.get(zonegroupObj).pipe(
85 .subscribe((zonegroupData: any) => {
86 if (zonegroupData && zonegroupData?.zones?.length > 0) {
87 const zones: any = [];
88 zonegroupData.zones.forEach((zone: any) => {
89 zones.push(new SelectOption(false, zone.name, ''));
91 this.zones.data.available = [...zones];
92 this.sourceZones.data.available = [...zones];
94 if (this.groupType === FlowType.symmetrical) {
95 this.zones.data.selected = this.flowSelectedRow.zones;
97 this.destinationZones.data.selected = [this.flowSelectedRow.dest_zone];
98 this.sourceZones.data.selected = [this.flowSelectedRow.source_zone];
100 this.zoneSelection();
106 createSymmetricalFlowForm() {
107 this.syncPolicySymmetricalFlowForm = new CdFormGroup({
108 ...this.commonFormControls(FlowType.symmetrical),
109 zones: new UntypedFormControl([], {
110 validators: [Validators.required]
115 createDirectionalFlowForm() {
116 this.syncPolicyDirectionalFlowForm = new CdFormGroup({
117 ...this.commonFormControls(FlowType.directional),
118 source_zone: new UntypedFormControl('', {
119 validators: [Validators.required]
121 destination_zone: new UntypedFormControl('', {
122 validators: [Validators.required]
127 commonFormControls(flowType: FlowType) {
129 bucket_name: new UntypedFormControl(this.groupExpandedRow?.bucket),
130 group_id: new UntypedFormControl(this.groupExpandedRow?.groupName, {
131 validators: [Validators.required]
133 flow_id: new UntypedFormControl('', {
134 validators: [Validators.required]
136 flow_type: new UntypedFormControl(flowType, {
137 validators: [Validators.required]
143 if (this.groupType === FlowType.symmetrical) {
144 this.currentFormGroupContext.patchValue({
145 zones: this.zones.data.selected
148 this.currentFormGroupContext.patchValue({
149 source_zone: this.sourceZones.data.selected,
150 destination_zone: this.destinationZones.data.selected
156 if (this.currentFormGroupContext.invalid) {
159 // Ensure that no validation is pending
160 if (this.currentFormGroupContext.pending) {
161 this.currentFormGroupContext.setErrors({ cdSubmitButton: true });
164 this.rgwMultisiteService
165 .createEditSyncFlow(this.currentFormGroupContext.getRawValue())
168 const action = this.editing ? 'Modified' : 'Created';
169 this.notificationService.show(
170 NotificationType.success,
171 $localize`${action} Sync Flow '${this.currentFormGroupContext.getValue('flow_id')}'`
173 this.activeModal.close('success');
176 // Reset the 'Submit' button.
177 this.currentFormGroupContext.setErrors({ cdSubmitButton: true });
178 this.activeModal.dismiss();