Skip to content

Commit

Permalink
Add r2 bucket domain get command (#7383)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonesphillip authored Dec 12, 2024
1 parent 0117820 commit 8af3365
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/happy-ears-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": minor
---

Added wrangler r2 domain get command
47 changes: 47 additions & 0 deletions packages/wrangler/src/__tests__/r2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1499,6 +1499,53 @@ describe("r2", () => {
const { setIsTTY } = useMockIsTTY();
mockAccountId();
mockApiToken();
describe("get", () => {
it("should get custom domain for the bucket as expected", async () => {
const bucketName = "my-bucket";
const domainName = "test.com";
const mockDomain = {
domain: domainName,
enabled: false,
status: {
ownership: "pending",
ssl: "pending",
},
minTLS: "1.0",
zoneId: "zone-id-456",
zoneName: "test-zone",
};
msw.use(
http.get(
"*/accounts/:accountId/r2/buckets/:bucketName/domains/custom/:domainName",
async ({ params }) => {
const {
accountId,
bucketName: bucketParam,
domainName: domainParam,
} = params;
expect(accountId).toEqual("some-account-id");
expect(bucketParam).toEqual(bucketName);
expect(domainParam).toEqual(domainName);
return HttpResponse.json(createFetchResult(mockDomain));
},
{ once: true }
)
);
await runWrangler(
`r2 bucket domain get ${bucketName} --domain ${domainName}`
);
expect(std.out).toMatchInlineSnapshot(`
"Retrieving custom domain 'test.com' connected to bucket 'my-bucket'...
domain: test.com
enabled: No
ownership_status: pending
ssl_status: pending
min_tls_version: 1.0
zone_id: zone-id-456
zone_name: test-zone"
`);
});
});
describe("add", () => {
it("should add custom domain to the bucket as expected", async () => {
const bucketName = "my-bucket";
Expand Down
5 changes: 5 additions & 0 deletions packages/wrangler/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ import {
} from "./r2/cors";
import {
r2BucketDomainAddCommand,
r2BucketDomainGetCommand,
r2BucketDomainListCommand,
r2BucketDomainNamespace,
r2BucketDomainRemoveCommand,
Expand Down Expand Up @@ -787,6 +788,10 @@ export function createCLIParser(argv: string[]) {
command: "wrangler r2 bucket domain list",
definition: r2BucketDomainListCommand,
},
{
command: "wrangler r2 bucket domain get",
definition: r2BucketDomainGetCommand,
},
{
command: "wrangler r2 bucket domain add",
definition: r2BucketDomainAddCommand,
Expand Down
45 changes: 45 additions & 0 deletions packages/wrangler/src/r2/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import formatLabelledValues from "../utils/render-labelled-values";
import {
attachCustomDomainToBucket,
configureCustomDomainSettings,
getCustomDomain,
listCustomDomainsOfBucket,
removeCustomDomainFromBucket,
tableFromCustomDomainListResponse,
Expand All @@ -19,6 +20,50 @@ export const r2BucketDomainNamespace = createNamespace({
},
});

export const r2BucketDomainGetCommand = createCommand({
metadata: {
description: "Get custom domain connected to an R2 bucket",
status: "stable",
owner: "Product: R2",
},
positionalArgs: ["bucket"],
args: {
bucket: {
describe: "The name of the R2 bucket whose custom domain to retrieve",
type: "string",
demandOption: true,
},
domain: {
describe: "The custom domain to get information for",
type: "string",
demandOption: true,
},
jurisdiction: {
describe: "The jurisdiction where the bucket exists",
alias: "J",
requiresArg: true,
type: "string",
},
},
async handler({ bucket, domain, jurisdiction }, { config }) {
const accountId = await requireAuth(config);

logger.log(
`Retrieving custom domain '${domain}' connected to bucket '${bucket}'...`
);

const domainResponse = await getCustomDomain(
accountId,
bucket,
domain,
jurisdiction
);

const tableOutput = tableFromCustomDomainListResponse([domainResponse]);
logger.log(tableOutput.map((x) => formatLabelledValues(x)).join("\n\n"));
},
});

export const r2BucketDomainListCommand = createCommand({
metadata: {
description: "List custom domains for an R2 bucket",
Expand Down
22 changes: 22 additions & 0 deletions packages/wrangler/src/r2/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,28 @@ export interface CustomDomainInfo {
zoneName: string;
}

export async function getCustomDomain(
accountId: string,
bucketName: string,
domainName: string,
jurisdiction?: string
): Promise<CustomDomainInfo> {
const headers: HeadersInit = {};
if (jurisdiction) {
headers["cf-r2-jurisdiction"] = jurisdiction;
}

const result = await fetchResult<CustomDomainInfo>(
`/accounts/${accountId}/r2/buckets/${bucketName}/domains/custom/${domainName}`,
{
method: "GET",
headers,
}
);

return result;
}

export async function attachCustomDomainToBucket(
accountId: string,
bucketName: string,
Expand Down

0 comments on commit 8af3365

Please sign in to comment.