Skip to content

Commit

Permalink
feat: implement the logic for checking a collection of cloud providers
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Coe committed Jun 3, 2016
1 parent c521eb0 commit a4b7bf9
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -4,7 +4,7 @@
[![Coverage Status](https://coveralls.io/repos/github/bcoe/which-cloud/badge.svg?branch=master)](https://coveralls.io/github/bcoe/which-cloud?branch=master)
[![Standard Version](https://img.shields.io/badge/release-standard%20version-brightgreen.svg)](https://github.com/conventional-changelog/standard-version)

given an IP address, return which cloud provider it belongs to (EC2, GCE, etc)
given an ip address, return which cloud provider it belongs to (EC2, GCE, etc)

```sh
> which-cloud 104.196.27.39
Expand Down
42 changes: 42 additions & 0 deletions lib/which.js
@@ -0,0 +1,42 @@
const parallel = require('async').parallel
const ipRangeCheck = require('ip-range-check')
const azure = require('./azure')
const aws = require('./aws')

module.exports = function (ip, done) {
var name = 'unknown'
parallel([
function (cb) {
check(ip, azure(), function (err, _name) {
if (err) return cb(err)
else {
if (_name) name = _name
return cb()
}
})
},
function (cb) {
check(ip, aws(), function (err, _name) {
if (err) return cb(err)
else {
if (_name) name = _name
return cb()
}
})
}
], function (err) {
if (err) return done(err)
else return done(null, name)
})
}

function check (ip, cloud, cb) {
cloud.list(function (err, ranges) {
if (err) return cb(err)
else if (ipRangeCheck(ip, ranges)) {
return cb(null, cloud.name)
} else {
return cb()
}
})
}
4 changes: 2 additions & 2 deletions package.json
@@ -1,12 +1,12 @@
{
"name": "which-cloud",
"version": "1.0.0",
"description": "given an IP address, return which cloud provider it belongs to (EC2, GCE, etc)",
"description": "given an ip address, return which cloud provider it belongs to (AWS, GCE, etc)",
"main": "index.js",
"global": true,
"scripts": {
"pretest": "standard",
"test": "nyc mocha test/*.js",
"test": "nyc mocha --timeout=2500 test/*.js",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"release": "standard-version"
},
Expand Down
17 changes: 11 additions & 6 deletions test/aws.js
@@ -1,13 +1,13 @@
/* global describe, it */
/* global describe, it, after */

require('chai').should()

var AWS = require('../lib/aws')
var nock = require('nock')
const AWS = require('../lib/aws')
const nock = require('nock')

describe('AWS', function () {
it('returns a list of IP ranges for AWS', function (done) {
var aws = AWS()
const aws = AWS()
aws.list(function (err, ranges) {
if (err) return done(err)
ranges.should.include('23.20.0.0/14')
Expand All @@ -16,8 +16,8 @@ describe('AWS', function () {
})

it('handles an upstream error', function (done) {
var aws = AWS()
var getRanges = nock('https://ip-ranges.amazonaws.com')
const aws = AWS()
const getRanges = nock('https://ip-ranges.amazonaws.com')
.get('/ip-ranges.json')
.reply(500)
aws.list(function (err, ranges) {
Expand All @@ -26,4 +26,9 @@ describe('AWS', function () {
return done()
})
})

after(function () {
nock.cleanAll()
nock.enableNetConnect()
})
})
4 changes: 2 additions & 2 deletions test/azure.js
Expand Up @@ -2,11 +2,11 @@

require('chai').should()

var Azure = require('../lib/azure')
const Azure = require('../lib/azure')

describe('Azure', function () {
it('returns a list of IP ranges for Azure', function (done) {
var azure = Azure()
const azure = Azure()
azure.list(function (err, ranges) {
if (err) return done(err)
ranges.should.include('40.112.124.0/24')
Expand Down
31 changes: 31 additions & 0 deletions test/which.js
@@ -0,0 +1,31 @@
/* global describe, it */

require('chai').should()

const which = require('../lib/which')

describe('which', function () {
it('returns appropriate response for azure ip', function (done) {
which('94.245.97.0', function (err, name) {
if (err) return done(err)
name.should.equal('azure')
return done()
})
})

it('returns appropriate response for aws ip', function (done) {
which('54.173.231.160', function (err, name) {
if (err) return done(err)
name.should.equal('aws')
return done()
})
})

it("returns 'unknown' if ip is not recognized", function (done) {
which('9.9.9.9', function (err, name) {
if (err) return done(err)
name.should.equal('unknown')
return done()
})
})
})

0 comments on commit a4b7bf9

Please sign in to comment.