Code coverage report for ec2/doesSecurityGroupExist.js

Statements: 100% (9 / 9)      Branches: 100% (2 / 2)      Functions: 100% (2 / 2)      Lines: 100% (9 / 9)      Ignored: none     

All files » ec2/ » doesSecurityGroupExist.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38    1 1                               1 1 2 1     1     3               1  
'use strict'
 
var _ = require('lodash')
var Bluebird = require('bluebird')
 
/**
 * Uses EC2.describeSecurity groups to check the existence of the security
 * group. If the response comes back successful doesSecurityGroupExist is
 * resolved with True. If an operational error occurs in the underlying ec2
 * client call, the error is inspected to see if it is an
 * "InvalidGroup.NotFound" error. This means the security group is not present,
 * so doesSecurityGroupExist is resolved with false. All other errors are
 * propagated along via a rejection.
 *
 * @param ec2
 * @param securityGroupName
 *
 * @returns {Promise}
 */
function doesSecurityGroupExist (ec2, securityGroupName) {
  function inspectError (err) {
    if (err.code === 'InvalidGroup.NotFound') {
      return false
    }
 
    return Bluebird.reject(err)
  }
 
  return ec2
    .describeSecurityGroupsPromised({
      GroupNames: [ securityGroupName ]
    })
    .then(_.constant(true))
    .error(inspectError)
}
 
module.exports = doesSecurityGroupExist