Friday 28 April 2017

Codeblock works, the return doesn't

Not even sure how to phrase this exactly.

I have the two functions you can see below. checkDescription works and returns as expected and then rejects the promise. The second block "checkAwsVsSgs" also executes as expected, but never rejects the promise or even executes the test console.log section.

// Library Module, Public functions
module.exports = {


  checkSG: function(block) { 
    return new Promise((resolve, reject) => {
      let found = false;

      Object.keys(awsSecurityGroups).forEach( (key)=> {
        let group = awsSecurityGroups[key];
        if (block.local_action.name === group.GroupName) {
          found = true;

          if (!checkDescription(group, block)) { reject("Description doesn't match.") }
          if (!checkAwsVsSgs(group, block)) { console.log("!!!!! I'm never called"); reject("Found rules in aws that are not in sgs") }

        }
      });

      resolve(found);
    });
  },

};

// Private functions
function checkDescription(group, block) {
  return group.Description === block.local_action.description
}

function checkAwsVsSgs(group, block) { // Look for rules that are in AWS but not sgs
  group.IpPermissions.forEach( (awsRule) => {
    let foundRuleMatch = false;
    block.local_action.rules.forEach ( (blockRule) => {
      //console.log("Checking FromPort %s vs %s and ToPort %s vs %s for %s", awsRule.FromPort, blockRule.from_port, awsRule.ToPort, blockRule.to_port, group.GroupName)
      if (awsRule.ToPort === blockRule.to_port && awsRule.FromPort === blockRule.from_port) { foundRuleMatch = true }
    });
    if (!foundRuleMatch) {
      console.log("Error: Could not find matching rule for %s FromPort: %s ToPort: %s", group.GroupName, awsRule.FromPort, awsRule.ToPort);
      return false;
    }
  });
  return true;
}



via The Digital Ninja

No comments:

Post a Comment