Skip to content

Commit 60f9f79

Browse files
Jake ChampionJakeChampion
authored andcommitted
use latest fastly cli
1 parent 0d0f1bf commit 60f9f79

File tree

3 files changed

+38
-83
lines changed

3 files changed

+38
-83
lines changed

integration-tests/js-compute/fixtures/secret-store/bin/index.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,24 @@ addEventListener("fetch", event => {
1212
* @returns {Response}
1313
*/
1414
async function app(event) {
15+
let response;
16+
const version = env('FASTLY_SERVICE_VERSION') || 'local'
1517
try {
1618
const path = (new URL(event.request.url)).pathname;
17-
console.log(`path: ${path}`)
18-
console.log(`FASTLY_SERVICE_VERSION: ${env('FASTLY_SERVICE_VERSION')}`)
19+
console.log(`path: ${path}`);
20+
console.log(`FASTLY_SERVICE_VERSION: ${version}`);
1921
if (routes.has(path)) {
2022
const routeHandler = routes.get(path);
21-
return await routeHandler()
23+
response = await routeHandler();
24+
} else {
25+
response = fail(`${path} endpoint does not exist`);
2226
}
23-
return fail(`${path} endpoint does not exist`)
2427
} catch (error) {
25-
return fail(`The routeHandler threw an error: ${error.message}` + '\n' + error.stack)
28+
response = fail(`The routeHandler threw an error: ${error.message}` + '\n' + error.stack);
2629
}
30+
31+
response.headers.set('FASTLY_SERVICE_VERSION', version);
32+
return response;
2733
}
2834

2935
const routes = new Map();
Lines changed: 13 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env node
22

3-
import { $ as zx, fetch } from 'zx'
3+
import { $ as zx } from 'zx'
44
import { retry } from 'zx/experimental'
55

66
const startTime = Date.now();
@@ -18,79 +18,30 @@ if (process.env.FASTLY_API_TOKEN === undefined) {
1818
process.exit(1)
1919
}
2020
}
21-
const FASTLY_API_TOKEN = process.env.FASTLY_API_TOKEN;
2221

2322
zx.verbose = true;
2423

2524
let stores = await (async function() {
2625
try {
27-
let response = await fetch("https://api.fastly.com/resources/stores/secret", {
28-
method: 'GET',
29-
headers: {
30-
"Content-Type": "application/json",
31-
Accept: "application/json",
32-
"Fastly-Key": FASTLY_API_TOKEN
33-
}
34-
});
35-
return await response.json();
26+
return JSON.parse(await zx`fastly secret-store list --json --token $FASTLY_API_TOKEN`)
3627
} catch {
3728
return {data:[]}
3829
}
3930
}())
4031

41-
let STORE_ID = stores.data.find(({ name }) => name === 'example-test-secret-store')?.id
42-
if (!STORE_ID) {
43-
STORE_ID = await fetch("https://api.fastly.com/resources/stores/secret", {
44-
method: 'POST',
45-
headers: {
46-
"Content-Type": "application/json",
47-
Accept: "application/json",
48-
"Fastly-Key": FASTLY_API_TOKEN
49-
},
50-
body: '{"name":"example-test-secret-store"}'
51-
})
52-
STORE_ID = (await STORE_ID.json()).id
32+
process.env.STORE_ID = stores.data.find(({ name }) => name === 'example-test-secret-store')?.id
33+
if (!process.env.STORE_ID) {
34+
process.env.STORE_ID = JSON.parse(await zx`fastly secret-store create --name=example-test-secret-store --json --token $FASTLY_API_TOKEN`).id
5335
}
5436

55-
await fetch(`https://api.fastly.com/resources/stores/secret/${STORE_ID}/secrets`, {
56-
method: 'POST',
57-
headers: {
58-
"Content-Type": "application/json",
59-
Accept: "application/json",
60-
"Fastly-Key": FASTLY_API_TOKEN
61-
},
62-
body: JSON.stringify({
63-
name: "first",
64-
secret: 'This is also some secret data'
65-
})
66-
})
67-
await fetch(`https://api.fastly.com/resources/stores/secret/${STORE_ID}/secrets`, {
68-
method: 'POST',
69-
headers: {
70-
"Content-Type": "application/json",
71-
Accept: "application/json",
72-
"Fastly-Key": FASTLY_API_TOKEN
73-
},
74-
body: JSON.stringify({
75-
name: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
76-
secret: 'This is some secret data'
77-
})
78-
})
37+
try {
38+
await zx`echo -n 'This is also some secret data' | fastly secret-store-entry create --name first --store-id=$STORE_ID --stdin --token $FASTLY_API_TOKEN`
39+
} catch {}
40+
try {
41+
let key = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
42+
await zx`echo -n 'This is some secret data' | fastly secret-store-entry create --name ${key} --store-id=$STORE_ID --stdin --token $FASTLY_API_TOKEN`
43+
} catch {}
7944

80-
let VERSION = String(await $`fastly service-version clone --version=latest --token $FASTLY_API_TOKEN`).trim()
81-
VERSION = VERSION.match(/\d+$/)?.[0]
82-
83-
let SERVICE_ID = await $`fastly service describe --json --quiet --token $FASTLY_API_TOKEN`
84-
SERVICE_ID = JSON.parse(SERVICE_ID).ID
85-
await fetch(`https://api.fastly.com/service/${SERVICE_ID}/version/${VERSION}/resource`, {
86-
method: 'POST',
87-
headers: {
88-
"Content-Type": "application/x-www-form-urlencoded",
89-
Accept: "application/json",
90-
"Fastly-Key": FASTLY_API_TOKEN
91-
},
92-
body: `name=example-test-secret-store&resource_id=${STORE_ID}`
93-
})
94-
await $`fastly service-version activate --version=${VERSION} --quiet --token $FASTLY_API_TOKEN`
45+
await zx`fastly resource-link create --version latest --resource-id $STORE_ID --token $FASTLY_API_TOKEN --autoclone`
9546

9647
console.log(`Set up has finished! Took ${(Date.now() - startTime) / 1000} seconds to complete`);

integration-tests/js-compute/fixtures/secret-store/teardown.js

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,23 @@ if (process.env.FASTLY_API_TOKEN === undefined) {
1515
process.exit(1)
1616
}
1717
}
18-
const FASTLY_API_TOKEN = process.env.FASTLY_API_TOKEN;
1918
zx.verbose = true;
20-
let stores = await fetch("https://api.fastly.com/resources/stores/secret", {
21-
method: 'GET',
22-
headers: {
23-
"Content-Type": "application/json",
24-
Accept: "application/json",
25-
"Fastly-Key": FASTLY_API_TOKEN
19+
let stores = await (async function() {
20+
try {
21+
return JSON.parse(await zx`fastly secret-store list --json --token $FASTLY_API_TOKEN`)
22+
} catch {
23+
return {data:[]}
2624
}
27-
})
25+
}())
2826

29-
let STORE_ID = (await stores.json()).data.find(({ name }) => name === 'example-test-secret-store')?.id
30-
if (STORE_ID) {
31-
await fetch(`https://api.fastly.com/resources/stores/secret/${STORE_ID}`, {
32-
method: 'DELETE',
33-
headers: {
34-
"Fastly-Key": FASTLY_API_TOKEN
35-
}
36-
})
27+
process.env.STORE_ID = stores.data.find(({ name }) => name === 'example-test-secret-store')?.id
28+
if (process.env.STORE_ID) {
29+
try {
30+
await zx`fastly resource-link delete --version latest --autoclone --id=$STORE_ID --token $FASTLY_API_TOKEN`
31+
} catch {}
32+
try {
33+
await zx`fastly secret-store delete --store-id=$STORE_ID --token $FASTLY_API_TOKEN`
34+
} catch {}
3735
}
3836

3937
console.log(`Tear down has finished! Took ${(Date.now() - startTime) / 1000} seconds to complete`);

0 commit comments

Comments
 (0)