1 import { Component, OnInit } from '@angular/core';
2 import { UntypedFormControl, Validators } from '@angular/forms';
3 import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
4 import { RgwZonegroup } from '../models/rgw-multisite';
5 import { SelectOption } from '~/app/shared/components/select/select-option.model';
6 import { catchError, switchMap } from 'rxjs/operators';
7 import { of } from 'rxjs';
8 import { RgwDaemon } from '../models/rgw-daemon';
9 import { RgwDaemonService } from '~/app/shared/api/rgw-daemon.service';
10 import { RgwZonegroupService } from '~/app/shared/api/rgw-zonegroup.service';
11 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
12 import _ from 'lodash';
13 import { Icons } from '~/app/shared/enum/icons.enum';
14 import { RgwMultisiteService } from '~/app/shared/api/rgw-multisite.service';
15 import { NotificationType } from '~/app/shared/enum/notification-type.enum';
16 import { NotificationService } from '~/app/shared/services/notification.service';
17 import { ZoneData } from '../models/rgw-multisite-zone-selector';
20 selector: 'cd-rgw-multisite-sync-pipe-modal',
21 templateUrl: './rgw-multisite-sync-pipe-modal.component.html',
22 styleUrls: ['./rgw-multisite-sync-pipe-modal.component.scss']
24 export class RgwMultisiteSyncPipeModalComponent implements OnInit {
25 groupExpandedRow: any;
27 pipeForm: CdFormGroup;
30 sourceZones = new ZoneData(false, 'Filter Zones');
31 destZones = new ZoneData(true, 'Filter or Add Zones');
35 public activeModal: NgbActiveModal,
36 private rgwDaemonService: RgwDaemonService,
37 private rgwZonegroupService: RgwZonegroupService,
38 private rgwMultisiteService: RgwMultisiteService,
39 private notificationService: NotificationService
43 this.editing = this.action === 'create' ? false : true;
44 this.pipeForm = new CdFormGroup({
45 pipe_id: new UntypedFormControl('', {
46 validators: [Validators.required]
48 group_id: new UntypedFormControl(this.groupExpandedRow?.groupName || '', {
49 validators: [Validators.required]
51 bucket_name: new UntypedFormControl(this.groupExpandedRow?.bucket || ''),
52 source_bucket: new UntypedFormControl(''),
53 source_zones: new UntypedFormControl('', {
54 validators: [Validators.required]
56 destination_bucket: new UntypedFormControl(''),
57 destination_zones: new UntypedFormControl('', {
58 validators: [Validators.required]
61 this.pipeForm.get('bucket_name').disable();
62 this.rgwDaemonService.selectedDaemon$
64 switchMap((daemon: RgwDaemon) => {
66 const zonegroupObj = new RgwZonegroup();
67 zonegroupObj.name = daemon.zonegroup_name;
68 return this.rgwZonegroupService.get(zonegroupObj).pipe(
78 .subscribe((zonegroupData: any) => {
79 if (zonegroupData && zonegroupData?.zones?.length > 0) {
80 let zones: any[] = [];
81 zonegroupData.zones.forEach((zone: any) => {
82 zones.push(new SelectOption(false, zone.name, ''));
84 this.sourceZones.data.available = [...zones];
86 this.pipeForm.get('pipe_id').disable();
87 this.sourceZones.data.selected = this.pipeSelectedRow.source.zones;
88 this.destZones.data.selected = this.pipeSelectedRow.dest.zones;
89 this.pipeForm.patchValue({
90 pipe_id: this.pipeSelectedRow.id,
91 source_zones: this.pipeSelectedRow.source.zones,
92 destination_zones: this.pipeSelectedRow.dest.zones,
93 source_bucket: this.pipeSelectedRow.source.bucket,
94 destination_bucket: this.pipeSelectedRow.dest.bucket
101 onZoneSelection(zoneType: string) {
102 if (zoneType === 'source_zones') {
103 this.pipeForm.patchValue({
104 source_zones: this.sourceZones.data.selected
107 this.pipeForm.patchValue({
108 destination_zones: this.destZones.data.selected
114 if (this.pipeForm.invalid) {
117 // Ensure that no validation is pending
118 if (this.pipeForm.pending) {
119 this.pipeForm.setErrors({ cdSubmitButton: true });
122 this.rgwMultisiteService.createEditSyncPipe(this.pipeForm.getRawValue()).subscribe(
124 const action = this.editing ? 'Modified' : 'Created';
125 this.notificationService.show(
126 NotificationType.success,
127 $localize`${action} Sync Pipe '${this.pipeForm.getValue('pipe_id')}'`
129 this.activeModal.close('success');
132 // Reset the 'Submit' button.
133 this.pipeForm.setErrors({ cdSubmitButton: true });
134 this.activeModal.dismiss();