Skip to content

Commit 3231d8a

Browse files
jonesphillippenalosa
authored andcommitted
Add r2 bucket domain get command (#7383)
1 parent 5c65c78 commit 3231d8a

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

packages/wrangler/src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ import {
107107
} from "./r2/cors";
108108
import {
109109
r2BucketDomainAddCommand,
110+
r2BucketDomainGetCommand,
110111
r2BucketDomainListCommand,
111112
r2BucketDomainNamespace,
112113
r2BucketDomainRemoveCommand,
@@ -787,6 +788,10 @@ export function createCLIParser(argv: string[]) {
787788
command: "wrangler r2 bucket domain list",
788789
definition: r2BucketDomainListCommand,
789790
},
791+
{
792+
command: "wrangler r2 bucket domain get",
793+
definition: r2BucketDomainGetCommand,
794+
},
790795
{
791796
command: "wrangler r2 bucket domain add",
792797
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)