]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
73bca12ece3aa091752644f40d9395edd7b57dfb
[ceph.git] /
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';
18
19 @Component({
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']
23 })
24 export class RgwMultisiteSyncPipeModalComponent implements OnInit {
25   groupExpandedRow: any;
26   pipeSelectedRow: any;
27   pipeForm: CdFormGroup;
28   action: string;
29   editing: boolean;
30   sourceZones = new ZoneData(false, 'Filter Zones');
31   destZones = new ZoneData(true, 'Filter or Add Zones');
32   icons = Icons;
33
34   constructor(
35     public activeModal: NgbActiveModal,
36     private rgwDaemonService: RgwDaemonService,
37     private rgwZonegroupService: RgwZonegroupService,
38     private rgwMultisiteService: RgwMultisiteService,
39     private notificationService: NotificationService
40   ) {}
41
42   ngOnInit(): void {
43     this.editing = this.action === 'create' ? false : true;
44     this.pipeForm = new CdFormGroup({
45       pipe_id: new UntypedFormControl('', {
46         validators: [Validators.required]
47       }),
48       group_id: new UntypedFormControl(this.groupExpandedRow?.groupName || '', {
49         validators: [Validators.required]
50       }),
51       bucket_name: new UntypedFormControl(this.groupExpandedRow?.bucket || ''),
52       source_bucket: new UntypedFormControl(''),
53       source_zones: new UntypedFormControl('', {
54         validators: [Validators.required]
55       }),
56       destination_bucket: new UntypedFormControl(''),
57       destination_zones: new UntypedFormControl('', {
58         validators: [Validators.required]
59       })
60     });
61     this.pipeForm.get('bucket_name').disable();
62     this.rgwDaemonService.selectedDaemon$
63       .pipe(
64         switchMap((daemon: RgwDaemon) => {
65           if (daemon) {
66             const zonegroupObj = new RgwZonegroup();
67             zonegroupObj.name = daemon.zonegroup_name;
68             return this.rgwZonegroupService.get(zonegroupObj).pipe(
69               catchError(() => {
70                 return of([]);
71               })
72             );
73           } else {
74             return of([]);
75           }
76         })
77       )
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, ''));
83           });
84           this.sourceZones.data.available = [...zones];
85           if (this.editing) {
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
95             });
96           }
97         }
98       });
99   }
100
101   onZoneSelection(zoneType: string) {
102     if (zoneType === 'source_zones') {
103       this.pipeForm.patchValue({
104         source_zones: this.sourceZones.data.selected
105       });
106     } else {
107       this.pipeForm.patchValue({
108         destination_zones: this.destZones.data.selected
109       });
110     }
111   }
112
113   submit() {
114     if (this.pipeForm.invalid) {
115       return;
116     }
117     // Ensure that no validation is pending
118     if (this.pipeForm.pending) {
119       this.pipeForm.setErrors({ cdSubmitButton: true });
120       return;
121     }
122     this.rgwMultisiteService.createEditSyncPipe(this.pipeForm.getRawValue()).subscribe(
123       () => {
124         const action = this.editing ? 'Modified' : 'Created';
125         this.notificationService.show(
126           NotificationType.success,
127           $localize`${action} Sync Pipe '${this.pipeForm.getValue('pipe_id')}'`
128         );
129         this.activeModal.close('success');
130       },
131       () => {
132         // Reset the 'Submit' button.
133         this.pipeForm.setErrors({ cdSubmitButton: true });
134         this.activeModal.dismiss();
135       }
136     );
137   }
138 }