Skip to content

Commit 88b5b7f

Browse files
penalosadario-piotrowiczjamesopstad
authored
Improve resource provisioning (#10521)
* Implement updates to provisioning * add tests * fix tests * remove logging * fix KV namespace ID * fix tests * fix tests * fix e2e snapshots * Create rotten-glasses-smile.md * improve changeset * address comments * support provisioning remote bindings * Update .changeset/rotten-glasses-smile.md Co-authored-by: Dario Piotrowicz <[email protected]> * Update packages/wrangler/src/deployment-bundle/bindings.ts Co-authored-by: Dario Piotrowicz <[email protected]> * Update .changeset/rotten-glasses-smile.md Co-authored-by: Dario Piotrowicz <[email protected]> * Update packages/wrangler/src/deployment-bundle/bindings.ts Co-authored-by: James Opstad <[email protected]> * address comments * fix tests & remove dev behaviour * add test for redirected config * fix lint * update header * Revert various changes --------- Co-authored-by: Dario Piotrowicz <[email protected]> Co-authored-by: James Opstad <[email protected]>
1 parent e52d0ec commit 88b5b7f

26 files changed

+653
-387
lines changed

.changeset/rotten-glasses-smile.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
Improves the Wrangler auto-provisioning feature (gated behind the experimental flag `--x-provision`) by:
6+
7+
- Writing back changes to the user's config file (not necessary, but can make it resilient to binding name changes)
8+
- Fixing `--dry-run`, which previously threw an error when your config file had auto provisioned resources
9+
- Improve R2 bindings display to include the `bucket_name` from the config file on upload
10+
- Fixing bindings view for specific versions to not display TOML

packages/wrangler/e2e/provision.test.ts

Lines changed: 81 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import assert from "node:assert";
22
import dedent from "ts-dedent";
33
import { fetch } from "undici";
4-
import { afterAll, beforeAll, describe, expect, it } from "vitest";
4+
import { afterAll, beforeEach, describe, expect, it } from "vitest";
55
import { CLOUDFLARE_ACCOUNT_ID } from "./helpers/account-id";
66
import { WranglerE2ETestHelper } from "./helpers/e2e-wrangler-test";
77
import { fetchText } from "./helpers/fetch-text";
@@ -38,23 +38,27 @@ describe.skipIf(!CLOUDFLARE_ACCOUNT_ID)(
3838
expect(text).toMatchInlineSnapshot(`"Hello World!"`);
3939
});
4040

