Skip to content

Commit 624882e

Browse files
authoredApr 1, 2025··
fix: General improvements for the R2 catalog commands (#8737)
1 parent 56637e3 commit 624882e

File tree

4 files changed

+88
-21
lines changed

4 files changed

+88
-21
lines changed
 

‎.changeset/tangy-badgers-grow.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
fix: General improvements for the R2 catalog commands

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

+48-7
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,7 @@ describe("r2", () => {
967967
it("should enable R2 catalog for the given bucket", async () => {
968968
msw.use(
969969
http.post(
970-
"*/accounts/some-account-id/r2-catalog/testBucket",
970+
"*/accounts/some-account-id/r2-catalog/testBucket/enable",
971971
async () => {
972972
return HttpResponse.json(
973973
createFetchResult(
@@ -987,9 +987,10 @@ describe("r2", () => {
987987
`"✨ Successfully enabled data catalog on bucket 'testBucket'.
988988
989989
Catalog URI: 'https://catalog.cloudflarestorage.com/test-warehouse-name'
990+
Warehouse: 'test-warehouse-name'
990991
991992
Use this Catalog URI with Iceberg-compatible query engines (Spark, DuckDB, Trino, etc.) to query data as tables.
992-
Note: You'll need a Cloudflare API token with 'R2 Data Catalog' permission to authenticate your client with this catalog.
993+
Note: You will need a Cloudflare API token with 'R2 Data Catalog' permission to authenticate your client with this catalog.
993994
For more details, refer to: https://developers.cloudflare.com/r2/api/s3/tokens/"`
994995
);
995996
});
@@ -1058,12 +1059,12 @@ For more details, refer to: https://developers.cloudflare.com/r2/api/s3/tokens/"
10581059
it("should disable R2 catalog for the given bucket", async () => {
10591060
setIsTTY(true);
10601061
mockConfirm({
1061-
text: "Are you sure you want to disable the data catalog for bucket 'testBucket'? This action is irreversible, and you cannot re-enable it on this bucket.",
1062+
text: "Are you sure you want to disable the data catalog for bucket 'testBucket'?",
10621063
result: true,
10631064
});
10641065
msw.use(
1065-
http.delete(
1066-
"*/accounts/some-account-id/r2-catalog/testBucket",
1066+
http.post(
1067+
"*/accounts/some-account-id/r2-catalog/testBucket/disable",
10671068
async () => {
10681069
return HttpResponse.json(createFetchResult({}));
10691070
},
@@ -1075,6 +1076,43 @@ For more details, refer to: https://developers.cloudflare.com/r2/api/s3/tokens/"
10751076
`"Successfully disabled the data catalog on bucket 'testBucket'."`
10761077
);
10771078
});
1079+
1080+
it("should inform user if the catalog was never enabled for the bucket", async () => {
1081+
setIsTTY(true);
1082+
mockConfirm({
1083+
text: "Are you sure you want to disable the data catalog for bucket 'testBucket'?",
1084+
result: true,
1085+
});
1086+
msw.use(
1087+
http.post(
1088+
"*/accounts/:accountId/r2-catalog/:bucketName/disable",
1089+
async ({ request, params }) => {
1090+
const { accountId } = params;
1091+
expect(accountId).toEqual("some-account-id");
1092+
expect(await request.text()).toEqual("");
1093+
return HttpResponse.json(
1094+
{
1095+
success: false,
1096+
errors: [
1097+
{
1098+
code: 40401,
1099+
message: "Warehouse not found",
1100+
},
1101+
],
1102+
result: null,
1103+
},
1104+
{ status: 404 }
1105+
);
1106+
},
1107+
{ once: true }
1108+
)
1109+
);
1110+
1111+
await runWrangler("r2 bucket catalog disable testBucket");
1112+
expect(std.out).toMatchInlineSnapshot(`
1113+
"Data catalog is not enabled for bucket 'testBucket'. Please use 'wrangler r2 bucket catalog enable testBucket' to first enable the data catalog on this bucket."
1114+
`);
1115+
});
10781116
});
10791117

10801118
describe("get", () => {
@@ -1133,13 +1171,15 @@ For more details, refer to: https://developers.cloudflare.com/r2/api/s3/tokens/"
11331171
await runWrangler("r2 bucket catalog get test-bucket");
11341172
expect(std.out).toMatchInlineSnapshot(`
11351173
"Getting data catalog status for 'test-bucket'...
1174+
11361175
Bucket: test-bucket
11371176
Catalog URI: https://catalog.cloudflarestorage.com/test-name
1177+
Warehouse: test-name
11381178
Status: active"
11391179
`);
11401180
});
11411181

1142-
it("should inform user if no active warehouse is present for the bucket", async () => {
1182+
it("should inform user if the catalog was never enabled for the bucket", async () => {
11431183
msw.use(
11441184
http.get(
11451185
"*/accounts/:accountId/r2-catalog/:bucketName",
@@ -1167,7 +1207,8 @@ For more details, refer to: https://developers.cloudflare.com/r2/api/s3/tokens/"
11671207
await runWrangler("r2 bucket catalog get test-bucket");
11681208
expect(std.out).toMatchInlineSnapshot(`
11691209
"Getting data catalog status for 'test-bucket'...
1170-
Data catalog isn't enabled for bucket 'test-bucket'."
1210+
1211+
Data catalog is not enabled for bucket 'test-bucket'. Please use 'wrangler r2 bucket catalog enable test-bucket' to first enable the data catalog on this bucket."
11711212
`);
11721213
});
11731214
});

‎packages/wrangler/src/r2/catalog.ts

+23-8
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@ export const r2BucketCatalogEnableCommand = createCommand({
4747
`✨ Successfully enabled data catalog on bucket '${args.bucket}'.
4848
4949
Catalog URI: '${catalogHost}'
50+
Warehouse: '${response.name}'
5051
5152
Use this Catalog URI with Iceberg-compatible query engines (Spark, DuckDB, Trino, etc.) to query data as tables.
52-
Note: You'll need a Cloudflare API token with 'R2 Data Catalog' permission to authenticate your client with this catalog.
53+
Note: You will need a Cloudflare API token with 'R2 Data Catalog' permission to authenticate your client with this catalog.
5354
For more details, refer to: https://developers.cloudflare.com/r2/api/s3/tokens/`
5455
);
5556
},
@@ -73,18 +74,29 @@ export const r2BucketCatalogDisableCommand = createCommand({
7374
const accountId = await requireAuth(config);
7475

7576
const confirmedDisable = await confirm(
76-
`Are you sure you want to disable the data catalog for bucket '${args.bucket}'? This action is irreversible, and you cannot re-enable it on this bucket.`
77+
`Are you sure you want to disable the data catalog for bucket '${args.bucket}'?`
7778
);
7879
if (!confirmedDisable) {
7980
logger.log("Disable cancelled.");
8081
return;
8182
}
8283

83-
await disableR2Catalog(accountId, args.bucket);
84+
try {
85+
await disableR2Catalog(accountId, args.bucket);
8486

85-
logger.log(
86-
`Successfully disabled the data catalog on bucket '${args.bucket}'.`
87-
);
87+
logger.log(
88+
`Successfully disabled the data catalog on bucket '${args.bucket}'.`
89+
);
90+
} catch (e) {
91+
// R2 Data Catalog 40401 corresponds to a 404
92+
if (e instanceof APIError && e.code == 40401) {
93+
logger.log(
94+
`Data catalog is not enabled for bucket '${args.bucket}'. Please use 'wrangler r2 bucket catalog enable ${args.bucket}' to first enable the data catalog on this bucket.`
95+
);
96+
} else {
97+
throw e;
98+
}
99+
}
88100
},
89101
});
90102

@@ -106,7 +118,7 @@ export const r2BucketCatalogGetCommand = createCommand({
106118
async handler(args, { config }) {
107119
const accountId = await requireAuth(config);
108120

109-
logger.log(`Getting data catalog status for '${args.bucket}'...`);
121+
logger.log(`Getting data catalog status for '${args.bucket}'...\n`);
110122

111123
try {
112124
const catalog = await getR2Catalog(accountId, args.bucket);
@@ -122,14 +134,17 @@ export const r2BucketCatalogGetCommand = createCommand({
122134
const output = {
123135
Bucket: args.bucket,
124136
"Catalog URI": catalogHost,
137+
Warehouse: catalog.name,
125138
Status: catalog.status,
126139
};
127140

128141
logger.log(formatLabelledValues(output));
129142
} catch (e) {
130143
// R2 Data Catalog 40401 corresponds to a 404
131144
if (e instanceof APIError && e.code == 40401) {
132-
logger.log(`Data catalog isn't enabled for bucket '${args.bucket}'.`);
145+
logger.log(
146+
`Data catalog is not enabled for bucket '${args.bucket}'. Please use 'wrangler r2 bucket catalog enable ${args.bucket}' to first enable the data catalog on this bucket.`
147+
);
133148
} else {
134149
throw e;
135150
}

‎packages/wrangler/src/r2/helpers.ts

+12-6
Original file line numberDiff line numberDiff line change
@@ -523,9 +523,12 @@ export async function enableR2Catalog(
523523
accountId: string,
524524
bucketName: string
525525
): Promise<R2WarehouseEnableResponse> {
526-
return await fetchResult(`/accounts/${accountId}/r2-catalog/${bucketName}`, {
527-
method: "POST",
528-
});
526+
return await fetchResult(
527+
`/accounts/${accountId}/r2-catalog/${bucketName}/enable`,
528+
{
529+
method: "POST",
530+
}
531+
);
529532
}
530533

531534
/**
@@ -535,9 +538,12 @@ export async function disableR2Catalog(
535538
accountId: string,
536539
bucketName: string
537540
): Promise<R2WarehouseEnableResponse> {
538-
return await fetchResult(`/accounts/${accountId}/r2-catalog/${bucketName}`, {
539-
method: "DELETE",
540-
});
541+
return await fetchResult(
542+
`/accounts/${accountId}/r2-catalog/${bucketName}/disable`,
543+
{
544+
method: "POST",
545+
}
546+
);
541547
}
542548

543549
const R2EventableOperations = [

0 commit comments

Comments
 (0)
Please sign in to comment.