Skip to content

Commit

Permalink
Adding argument to specify rule to delete if don't want to delete ent…
Browse files Browse the repository at this point in the history
…ire config
  • Loading branch information
jkoe-cf committed Sep 12, 2024
1 parent 67711c2 commit b17d39e
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/lazy-jeans-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": minor
---

feat: adding option to specify a rule within the config to delete (if no rules are specified, all rules get deleted)
66 changes: 64 additions & 2 deletions packages/wrangler/src/__tests__/r2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,7 @@ describe("r2", () => {
});

describe("delete", () => {
it("follows happy path as expected", async () => {
it("follows happy path as expected without specified rules", async () => {
const bucketName = "my-bucket";
const queue = "my-queue";
msw.use(
Expand Down Expand Up @@ -1034,6 +1034,67 @@ describe("r2", () => {
`);
});

it("follows happy path as expected with specified rules", async () => {
const bucketName = "my-bucket";
const queue = "my-queue";
const ruleId = "rule123456789";
msw.use(
http.delete(
"*/accounts/:accountId/event_notifications/r2/:bucketName/configuration/queues/:queueUUID",
async ({ request, params }) => {
const { accountId } = params;
expect(accountId).toEqual("some-account-id");
expect(request.headers.get("authorization")).toEqual(
"Bearer some-api-token"
);
return HttpResponse.json(createFetchResult({}));
},
{ once: true }
),
http.get(
"*/accounts/:accountId/queues?*",
async ({ request, params }) => {
const url = new URL(request.url);
const { accountId } = params;
const nameParams = url.searchParams.getAll("name");

expect(accountId).toEqual("some-account-id");
expect(nameParams[0]).toEqual(queue);
expect(request.headers.get("authorization")).toEqual(
"Bearer some-api-token"
);
return HttpResponse.json({
success: true,
errors: [],
messages: [],
result: [
{
queue_id: "queue-id",
queue_name: queue,
created_on: "",
producers: [],
consumers: [],
producers_total_count: 1,
consumers_total_count: 0,
modified_on: "",
},
],
});
},
{ once: true }
)
);
await expect(
runWrangler(
`r2 bucket notification delete ${bucketName} --queue ${queue} --rule ${ruleId}`
)
).resolves.toBe(undefined);
expect(std.out).toMatchInlineSnapshot(`
"Disabling event notifications for \\"my-bucket\\" to queue my-queue...
Configuration deleted successfully!"
`);
});

it("errors if required options are not provided", async () => {
await expect(
runWrangler("r2 bucket notification delete notification-test-001")
Expand All @@ -1057,7 +1118,8 @@ describe("r2", () => {
-v, --version Show version number [boolean]
OPTIONS
--queue The name of the queue that is configured to receive notifications. ex '--queue my-queue' [string] [required]"
--queue The name of the queue that is configured to receive notifications. ex '--queue my-queue' [string] [required]
--rule The id of the rule to delete. If no rule is specified, all rules for the bucket/queue configuration will be deleted. [string]"
`);
});
});
Expand Down
18 changes: 16 additions & 2 deletions packages/wrangler/src/r2/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,11 @@ export type PutNotificationRequestBody = {
rules: NotificationRule[];
};

// This type captures the shape of the data expected by EWC API.
export type DeleteNotificationRequestBody = {
ruleIds?: string[];
};

export function eventNotificationHeaders(
apiCredentials: ApiCredentials
): HeadersInit {
Expand Down Expand Up @@ -537,16 +542,25 @@ export async function deleteEventNotificationConfig(
apiCredentials: ApiCredentials,
accountId: string,
bucketName: string,
queueName: string
queueName: string,
ruleId: string | undefined
): Promise<null> {
const queue = await getQueue(config, queueName);
const headers = eventNotificationHeaders(apiCredentials);
logger.log(
`Disabling event notifications for "${bucketName}" to queue ${queueName}...`
);

const body: DeleteNotificationRequestBody =
ruleId !== undefined
? {
ruleIds: [ruleId],
}
: {};

return await fetchResult<null>(
`/accounts/${accountId}/event_notifications/r2/${bucketName}/configuration/queues/${queue.queue_id}`,
{ method: "DELETE", headers }
{ method: "DELETE", body: JSON.stringify(body), headers }
);
}

Expand Down
21 changes: 14 additions & 7 deletions packages/wrangler/src/r2/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ export async function CreateHandler(
config,
apiCreds,
accountId,
`${bucket}`,
`${queue}`,
bucket,
queue,
eventTypes as R2EventType[],
`${prefix}`,
`${suffix}`
prefix,
suffix
);
logger.log("Configuration created successfully!");
}
Expand All @@ -116,6 +116,12 @@ export function DeleteOptions(yargs: CommonYargsArgv) {
demandOption: true,
requiresArg: true,
type: "string",
})
.option("rule", {
describe:
"The id of the rule to delete. If no rule is specified, all rules for the bucket/queue configuration will be deleted.",
requiresArg: false,
type: "string",
});
}

Expand All @@ -126,13 +132,14 @@ export async function DeleteHandler(
const config = readConfig(args.config, args);
const accountId = await requireAuth(config);
const apiCreds = requireApiToken();
const { bucket, queue } = args;
const { bucket, queue, rule } = args;
await deleteEventNotificationConfig(
config,
apiCreds,
accountId,
`${bucket}`,
`${queue}`
bucket,
queue,
rule
);
logger.log("Configuration deleted successfully!");
}

0 comments on commit b17d39e

Please sign in to comment.