Code coverage report for iam/doesRoleExist.js

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

All files » iam/ » doesRoleExist.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    1 1                     1 1 2 1     1     3           1  
'use strict'
 
var _ = require('lodash')
var Bluebird = require('bluebird')
 
/**
 * Uses IAM.getRole to check the existence of the role. If the response comes
 * back with role data, the promise is resolved with True. If an error
 * occurs the err is inspected to see if it is a 'NoSuchEntity' / 404 error.
 * If it is, this means the role does not exist, and the promise is resolved
 * with False. All other errors result in a rejection.
 *
 * @returns {Promise}
 */
function doesRoleExist (iam, roleName) {
  function inspectError (err) {
    if (err.code === 'NoSuchEntity') {
      return false
    }
 
    return Bluebird.reject(err)
  }
 
  return iam
    .getRolePromised({ RoleName: roleName })
    .then(_.constant(true))
    .error(inspectError)
}
 
module.exports = doesRoleExist