Skip to content

Commit ef0c028

Browse files
committed
ARSN-528: add BucketLoggingStatus to BucketInfo model
1 parent fb44b3b commit ef0c028

File tree

2 files changed

+168
-3
lines changed

2 files changed

+168
-3
lines changed

lib/models/BucketInfo.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { ACL as OACL } from './ObjectMD';
1111
import { areTagsValid, BucketTag } from '../s3middleware/tagging';
1212
import { VeeamCapability, VeeamSOSApiSchema, VeeamSOSApiSerializable } from './Veeam';
1313
import { AzureInfoMetadata } from './BucketAzureInfo';
14+
import BucketLoggingStatus from './BucketLoggingStatus';
1415

1516
// WHEN UPDATING THIS NUMBER, UPDATE BucketInfoModelVersion.md CHANGELOG
1617
// BucketInfoModelVersion.md can be found in documentation/ at the root
@@ -78,6 +79,7 @@ export type BucketMetadata = {
7879
tags: Array<BucketTag>,
7980
capabilities?: Capabilities,
8081
quotaMax: bigint | number,
82+
bucketLoggingStatus?: BucketLoggingStatus,
8183
};
8284

8385
export type BucketMetadataJSON = Omit<BucketMetadata, 'quotaMax' | 'capabilities'> & {
@@ -115,6 +117,7 @@ export default class BucketInfo implements BucketMetadata {
115117
private _ingestion?: { status: 'enabled' | 'disabled' };
116118
private _capabilities?: Capabilities;
117119
private _quotaMax: bigint;
120+
private _bucketLoggingStatus?: BucketLoggingStatus;
118121

119122
/**
120123
* Represents all bucket information.
@@ -201,6 +204,7 @@ export default class BucketInfo implements BucketMetadata {
201204
tags?: Array<BucketTag> | [],
202205
capabilities?: Capabilities,
203206
quotaMax?: bigint | number,
207+
bucketLoggingStatus?: BucketLoggingStatus,
204208
) {
205209
assert.strictEqual(typeof name, 'string');
206210
assert.strictEqual(typeof owner, 'string');
@@ -327,6 +331,10 @@ export default class BucketInfo implements BucketMetadata {
327331
}
328332
assert.strictEqual(areTagsValid(tags), true);
329333

334+
if (bucketLoggingStatus) {
335+
assert(bucketLoggingStatus instanceof BucketLoggingStatus);
336+
}
337+
330338
// IF UPDATING PROPERTIES, INCREMENT MODELVERSION NUMBER ABOVE
331339
this._acl = aclInstance;
332340
this._name = name;
@@ -353,6 +361,7 @@ export default class BucketInfo implements BucketMetadata {
353361
this._objectLockConfiguration = objectLockConfiguration;
354362
this._notificationConfiguration = notificationConfiguration;
355363
this._tags = tags;
364+
this._bucketLoggingStatus = bucketLoggingStatus;
356365

357366
this._capabilities = capabilities && {
358367
...capabilities,
@@ -401,6 +410,7 @@ export default class BucketInfo implements BucketMetadata {
401410
VeeamCapability.serialize(this._capabilities.VeeamSOSApi),
402411
},
403412
quotaMax: this._quotaMax.toString(),
413+
bucketLoggingStatus: this._bucketLoggingStatus,
404414
};
405415
const final = this._websiteConfiguration
406416
? {
@@ -441,7 +451,7 @@ export default class BucketInfo implements BucketMetadata {
441451
obj.bucketPolicy, obj.uid, obj.readLocationConstraint, obj.isNFS,
442452
obj.ingestion, obj.azureInfo, obj.objectLockEnabled,
443453
obj.objectLockConfiguration, obj.notificationConfiguration, obj.tags,
444-
capabilities, BigInt(obj.quotaMax || 0n));
454+
capabilities, BigInt(obj.quotaMax || 0n), obj.bucketLoggingStatus);
445455
}
446456

447457
/**
@@ -474,7 +484,7 @@ export default class BucketInfo implements BucketMetadata {
474484
data._isNFS, data._ingestion, data._azureInfo,
475485
data._objectLockEnabled, data._objectLockConfiguration,
476486
data._notificationConfiguration, data._tags, capabilities,
477-
BigInt(data._quotaMax || 0n));
487+
BigInt(data._quotaMax || 0n), data._bucketLoggingStatus);
478488
}
479489

480490
/**
@@ -497,7 +507,7 @@ export default class BucketInfo implements BucketMetadata {
497507
...data.capabilities,
498508
VeeamSOSApi: data.capabilities?.VeeamSOSApi &&
499509
VeeamCapability.parse(data.capabilities?.VeeamSOSApi),
500-
}, BigInt(data.quotaMax || 0n));
510+
}, BigInt(data.quotaMax || 0n), data.bucketLoggingStatus);
501511
}
502512

503513
/**
@@ -1070,4 +1080,22 @@ export default class BucketInfo implements BucketMetadata {
10701080
this._quotaMax = BigInt(quota || 0n);
10711081
return this;
10721082
}
1083+
1084+
/**
1085+
* Get bucket logging status
1086+
* @returns - bucket logging status
1087+
*/
1088+
getBucketLoggingStatus() : BucketLoggingStatus | undefined {
1089+
return this._bucketLoggingStatus;
1090+
}
1091+
1092+
/**
1093+
* Set bucket logging status
1094+
* @param bucketLoggingStatus - bucket logging status
1095+
* @returns - this
1096+
*/
1097+
setBucketLoggingStatus(bucketLoggingStatus : BucketLoggingStatus) {
1098+
this._bucketLoggingStatus = bucketLoggingStatus;
1099+
return this;
1100+
}
10731101
}

lib/models/BucketLoggingStatus.ts

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
import { parseStringPromise } from 'xml2js';
2+
import errors, { ArsenalError } from '../errors';
3+
4+
/**
5+
* Format of xml request:
6+
<?xml version="1.0" encoding="UTF-8"?>
7+
<BucketLoggingStatus xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
8+
<LoggingEnabled>
9+
<TargetBucket>string</TargetBucket>
10+
<TargetGrants>
11+
<Grant>
12+
<Grantee>
13+
<DisplayName>string</DisplayName>
14+
<EmailAddress>string</EmailAddress>
15+
<ID>string</ID>
16+
<xsi:type>string</xsi:type>
17+
<URI>string</URI>
18+
</Grantee>
19+
<Permission>string</Permission>
20+
</Grant>
21+
</TargetGrants>
22+
<TargetObjectKeyFormat>
23+
<PartitionedPrefix>
24+
<PartitionDateSource>string</PartitionDateSource>
25+
</PartitionedPrefix>
26+
<SimplePrefix>
27+
</SimplePrefix>
28+
</TargetObjectKeyFormat>
29+
<TargetPrefix>string</TargetPrefix>
30+
</LoggingEnabled>
31+
</BucketLoggingStatus>
32+
*/
33+
34+
export type LoggingEnabled = {
35+
TargetBucket: string;
36+
TargetPrefix: string;
37+
};
38+
39+
export default class BucketLoggingStatus {
40+
private _loggingEnabled?: LoggingEnabled;
41+
42+
constructor(loggingEnabled?: LoggingEnabled) {
43+
this._loggingEnabled = loggingEnabled;
44+
}
45+
46+
toXML(): string {
47+
let loggingEnabledXML = "";
48+
if (this._loggingEnabled) {
49+
loggingEnabledXML = `<LoggingEnabled>
50+
<TargetBucket>${this._loggingEnabled.TargetBucket}</TargetBucket>
51+
<TargetPrefix>${this._loggingEnabled.TargetPrefix}</TargetPrefix>
52+
</LoggingEnabled>
53+
`;
54+
}
55+
56+
return `<?xml version="1.0" encoding="UTF-8"?>
57+
<BucketLoggingStatus xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
58+
${loggingEnabledXML}
59+
</BucketLoggingStatus>
60+
`;
61+
}
62+
63+
static async fromXML(
64+
data: string,
65+
): Promise<{ error?: { arsenalError: ArsenalError, details: any }; res?: BucketLoggingStatus; }> {
66+
let parsed;
67+
try {
68+
parsed = await parseStringPromise(data);
69+
} catch (err) {
70+
return {
71+
error: { arsenalError: errors.MalformedXML, details: err },
72+
res: undefined,
73+
};
74+
}
75+
76+
if (!parsed) {
77+
return {
78+
error: { arsenalError: errors.MalformedXML, details: 'request xml is undefined or empty' },
79+
res: undefined,
80+
};
81+
}
82+
83+
if (!parsed.BucketLoggingStatus) {
84+
return {
85+
error: { arsenalError: errors.MalformedXML, details: 'missing BucketLoggingStatus root tag' },
86+
res: undefined,
87+
};
88+
}
89+
90+
let loggingEnabled : LoggingEnabled | undefined = undefined;
91+
if (parsed.BucketLoggingStatus.LoggingEnabled) {
92+
if (!parsed.BucketLoggingStatus.LoggingEnabled.TargetBucket) {
93+
return {
94+
error: { arsenalError: errors.MalformedXML, details: 'missing TargetBucket tag in LoggingEnabled' },
95+
res: undefined,
96+
};
97+
}
98+
99+
if (!parsed.BucketLoggingStatus.LoggingEnabled.TargetPrefix) {
100+
return {
101+
error: { arsenalError: errors.MalformedXML, details: 'missing TargetPrefix tag in LoggingEnabled' },
102+
res: undefined,
103+
};
104+
}
105+
106+
if (parsed.BuckerLoggingStatus.LoggingEnabled.TargetGrants) {
107+
return {
108+
error: {
109+
arsenalError: errors.NotImplemented,
110+
details: 'TargetGrants tag in LoggingEnabled is not implemented',
111+
},
112+
res: undefined,
113+
};
114+
}
115+
116+
if (parsed.BuckerLoggingStatus.LoggingEnabled.TargetObjectKeyFormat) {
117+
return {
118+
error: {
119+
arsenalError: errors.NotImplemented,
120+
details: 'TargetObjectKeyFormat tag in LoggingEnabled is not implemented',
121+
},
122+
res: undefined,
123+
};
124+
}
125+
126+
loggingEnabled = {
127+
TargetPrefix: parsed.BucketLoggingStatus.LoggingEnabled.TargetPrefix[0],
128+
TargetBucket: parsed.BucketLoggingStatus.LoggingEnabled.TargetBucket[0],
129+
};
130+
}
131+
132+
return {
133+
error: undefined,
134+
res: new BucketLoggingStatus(loggingEnabled),
135+
};
136+
}
137+
};

0 commit comments

Comments
 (0)