41-
beforeAll(async () => {
41+
beforeEach(async () => {
4242
await helper.seed({
4343
"wrangler.toml": dedent`
4444
name = "${workerName}"
4545
main = "src/index.ts"
4646
compatibility_date = "2023-01-01"
4747
workers_dev = true
4848
49-
[[kv_namespaces]]
50-
binding = "KV"
49+
[[kv_namespaces]]
50+
binding = "KV"
5151
52-
[[r2_buckets]]
53-
binding = "R2"
52+
[[r2_buckets]]
53+
binding = "R2"
5454
55-
[[d1_databases]]
56-
binding = "D1"
57-
`,
55+
[[r2_buckets]]
56+
binding = "R2_WITH_NAME"
57+
bucket_name = "does-not-exist"
58+
59+
[[d1_databases]]
60+
binding = "D1"
61+
`,
5862
"src/index.ts": dedent`
5963
export default {
6064
fetch(request) {
@@ -76,32 +80,33 @@ describe.skipIf(!CLOUDFLARE_ACCOUNT_ID)(
7680
await worker.exitCode;
7781
const output = await worker.output;
7882
expect(normalize(output)).toMatchInlineSnapshot(`
79-
"Total Upload: xx KiB / gzip: xx KiB
80-
The following bindings need to be provisioned:
81-
Binding Resource
82-
env.KV KV Namespace
83-
env.D1 D1 Database
84-
env.R2 R2 Bucket
85-
Provisioning KV (KV Namespace)...
86-
🌀 Creating new KV Namespace "tmp-e2e-worker-00000000-0000-0000-0000-000000000000-kv"...
87-
✨ KV provisioned 🎉
88-
Provisioning D1 (D1 Database)...
89-
🌀 Creating new D1 Database "tmp-e2e-worker-00000000-0000-0000-0000-000000000000-d1"...
90-
✨ D1 provisioned 🎉
91-
Provisioning R2 (R2 Bucket)...
92-
🌀 Creating new R2 Bucket "tmp-e2e-worker-00000000-0000-0000-0000-000000000000-r2"...
93-
✨ R2 provisioned 🎉
94-
🎉 All resources provisioned, continuing with deployment...
95-
Your Worker has access to the following bindings:
96-
Binding Resource
97-
env.KV (00000000000000000000000000000000) KV Namespace
98-
env.D1 (00000000-0000-0000-0000-000000000000) D1 Database
99-
env.R2 (tmp-e2e-worker-00000000-0000-0000-0000-000000000000-r2) R2 Bucket
100-
Uploaded tmp-e2e-worker-00000000-0000-0000-0000-000000000000 (TIMINGS)
101-
Deployed tmp-e2e-worker-00000000-0000-0000-0000-000000000000 triggers (TIMINGS)
102-
https://tmp-e2e-worker-00000000-0000-0000-0000-000000000000.SUBDOMAIN.workers.dev
103-
Current Version ID: 00000000-0000-0000-0000-000000000000"
104-
`);
83+
"Total Upload: xx KiB / gzip: xx KiB
84+
The following bindings need to be provisioned:
85+
Binding Resource
86+
env.KV KV Namespace
87+
env.D1 D1 Database
88+
env.R2 R2 Bucket
89+
Provisioning KV (KV Namespace)...
90+
🌀 Creating new KV Namespace "tmp-e2e-worker-00000000-0000-0000-0000-000000000000-kv"...
91+
✨ KV provisioned 🎉
92+
Provisioning D1 (D1 Database)...
93+
🌀 Creating new D1 Database "tmp-e2e-worker-00000000-0000-0000-0000-000000000000-d1"...
94+
✨ D1 provisioned 🎉
95+
Provisioning R2 (R2 Bucket)...
96+
🌀 Creating new R2 Bucket "tmp-e2e-worker-00000000-0000-0000-0000-000000000000-r2"...
97+
✨ R2 provisioned 🎉
98+
🎉 All resources provisioned, continuing with deployment...
99+
Your Worker has access to the following bindings:
100+
Binding Resource
101+
env.KV (00000000000000000000000000000000) KV Namespace
102+
env.D1 (00000000-0000-0000-0000-000000000000) D1 Database
103+
env.R2 (tmp-e2e-worker-00000000-0000-0000-0000-000000000000-r2) R2 Bucket
104+
env.R2_WITH_NAME (does-not-exist) R2 Bucket
105+
Uploaded tmp-e2e-worker-00000000-0000-0000-0000-000000000000 (TIMINGS)
106+
Deployed tmp-e2e-worker-00000000-0000-0000-0000-000000000000 triggers (TIMINGS)
107+
https://tmp-e2e-worker-00000000-0000-0000-0000-000000000000.SUBDOMAIN.workers.dev
108+
Current Version ID: 00000000-0000-0000-0000-000000000000"
109+
`);
105110
const urlMatch = output.match(
106111
/(?<url>https:\/\/tmp-e2e-.+?\..+?\.workers\.dev)/
107112
);
@@ -130,25 +135,55 @@ describe.skipIf(!CLOUDFLARE_ACCOUNT_ID)(
130135
await worker.exitCode;
131136
const output = await worker.output;
132137
expect(normalize(output)).toMatchInlineSnapshot(`
133-
"Total Upload: xx KiB / gzip: xx KiB
134-
Your Worker has access to the following bindings:
135-
Binding Resource
136-
env.KV (inherited) KV Namespace
137-
env.D1 (inherited) D1 Database
138-
env.R2 (inherited) R2 Bucket
139-
Uploaded tmp-e2e-worker-00000000-0000-0000-0000-000000000000 (TIMINGS)
140-
Deployed tmp-e2e-worker-00000000-0000-0000-0000-000000000000 triggers (TIMINGS)
141-
https://tmp-e2e-worker-00000000-0000-0000-0000-000000000000.SUBDOMAIN.workers.dev
142-
Current Version ID: 00000000-0000-0000-0000-000000000000"
143-
`);
138+
"Total Upload: xx KiB / gzip: xx KiB
139+
Your Worker has access to the following bindings:
140+
Binding Resource
141+
env.KV (inherited) KV Namespace
142+
env.D1 (inherited) D1 Database
143+
env.R2 (inherited) R2 Bucket
144+
env.R2_WITH_NAME (does-not-exist) R2 Bucket
145+
Uploaded tmp-e2e-worker-00000000-0000-0000-0000-000000000000 (TIMINGS)
146+
Deployed tmp-e2e-worker-00000000-0000-0000-0000-000000000000 triggers (TIMINGS)
147+
https://tmp-e2e-worker-00000000-0000-0000-0000-000000000000.SUBDOMAIN.workers.dev
148+
Current Version ID: 00000000-0000-0000-0000-000000000000"
149+
`);
144150

145151
const response = await retry(
146152
(resp) => !resp.ok,
147153
async () => await fetch(deployedUrl)
148154
);
149155
await expect(response.text()).resolves.toEqual("Hello World!");
150156
});
157+
it("can inspect current bindings", async () => {
158+
const versionsRaw = await helper.run(
159+
`wrangler versions list --json --x-provision`
160+
);
151161

162+
const versions = JSON.parse(versionsRaw.stdout) as unknown[];
163+
164+
const latest = versions.at(-1) as { id: string };
165+
166+
const versionView = await helper.run(
167+
`wrangler versions view ${latest.id} --x-provision`
168+
);
169+
170+
expect(normalizeOutput(versionView.output)).toMatchInlineSnapshot(`
171+
"Version ID: 00000000-0000-0000-0000-000000000000
172+
Created: TIMESTAMP
173+
Author:
174+
Source: Unknown (version_upload)
175+
Tag: -
176+
Message: -
177+
Handlers: fetch
178+
Compatibility Date: 2023-01-01
179+
Your Worker has access to the following bindings:
180+
Binding Resource
181+
env.KV (00000000000000000000000000000000) KV Namespace
182+
env.D1 (00000000-0000-0000-0000-000000000000) D1 Database
183+
env.R2 (tmp-e2e-worker-00000000-0000-0000-0000-000000000000-r2) R2 Bucket
184+
env.R2_WITH_NAME (does-not-exist) R2 Bucket"
185+
`);
186+
});
152187
it("can inherit and provision resources on version upload", async () => {
153188
await helper.seed({
154189
"wrangler.toml": dedent`

packages/wrangler/e2e/versions.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,6 @@ describe.skipIf(!CLOUDFLARE_ACCOUNT_ID)(
643643
Source: Unknown (version_upload)
644644
Tag: e2e-upload-assets
645645
Message: Upload via e2e test
646-
------------------------------------------------------------
647646
Compatibility Date: 2023-01-01"
648647
`);
649648
});

packages/wrangler/src/__tests__/config/configuration.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2820,6 +2820,7 @@ describe("normalizeAndValidateConfig()", () => {
28202820
MULTIWORKER: false,
28212821
REMOTE_BINDINGS: false,
28222822
DEPLOY_REMOTE_DIFF_CHECK: false,
2823+
AUTOCREATE_RESOURCES: false,
28232824
},
28242825
() =>
28252826
normalizeAndValidateConfig(
@@ -2977,6 +2978,7 @@ describe("normalizeAndValidateConfig()", () => {
29772978
MULTIWORKER: false,
29782979
REMOTE_BINDINGS: false,
29792980
DEPLOY_REMOTE_DIFF_CHECK: false,
2981+
AUTOCREATE_RESOURCES: false,
29802982
},
29812983
() =>
29822984
normalizeAndValidateConfig(
@@ -3316,6 +3318,7 @@ describe("normalizeAndValidateConfig()", () => {
33163318
MULTIWORKER: false,
33173319
REMOTE_BINDINGS: false,
33183320
DEPLOY_REMOTE_DIFF_CHECK: false,
3321+
AUTOCREATE_RESOURCES: false,
33193322
},
33203323
() =>
33213324
normalizeAndValidateConfig(

packages/wrangler/src/__tests__/dev/remote-bindings.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,6 @@ describe("dev with remote bindings", { sequential: true }, () => {
472472
name: "EMAIL",
473473
remote: true,
474474
remoteProxyConnectionString,
475-
type: "send_email",
476475
},
477476
],
478477
},

packages/wrangler/src/__tests__/experimental-commands-api.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ describe("experimental_getWranglerCommands", () => {
3030
"requiresArg": true,
3131
"type": "string",
3232
},
33+
"experimental-auto-create": Object {
34+
"alias": "x-auto-create",
35+
"default": true,
36+
"describe": "Automatically provision draft bindings with new resources",
37+
"hidden": true,
38+
"type": "boolean",
39+
},
3340
"experimental-provision": Object {
3441
"alias": Array [
3542
"x-provision",

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ describe("metrics", () => {
198198
command: "wrangler docs",
199199
args: {
200200
xRemoteBindings: true,
201+
xAutoCreate: true,
201202
search: ["<REDACTED>"],
202203
},
203204
};

0 commit comments

Comments
 (0)