From 688ec705f8faa3dbf9eaac0af119324004757677 Mon Sep 17 00:00:00 2001 From: Troy Sellers Date: Wed, 20 May 2020 09:29:07 +1000 Subject: [PATCH 1/2] initial commit --- filterTools/README.md | 45 ++++++++++++++++++++++++ filterTools/createFilters.js | 45 ++++++++++++++++++++++++ filterTools/deleteFilters.js | 66 ++++++++++++++++++++++++++++++++++++ filterTools/environment.js | 8 +++++ filterTools/listFilters.js | 42 +++++++++++++++++++++++ 5 files changed, 206 insertions(+) create mode 100644 filterTools/README.md create mode 100644 filterTools/createFilters.js create mode 100644 filterTools/deleteFilters.js create mode 100644 filterTools/environment.js create mode 100644 filterTools/listFilters.js diff --git a/filterTools/README.md b/filterTools/README.md new file mode 100644 index 0000000..d06a32b --- /dev/null +++ b/filterTools/README.md @@ -0,0 +1,45 @@ +# Filter Tools + +This is a utility for creating, listing and deleting [destination filters](https://segment.com/docs/connections/destinations/destination-filters/). +The main value is a tool that allows a user to pull filters from an collection of Source / Destination comibinations (i.e. non-prod) and then push those filters into a different configuration (i.e. prod) + +You can use this tool to setup multiple sources into the same destination. For multiple destinations, you would need to manage in configuration and run mulitple times. + +## Usage + +### Configuration +The configuration is managed in the environment.js file (you can clone and have multiples of this) and this is passed into the various tools listed below. +``` +exports.settings = { + workspace:'your_workspace', + access_token:'your_access_token', + sources:["list","of","sources"], + destination:"destination_source_is_connected_to", + filters:['filters/filter1.json','filters/filter2.json'] +}; +``` + +This configuration would set two destination filters on three sources, all pointing to the same destination. +Pass the environment file into the tool on the commandline, leave the ".js" off it + +### Listing Filters +Get a list of the destination filters, including the Filter JSON. +This simply prints to the console.log output. + +``` +> node listFilters.js environment +``` + +### Create Filters +Will create all the filters listed in the configuration files, accepts the environment configuration as command parameter + +``` +> node createFilters.js environment +``` + +### Delete Filters +Deletes all the filters on a source / destination pair. + +``` +> node deleteFilters.js environment +``` \ No newline at end of file diff --git a/filterTools/createFilters.js b/filterTools/createFilters.js new file mode 100644 index 0000000..c036410 --- /dev/null +++ b/filterTools/createFilters.js @@ -0,0 +1,45 @@ +`use strict` + +const https = require('https'); +const fs = require('fs'); +if (process.argv.length <= 2) { + console.log("\nspecifcy which settings module you want use"); + console.log("e.g. node createFilters.js segment-test-env\n"); + process.exit(1); +} +let settings = require(`./${process.argv[2]}`).settings; + + +settings.sources.forEach(source => { + + var options = { + hostname: 'platform.segmentapis.com', + port: 443, + method: 'POST', + path: `/v1beta/workspaces/${settings.workspace}/sources/${source}/destinations/${settings.destination}/filters`, + headers: { + 'Content-Type':'application/json', + 'Authorization':`Bearer ${settings.access_token}` + } + }; + + for(var i=0 ; i { + var result = ''; + res.on('data', (d) => { + result += d; + }); + res.on('end', () => { + console.log(result); + }); + }); + req.on('error', (e) => { + console.error(e); + }); + req.write(filterString); + req.end(); + } +}); \ No newline at end of file diff --git a/filterTools/deleteFilters.js b/filterTools/deleteFilters.js new file mode 100644 index 0000000..2932895 --- /dev/null +++ b/filterTools/deleteFilters.js @@ -0,0 +1,66 @@ +`use strict` + +const https = require('https'); +if (process.argv.length <= 2) { + console.log("\nspecifcy which settings module you want use"); + console.log("e.g. node deleteFilters.js segment-test-env\n"); + process.exit(1); +} +let settings = require(`./${process.argv[2]}`).settings; + + +settings.sources.forEach(source => { + + var options = { + hostname: 'platform.segmentapis.com', + path: `/v1beta/workspaces/${settings.workspace}/sources/${source}/destinations/${settings.destination}/filters`, + headers: { + 'Content-Type':'application/json', + 'Authorization':`Bearer ${settings.access_token}` + } + }; + + var req = https.get(options, (res) => { + var result = ''; + res.on('data', (chunk) => { + result += chunk; + }); + res.on('end', () => { + + var filters = JSON.parse(result).filters; + + for(var i=0 ; i { + var result = ''; + res.on('data', (d) => { + result += d; + }); + res.on('end', () => { + console.log(result); + }); + }); + deleteRequest.on('error', (e) => { + console.log(e) + }); + deleteRequest.end(); + } + + }); + }); + req.on('error', (e) => { + console.error(e); + }); + req.end(); +}); \ No newline at end of file diff --git a/filterTools/environment.js b/filterTools/environment.js new file mode 100644 index 0000000..8308f31 --- /dev/null +++ b/filterTools/environment.js @@ -0,0 +1,8 @@ + +exports.settings = { + workspace:'segment-sa', // workplace slug can be found in the URL of your segment home page (https://app.segment.com/segment-sa/overview - my workspace in this instance is segment-sa) + access_token:'your_access_token', // https://segment.com/docs/config-api/#access-tokens + sources:["list","of","sources"], + destination:"destination_source_is_connected_to", // this is not going to handle a list of destinations + filters:['filters/filter1.json','filters/filter2.json'] +}; diff --git a/filterTools/listFilters.js b/filterTools/listFilters.js new file mode 100644 index 0000000..9af2ddb --- /dev/null +++ b/filterTools/listFilters.js @@ -0,0 +1,42 @@ +`use strict` + +const https = require('https'); + +if (process.argv.length <= 2) { + console.log("\nspecifcy which settings module you want use"); + console.log("e.g. node listFilters.js segment-test-env\n"); + process.exit(1); +} +let settings = require(`./${process.argv[2]}`).settings; + +settings.sources.forEach(source => { + + var options = { + hostname: 'platform.segmentapis.com', + path: `/v1beta/workspaces/${settings.workspace}/sources/${source}/destinations/${settings.destination}/filters`, + headers: { + 'Content-Type':'application/json', + 'Authorization':`Bearer ${settings.access_token}` + } + }; + var req = https.get(options, (res) => { + var result = ''; + res.on('data', (d) => { + result += d; + }); + res.on('end', () => { + + var filters = JSON.parse(result).filters; + for(var i=0 ; i { + console.error(e); + }); + req.end(); +}); \ No newline at end of file From 2b02ca78b010f2e0edb0d7ee7d14a020fa3f2342 Mon Sep 17 00:00:00 2001 From: Troy Sellers Date: Wed, 20 May 2020 09:32:00 +1000 Subject: [PATCH 2/2] add empty filters folder --- filterTools/filters/.gitkeep | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 filterTools/filters/.gitkeep diff --git a/filterTools/filters/.gitkeep b/filterTools/filters/.gitkeep new file mode 100644 index 0000000..e69de29