-
Notifications
You must be signed in to change notification settings - Fork 206
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
GCS Enhancement #981
GCS Enhancement #981
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor nits. Hoping we can find a way to reduce some repetition in code.
src/v2/providers/storage.ts
Outdated
/** @hidden */ | ||
export const provider = 'google.cloud.storage'; | ||
/** @hidden */ | ||
export const service = 'storage.googleapis.com'; | ||
/** @hidden */ | ||
export const archivedEvent = 'object.v1.archived'; | ||
/** @hidden */ | ||
export const finalizedEvent = 'object.v1.finalized'; | ||
/** @hidden */ | ||
export const deletedEvent = 'object.v1.deleted'; | ||
/** @hidden */ | ||
export const metadataUpdatedEvent = 'object.v1.metadataUpdated'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to export these constants?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm using them in the unit tests
src/v2/providers/storage.ts
Outdated
if (arguments.length === 1) { | ||
handler = optsOrHandler as ( | ||
event: CloudEvent<StorageObjectData> | ||
) => any | Promise<any>; | ||
_onOperation(handler, archivedEvent); | ||
} | ||
return _onOperation( | ||
handler, | ||
archivedEvent, | ||
optsOrHandler as string | StorageOptions | ||
); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm, it looks like we repeat this block for each of the four event type. It would be cool if we can contain all "make sure we handle optOrHandler
appropriately in _onOperation
; can we start a discussion in our chat room to see if there's a clever TS thing we can do?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've cleaned it up and moved the logic into _onOperation
. The 4 functions now only serve to inject the correct event type.
src/v2/providers/storage.ts
Outdated
* An object within Google Cloud Storage. | ||
* Ref: https://github.com/googleapis/google-cloudevents-nodejs/blob/main/cloud/storage/v1/StorageObjectData.ts | ||
*/ | ||
export interface StorageObjectData { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit* Can this be declared at the top of the file instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved to the top
src/v2/providers/storage.ts
Outdated
}, | ||
eventTrigger: { | ||
eventType: provider + '.' + eventType, | ||
resource: bucket, // TODO(colerogers): replace with bucket: 'my-bucket' when container contract is finished |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you expand on what you mean by this TODO? I'm a confused by the suggestion that we should set this to a constant my-bucket
. Also probably better to leave out mentioning container contract here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, I removed the CC part. Eventually, we'll need to remove the resource field and just map to a bucket field.
src/v2/providers/storage.ts
Outdated
'Missing bucket name. If you are unit testing, please provide a bucket name' + | ||
' through set process.env.FIREBASE_CONFIG.' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'Missing bucket name. If you are unit testing, please provide a bucket name' + | |
' through set process.env.FIREBASE_CONFIG.' | |
'Missing bucket name. If you are unit testing, please provide a bucket name' + | |
' by providing bucket name directly in the event handler or by setting process.env.FIREBASE_CONFIG.' | |
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
src/v2/providers/storage.ts
Outdated
): CloudFunction<StorageObjectData>; | ||
|
||
export function onObjectMetadataUpdated( | ||
optsOrHandler: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nits* It's a mouthful, but I think I would prefer butcketOrOptsOrHandler
for accuracy.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense, fixed
spec/v2/providers/storage.spec.ts
Outdated
}; | ||
|
||
beforeEach(() => { | ||
options.setGlobalOptions({}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems redundant no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed
|
||
beforeEach(() => { | ||
options.setGlobalOptions({}); | ||
process.env.GCLOUD_PROJECT = 'aProject'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm can you remind me why setting this environment variable is required for the test to work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we don't set it, then when we try and perform optionsToTriggerAnnotations
it will call serviceAccountFromShorthand
which requires this env var to be set and throw an error otherwise
spec/v2/providers/storage.spec.ts
Outdated
const EVENT_TRIGGER = { | ||
eventType: 'google.cloud.storage.event-type', | ||
resource: 'my-bucket', | ||
service: 'storage.googleapis.com', | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit* I'd make this module-level constant instead of scoping it to this test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved to the top and added in specific event constants for each event type.
}, | ||
regions: ['us-west1'], | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we also test where opt is provided without a bucket
property?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added tests for each event type tests and the onOperation tests.
src/v2/providers/storage.ts
Outdated
handler: (event: CloudEvent<StorageObjectData>) => any | Promise<any> | ||
): CloudFunction<StorageObjectData>; | ||
|
||
/** Handle a storage object archived */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ugh does ESLint for us to repeat JSDoc comment on each of the overloaded function 🤦♂️ ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was following what we did with pubsub, but since GCS has a lot more overloads, I removed the repeated ones and left the top comment.
src/v2/providers/storage.ts
Outdated
// handler: (event: CloudEvent<StorageObjectData>) => any | Promise<any>, | ||
// eventType: string, | ||
// bucketOrOpts?: string | StorageOptions |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// handler: (event: CloudEvent<StorageObjectData>) => any | Promise<any>, | |
// eventType: string, | |
// bucketOrOpts?: string | StorageOptions |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, removed
src/v2/providers/storage.ts
Outdated
| ((event: CloudEvent<StorageObjectData>) => any | Promise<any>), | ||
handler?: (event: CloudEvent<StorageObjectData>) => any | Promise<any> | ||
): CloudFunction<StorageObjectData> { | ||
if (arguments.length === 2) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't there a subtle bug here since _onOperation
is always called with 3 arguments now?
e.g.
> function foo(a, b) { return arguments.length }
undefined
> foo(1)
1
> foo(1, 2)
2
> foo(1, undefined)
2
So I'd expect arguments.length
to always be 3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this will work:
if (typeof bucketOrOptsOrHandler === "function") {
...
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh dang, yeah that's a bug alright. For some reason in my head passing an undefined handler was equivalent to leaving off the last parameter. Thanks for catching this!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm; I'd recommend we wait until @inlined review as this is our first major enhancement on v2 provider namespace outside of his code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry... I reviewed this a while ago and never hit "submit"
src/v2/providers/storage.ts
Outdated
} | ||
|
||
/** @hidden */ | ||
export function _getOptsAndBucket( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed the _
prefixes and changed to using @internal
. The typedoc
docs indicate that both hidden and internal will remove from the code from documentation as long as we don't pass in --excludeInternal
.
Description
Enhancement to how customers use GCS Triggered Functions.
Code sample