1 import { Component, Input, OnChanges, SimpleChanges, TemplateRef, ViewChild } from '@angular/core';
2 import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
3 import { ActionLabelsI18n } from '~/app/shared/constants/app.constants';
4 import { Icons } from '~/app/shared/enum/icons.enum';
5 import { CdTableAction } from '~/app/shared/models/cd-table-action';
6 import { CdTableColumn } from '~/app/shared/models/cd-table-column';
7 import { CdTableSelection } from '~/app/shared/models/cd-table-selection';
8 import { Permission } from '~/app/shared/models/permissions';
9 import { ModalService } from '~/app/shared/services/modal.service';
10 import { RgwMultisiteService } from '~/app/shared/api/rgw-multisite.service';
11 import { CriticalConfirmationModalComponent } from '~/app/shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
12 import { FinishedTask } from '~/app/shared/models/finished-task';
13 import { Observable, Subscriber, forkJoin as observableForkJoin } from 'rxjs';
14 import { TaskWrapperService } from '~/app/shared/services/task-wrapper.service';
15 import { TableComponent } from '~/app/shared/datatable/table/table.component';
16 import { RgwMultisiteSyncFlowModalComponent } from '../rgw-multisite-sync-flow-modal/rgw-multisite-sync-flow-modal.component';
17 import { FlowType } from '../models/rgw-multisite';
18 import { RgwMultisiteSyncPipeModalComponent } from '../rgw-multisite-sync-pipe-modal/rgw-multisite-sync-pipe-modal.component';
19 import { ModalCdsService } from '~/app/shared/services/modal-cds.service';
20 import { NotificationType } from '~/app/shared/enum/notification-type.enum';
22 enum MultisiteResourceType {
28 selector: 'cd-rgw-multisite-sync-policy-details',
29 templateUrl: './rgw-multisite-sync-policy-details.component.html',
30 styleUrls: ['./rgw-multisite-sync-policy-details.component.scss']
32 export class RgwMultisiteSyncPolicyDetailsComponent implements OnChanges {
36 permission: Permission;
38 @ViewChild(TableComponent)
39 table: TableComponent;
40 @ViewChild('deleteTpl', { static: true })
41 deleteTpl: TemplateRef<any>;
43 resourceType: MultisiteResourceType = MultisiteResourceType.flow;
45 modalRef: NgbModalRef;
46 symmetricalFlowData: any = [];
47 directionalFlowData: any = [];
49 symmetricalFlowCols: CdTableColumn[];
50 directionalFlowCols: CdTableColumn[];
51 pipeCols: CdTableColumn[];
52 symFlowTableActions: CdTableAction[];
53 dirFlowTableActions: CdTableAction[];
54 pipeTableActions: CdTableAction[];
55 symFlowSelection = new CdTableSelection();
56 dirFlowSelection = new CdTableSelection();
57 pipeSelection = new CdTableSelection();
60 private actionLabels: ActionLabelsI18n,
61 private modalService: ModalService,
62 private rgwMultisiteService: RgwMultisiteService,
63 private taskWrapper: TaskWrapperService,
64 private cdsModalService: ModalCdsService
66 this.symmetricalFlowCols = [
78 this.directionalFlowCols = [
85 name: 'Destination Zone',
102 name: 'Destination Zone',
107 name: 'Source Bucket',
108 prop: 'source.bucket',
112 name: 'Destination Bucket',
117 const symAddAction: CdTableAction = {
118 permission: 'create',
120 name: this.actionLabels.CREATE,
121 click: () => this.openModal(FlowType.symmetrical),
122 canBePrimary: (selection: CdTableSelection) => !selection.hasSelection
124 const symEditAction: CdTableAction = {
125 permission: 'update',
127 name: this.actionLabels.EDIT,
128 click: () => this.openModal(FlowType.symmetrical, true)
130 const symDeleteAction: CdTableAction = {
131 permission: 'delete',
133 disable: () => !this.symFlowSelection.hasSelection,
134 name: this.actionLabels.DELETE,
135 click: () => this.deleteFlow(FlowType.symmetrical),
136 canBePrimary: (selection: CdTableSelection) => selection.hasMultiSelection
138 this.symFlowTableActions = [symAddAction, symEditAction, symDeleteAction];
139 const dirAddAction: CdTableAction = {
140 permission: 'create',
142 name: this.actionLabels.CREATE,
143 click: () => this.openModal(FlowType.directional),
144 canBePrimary: (selection: CdTableSelection) => !selection.hasSelection
146 const dirDeleteAction: CdTableAction = {
147 permission: 'delete',
149 // TODO: disabling 'delete' as we are not getting flow_id from backend which is needed for deletion
151 'Deleting the directional flow is disabled in the UI. Please use CLI to delete the directional flow',
152 name: this.actionLabels.DELETE,
153 click: () => this.deleteFlow(FlowType.directional),
154 canBePrimary: (selection: CdTableSelection) => selection.hasSelection
156 this.dirFlowTableActions = [dirAddAction, dirDeleteAction];
157 const pipeAddAction: CdTableAction = {
158 permission: 'create',
160 name: this.actionLabels.CREATE,
161 click: () => this.openPipeModal(),
162 canBePrimary: (selection: CdTableSelection) => !selection.hasSelection
164 const pipeEditAction: CdTableAction = {
165 permission: 'update',
167 name: this.actionLabels.EDIT,
168 click: () => this.openPipeModal(true)
170 const pipeDeleteAction: CdTableAction = {
171 permission: 'delete',
173 disable: () => !this.pipeSelection.hasSelection,
174 name: this.actionLabels.DELETE,
175 click: () => this.deletePipe(),
176 canBePrimary: (selection: CdTableSelection) => selection.hasMultiSelection
178 this.pipeTableActions = [pipeAddAction, pipeEditAction, pipeDeleteAction];
181 ngOnChanges(changes: SimpleChanges): void {
182 if (changes.expandedRow.currentValue && changes.expandedRow.currentValue.groupName) {
183 this.symmetricalFlowData = [];
184 this.directionalFlowData = [];
189 loadData(context?: any) {
190 if (this.expandedRow) {
191 this.rgwMultisiteService
192 .getSyncPolicyGroup(this.expandedRow.groupName, this.expandedRow.bucket)
195 this.symmetricalFlowData = policy.data_flow[FlowType.symmetrical] || [];
196 this.directionalFlowData = policy.data_flow[FlowType.directional] || [];
197 this.pipeData = policy.pipes || [];
208 updateSelection(selection: any, type: FlowType) {
209 if (type === FlowType.directional) {
210 this.dirFlowSelection = selection;
212 this.symFlowSelection = selection;
216 async openModal(flowType: FlowType, edit = false) {
217 const action = edit ? 'edit' : 'create';
218 const initialState = {
220 groupExpandedRow: this.expandedRow,
222 flowType === FlowType.symmetrical
223 ? this.symFlowSelection.first()
224 : this.dirFlowSelection.first(),
228 this.modalRef = this.modalService.show(RgwMultisiteSyncFlowModalComponent, initialState, {
233 const res = await this.modalRef.result;
234 if (res === NotificationType.success) {
240 deleteFlow(flowType: FlowType) {
241 this.resourceType = MultisiteResourceType.flow;
242 let selection = this.symFlowSelection;
243 if (flowType === FlowType.directional) {
244 selection = this.dirFlowSelection;
246 const flowIds = selection.selected.map((flow: any) => flow.id);
247 this.cdsModalService.show(CriticalConfirmationModalComponent, {
248 itemDescription: selection.hasSingleSelection ? $localize`Flow` : $localize`Flows`,
250 bodyTemplate: this.deleteTpl,
251 submitActionObservable: () => {
252 return new Observable((observer: Subscriber<any>) => {
254 .wrapTaskAroundCall({
255 task: new FinishedTask('rgw/multisite/sync-flow/delete', {
258 call: observableForkJoin(
259 selection.selected.map((flow: any) => {
260 return this.rgwMultisiteService.removeSyncFlow(
263 this.expandedRow.groupName,
264 this.expandedRow.bucket
270 error: (error: any) => {
271 // Forward the error to the observer.
272 observer.error(error);
273 // Reload the data table content because some deletions might
274 // have been executed successfully in the meanwhile.
275 this.table.refreshBtn();
278 // Notify the observer that we are done.
280 // Reload the data table content.
281 this.table.refreshBtn();
289 async openPipeModal(edit = false) {
290 const action = edit ? 'edit' : 'create';
291 const initialState = {
292 groupExpandedRow: this.expandedRow,
293 pipeSelectedRow: this.pipeSelection.first(),
297 this.modalRef = this.modalService.show(RgwMultisiteSyncPipeModalComponent, initialState, {
302 const res = await this.modalRef.result;
303 if (res === NotificationType.success) {
310 this.resourceType = MultisiteResourceType.pipe;
311 const pipeIds = this.pipeSelection.selected.map((pipe: any) => pipe.id);
312 this.cdsModalService.show(CriticalConfirmationModalComponent, {
313 itemDescription: this.pipeSelection.hasSingleSelection ? $localize`Pipe` : $localize`Pipes`,
315 bodyTemplate: this.deleteTpl,
316 submitActionObservable: () => {
317 return new Observable((observer: Subscriber<any>) => {
319 .wrapTaskAroundCall({
320 task: new FinishedTask('rgw/multisite/sync-pipe/delete', {
323 call: observableForkJoin(
324 this.pipeSelection.selected.map((pipe: any) => {
325 return this.rgwMultisiteService.removeSyncPipe(
327 this.expandedRow.groupName,
328 this.expandedRow.bucket
334 error: (error: any) => {
335 // Forward the error to the observer.
336 observer.error(error);
337 // Reload the data table content because some deletions might
338 // have been executed successfully in the meanwhile.
339 this.table.refreshBtn();
342 // Notify the observer that we are done.
344 // Reload the data table content.
345 this.table.refreshBtn();