Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: stronger typing for lifecycle rules #2215

Merged
merged 1 commit into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions conformance-test/libraryMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ export async function addLifecycleRuleInstancePrecondition(
options: ConformanceTestOptions
) {
await options.bucket!.addLifecycleRule({
action: 'delete',
action: {
type: 'Delete',
},
condition: {
age: 365 * 3, // Specified in days.
},
Expand All @@ -53,7 +55,9 @@ export async function addLifecycleRule(options: ConformanceTestOptions) {
if (options.preconditionRequired) {
await options.bucket!.addLifecycleRule(
{
action: 'delete',
action: {
type: 'Delete',
},
condition: {
age: 365 * 3, // Specified in days.
},
Expand All @@ -64,7 +68,9 @@ export async function addLifecycleRule(options: ConformanceTestOptions) {
);
} else {
await options.bucket!.addLifecycleRule({
action: 'delete',
action: {
type: 'Delete',
},
condition: {
age: 365 * 3, // Specified in days.
},
Expand Down
73 changes: 36 additions & 37 deletions src/bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,30 @@ export interface AddLifecycleRuleOptions extends PreconditionOptions {
append?: boolean;
}

export interface LifecycleRule {
action: {type: string; storageClass?: string} | string;
condition: {[key: string]: boolean | Date | number | string | string[]};
export interface LifecycleAction {
type: 'Delete' | 'SetStorageClass' | 'AbortIncompleteMultipartUpload';
storageClass?: string;
}

export interface LifecycleCondition {
age?: number;
createdBefore?: Date | string;
customTimeBefore?: Date | string;
daysSinceCustomTime?: number;
daysSinceNoncurrentTime?: number;
isLive?: boolean;
matchesPrefix?: string[];
matchesSuffix?: string[];
matchesStorageClass?: string[];
noncurrentTimeBefore?: Date | string;
numNewerVersions?: number;
}

export interface LifecycleRule {
action: LifecycleAction;
condition: LifecycleCondition;
}

export interface EnableLoggingOptions extends PreconditionOptions {
bucket?: string | Bucket;
prefix: string;
Expand Down Expand Up @@ -1344,46 +1362,27 @@ class Bucket extends ServiceObject {
options = options || {};

const rules = Array.isArray(rule) ? rule : [rule];

const newLifecycleRules = rules.map(rule => {
if (typeof rule.action === 'object') {
// This is a raw-formatted rule object, the way the API expects.
// Just pass it through as-is.
return rule;
for (const curRule of rules) {
if (curRule.condition.createdBefore instanceof Date) {
curRule.condition.createdBefore = curRule.condition.createdBefore
.toISOString()
.replace(/T.+$/, '');
}

const apiFormattedRule = {} as LifecycleRule;

apiFormattedRule.condition = {};
apiFormattedRule.action = {
type: rule.action.charAt(0).toUpperCase() + rule.action.slice(1),
};

if (rule.storageClass) {
apiFormattedRule.action.storageClass = rule.storageClass;
if (curRule.condition.customTimeBefore instanceof Date) {
curRule.condition.customTimeBefore = curRule.condition.customTimeBefore
.toISOString()
.replace(/T.+$/, '');
}

for (const condition in rule.condition) {
if (rule.condition[condition] instanceof Date) {
apiFormattedRule.condition[condition] = (
rule.condition[condition] as Date
)
if (curRule.condition.noncurrentTimeBefore instanceof Date) {
curRule.condition.noncurrentTimeBefore =
curRule.condition.noncurrentTimeBefore
.toISOString()
.replace(/T.+$/, '');
} else {
apiFormattedRule.condition[condition] = rule.condition[condition];
}
}

return apiFormattedRule;
});
}

if (options.append === false) {
this.setMetadata(
{lifecycle: {rule: newLifecycleRules}},
options,
callback!
);
this.setMetadata({lifecycle: {rule: rules}}, options, callback!);
return;
}

Expand All @@ -1404,7 +1403,7 @@ class Bucket extends ServiceObject {
this.setMetadata(
{
lifecycle: {
rule: currentLifecycleRules.concat(newLifecycleRules),
rule: currentLifecycleRules.concat(rules),
},
},
options as AddLifecycleRuleOptions,
Expand Down
34 changes: 25 additions & 9 deletions system-test/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1165,7 +1165,9 @@ describe('storage', () => {
describe('bucket object lifecycle management', () => {
it('should add a rule', async () => {
await bucket.addLifecycleRule({
action: 'delete',
action: {
type: 'Delete',
},
condition: {
age: 30,
isLive: true,
Expand All @@ -1190,19 +1192,23 @@ describe('storage', () => {
0;

await bucket.addLifecycleRule({
action: 'delete',
action: {
type: 'Delete',
},
condition: {
age: 30,
isLive: true,
},
});
await bucket.addLifecycleRule({
action: 'setStorageClass',
action: {
type: 'SetStorageClass',
storageClass: 'coldline',
},
condition: {
age: 60,
isLive: true,
},
storageClass: 'coldline',
});
assert.strictEqual(
bucket.metadata.lifecycle.rule.length,
Expand All @@ -1212,7 +1218,9 @@ describe('storage', () => {

it('should add a prefix rule', async () => {
await bucket.addLifecycleRule({
action: 'delete',
action: {
type: 'Delete',
},
condition: {
matchesPrefix: [TESTS_PREFIX],
},
Expand All @@ -1232,7 +1240,9 @@ describe('storage', () => {

it('should add a suffix rule', async () => {
await bucket.addLifecycleRule({
action: 'delete',
action: {
type: 'Delete',
},
condition: {
matchesSuffix: [TESTS_PREFIX, 'test_suffix'],
},
Expand All @@ -1250,7 +1260,9 @@ describe('storage', () => {

it('should convert a rule with createdBefore to a date in string', async () => {
await bucket.addLifecycleRule({
action: 'delete',
action: {
type: 'Delete',
},
condition: {
createdBefore: new Date('2018'),
},
Expand All @@ -1270,7 +1282,9 @@ describe('storage', () => {
const NONCURRENT_TIME_BEFORE = '2020-01-01';

await bucket.addLifecycleRule({
action: 'delete',
action: {
type: 'Delete',
},
condition: {
noncurrentTimeBefore: new Date(NONCURRENT_TIME_BEFORE),
daysSinceNoncurrentTime: 100,
Expand All @@ -1292,7 +1306,9 @@ describe('storage', () => {
const CUSTOM_TIME_BEFORE = '2020-01-01';

await bucket.addLifecycleRule({
action: 'delete',
action: {
type: 'Delete',
},
condition: {
customTimeBefore: new Date(CUSTOM_TIME_BEFORE),
daysSinceCustomTime: 100,
Expand Down
56 changes: 7 additions & 49 deletions test/bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,55 +478,11 @@ describe('Bucket', () => {
bucket.addLifecycleRule(rule, assert.ifError);
});

it('should properly capitalize rule action', done => {
const rule = {
action: 'delete',
condition: {},
};

bucket.setMetadata = (metadata: Metadata) => {
assert.deepStrictEqual(metadata.lifecycle.rule, [
{
action: {
type: rule.action.charAt(0).toUpperCase() + rule.action.slice(1),
},
condition: rule.condition,
},
]);

done();
};

bucket.addLifecycleRule(rule, assert.ifError);
});

it('should properly set the storage class', done => {
const rule = {
action: 'setStorageClass',
storageClass: 'storage class',
condition: {},
};

bucket.setMetadata = (metadata: Metadata) => {
assert.deepStrictEqual(metadata.lifecycle.rule, [
{
action: {
type: rule.action.charAt(0).toUpperCase() + rule.action.slice(1),
storageClass: rule.storageClass,
},
condition: rule.condition,
},
]);

done();
};

bucket.addLifecycleRule(rule, assert.ifError);
});

it('should properly set condition', done => {
const rule = {
action: 'delete',
action: {
type: 'Delete',
},
condition: {
age: 30,
},
Expand All @@ -536,7 +492,7 @@ describe('Bucket', () => {
assert.deepStrictEqual(metadata.lifecycle.rule, [
{
action: {
type: rule.action.charAt(0).toUpperCase() + rule.action.slice(1),
type: 'Delete',
},
condition: rule.condition,
},
Expand All @@ -551,7 +507,9 @@ describe('Bucket', () => {
const date = new Date();

const rule = {
action: 'delete',
action: {
type: 'Delete',
},
condition: {
createdBefore: date,
},
Expand Down