</div>
<td>
<pre *ngIf="lifecycleFormat === 'json'">{{selection.lifecycle | json}}</pre>
- <pre *ngIf="lifecycleFormat === 'xml'">{{ (selection.lifecycle | xml) || '-'}}</pre>
+ <pre *ngIf="lifecycleFormat === 'xml'">{{ (selection.lifecycle | xml:{'Rules':'Rule'}) || '-'}}</pre>
</td>
</tr>
<tr>
export class XmlPipe implements PipeTransform {
constructor(private jsonToXmlService: JsonToXmlService) {}
- transform(value: string, valueFormat: string = 'json'): string {
+ transform(
+ value: string,
+ replaceKey: Record<string, string> = {},
+ valueFormat: string = 'json'
+ ): string {
if (valueFormat === 'json') {
- value = this.jsonToXmlService.format(value);
+ value = this.jsonToXmlService.format(value, replaceKey);
}
return value;
}
export class JsonToXmlService {
constructor() {}
- format(json: any, indentSize: number = 2, currentIndent: number = 0): string {
+ format(
+ json: any,
+ replaceKey: Record<string, string> = null,
+ indentSize: number = 2,
+ currentIndent: number = 0
+ ): string {
if (!json) return null;
let xml = '';
if (typeof json === 'string') {
json = JSON.parse(json);
}
- for (const key in json) {
+ for (let key in json) {
if (json.hasOwnProperty(key)) {
const value = json[key];
const indentation = ' '.repeat(currentIndent);
-
+ if (replaceKey) {
+ const [oldKey, newKey] = Object.entries(replaceKey)[0];
+ if (key === oldKey) {
+ key = newKey;
+ }
+ }
if (Array.isArray(value)) {
value.forEach((item) => {
xml +=
`${indentation}<${key}>\n` +
- this.format(item, indentSize, currentIndent + indentSize) +
+ this.format(item, replaceKey, indentSize, currentIndent + indentSize) +
`${indentation}</${key}>\n`;
});
} else if (typeof value === 'object') {
xml +=
`${indentation}<${key}>\n` +
- this.format(value, indentSize, currentIndent + indentSize) +
+ this.format(value, replaceKey, indentSize, currentIndent + indentSize) +
`${indentation}</${key}>\n`;
} else {
xml += `${indentation}<${key}>${value}</${key}>\n`;
import os
import re
import xml.etree.ElementTree as ET # noqa: N814
+from collections import defaultdict
from enum import Enum
from subprocess import SubprocessError
raise DashboardException(msg=str(e), component='rgw')
return result
+ @staticmethod
+ def _handle_rules(pairs):
+ result = defaultdict(list)
+ for key, value in pairs:
+ if key == 'Rule':
+ result['Rules'].append(value)
+ else:
+ result[key] = value
+ return result
+
@RestClient.api_get('/{bucket_name}?lifecycle')
def get_lifecycle(self, bucket_name, request=None):
# pylint: disable=unused-argument
try:
- result = request() # type: ignore
- result = {'LifecycleConfiguration': result}
+ decoded_request = request(raw_content=True).decode("utf-8") # type: ignore
+ result = {
+ 'LifecycleConfiguration':
+ json.loads(
+ decoded_request,
+ object_pairs_hook=RgwClient._handle_rules
+ )
+ }
except RequestException as e:
if e.content:
content = json_str_to_object(e.content)
lifecycle = RgwClient.dict_to_xml(lifecycle)
try:
if lifecycle and '<LifecycleConfiguration>' not in str(lifecycle):
- lifecycle = f'<LifecycleConfiguration>{lifecycle}</LifecycleConfiguration>'
+ lifecycle = f'<LifecycleConfiguration>\n{lifecycle}\n</LifecycleConfiguration>'
result = request(data=lifecycle) # type: ignore
except RequestException as e:
+ msg = ''
if e.content:
content = json_str_to_object(e.content)
if content.get("Code") == "MalformedXML":
msg = "Invalid Lifecycle document"
- raise DashboardException(msg=msg, component='rgw')
- raise DashboardException(msg=str(e), component='rgw')
+ raise DashboardException(msg=msg or str(e), component='rgw')
return result
@RestClient.api_delete('/{bucket_name}?lifecycle')