Skip to content

Commit ec7e39b

Browse files
committed
Add r2 bucket domain get command
1 parent a3f56d1 commit ec7e39b

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed

.changeset/happy-ears-cough.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+
Added wrangler r2 domain get command

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,6 +1497,53 @@ describe("r2", () => {
14971497
const { setIsTTY } = useMockIsTTY();
14981498
mockAccountId();
14991499
mockApiToken();
1500+
describe("get", () => {
1501+
it("should get custom domain for the bucket as expected", async () => {
1502+
const bucketName = "my-bucket";
1503+
const domainName = "test.com";
1504+
const mockDomain = {
1505+
domain: domainName,
1506+
enabled: false,
1507+
status: {
1508+
ownership: "pending",
1509+
ssl: "pending",
1510+
},
1511+
minTLS: "1.0",
1512+
zoneId: "zone-id-456",
1513+
zoneName: "test-zone",
1514+
};
1515+
msw.use(
1516+
http.get(
1517+
"*/accounts/:accountId/r2/buckets/:bucketName/domains/custom/:domainName",
1518+
async ({ params }) => {
1519+
const {
1520+
accountId,
1521+
bucketName: bucketParam,
1522+
domainName: domainParam,
1523+
} = params;
1524+
expect(accountId).toEqual("some-account-id");
1525+
expect(bucketParam).toEqual(bucketName);
1526+
expect(domainParam).toEqual(domainName);
1527+
return HttpResponse.json(createFetchResult(mockDomain));
1528+
},
1529+
{ once: true }
1530+
)
1531+
);
1532+
await runWrangler(
1533+
`r2 bucket domain get ${bucketName} --domain ${domainName}`
1534+
);
1535+
expect(std.out).toMatchInlineSnapshot(`
1536+
"Retrieving custom domain 'test.com' connected to bucket 'my-bucket'...
1537+
domain: test.com
1538+
enabled: No
1539+
ownership_status: pending
1540+
ssl_status: pending
1541+
min_tls_version: 1.0
1542+
zone_id: zone-id-456
1543+
zone_name: test-zone"
1544+
`);
1545+
});
1546+
});
15001547
describe("add", () => {
15011548
it("should add custom domain to the bucket as expected", async () => {
15021549
const bucketName = "my-bucket";

packages/wrangler/src/r2/domain.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import formatLabelledValues from "../utils/render-labelled-values";
66
import {
77
attachCustomDomainToBucket,
88
configureCustomDomainSettings,
9+
getCustomDomain,
910
listCustomDomainsOfBucket,
1011
removeCustomDomainFromBucket,
1112
tableFromCustomDomainListResponse,
@@ -20,6 +21,51 @@ defineNamespace({
2021
},
2122
});
2223

24+
defineCommand({
25+
command: "wrangler r2 bucket domain get",
26+
metadata: {
27+
description: "Get custom domain connected to an R2 bucket",
28+
status: "stable",
29+
owner: "Product: R2",
30+
},
31+
positionalArgs: ["bucket"],
32+
args: {
33+
bucket: {
34+
describe: "The name of the R2 bucket whose custom domain to retrieve",
35+
type: "string",
36+
demandOption: true,
37+
},
38+
domain: {
39+
describe: "The custom domain to get information for",
40+
type: "string",
41+
demandOption: true,
42+
},
43+
jurisdiction: {
44+
describe: "The jurisdiction where the bucket exists",
45+
alias: "J",
46+
requiresArg: true,
47+
type: "string",
48+
},
49+
},
50+
async handler({ bucket, domain, jurisdiction }, { config }) {
51+
const accountId = await requireAuth(config);
52+
53+
logger.log(
54+
`Retrieving custom domain '${domain}' connected to bucket '${bucket}'...`
55+
);
56+
57+
const domainResponse = await getCustomDomain(
58+
accountId,
59+
bucket,
60+
domain,
61+
jurisdiction
62+
);
63+
64+
const tableOutput = tableFromCustomDomainListResponse([domainResponse]);
65+
logger.log(tableOutput.map((x) => formatLabelledValues(x)).join("\n\n"));
66+
},
67+
});
68+
2369
defineCommand({
2470
command: "wrangler r2 bucket domain list",
2571
metadata: {

packages/wrangler/src/r2/helpers.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,28 @@ export interface CustomDomainInfo {
759759
zoneName: string;
760760
}
761761

762+
export async function getCustomDomain(
763+
accountId: string,
764+
bucketName: string,
765+
domainName: string,
766+
jurisdiction?: string
767+
): Promise<CustomDomainInfo> {
768+
const headers: HeadersInit = {};
769+
if (jurisdiction) {
770+
headers["cf-r2-jurisdiction"] = jurisdiction;
771+
}
772+
773+
const result = await fetchResult<CustomDomainInfo>(
774+
`/accounts/${accountId}/r2/buckets/${bucketName}/domains/custom/${domainName}`,
775+
{
776+
method: "GET",
777+
headers,
778+
}
779+
);
780+
781+
return result;
782+
}
783+
762784
export async function attachCustomDomainToBucket(
763785
accountId: string,
764786
bucketName: string,

0 commit comments

Comments
 (0)