Created
March 20, 2017 15:27
-
-
Save phamann/b6db38b3ea8a59bdbb9c58d2191d5b4f to your computer and use it in GitHub Desktop.
Delete all services within a Fastly acconut
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('dotenv').config(); | |
const fetch = require('node-fetch'); | |
const API = 'https://api.fastly.com'; | |
const TOKEN = process.env.FASTLY_API_TOKEN; | |
const OPTIONS = { | |
headers: { | |
'Fastly-Key': TOKEN, | |
'Accept': 'application/json' | |
} | |
}; | |
let SERVICES; | |
function apiRequest(path, method) { | |
method = method || 'GET'; | |
let options = Object.assign(OPTIONS, { method }); | |
return fetch(API + path, options).then(r => r.json()); | |
} | |
console.log('Fetching services'); | |
apiRequest('/service') | |
.then(serviceData => { | |
SERVICES = serviceData.map(s => s.id); | |
const serviceRequests = SERVICES.map(id => apiRequest(`/service/${id}/version`)); | |
console.log('Fetching versions'); | |
return Promise.all(serviceRequests); | |
}) | |
.then(versionData => { | |
const versions = [].concat.apply([], versionData.map(v => v.filter(d => d.active))); | |
const versionRequests = versions.map(v => apiRequest(`/service/${v.service_id}/version/${v.number}/deactivate`, 'PUT')); | |
console.log('Deactivating services'); | |
return Promise.all(versionRequests); | |
}) | |
.then(() => { | |
const deleteRequests = SERVICES.map(id => apiRequest(`/service/${id}`, 'DELETE')); | |
console.log('Deleting services'); | |
return Promise.all(deleteRequests); | |
}) | |
.then(console.log) | |
.catch(console.log); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment