Skip to content

Commit 648cfdd

Browse files
authored
Bradley/r2 event notification get (#6652)
* Update R2 Get Event Notification response, display, and actions * Add open-beta marker to R2 event notification wrangler commands * changeset
1 parent 8dcd456 commit 648cfdd

File tree

8 files changed

+192
-94
lines changed

8 files changed

+192
-94
lines changed

.changeset/nice-beds-raise.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"wrangler": minor
3+
---
4+
5+
feat: Update R2 Get Event Notification response, display, and actions

packages/wrangler/src/__tests__/r2.test.ts

Lines changed: 67 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ describe("r2", () => {
9696
wrangler r2 bucket list List R2 buckets
9797
wrangler r2 bucket delete <name> Delete an R2 bucket
9898
wrangler r2 bucket sippy Manage Sippy incremental migration on an R2 bucket
99-
wrangler r2 bucket notification Manage event notifications for an R2 bucket
99+
wrangler r2 bucket notification Manage event notifications for an R2 bucket [open beta]
100100
101101
GLOBAL FLAGS
102102
-j, --experimental-json-config Experimental: support wrangler.json [boolean]
@@ -130,7 +130,7 @@ describe("r2", () => {
130130
wrangler r2 bucket list List R2 buckets
131131
wrangler r2 bucket delete <name> Delete an R2 bucket
132132
wrangler r2 bucket sippy Manage Sippy incremental migration on an R2 bucket
133-
wrangler r2 bucket notification Manage event notifications for an R2 bucket
133+
wrangler r2 bucket notification Manage event notifications for an R2 bucket [open beta]
134134
135135
GLOBAL FLAGS
136136
-j, --experimental-json-config Experimental: support wrangler.json [boolean]
@@ -772,6 +772,61 @@ describe("r2", () => {
772772
describe("notification", () => {
773773
describe("get", () => {
774774
it("follows happy path as expected", async () => {
775+
const bucketName = "my-bucket";
776+
const queueId = "471537e8-6e5a-4163-a4d4-9478087c32c3";
777+
const queueName = "my-queue";
778+
msw.use(
779+
http.get(
780+
"*/accounts/:accountId/event_notifications/r2/:bucketName/configuration",
781+
async ({ request, params }) => {
782+
const { accountId, bucketName: bucketParam } = params;
783+
expect(accountId).toEqual("some-account-id");
784+
expect(bucketName).toEqual(bucketParam);
785+
expect(request.headers.get("authorization")).toEqual(
786+
"Bearer some-api-token"
787+
);
788+
const getResponse = {
789+
bucketName,
790+
queues: [
791+
{
792+
queueId: queueId,
793+
queueName,
794+
rules: [
795+
{
796+
ruleId: "8cdcce8a-89b3-474f-a087-3eb4fcacfa37",
797+
createdAt: "2024-09-05T01:02:03.000Z",
798+
prefix: "",
799+
suffix: "",
800+
actions: [
801+
"PutObject",
802+
"CompleteMultipartUpload",
803+
"CopyObject",
804+
],
805+
},
806+
],
807+
},
808+
],
809+
};
810+
return HttpResponse.json(createFetchResult(getResponse));
811+
},
812+
{ once: true }
813+
)
814+
);
815+
await expect(
816+
await runWrangler(`r2 bucket notification get ${bucketName}`)
817+
).toBe(undefined);
818+
expect(std.out).toMatchInlineSnapshot(`
819+
"Fetching notification configuration for bucket my-bucket...
820+
rule_id: 8cdcce8a-89b3-474f-a087-3eb4fcacfa37
821+
created_at: 2024-09-05T01:02:03.000Z
822+
queue_name: my-queue
823+
prefix:
824+
suffix:
825+
event_type: PutObject,CompleteMultipartUpload,CopyObject"
826+
`);
827+
});
828+
829+
it("is backwards compatible with old API version", async () => {
775830
const bucketName = "my-bucket";
776831
const queueId = "471537e8-6e5a-4163-a4d4-9478087c32c3";
777832
const queueName = "my-queue";
@@ -828,11 +883,12 @@ describe("r2", () => {
828883
).toBe(undefined);
829884
expect(std.out).toMatchInlineSnapshot(`
830885
"Fetching notification configuration for bucket my-bucket...
831-
┌────────────┬────────┬────────┬───────────────┐
832-
│ queue_name │ prefix │ suffix │ event_type │
833-
├────────────┼────────┼────────┼───────────────┤
834-
│ my-queue │ │ │ object-create │
835-
└────────────┴────────┴────────┴───────────────┘"
886+
rule_id:
887+
created_at:
888+
queue_name: my-queue
889+
prefix:
890+
suffix:
891+
event_type: PutObject,CompleteMultipartUpload,CopyObject"
836892
`);
837893
});
838894

@@ -846,7 +902,7 @@ describe("r2", () => {
846902
"
847903
wrangler r2 bucket notification get <bucket>
848904
849-
Get event notification configuration for a bucket
905+
Get event notification configuration for a bucket [open beta]
850906
851907
POSITIONALS
852908
bucket The name of the bucket for which notifications will be emitted [string] [required]
@@ -937,7 +993,7 @@ describe("r2", () => {
937993
)
938994
).resolves.toBe(undefined);
939995
expect(std.out).toMatchInlineSnapshot(`
940-
"Creating event notification rule for object creation and deletion (PutObject,CompleteMultipartUpload,CopyObject,DeleteObject)
996+
"Creating event notification rule for object creation and deletion (PutObject,CompleteMultipartUpload,CopyObject,DeleteObject,LifecycleDeletion)
941997
Configuration created successfully!"
942998
`);
943999
});
@@ -952,7 +1008,7 @@ describe("r2", () => {
9521008
"
9531009
wrangler r2 bucket notification create <bucket>
9541010
955-
Create new event notification configuration for an R2 bucket
1011+
Create new event notification configuration for an R2 bucket [open beta]
9561012
9571013
POSITIONALS
9581014
bucket The name of the bucket for which notifications will be emitted [string] [required]
@@ -1044,7 +1100,7 @@ describe("r2", () => {
10441100
"
10451101
wrangler r2 bucket notification delete <bucket>
10461102
1047-
Delete event notification configuration for an R2 bucket and queue
1103+
Delete event notification configuration for an R2 bucket and queue [open beta]
10481104
10491105
POSITIONALS
10501106
bucket The name of the bucket for which notifications will be emitted [string] [required]

packages/wrangler/src/__tests__/r2/helpers.test.ts

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import crypto from "crypto";
2-
import { vi } from "vitest";
31
import { logger } from "../../logger";
42
import {
53
eventNotificationHeaders,
64
tableFromNotificationGetResponse,
75
} from "../../r2/helpers";
6+
import formatLabelledValues from "../../utils/render-labelled-values";
87
import { mockConsoleMethods } from "../helpers/mock-console";
9-
import type { Config } from "../../config";
108
import type { GetNotificationConfigResponse } from "../../r2/helpers";
119
import type { ApiCredentials } from "../../user";
1210

@@ -16,63 +14,67 @@ describe("event notifications", () => {
1614
test("tableFromNotificationsGetResponse", async () => {
1715
const bucketName = "my-bucket";
1816
const config = { account_id: "my-account" };
19-
const queueMap: Record<string, string> = {
20-
"471537e8-6e5a-4163-a4d4-9478087c32c3": "my-queue-1",
21-
"be6b6a37-ae49-4eea-9032-5e8d3ad1d29b": "my-queue-2",
22-
};
2317
const response: GetNotificationConfigResponse = {
24-
[bucketName]: {
25-
"9d738cb7-be18-433a-957f-a9b88793de2c": {
26-
queue: "471537e8-6e5a-4163-a4d4-9478087c32c3",
18+
bucketName,
19+
queues: [
20+
{
21+
queueId: "471537e8-6e5a-4163-a4d4-9478087c32c3",
22+
queueName: "my-queue-1",
2723
rules: [
2824
{
25+
ruleId: "68746106-12f8-4bba-a57b-adaf37fe11ca",
2926
prefix: "/p1",
3027
suffix: "s1/",
31-
actions: [
32-
"PutObject",
33-
"CompleteMultipartUpload",
34-
"CopyObject",
35-
"DeleteObject",
36-
],
28+
actions: ["PutObject", "CopyObject", "DeleteObject"],
3729
},
3830
{
31+
ruleId: "5aa280bb-39d7-48ed-8c21-405fcd078192",
3932
actions: ["DeleteObject"],
4033
},
4134
],
4235
},
43-
[crypto.randomUUID()]: {
44-
queue: "be6b6a37-ae49-4eea-9032-5e8d3ad1d29b",
36+
{
37+
queueId: "be6b6a37-ae49-4eea-9032-5e8d3ad1d29b",
38+
queueName: "my-queue-2",
4539
rules: [
4640
{
41+
ruleId: "c4725929-3799-477a-a8d9-2d300f957e51",
42+
createdAt: "2024-09-05T01:02:03.000Z",
4743
prefix: "//1",
4844
suffix: "2//",
49-
actions: ["DeleteObject"],
45+
actions: ["LifecycleDeletion"],
5046
},
5147
],
5248
},
53-
},
49+
],
5450
};
5551
const tableOutput = await tableFromNotificationGetResponse(
5652
config,
57-
response[bucketName],
58-
vi
59-
.fn()
60-
.mockImplementation((_: Pick<Config, "account_id">, queue: string) => ({
61-
queue_name: queueMap[queue],
62-
}))
53+
response
6354
);
64-
logger.table(tableOutput);
55+
logger.log(tableOutput.map((x) => formatLabelledValues(x)).join("\n\n"));
6556

6657
await expect(std.out).toMatchInlineSnapshot(`
67-
"┌────────────┬────────┬────────┬─────────────────────────────┐
68-
│ queue_name │ prefix │ suffix │ event_type │
69-
├────────────┼────────┼────────┼─────────────────────────────┤
70-
│ my-queue-1 │ /p1 │ s1/ │ object-create,object-delete │
71-
├────────────┼────────┼────────┼─────────────────────────────┤
72-
│ my-queue-1 │ │ │ object-delete │
73-
├────────────┼────────┼────────┼─────────────────────────────┤
74-
│ my-queue-2 │ //1 │ 2// │ object-delete │
75-
└────────────┴────────┴────────┴─────────────────────────────┘"
58+
"rule_id: 68746106-12f8-4bba-a57b-adaf37fe11ca
59+
created_at:
60+
queue_name: my-queue-1
61+
prefix: /p1
62+
suffix: s1/
63+
event_type: PutObject,CopyObject,DeleteObject
64+
65+
rule_id: 5aa280bb-39d7-48ed-8c21-405fcd078192
66+
created_at:
67+
queue_name: my-queue-1
68+
prefix:
69+
suffix:
70+
event_type: DeleteObject
71+
72+
rule_id: c4725929-3799-477a-a8d9-2d300f957e51
73+
created_at: 2024-09-05T01:02:03.000Z
74+
queue_name: my-queue-2
75+
prefix: //1
76+
suffix: 2//
77+
event_type: LifecycleDeletion"
7678
`);
7779
});
7880
test("auth email eventNotificationHeaders", () => {

packages/wrangler/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ import type { Arguments } from "yargs";
8181

8282
const resetColor = "\x1b[0m";
8383
const fgGreenColor = "\x1b[32m";
84-
const betaCmdColor = "#BD5B08";
84+
export const betaCmdColor = "#BD5B08";
8585

8686
export const DEFAULT_LOCAL_PORT = 8787;
8787
export const DEFAULT_INSPECTOR_PORT = 9229;

packages/wrangler/src/queues/client.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,9 @@ async function ensureQueuesExist(config: Config, queueNames: string[]) {
204204
}
205205

206206
export async function getQueueById(
207-
config: Pick<Config, "account_id">,
207+
accountId: string,
208208
queueId: string
209209
): Promise<QueueResponse> {
210-
const accountId = await requireAuth(config);
211210
return fetchResult(queuesUrl(accountId, queueId), {});
212211
}
213212

0 commit comments

Comments
 (0)