1
1
#!/usr/bin/env node
2
2
3
- import { $ as zx , fetch } from 'zx'
3
+ import { $ as zx } from 'zx'
4
4
import { retry } from 'zx/experimental'
5
5
6
6
const startTime = Date . now ( ) ;
@@ -18,79 +18,30 @@ if (process.env.FASTLY_API_TOKEN === undefined) {
18
18
process . exit ( 1 )
19
19
}
20
20
}
21
- const FASTLY_API_TOKEN = process . env . FASTLY_API_TOKEN ;
22
21
23
22
zx . verbose = true ;
24
23
25
24
let stores = await ( async function ( ) {
26
25
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` )
36
27
} catch {
37
28
return { data :[ ] }
38
29
}
39
30
} ( ) )
40
31
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
53
35
}
54
36
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 { }
79
44
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`
95
46
96
47
console . log ( `Set up has finished! Took ${ ( Date . now ( ) - startTime ) / 1000 } seconds to complete` ) ;
0 commit comments