Skip to content

Instantly share code, notes, and snippets.

@eladidan
Last active July 1, 2022 22:32
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 eladidan/32b0e01e143042daea7e262c09409775 to your computer and use it in GitHub Desktop.
Save eladidan/32b0e01e143042daea7e262c09409775 to your computer and use it in GitHub Desktop.
DD: Batch update SLOS

Usage

To run this, first initialize DD secrets from 1Password via:

eval $(op signin my)
DD_API_KEY=`op get item jmmuyt4oreaw2fkkega2x4vuqq --fields password` \
DD_APP_KEY=`op get item 6lwjvw53rhtxcmw6gwuyccutpq --fields password` \

Edit the code to update the old and new team tags TODO: make this a cli argument

Then, in the same shell, run (from the code's location):

./index.js
#!/usr/bin/env node
import { client, v1 } from '@datadog/datadog-api-client';
import _ from 'lodash';
(async () => {
try {
const configuration = client.createConfiguration({
authMethods: {
apiKeyAuth: process.env['DD_API_KEY'],
appKeyAuth: process.env['DD_APP_KEY'],
},
});
const sloApi = new v1.ServiceLevelObjectivesApi(configuration);
const slos = await sloApi.listSLOs({
tagsQuery: 'team:vb-payments',
});
slos.data.forEach(async slo => {
_.$update(slo.tags, 'team:vb-payments', 'team:employee-access')
const resp = await sloApi.updateSLO({
sloId: slo.id,
body: slo,
})
console.log(`Updated ${resp.data.sloId}`)
});
// console.log(`Found ${slos.data.length} slos`)
// console.log(_.map(slos.data, slo => slo.name))
} catch (e) {
console.error('An unexpected error occured', e);
}
})();
_.mixin({ '$update': (arr, oldVal, newVal) => {
const index = _.findIndex(arr, (val) => val === oldVal);
if(index != -1){
arr.splice(index, 1, newVal);
} else {
arr.push(newVal);
}
}});
{
"name": "dd-slo-mutate",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"@datadog/datadog-api-client": "^1.0.0",
"lodash": "^4.17.21"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment