Skip to content

Commit

Permalink
respect bucket_name, database_name and jurisdiction
Browse files Browse the repository at this point in the history
  • Loading branch information
emily-shen committed Jan 10, 2025
1 parent 22e9484 commit 6f29dc2
Show file tree
Hide file tree
Showing 3 changed files with 345 additions and 78 deletions.
221 changes: 219 additions & 2 deletions packages/wrangler/src/__tests__/provision.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { http, HttpResponse } from "msw";
import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
import { mockConsoleMethods } from "./helpers/mock-console";
import { clearDialogs, mockPrompt, mockSelect } from "./helpers/mock-dialogs";
import {
clearDialogs,
mockConfirm,
mockPrompt,
mockSelect,
} from "./helpers/mock-dialogs";
import { useMockIsTTY } from "./helpers/mock-istty";
import {
mockCreateKVNamespace,
Expand Down Expand Up @@ -479,6 +484,212 @@ describe("--x-provision", () => {
expect(std.err).toMatchInlineSnapshot(`""`);
expect(std.warn).toMatchInlineSnapshot(`""`);
});

it("can prefill d1 database name from config file if provided", async () => {
writeWranglerConfig({
main: "index.js",
d1_databases: [{ binding: "D1", database_name: "prefilled-d1-name" }],
});
mockGetSettings();
msw.use(
http.get("*/accounts/:accountId/d1/database", async () => {
return HttpResponse.json(
createFetchResult([
{
name: "db-name",
uuid: "existing-d1-id",
},
])
);
})
);

// no name prompt
mockCreateD1Database({
assertName: "prefilled-d1-name",
resultId: "new-d1-id",
});

mockConfirm({
text: `Would you like to create a new D1 Database named "prefilled-d1-name"?`,
result: true,
});
mockUploadWorkerRequest({
expectedBindings: [
{
name: "D1",
type: "d1",
id: "new-d1-id",
},
],
});

await runWrangler("deploy --x-provision");

expect(std.out).toMatchInlineSnapshot(`
"Total Upload: xx KiB / gzip: xx KiB
The following bindings need to be provisioned:
- D1 Databases:
- D1
Provisioning D1 (D1 Database)...
Resource name found in config: prefilled-d1-name
No pre-existing resource found with that name
🌀 Creating new D1 Database \\"prefilled-d1-name\\"...
✨ D1 provisioned with prefilled-d1-name
--------------------------------------
🎉 All resources provisioned, continuing with deployment...
Worker Startup Time: 100 ms
Your worker has access to the following bindings:
- D1 Databases:
- D1: prefilled-d1-name (new-d1-id)
Uploaded test-name (TIMINGS)
Deployed test-name triggers (TIMINGS)
https://test-name.test-sub-domain.workers.dev
Current Version ID: Galaxy-Class"
`);
expect(std.err).toMatchInlineSnapshot(`""`);
expect(std.warn).toMatchInlineSnapshot(`""`);
});

it("can prefill r2 bucket name from config file if provided", async () => {
writeWranglerConfig({
main: "index.js",
r2_buckets: [
{
binding: "BUCKET",
bucket_name: "prefilled-r2-name",
// note it will also respect jurisdiction if provided, but wont prompt for it
jurisdiction: "eu",
},
],
});
mockGetSettings();
msw.use(
http.get("*/accounts/:accountId/r2/buckets", async () => {
return HttpResponse.json(
createFetchResult({
buckets: [
{
name: "existing-bucket-name",
},
],
})
);
})
);

// no name prompt
mockCreateR2Bucket({
assertBucketName: "prefilled-r2-name",
assertJurisdiction: "eu",
});

mockConfirm({
text: `Would you like to create a new R2 Bucket named "prefilled-r2-name"?`,
result: true,
});
mockUploadWorkerRequest({
expectedBindings: [
{
name: "BUCKET",
type: "r2_bucket",
bucket_name: "prefilled-r2-name",
jurisdiction: "eu",
},
],
});

await runWrangler("deploy --x-provision");

expect(std.out).toMatchInlineSnapshot(`
"Total Upload: xx KiB / gzip: xx KiB
The following bindings need to be provisioned:
- R2 Buckets:
- BUCKET
Provisioning BUCKET (R2 Bucket)...
Resource name found in config: prefilled-r2-name
No pre-existing resource found with that name
🌀 Creating new R2 Bucket \\"prefilled-r2-name\\"...
✨ BUCKET provisioned with prefilled-r2-name
--------------------------------------
🎉 All resources provisioned, continuing with deployment...
Worker Startup Time: 100 ms
Your worker has access to the following bindings:
- R2 Buckets:
- BUCKET: prefilled-r2-name (eu)
Uploaded test-name (TIMINGS)
Deployed test-name triggers (TIMINGS)
https://test-name.test-sub-domain.workers.dev
Current Version ID: Galaxy-Class"
`);
expect(std.err).toMatchInlineSnapshot(`""`);
expect(std.warn).toMatchInlineSnapshot(`""`);
});

// to maintain current behaviour
it("wont prompt to provision if an r2 bucket name belongs to an existing bucket", async () => {
writeWranglerConfig({
main: "index.js",
r2_buckets: [
{
binding: "BUCKET",
bucket_name: "existing-bucket-name",
jurisdiction: "eu",
},
],
});
mockGetSettings();
msw.use(
http.get("*/accounts/:accountId/r2/buckets", async () => {
return HttpResponse.json(
createFetchResult({
buckets: [
{
name: "existing-bucket-name",
},
],
})
);
})
);

mockUploadWorkerRequest({
expectedBindings: [
{
name: "BUCKET",
type: "r2_bucket",
bucket_name: "existing-bucket-name",
jurisdiction: "eu",
},
],
});

await runWrangler("deploy --x-provision");

expect(std.out).toMatchInlineSnapshot(`
"Total Upload: xx KiB / gzip: xx KiB
Worker Startup Time: 100 ms
Your worker has access to the following bindings:
- R2 Buckets:
- BUCKET: existing-bucket-name (eu)
Uploaded test-name (TIMINGS)
Deployed test-name triggers (TIMINGS)
https://test-name.test-sub-domain.workers.dev
Current Version ID: Galaxy-Class"
`);
expect(std.err).toMatchInlineSnapshot(`""`);
expect(std.warn).toMatchInlineSnapshot(`""`);
});
});

it("should error if used with a service environment", async () => {
Expand Down Expand Up @@ -555,6 +766,7 @@ function mockCreateD1Database(
function mockCreateR2Bucket(
options: {
assertBucketName?: string;
assertJurisdiction?: string;
} = {}
) {
msw.use(
Expand All @@ -563,7 +775,12 @@ function mockCreateR2Bucket(
async ({ request }) => {
if (options.assertBucketName) {
const requestBody = await request.json();
expect(requestBody).toEqual({ name: options.assertBucketName });
expect(requestBody).toMatchObject({ name: options.assertBucketName });
}
if (options.assertJurisdiction) {
expect(request.headers.get("cf-r2-jurisdiction")).toEqual(
options.assertJurisdiction
);
}
return HttpResponse.json(createFetchResult({}));
},
Expand Down
2 changes: 1 addition & 1 deletion packages/wrangler/src/d1/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const listDatabases = async (
);
page++;
results.push(...json);
if (limitCalls) {
if (limitCalls && page > 3) {
break;
}
if (json.length < pageSize) {
Expand Down
Loading

0 comments on commit 6f29dc2

Please sign in to comment.