packages/iam/src/helper.js
function retrievePolicyArn(identifier, context, searchParams) {
  if (/arn:aws:iam::\d{12}:policy\/?[a-zA-Z_0-9+=,.@\-_/]+]/.test(identifier)) {
    return Promise.resolve(identifier);
  }

  // @TODO make this code more beautiful
  // Try ENV_PolicyName_stage
  let policyIdentifier = context.environment + '_' + identifier + '_' + context.stage;
  return getPolicyByName(policyIdentifier, searchParams)
  .then(policy => {
    if (!policy) {
      // Try ENV_PolicyName
      policyIdentifier = context.environment + '_' + identifier;
      return getPolicyByName(policyIdentifier, searchParams)
      .then(policy => {
        if (!policy) {
          // Try PolicyName
          return getPolicyByName(identifier, searchParams)
          .then(policy => {
            if (!policy) {
              return findAndDeployPolicy(identifier, context)
              .then(report => {
                if (!report) {
                  throw new Error('The policy ' + identifier + ' could not be found.');
                }
                return { Arn: report.arn };
              });
            }
            return policy;
          });
        }
        return policy;
      });
    }
    return policy;
  })
  .then(policy => {
    return policy.Arn;
  });
}
function retrievePolicyArn(identifier, context, searchParams) {
  if (/arn:aws:iam::\d{12}:policy\/?[a-zA-Z_0-9+=,.@\-_/]+]/.test(identifier)) {
    return Promise.resolve(identifier);
  }

  // @TODO make this code more beautiful
  // Try ENV_PolicyName_stage
  let policyIdentifier = context.environment + '_' + identifier + '_' + context.stage;
  return getPolicyByName(policyIdentifier, searchParams)
  .then(policy => {
    if (!policy) {
      // Try ENV_PolicyName
      policyIdentifier = context.environment + '_' + identifier;
      return getPolicyByName(policyIdentifier, searchParams)
      .then(policy => {
        if (!policy) {
          // Try PolicyName
          return getPolicyByName(identifier, searchParams)
          .then(policy => {
            if (!policy) {
              return findAndDeployPolicy(identifier, context)
              .then(report => {
                if (!report) {
                  throw new Error('The policy ' + identifier + ' could not be found.');
                }
                return { Arn: report.arn };
              });
            }
            return policy;
          });
        }
        return policy;
      });
    }
    return policy;
  })
  .then(policy => {
    return policy.Arn;
  });
}
function retrievePolicyArn ( identifier , context , searchParams ) { if ( / arn:aws:iam::\d{12}:policy\/?[a-zA-Z_0-9+=,.@\-_/]+] / . test ( identifier ) ) { return Promise . resolve ( identifier ) ; } // @TODO make this code more beautiful // Try ENV_PolicyName_stage let policyIdentifier = context . environment + '_' + identifier + '_' + context . stage ; return getPolicyByName ( policyIdentifier , searchParams ) . then ( policy => { if ( ! policy ) { // Try ENV_PolicyName policyIdentifier = context . environment + '_' + identifier ; return getPolicyByName ( policyIdentifier , searchParams ) . then ( policy => { if ( ! policy ) { // Try PolicyName return getPolicyByName ( identifier , searchParams ) . then ( policy => { if ( ! policy ) { return findAndDeployPolicy ( identifier , context ) . then ( report => { if ( ! report ) { throw new Error ( 'The policy ' + identifier + ' could not be found.' ) ; } return { Arn : report . arn } ; } ) ; } return policy ; } ) ; } return policy ; } ) ; } return policy ; } ) . then ( policy => { return policy . Arn ; } ) ; }
Retrieve a policy Arn from a identifier that can be either the ARN or the name
@param {String} identifier
@param {[type]} context
@param {[type]} searchParams
@returns {[type]}
Retrieve a policy Arn from a identifier that can be either the ARN or the name