@Test public void executeTest() throws Exception { mockStatic(PacmanUtils.class); when(PacmanUtils.doesAllHaveValue(anyString(),anyString(),anyString())).thenReturn( true); when(PacmanUtils.formatUrl(anyObject(),anyString())).thenReturn("host"); when(PacmanUtils.checkResourceIdFromElasticSearch(anyString(),anyString(),anyString(),anyString())).thenReturn(true); assertThat(unusedEBSRule.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 ")), is(notNullValue())); when(PacmanUtils.checkResourceIdFromElasticSearch(anyString(),anyString(),anyString(),anyString())).thenReturn(false); assertThat(unusedEBSRule.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 ")), is(notNullValue())); when(PacmanUtils.checkResourceIdFromElasticSearch(anyString(),anyString(),anyString(),anyString())).thenThrow(new Exception()); assertThatThrownBy( () -> unusedEBSRule.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 "))).isInstanceOf(RuleExecutionFailedExeption.class); when(PacmanUtils.doesAllHaveValue(anyString(),anyString(),anyString())).thenReturn( false); assertThatThrownBy( () -> unusedEBSRule.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 "))).isInstanceOf(InvalidInputException.class); }
public RuleResult execute(final Map<String, String> ruleParam,Map<String, String> resourceAttributes) { logger.debug("========UnusedEBSRule started========="); Annotation annotation = null; String volumeId = null; String region = null; String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); String ebsUrl = null; String formattedUrl = PacmanUtils.formatUrl(ruleParam,PacmanRuleConstants.ES_EBS_WITH_INSTANCE_URL); if(!StringUtils.isNullOrEmpty(formattedUrl)){ ebsUrl = formattedUrl; } MDC.put("executionId", ruleParam.get("executionId")); MDC.put("ruleId", ruleParam.get(PacmanSdkConstants.RULE_ID)); List<LinkedHashMap<String,Object>>issueList = new ArrayList<>(); LinkedHashMap<String,Object>issue = new LinkedHashMap<>(); if (!PacmanUtils.doesAllHaveValue(severity,category,ebsUrl)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } if (resourceAttributes != null) { volumeId = StringUtils.trim(resourceAttributes.get(PacmanRuleConstants.VOLUME_ID)); region = StringUtils.trim(resourceAttributes.get(PacmanRuleConstants.REGION_ATTR)); boolean isEbsWithEc2Exists = false; try{ isEbsWithEc2Exists = PacmanUtils.checkResourceIdFromElasticSearch(volumeId,ebsUrl,PacmanRuleConstants.VOLUME_ID,region); } catch (Exception e) { logger.error("unable to determine",e); throw new RuleExecutionFailedExeption("unable to determine"+e); } if (!isEbsWithEc2Exists) { annotation = Annotation.buildAnnotation(ruleParam,Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,"Unused EBS found!!"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.SUBTYPE, Annotation.Type.RECOMMENDATION.toString()); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, "EBS volume is not attached to an ec2 instance"); issueList.add(issue); annotation.put("issueDetails",issueList.toString()); logger.debug("========UnusedEBSRule ended with annotation {} :=========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } logger.debug("========UnusedEBSRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); }
UnusedEBSRule extends BaseRule { public RuleResult execute(final Map<String, String> ruleParam,Map<String, String> resourceAttributes) { logger.debug("========UnusedEBSRule started========="); Annotation annotation = null; String volumeId = null; String region = null; String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); String ebsUrl = null; String formattedUrl = PacmanUtils.formatUrl(ruleParam,PacmanRuleConstants.ES_EBS_WITH_INSTANCE_URL); if(!StringUtils.isNullOrEmpty(formattedUrl)){ ebsUrl = formattedUrl; } MDC.put("executionId", ruleParam.get("executionId")); MDC.put("ruleId", ruleParam.get(PacmanSdkConstants.RULE_ID)); List<LinkedHashMap<String,Object>>issueList = new ArrayList<>(); LinkedHashMap<String,Object>issue = new LinkedHashMap<>(); if (!PacmanUtils.doesAllHaveValue(severity,category,ebsUrl)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } if (resourceAttributes != null) { volumeId = StringUtils.trim(resourceAttributes.get(PacmanRuleConstants.VOLUME_ID)); region = StringUtils.trim(resourceAttributes.get(PacmanRuleConstants.REGION_ATTR)); boolean isEbsWithEc2Exists = false; try{ isEbsWithEc2Exists = PacmanUtils.checkResourceIdFromElasticSearch(volumeId,ebsUrl,PacmanRuleConstants.VOLUME_ID,region); } catch (Exception e) { logger.error("unable to determine",e); throw new RuleExecutionFailedExeption("unable to determine"+e); } if (!isEbsWithEc2Exists) { annotation = Annotation.buildAnnotation(ruleParam,Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,"Unused EBS found!!"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.SUBTYPE, Annotation.Type.RECOMMENDATION.toString()); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, "EBS volume is not attached to an ec2 instance"); issueList.add(issue); annotation.put("issueDetails",issueList.toString()); logger.debug("========UnusedEBSRule ended with annotation {} :=========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } logger.debug("========UnusedEBSRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); } }
UnusedEBSRule extends BaseRule { public RuleResult execute(final Map<String, String> ruleParam,Map<String, String> resourceAttributes) { logger.debug("========UnusedEBSRule started========="); Annotation annotation = null; String volumeId = null; String region = null; String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); String ebsUrl = null; String formattedUrl = PacmanUtils.formatUrl(ruleParam,PacmanRuleConstants.ES_EBS_WITH_INSTANCE_URL); if(!StringUtils.isNullOrEmpty(formattedUrl)){ ebsUrl = formattedUrl; } MDC.put("executionId", ruleParam.get("executionId")); MDC.put("ruleId", ruleParam.get(PacmanSdkConstants.RULE_ID)); List<LinkedHashMap<String,Object>>issueList = new ArrayList<>(); LinkedHashMap<String,Object>issue = new LinkedHashMap<>(); if (!PacmanUtils.doesAllHaveValue(severity,category,ebsUrl)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } if (resourceAttributes != null) { volumeId = StringUtils.trim(resourceAttributes.get(PacmanRuleConstants.VOLUME_ID)); region = StringUtils.trim(resourceAttributes.get(PacmanRuleConstants.REGION_ATTR)); boolean isEbsWithEc2Exists = false; try{ isEbsWithEc2Exists = PacmanUtils.checkResourceIdFromElasticSearch(volumeId,ebsUrl,PacmanRuleConstants.VOLUME_ID,region); } catch (Exception e) { logger.error("unable to determine",e); throw new RuleExecutionFailedExeption("unable to determine"+e); } if (!isEbsWithEc2Exists) { annotation = Annotation.buildAnnotation(ruleParam,Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,"Unused EBS found!!"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.SUBTYPE, Annotation.Type.RECOMMENDATION.toString()); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, "EBS volume is not attached to an ec2 instance"); issueList.add(issue); annotation.put("issueDetails",issueList.toString()); logger.debug("========UnusedEBSRule ended with annotation {} :=========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } logger.debug("========UnusedEBSRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); }  }
UnusedEBSRule extends BaseRule { public RuleResult execute(final Map<String, String> ruleParam,Map<String, String> resourceAttributes) { logger.debug("========UnusedEBSRule started========="); Annotation annotation = null; String volumeId = null; String region = null; String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); String ebsUrl = null; String formattedUrl = PacmanUtils.formatUrl(ruleParam,PacmanRuleConstants.ES_EBS_WITH_INSTANCE_URL); if(!StringUtils.isNullOrEmpty(formattedUrl)){ ebsUrl = formattedUrl; } MDC.put("executionId", ruleParam.get("executionId")); MDC.put("ruleId", ruleParam.get(PacmanSdkConstants.RULE_ID)); List<LinkedHashMap<String,Object>>issueList = new ArrayList<>(); LinkedHashMap<String,Object>issue = new LinkedHashMap<>(); if (!PacmanUtils.doesAllHaveValue(severity,category,ebsUrl)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } if (resourceAttributes != null) { volumeId = StringUtils.trim(resourceAttributes.get(PacmanRuleConstants.VOLUME_ID)); region = StringUtils.trim(resourceAttributes.get(PacmanRuleConstants.REGION_ATTR)); boolean isEbsWithEc2Exists = false; try{ isEbsWithEc2Exists = PacmanUtils.checkResourceIdFromElasticSearch(volumeId,ebsUrl,PacmanRuleConstants.VOLUME_ID,region); } catch (Exception e) { logger.error("unable to determine",e); throw new RuleExecutionFailedExeption("unable to determine"+e); } if (!isEbsWithEc2Exists) { annotation = Annotation.buildAnnotation(ruleParam,Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,"Unused EBS found!!"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.SUBTYPE, Annotation.Type.RECOMMENDATION.toString()); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, "EBS volume is not attached to an ec2 instance"); issueList.add(issue); annotation.put("issueDetails",issueList.toString()); logger.debug("========UnusedEBSRule ended with annotation {} :=========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } logger.debug("========UnusedEBSRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); }  RuleResult execute(final Map<String, String> ruleParam,Map<String, String> resourceAttributes); String getHelpText(); }
UnusedEBSRule extends BaseRule { public RuleResult execute(final Map<String, String> ruleParam,Map<String, String> resourceAttributes) { logger.debug("========UnusedEBSRule started========="); Annotation annotation = null; String volumeId = null; String region = null; String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); String ebsUrl = null; String formattedUrl = PacmanUtils.formatUrl(ruleParam,PacmanRuleConstants.ES_EBS_WITH_INSTANCE_URL); if(!StringUtils.isNullOrEmpty(formattedUrl)){ ebsUrl = formattedUrl; } MDC.put("executionId", ruleParam.get("executionId")); MDC.put("ruleId", ruleParam.get(PacmanSdkConstants.RULE_ID)); List<LinkedHashMap<String,Object>>issueList = new ArrayList<>(); LinkedHashMap<String,Object>issue = new LinkedHashMap<>(); if (!PacmanUtils.doesAllHaveValue(severity,category,ebsUrl)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } if (resourceAttributes != null) { volumeId = StringUtils.trim(resourceAttributes.get(PacmanRuleConstants.VOLUME_ID)); region = StringUtils.trim(resourceAttributes.get(PacmanRuleConstants.REGION_ATTR)); boolean isEbsWithEc2Exists = false; try{ isEbsWithEc2Exists = PacmanUtils.checkResourceIdFromElasticSearch(volumeId,ebsUrl,PacmanRuleConstants.VOLUME_ID,region); } catch (Exception e) { logger.error("unable to determine",e); throw new RuleExecutionFailedExeption("unable to determine"+e); } if (!isEbsWithEc2Exists) { annotation = Annotation.buildAnnotation(ruleParam,Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,"Unused EBS found!!"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.SUBTYPE, Annotation.Type.RECOMMENDATION.toString()); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, "EBS volume is not attached to an ec2 instance"); issueList.add(issue); annotation.put("issueDetails",issueList.toString()); logger.debug("========UnusedEBSRule ended with annotation {} :=========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } logger.debug("========UnusedEBSRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); }  RuleResult execute(final Map<String, String> ruleParam,Map<String, String> resourceAttributes); String getHelpText();  }