Skip to content

Commit fe392da

Browse files
jonesphillippenalosa
authored andcommitted
Add r2 bucket domain get command
1 parent a29a41c commit fe392da

File tree

5 files changed

+124
-0
lines changed

5 files changed

+124
-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/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ import {
101101
} from "./r2/bucket";
102102
import {
103103
r2BucketDomainAddCommand,
104+
r2BucketDomainGetCommand,
104105
r2BucketDomainListCommand,
105106
r2BucketDomainNamespace,
106107
r2BucketDomainRemoveCommand,
@@ -780,6 +781,10 @@ export function createCLIParser(argv: string[]) {
780781
command: "wrangler r2 bucket domain list",
781782
definition: r2BucketDomainListCommand,
782783
},
784+
{
785+
command: "wrangler r2 bucket domain get",
786+
definition: r2BucketDomainGetCommand,
787+
},
783788
{
784789
command: "wrangler r2 bucket domain add",
785790
definition: r2BucketDomainAddCommand,

packages/wrangler/src/r2/domain.ts

Lines changed: 45 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,
@@ -19,6 +20,50 @@ export const r2BucketDomainNamespace = createNamespace({
1920
},
2021
});
2122

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

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)