Skip to content

Commit

Permalink
Merge pull request #23 from wyvern8/feature/proxy-support
Browse files Browse the repository at this point in the history
feat(proxy): respect https_proxy env var
  • Loading branch information
wyvern8 authored Mar 15, 2018
2 parents 4c7dad4 + 762cbc4 commit 34ceb4d
Show file tree
Hide file tree
Showing 11 changed files with 264 additions and 23 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ ldutils getFeatureFlags <myProjectId>
ldutils upsertCustomRole <customRoleKey> <customRoleName> '[{"resources":["proj/*"],"actions":["*"],"effect":"allow"}]'
```

### proxies
If you are executing ldutils via a proxy, just set the https_proxy env var, eg:
```
export https_proxy=http://localhost:3128
```


### commandline modes and parameters
The command line modes and parameters map directly to the functions exposed for use in nodejs apps.
Each call to `ldutils` takes a 'mode', and associated parameters as below:
Expand Down
6 changes: 3 additions & 3 deletions ldutils
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ new LaunchDarklyUtils().create(process.env.LAUNCHDARKLY_API_TOKEN, log).then(fun

program
.command('getTeamMember <memberId>')
.description('get a team members by id')
.description('get a team member by id')
.action(function(memberId) {
ldUtils.members.getTeamMember(memberId).then(function(response) {
console.log(json.plain(response));
Expand All @@ -193,15 +193,15 @@ new LaunchDarklyUtils().create(process.env.LAUNCHDARKLY_API_TOKEN, log).then(fun

program
.command('getTeamMemberCustomRoles <emailAddress>')
.description('get a team member by email address')
.description('get a team members custom roles by email address')
.action(function(emailAddress) {
ldUtils.members.getTeamMemberCustomRoles(emailAddress).then(function(response) {
console.log(json.plain(response));
});
});

program.on('--help', function() {
console.log('\n See https://github.com/wyvern8/launchdarkly-nodeutils for details.\n');
console.log('\n See https://github.com/wyvern8/launchdarkly-nodeutils/API.md for examples.\n');
});

if (process.argv.length < 3) {
Expand Down
97 changes: 82 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@
"bunyan-format": "^0.2.1",
"fast-json-patch": "^2.0.6",
"globule": "^1.2.0",
"https-proxy-agent": "^2.2.0",
"js-yaml": "^3.10.0",
"json-diff": "^0.5.2",
"json-format": "^1.0.1",
"lodash": "^4.17.5",
"node-fetch": "^2.1.1",
"swagger-client": "^3.4.7",
"webpack": "^4.0.0"
},
Expand Down
2 changes: 1 addition & 1 deletion scripts/jqJsonDiff.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ if [ $# -eq 0 ]; then
exit 1
fi

diff <(cat "$1" | jq . --sort-keys) <(cat "$2" | jq . --sort-keys)
diff -U5 <(cat "$1" | jq . --sort-keys) <(cat "$2" | jq . --sort-keys)
12 changes: 12 additions & 0 deletions scripts/jsonDiff.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

display_usage() {
echo "This script must be run with two files to compare."
echo "eg. ./jsonDiff.sh ./before.json ../after.json"
}

if [ $# -eq 0 ]; then
display_usage
exit 1
fi
node ../node_modules/json-diff/bin/json-diff.js $1 $2
11 changes: 11 additions & 0 deletions src/LaunchDarklyApiClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import Swagger from 'swagger-client';
import jsYaml from 'js-yaml';
import { default as fs } from 'fs';
import { default as json } from 'format-json';
import { default as HttpsProxyAgent } from 'https-proxy-agent';
import { default as fetch } from 'node-fetch';

/**
* @class
Expand All @@ -20,6 +22,13 @@ export class LaunchDarklyApiClient {
static async create(API_TOKEN, log, swaggerYamlString) {
log.debug(`creating api client with token: ${API_TOKEN}`);

let proxy = process.env.http_proxy || process.env.https_proxy || null;
let agent = null;
if (proxy) {
agent = new HttpsProxyAgent(proxy);
log.debug(`using proxy from env var https_proxy=${proxy}`);
}

// swagger.yaml from https://launchdarkly.github.io/ld-openapi/swagger.yaml
const swaggerYaml = swaggerYamlString || fs.readFileSync(__dirname + `/../swagger.yaml`, 'utf-8').toString();
const swaggerJson = jsYaml.safeLoad(swaggerYaml);
Expand All @@ -28,6 +37,8 @@ export class LaunchDarklyApiClient {
spec: swaggerJson,
usePromise: true,
requestInterceptor: req => {
req.userFetch = fetch;
req.agent = agent;
req.headers.Authorization = API_TOKEN;
log.debug(`REQUEST: ${json.plain(req)}`);

Expand Down
10 changes: 6 additions & 4 deletions src/LaunchDarklyUtilsRoles.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,13 +254,15 @@ export class LaunchDarklyUtilsRoles {
let globMatch = folderPath + '/*.json';
this.log.debug(`Looking for Files with Pattern '${globMatch}'`);
let fileArray = globule.find(globMatch);
let results = [];
let promises = [];
let that = this;
fileArray.forEach(async function(file) {
that.log.debug(`Found File '${file}'. Calling 'bulkUpsertCustomRoles'`);
let result = await that.bulkUpsertCustomRoles(file);
results.push(result);
promises.push(that.bulkUpsertCustomRoles(file));
});
// resolve all and flatten into single result array
return Promise.all(promises).then(results => {
return Promise.resolve([].concat.apply([], results));
});
return results;
}
}
58 changes: 58 additions & 0 deletions test/LaunchDarklyUtilsRoles.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,4 +278,62 @@ describe('LaunchDarklyUtilsRoles', function() {
});
});
});

describe('bulkUpsertCustomRoleFolder', function() {
before(done => {
let scope = nock('https://app.launchdarkly.com')
// update
.get('/api/v2/roles/test-role-one')
.replyWithFile(200, __dirname + '/fixtures/custom-roles-get.json', {
'Content-Type': 'application/json'
})
.get('/api/v2/roles/test-role-one')
.replyWithFile(200, __dirname + '/fixtures/custom-roles-get.json', {
'Content-Type': 'application/json'
})
.patch('/api/v2/roles/test-role-one')
.replyWithFile(200, __dirname + '/fixtures/custom-roles-update.json', {
'Content-Type': 'application/json'
})

.get('/api/v2/roles/test-role-two')
.replyWithFile(200, __dirname + '/fixtures/custom-roles-get.json', {
'Content-Type': 'application/json'
})
.get('/api/v2/roles/test-role-two')
.replyWithFile(200, __dirname + '/fixtures/custom-roles-get.json', {
'Content-Type': 'application/json'
})
.patch('/api/v2/roles/test-role-two')
.replyWithFile(200, __dirname + '/fixtures/custom-roles-update.json', {
'Content-Type': 'application/json'
})

// create
.get('/api/v2/roles/test-role-three')
.reply(
404,
{ error: 'Not Found' },
{
'Content-Type': 'application/json'
}
)
.post('/api/v2/roles')
.replyWithFile(200, __dirname + '/fixtures/custom-roles-create.json', {
'Content-Type': 'application/json'
});

assert(scope);
done();
});

it('should operate on json bulkload file', async function() {
let expected = JSON.parse(
fs.readFileSync(__dirname + '/fixtures/custom-roles-bulk-load-result.json', 'utf-8')
);
return ldutils.roles.bulkUpsertCustomRoleFolder(__dirname + '/fixtures/bulkroles').then(actual => {
expect(actual).to.deep.equal(expected);
});
});
});
});
Loading

0 comments on commit 34ceb4d

Please sign in to comment.