Code coverage report for iam/doesInstanceProfileExist.js

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

All files » iam/ » doesInstanceProfileExist.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    1 1                       1 1 2 1     1     3           1  
'use strict'
 
var _ = require('lodash')
var Bluebird = require('bluebird')
 
/**
 * Uses IAM.getInstanceProfile to check the existence of the instanceProfile.
 * If the response comes back with instanceProfile 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 instanceProfile does
 * not exist, and the promise is resolved with False. All other errors result
 * in a rejection.
 *
 * @returns {Promise}
 */
function doesInstanceProfileExist (iam, instanceProfileName) {
  function inspectError (err) {
    if (err.code === 'NoSuchEntity') {
      return false
    }
 
    return Bluebird.reject(err)
  }
 
  return iam
    .getInstanceProfilePromised({ InstanceProfileName: instanceProfileName })
    .then(_.constant(true))
    .error(inspectError)
}
 
module.exports = doesInstanceProfileExist