@@ -14,13 +14,10 @@ const {
1414 SECRET_STORE_PREFIX ,
1515} = getPrefixes ( ) ;
1616
17- function existingListIds ( stores , prefix ) {
18- return stores
19- . filter (
20- ( store ) =>
21- store . name ?. startsWith ( prefix ) || store . Name ?. startsWith ( prefix ) ,
22- )
23- . map ( ( existing ) => existing . id || existing . StoreID ) ;
17+ function existingList ( stores , prefix ) {
18+ return stores . filter (
19+ ( store ) => store . name ?. startsWith ( prefix ) || store . Name ?. startsWith ( prefix ) ,
20+ ) ;
2421}
2522
2623const startTime = Date . now ( ) ;
@@ -42,100 +39,130 @@ if (process.env.FASTLY_API_TOKEN === undefined) {
4239}
4340const FASTLY_API_TOKEN = process . env . FASTLY_API_TOKEN ;
4441
45- async function removeConfigStores ( links , serviceIds ) {
42+ async function removeConfigStores ( links , services ) {
43+ console . log ( 'Removing config stores...' ) ;
4644 const stores = JSON . parse (
4745 await zx `fastly config-store list --quiet --json --token $FASTLY_API_TOKEN` ,
4846 ) ;
4947
50- let STORE_IDS = existingListIds ( stores , DICTIONARY_PREFIX ) ;
51- for ( const STORE_ID of STORE_IDS ) {
52- await zx `fastly config-store delete --store-id=${ STORE_ID } --token $FASTLY_API_TOKEN` ;
48+ let dictionaries = existingList ( stores , DICTIONARY_PREFIX ) ;
49+ for ( const dictionary of dictionaries ) {
50+ console . log ( `\tDeleting dictionary ${ dictionary . name } ` ) ;
51+ try {
52+ await zx `fastly config-store delete --store-id=${ dictionary . id } --token $FASTLY_API_TOKEN` ;
53+ } catch { }
5354 }
55+ console . log ( 'All dictionaries removed' ) ;
5456
55- STORE_IDS = existingListIds ( stores , CONFIG_STORE_PREFIX ) ;
56- for ( const STORE_ID of STORE_IDS ) {
57+ let configStores = existingList ( stores , CONFIG_STORE_PREFIX ) ;
58+ for ( const store of configStores ) {
59+ console . log ( `\tDeleting config store ${ store . name } ` ) ;
5760 try {
58- await zx `fastly config-store delete --store-id=${ STORE_ID } --token $FASTLY_API_TOKEN` ;
61+ await zx `fastly config-store delete --store-id=${ store . id } --token $FASTLY_API_TOKEN` ;
5962 } catch { }
6063 }
64+ console . log ( 'All config stores removed' ) ;
6165}
6266
6367async function removeKVStores ( ) {
68+ console . log ( 'Removing KV stores...' ) ;
6469 const stores = JSON . parse (
6570 await zx `fastly kv-store list --quiet --json --token $FASTLY_API_TOKEN` ,
6671 ) . Data ;
6772
68- let STORE_IDS = existingListIds ( stores , KV_STORE_PREFIX ) ;
69- for ( const STORE_ID of STORE_IDS ) {
70- await zx `fastly kv-store delete --store-id=${ STORE_ID } --quiet --all -y --token $FASTLY_API_TOKEN` ;
73+ let kvStores = existingList ( stores , KV_STORE_PREFIX ) ;
74+ for ( const store of kvStores ) {
75+ console . log ( `\tDeleting KV store ${ store . Name } ` ) ;
76+ try {
77+ await zx `fastly kv-store delete --store-id=${ store . StoreID } --quiet --all -y --token $FASTLY_API_TOKEN` ;
78+ } catch { }
7179 }
80+ console . log ( 'All KV stores removed' ) ;
7281}
7382
74- async function removeSecretStores ( serviceIds ) {
83+ async function removeSecretStores ( services ) {
84+ console . log ( 'Removing secret stores...' ) ;
7585 const stores = JSON . parse (
7686 await zx `fastly secret-store list --quiet --json --token $FASTLY_API_TOKEN` ,
7787 ) ;
7888 if ( ! stores ) {
7989 return ;
8090 }
81- const STORE_IDS = existingListIds ( stores , SECRET_STORE_PREFIX ) ;
82- for ( const STORE_ID of STORE_IDS ) {
91+ const secretStores = existingList ( stores , SECRET_STORE_PREFIX ) ;
92+ for ( const store of secretStores ) {
93+ console . log ( `\tDeleting secret store ${ store . name } ` ) ;
8394 try {
84- await zx `fastly secret-store delete --store-id=${ STORE_ID } --token $FASTLY_API_TOKEN` ;
95+ await zx `fastly secret-store delete --store-id=${ store . id } --token $FASTLY_API_TOKEN` ;
8596 } catch { }
8697 }
98+ console . log ( 'All secret stores removed' ) ;
8799}
88100
89- async function removeAcls ( serviceIds ) {
90- const ACL_IDS = existingListIds (
101+ async function removeAcls ( services ) {
102+ console . log ( 'Removing ACLs...' ) ;
103+ const acls = existingList (
91104 JSON . parse (
92105 await zx `fastly compute acl list-acls --quiet --json --token $FASTLY_API_TOKEN` ,
93106 ) . data ,
94107 ACL_PREFIX ,
95108 ) ;
96109
97- for ( const ACL_ID of ACL_IDS ) {
98- await zx `fastly compute acl delete --acl-id=${ ACL_ID } --token $FASTLY_API_TOKEN` ;
110+ for ( const acl of acls ) {
111+ console . log ( `\tDeleting acl ${ acl . name } ` ) ;
112+ try {
113+ await zx `fastly compute acl delete --acl-id=${ acl . id } --token $FASTLY_API_TOKEN` ;
114+ } catch { }
99115 }
116+ console . log ( 'All ACLs removed' ) ;
100117}
101118
102- async function getServiceIds ( ) {
119+ async function getServices ( ) {
120+ console . log ( 'Getting services...' ) ;
103121 let services = JSON . parse (
104122 await zx `fastly service list --token $FASTLY_API_TOKEN --json` ,
105123 ) ;
106- return services
107- . filter ( ( { Name } ) => Name . startsWith ( SERVICE_PREFIX ) )
108- . map ( ( service ) => service . ServiceID ) ;
124+ services = services . filter ( ( { Name } ) => Name . startsWith ( SERVICE_PREFIX ) ) ;
125+ console . log ( 'Services to delete:' ) ;
126+ for ( const service of services ) {
127+ console . log ( '\t' , service . Name ) ;
128+ }
129+ return services ;
109130}
110131
111- async function removeServices ( serviceIds ) {
112- for ( const serviceId of serviceIds ) {
113- await zx `fastly service delete --force --service-id=${ serviceId } ` ;
132+ async function removeServices ( services ) {
133+ console . log ( 'Removing services...' ) ;
134+ for ( const service of services ) {
135+ console . log ( `\tDeleting service ${ service . Name } ` ) ;
136+ await zx `fastly service delete --force --service-id=${ service . ServiceID } ` ;
114137 }
138+ console . log ( 'ALl services removed' ) ;
115139}
116140
117- async function removeLinks ( serviceIds ) {
118- for ( const serviceId of serviceIds ) {
141+ async function removeLinks ( services ) {
142+ console . log ( 'Removing links...' ) ;
143+ for ( const service of services ) {
119144 const links = JSON . parse (
120- await zx `fastly resource-link list --service-id=${ serviceId } --quiet --json --version latest --token $FASTLY_API_TOKEN` ,
145+ await zx `fastly resource-link list --service-id=${ service . ServiceID } --quiet --json --version latest --token $FASTLY_API_TOKEN` ,
121146 ) ;
122147 for ( const link of links ) {
148+ console . log (
149+ `\tDeleting link between service ${ service . Name } and resource ${ link . name } ` ,
150+ ) ;
123151 await zx `fastly resource-link delete --version latest --autoclone --id=${ link . id } --service-id=${ link . service_id } --token $FASTLY_API_TOKEN` ;
124152 await zx `fastly service-version activate --version latest --service-id=${ link . service_id } --token $FASTLY_API_TOKEN` ;
125153 }
126154 }
155+ console . log ( 'All links removed' ) ;
127156}
128157
129- const serviceIds = await getServiceIds ( ) ;
130- await removeLinks ( serviceIds ) ;
131- await Promise . all ( [
132- removeConfigStores ( serviceIds ) ,
133- removeKVStores ( ) ,
134- removeSecretStores ( serviceIds ) ,
135- removeAcls ( serviceIds ) ,
136- ] ) ;
137- await removeServices ( serviceIds ) ;
158+ const services = await getServices ( ) ;
159+ await removeLinks ( services ) ;
160+ await removeConfigStores ( services ) ;
161+ await removeKVStores ( ) ;
162+ await removeSecretStores ( services ) ;
163+ await removeAcls ( services ) ;
164+ await removeServices ( services ) ;
138165
139166console . log (
140- `Tear down has finished! Took ${ ( Date . now ( ) - startTime ) / 1000 } seconds to complete` ,
167+ `Cleanup has finished! Took ${ ( Date . now ( ) - startTime ) / 1000 } seconds to complete` ,
141168) ;
0 commit comments