Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions filterTools/README.md
Original file line number Diff line number Diff line change
@@ -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
```
45 changes: 45 additions & 0 deletions filterTools/createFilters.js
Original file line number Diff line number Diff line change
@@ -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<settings.filters.length ; i++) {
var filterString = fs.readFileSync(settings.filters[i]);
options.headers['Content-Length'] = Buffer.byteLength(filterString);

req = https.request(options, (res) => {
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();
}
});
66 changes: 66 additions & 0 deletions filterTools/deleteFilters.js
Original file line number Diff line number Diff line change
@@ -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<filters.length ; i++) {
var filter = filters[i];
console.log(`attempting to delete ${filter.name}`);
var deleteOptions = {
hostname: 'platform.segmentapis.com',
port: 443,
method: 'DELETE',
path: `/v1beta/${filter.name}`,
headers: {
'Content-Type':'application/json',
'Authorization':`Bearer ${settings.access_token}`
}
};
var deleteRequest = https.request(deleteOptions, (res) => {
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();
});
8 changes: 8 additions & 0 deletions filterTools/environment.js
Original file line number Diff line number Diff line change
@@ -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']
};
Empty file added filterTools/filters/.gitkeep
Empty file.
42 changes: 42 additions & 0 deletions filterTools/listFilters.js
Original file line number Diff line number Diff line change
@@ -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<filters.length ; i++) {
var filter = filters[i];
console.log(JSON.stringify(filter));
console.log();
}

});
});
req.on('error', (e) => {
console.error(e);
});
req.end();
});