packages/iam/src/helper.js
function retrieveRoleArn(identifier, context) {
  const iam = new AWS.IAM();
  // First check if the parameter already is an ARN
  if (/arn:aws:iam::\d{12}:role\/?[a-zA-Z_0-9+=,.@\-_\/]+/.test(identifier)) {
    return Promise.resolve(identifier);
  }
  // Then, we check if a role exists with a name "ENVIRONMENT_identifier_stage"
  return iam.getRole({ RoleName: context.environment + '_' + identifier + '_' + context.stage }).promise()
  .then((data) => {
    return Promise.resolve(data.Role.Arn);
  })
  .catch(e => {
    // If it failed, we check if a role exists with a name "ENVIRONMENT_identifier"
    return iam.getRole({ RoleName: context.environment + '_' + identifier }).promise()
    .then((data) => {
      return Promise.resolve(data.Role.Arn);
    })
    .catch(e => {
      // If it failed again, we check if a role exists with a name "identifier"
      return iam.getRole({ RoleName: identifier }).promise()
      .then((data) => {
        return Promise.resolve(data.Role.Arn);
      })
      .catch(e => {
        const plugin = require('./index');
        return plugin.findRoles([identifier])
        .then(roles => {
          if (roles.length === 1) {
            return roles[0].deploy(context)
            .then(report => {
              return Promise.resolve(report.arn);
            });
          }
          return Promise.reject(new Error('Could not find role ' + identifier));
        });
      });
    });
  });
}
function retrieveRoleArn(identifier, context) {
  const iam = new AWS.IAM();
  // First check if the parameter already is an ARN
  if (/arn:aws:iam::\d{12}:role\/?[a-zA-Z_0-9+=,.@\-_\/]+/.test(identifier)) {
    return Promise.resolve(identifier);
  }
  // Then, we check if a role exists with a name "ENVIRONMENT_identifier_stage"
  return iam.getRole({ RoleName: context.environment + '_' + identifier + '_' + context.stage }).promise()
  .then((data) => {
    return Promise.resolve(data.Role.Arn);
  })
  .catch(e => {
    // If it failed, we check if a role exists with a name "ENVIRONMENT_identifier"
    return iam.getRole({ RoleName: context.environment + '_' + identifier }).promise()
    .then((data) => {
      return Promise.resolve(data.Role.Arn);
    })
    .catch(e => {
      // If it failed again, we check if a role exists with a name "identifier"
      return iam.getRole({ RoleName: identifier }).promise()
      .then((data) => {
        return Promise.resolve(data.Role.Arn);
      })
      .catch(e => {
        const plugin = require('./index');
        return plugin.findRoles([identifier])
        .then(roles => {
          if (roles.length === 1) {
            return roles[0].deploy(context)
            .then(report => {
              return Promise.resolve(report.arn);
            });
          }
          return Promise.reject(new Error('Could not find role ' + identifier));
        });
      });
    });
  });
}
function retrieveRoleArn ( identifier , context ) { const iam = new AWS . IAM ( ) ; // First check if the parameter already is an ARN if ( / arn:aws:iam::\d{12}:role\/?[a-zA-Z_0-9+=,.@\-_\/]+ / . test ( identifier ) ) { return Promise . resolve ( identifier ) ; } // Then, we check if a role exists with a name "ENVIRONMENT_identifier_stage" return iam . getRole ( { RoleName : context . environment + '_' + identifier + '_' + context . stage } ) . promise ( ) . then ( ( data ) => { return Promise . resolve ( data . Role . Arn ) ; } ) . catch ( e => { // If it failed, we check if a role exists with a name "ENVIRONMENT_identifier" return iam . getRole ( { RoleName : context . environment + '_' + identifier } ) . promise ( ) . then ( ( data ) => { return Promise . resolve ( data . Role . Arn ) ; } ) . catch ( e => { // If it failed again, we check if a role exists with a name "identifier" return iam . getRole ( { RoleName : identifier } ) . promise ( ) . then ( ( data ) => { return Promise . resolve ( data . Role . Arn ) ; } ) . catch ( e => { const plugin = require ( './index' ) ; return plugin . findRoles ( [ identifier ] ) . then ( roles => { if ( roles . length === 1 ) { return roles [ 0 ] . deploy ( context ) . then ( report => { return Promise . resolve ( report . arn ) ; } ) ; } return Promise . reject ( new Error ( 'Could not find role ' + identifier ) ) ; } ) ; } ) ; } ) ; } ) ; }
Takes a role ARN, or a role name as parameter, requests AWS,
and retruns the ARN of a role matching the ARN or the role name or the role name with context informations
@param  {string} identifier - a role ARN, or a role name
@param  {Object} context - an object containing the stage and the environment
@return {string} - an AWS role ARN
Takes a role ARN or a role name as parameter requests AWS and retruns the ARN of a role matching the ARN or the role name or the role name with context informations