Skip to content

Instantly share code, notes, and snippets.

@phamann
Created March 20, 2017 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phamann/b6db38b3ea8a59bdbb9c58d2191d5b4f to your computer and use it in GitHub Desktop.
Save phamann/b6db38b3ea8a59bdbb9c58d2191d5b4f to your computer and use it in GitHub Desktop.
Delete all services within a Fastly acconut
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