]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
b95faa3140eb63a9017a7dcc312c0446587addee
[ceph-ci.git] /
1 import { Component, Input, OnChanges } from '@angular/core';
2
3 import { RgwBucketService } from '~/app/shared/api/rgw-bucket.service';
4
5 import * as xml2js from 'xml2js';
6 import { RgwRateLimitConfig } from '../models/rgw-rate-limit';
7
8 @Component({
9   selector: 'cd-rgw-bucket-details',
10   templateUrl: './rgw-bucket-details.component.html',
11   styleUrls: ['./rgw-bucket-details.component.scss']
12 })
13 export class RgwBucketDetailsComponent implements OnChanges {
14   @Input()
15   selection: any;
16   lifecycleProgress: { bucket: string; status: string; started: string };
17   lifecycleProgressMap = new Map<string, { description: string; color: string }>([
18     ['UNINITIAL', { description: $localize`The process has not run yet`, color: 'cool-gray' }],
19     ['PROCESSING', { description: $localize`The process is currently running`, color: 'cyan' }],
20     ['COMPLETE', { description: $localize`The process has completed`, color: 'green' }]
21   ]);
22   lifecycleFormat: 'json' | 'xml' = 'json';
23   aclPermissions: Record<string, string[]> = {};
24   replicationStatus = $localize`Disabled`;
25   bucketRateLimit: RgwRateLimitConfig;
26
27   constructor(private rgwBucketService: RgwBucketService) {}
28
29   ngOnChanges() {
30     this.updateBucketDetails(this.extraxtDetailsfromResponse.bind(this));
31   }
32
33   parseXmlAcl(xml: any, bucketOwner: string): Record<string, string[]> {
34     const parser = new xml2js.Parser({ explicitArray: false, trim: true });
35     let data: Record<string, string[]> = {
36       Owner: ['-'],
37       AllUsers: ['-'],
38       AuthenticatedUsers: ['-']
39     };
40     parser.parseString(xml, (err, result) => {
41       if (err) return null;
42
43       const xmlGrantees: any = result['AccessControlPolicy']['AccessControlList']['Grant'];
44       if (Array.isArray(xmlGrantees)) {
45         for (let i = 0; i < xmlGrantees.length; i++) {
46           const grantee = xmlGrantees[i];
47           if (grantee?.Grantee?.URI) {
48             const granteeGroup = grantee.Grantee.URI.split('/').pop();
49             if (data[granteeGroup].includes('-')) {
50               data[granteeGroup] = [grantee?.Permission];
51             } else {
52               data[granteeGroup].push(grantee?.Permission);
53             }
54           }
55           if (grantee?.Grantee?.ID && bucketOwner === grantee?.Grantee?.ID) {
56             data['Owner'] = grantee?.Permission;
57           }
58         }
59       } else {
60         if (xmlGrantees?.Grantee?.ID && bucketOwner === xmlGrantees?.Grantee?.ID) {
61           data['Owner'] = xmlGrantees?.Permission;
62         }
63       }
64     });
65     return data;
66   }
67
68   updateBucketDetails(cbFn: Function) {
69     if (this.selection) {
70       this.rgwBucketService.get(this.selection.bid).subscribe((bucket: object) => {
71         bucket['lock_retention_period_days'] = this.rgwBucketService.getLockDays(bucket);
72         this.selection = bucket;
73         cbFn();
74       });
75     }
76   }
77
78   extraxtDetailsfromResponse() {
79     this.aclPermissions = this.parseXmlAcl(this.selection.acl, this.selection.owner);
80     if (this.selection.replication?.['Rule']?.['Status']) {
81       this.replicationStatus = this.selection.replication?.['Rule']?.['Status'];
82     }
83     this.rgwBucketService.getBucketRateLimit(this.selection.bid).subscribe((resp: any) => {
84       if (resp && resp.bucket_ratelimit !== undefined) {
85         this.bucketRateLimit = resp.bucket_ratelimit;
86       }
87     });
88     this.extractLifecycleDetails();
89   }
90
91   extractLifecycleDetails() {
92     if (this.lifecycleFormat === 'json' && !this.selection.lifecycle) {
93       this.selection.lifecycle = {};
94     }
95     if (this.selection.lifecycle_progress?.length > 0) {
96       this.selection.lifecycle_progress.forEach(
97         (progress: { bucket: string; status: string; started: string }) => {
98           if (progress.bucket.includes(this.selection.bucket)) {
99             this.lifecycleProgress = progress;
100           }
101         }
102       );
103     }
104   }
105 }