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';
21 selector: 'cd-rgw-multisite-sync-policy-details',
22 templateUrl: './rgw-multisite-sync-policy-details.component.html',
23 styleUrls: ['./rgw-multisite-sync-policy-details.component.scss']
25 export class RgwMultisiteSyncPolicyDetailsComponent implements OnChanges {
29 permission: Permission;
31 @ViewChild(TableComponent)
32 table: TableComponent;
33 @ViewChild('deleteTpl', { static: true })
34 deleteTpl: TemplateRef<any>;
37 modalRef: NgbModalRef;
38 symmetricalFlowData: any = [];
39 directionalFlowData: any = [];
41 symmetricalFlowCols: CdTableColumn[];
42 directionalFlowCols: CdTableColumn[];
43 pipeCols: CdTableColumn[];
44 symFlowTableActions: CdTableAction[];
45 dirFlowTableActions: CdTableAction[];
46 pipeTableActions: CdTableAction[];
47 symFlowSelection = new CdTableSelection();
48 dirFlowSelection = new CdTableSelection();
49 pipeSelection = new CdTableSelection();
52 private actionLabels: ActionLabelsI18n,
53 private modalService: ModalService,
54 private rgwMultisiteService: RgwMultisiteService,
55 private taskWrapper: TaskWrapperService
57 this.symmetricalFlowCols = [
69 this.directionalFlowCols = [
76 name: 'Destination Zone',
93 name: 'Destination Zone',
98 name: 'Source Bucket',
99 prop: 'source.bucket',
103 name: 'Destination Bucket',
108 const symAddAction: CdTableAction = {
109 permission: 'create',
111 name: this.actionLabels.CREATE,
112 click: () => this.openModal(FlowType.symmetrical),
113 canBePrimary: (selection: CdTableSelection) => !selection.hasSelection
115 const symEditAction: CdTableAction = {
116 permission: 'update',
118 name: this.actionLabels.EDIT,
119 click: () => this.openModal(FlowType.symmetrical, true)
121 const symDeleteAction: CdTableAction = {
122 permission: 'delete',
124 disable: () => !this.symFlowSelection.hasSelection,
125 name: this.actionLabels.DELETE,
126 click: () => this.deleteFlow(FlowType.symmetrical),
127 canBePrimary: (selection: CdTableSelection) => selection.hasMultiSelection
129 this.symFlowTableActions = [symAddAction, symEditAction, symDeleteAction];
130 const dirAddAction: CdTableAction = {
131 permission: 'create',
133 name: this.actionLabels.CREATE,
134 click: () => this.openModal(FlowType.directional),
135 canBePrimary: (selection: CdTableSelection) => !selection.hasSelection
137 const dirEditAction: CdTableAction = {
138 permission: 'update',
140 name: this.actionLabels.EDIT,
141 click: () => this.openModal(FlowType.directional, true),
142 disable: () => true // TODO: disabling 'edit' as we are not getting flow ID from backend which is needed for edit
144 const dirDeleteAction: CdTableAction = {
145 permission: 'delete',
147 disable: () => true, // TODO: disabling 'delete' as we are not getting flow ID from backend which is needed for deletion
148 name: this.actionLabels.DELETE,
149 click: () => this.deleteFlow(FlowType.directional),
150 canBePrimary: (selection: CdTableSelection) => selection.hasMultiSelection
152 this.dirFlowTableActions = [dirAddAction, dirEditAction, dirDeleteAction];
153 const pipeAddAction: CdTableAction = {
154 permission: 'create',
156 name: this.actionLabels.CREATE,
157 click: () => this.openPipeModal(),
158 canBePrimary: (selection: CdTableSelection) => !selection.hasSelection
160 const pipeEditAction: CdTableAction = {
161 permission: 'update',
163 name: this.actionLabels.EDIT,
164 click: () => this.openPipeModal(true)
166 const pipeDeleteAction: CdTableAction = {
167 permission: 'delete',
169 disable: () => !this.pipeSelection.hasSelection,
170 name: this.actionLabels.DELETE,
171 click: () => this.deletePipe(),
172 canBePrimary: (selection: CdTableSelection) => selection.hasMultiSelection
174 this.pipeTableActions = [pipeAddAction, pipeEditAction, pipeDeleteAction];
177 ngOnChanges(changes: SimpleChanges): void {
178 if (changes.expandedRow.currentValue && changes.expandedRow.currentValue.groupName) {
179 this.symmetricalFlowData = [];
180 this.directionalFlowData = [];
185 loadData(context?: any) {
186 if (this.expandedRow) {
187 this.rgwMultisiteService
188 .getSyncPolicyGroup(this.expandedRow.groupName, this.expandedRow.bucket)
191 this.symmetricalFlowData = policy.data_flow[FlowType.symmetrical] || [];
192 this.directionalFlowData = policy.data_flow[FlowType.directional] || [];
193 this.pipeData = policy.pipes || [];
204 updateSelection(selection: any, type: FlowType) {
205 if (type === FlowType.directional) {
206 this.dirFlowSelection = selection;
208 this.symFlowSelection = selection;
212 async openModal(flowType: FlowType, edit = false) {
213 const action = edit ? 'edit' : 'create';
214 const initialState = {
216 groupExpandedRow: this.expandedRow,
218 flowType === FlowType.symmetrical
219 ? this.symFlowSelection.first()
220 : this.dirFlowSelection.first(),
224 this.modalRef = this.modalService.show(RgwMultisiteSyncFlowModalComponent, initialState, {
229 const res = await this.modalRef.result;
230 if (res === 'success') {
236 deleteFlow(flowType: FlowType) {
237 let selection = this.symFlowSelection;
238 if (flowType === FlowType.directional) {
239 selection = this.dirFlowSelection;
241 const flowIds = selection.selected.map((flow: any) => flow.id);
242 this.modalService.show(CriticalConfirmationModalComponent, {
243 itemDescription: selection.hasSingleSelection ? $localize`Flow` : $localize`Flows`,
245 bodyTemplate: this.deleteTpl,
246 submitActionObservable: () => {
247 return new Observable((observer: Subscriber<any>) => {
249 .wrapTaskAroundCall({
250 task: new FinishedTask('rgw/multisite/sync-flow/delete', {
253 call: observableForkJoin(
254 selection.selected.map((flow: any) => {
255 return this.rgwMultisiteService.removeSyncFlow(
258 this.expandedRow.groupName,
259 this.expandedRow.bucket
265 error: (error: any) => {
266 // Forward the error to the observer.
267 observer.error(error);
268 // Reload the data table content because some deletions might
269 // have been executed successfully in the meanwhile.
270 this.table.refreshBtn();
273 // Notify the observer that we are done.
275 // Reload the data table content.
276 this.table.refreshBtn();
284 async openPipeModal(edit = false) {
285 const action = edit ? 'edit' : 'create';
286 const initialState = {
287 groupExpandedRow: this.expandedRow,
288 pipeSelectedRow: this.pipeSelection.first(),
292 this.modalRef = this.modalService.show(RgwMultisiteSyncPipeModalComponent, initialState, {
297 const res = await this.modalRef.result;
298 if (res === 'success') {
305 const pipeIds = this.pipeSelection.selected.map((pipe: any) => pipe.id);
306 this.modalService.show(CriticalConfirmationModalComponent, {
307 itemDescription: this.pipeSelection.hasSingleSelection ? $localize`Pipe` : $localize`Pipes`,
309 bodyTemplate: this.deleteTpl,
310 submitActionObservable: () => {
311 return new Observable((observer: Subscriber<any>) => {
313 .wrapTaskAroundCall({
314 task: new FinishedTask('rgw/multisite/sync-pipe/delete', {
317 call: observableForkJoin(
318 this.pipeSelection.selected.map((pipe: any) => {
319 return this.rgwMultisiteService.removeSyncPipe(
321 this.expandedRow.groupName,
322 this.expandedRow.bucket
328 error: (error: any) => {
329 // Forward the error to the observer.
330 observer.error(error);
331 // Reload the data table content because some deletions might
332 // have been executed successfully in the meanwhile.
333 this.table.refreshBtn();
336 // Notify the observer that we are done.
338 // Reload the data table content.
339 this.table.refreshBtn();