Skip to content

Commit

Permalink
feat: add multiple lifecycle rules (#2062)
Browse files Browse the repository at this point in the history
* feat: add multiple lifecycle rules

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* change to rules

Co-authored-by: Daniel Bankhead <danielbankhead@google.com>

* modified doc comment

* changed to LifecycleRule[]

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
Co-authored-by: Daniel Bankhead <danielbankhead@google.com>
  • Loading branch information
3 people committed Sep 8, 2022
1 parent 5007542 commit fbe2deb
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1148,16 +1148,16 @@ class Bucket extends ServiceObject {
}

addLifecycleRule(
rule: LifecycleRule,
rule: LifecycleRule | LifecycleRule[],
options?: AddLifecycleRuleOptions
): Promise<SetBucketMetadataResponse>;
addLifecycleRule(
rule: LifecycleRule,
rule: LifecycleRule | LifecycleRule[],
options: AddLifecycleRuleOptions,
callback: SetBucketMetadataCallback
): void;
addLifecycleRule(
rule: LifecycleRule,
rule: LifecycleRule | LifecycleRule[],
callback: SetBucketMetadataCallback
): void;
/**
Expand Down Expand Up @@ -1187,10 +1187,14 @@ class Bucket extends ServiceObject {
* will be included to the existing policy. To replace all existing rules,
* supply the `options` argument, setting `append` to `false`.
*
* To add multiple rules, pass a list to the `rule` parameter. Calling this
* function multiple times asynchronously does not guarantee that all rules
* are added correctly.
*
* See {@link https://cloud.google.com/storage/docs/lifecycle| Object Lifecycle Management}
* See {@link https://cloud.google.com/storage/docs/json_api/v1/buckets/patch| Buckets: patch API Documentation}
*
* @param {LifecycleRule} rule The new lifecycle rule to be added to objects
* @param {LifecycleRule|LifecycleRule[]} rule The new lifecycle rule or rules to be added to objects
* in this bucket.
* @param {string|object} rule.action The action to be taken upon matching of
* all the conditions 'delete', 'setStorageClass', or 'AbortIncompleteMultipartUpload'.
Expand Down Expand Up @@ -1325,7 +1329,7 @@ class Bucket extends ServiceObject {
* ```
*/
addLifecycleRule(
rule: LifecycleRule,
rule: LifecycleRule | LifecycleRule[],
optionsOrCallback?: AddLifecycleRuleOptions | SetBucketMetadataCallback,
callback?: SetBucketMetadataCallback
): Promise<SetBucketMetadataResponse> | void {
Expand All @@ -1339,7 +1343,9 @@ class Bucket extends ServiceObject {

options = options || {};

const newLifecycleRules = arrify(rule).map(rule => {
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.
Expand Down
40 changes: 40 additions & 0 deletions test/bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,46 @@ describe('Bucket', () => {
bucket.addLifecycleRule(newRule, assert.ifError);
});

it('should accept multiple rules', done => {
const existingRule = {
action: {
type: 'type',
},
condition: {},
};

const newRules = [
{
action: {
type: 'type',
},
condition: {},
},
{
action: {
type: 'type2',
},
condition: {},
},
];

bucket.getMetadata = (callback: GetBucketMetadataCallback) => {
callback(null, {lifecycle: {rule: [existingRule]}}, {});
};

bucket.setMetadata = (metadata: Metadata) => {
assert.strictEqual(metadata.lifecycle.rule.length, 3);
assert.deepStrictEqual(metadata.lifecycle.rule, [
existingRule,
newRules[0],
newRules[1],
]);
done();
};

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

it('should pass error from getMetadata to callback', done => {
const error = new Error('from getMetadata');
const rule = {
Expand Down

0 comments on commit fbe2deb

Please sign in to comment.