@Test public void executeTest() throws Exception { mockStatic(PacmanUtils.class); when(PacmanUtils.doesAllHaveValue(anyString(),anyString(),anyString(), anyString())).thenReturn( true); when(PacmanUtils.formatUrl(anyObject(),anyString())).thenReturn("host"); when(PacmanUtils.getEBSVolumeWithCheckId(anyString(),anyString(),anyString(),anyString(),anyString())).thenReturn(CommonTestUtils.getMapString("r_123 ")); assertThat(underutilizedAmazonEBSVolumesRule.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 ")), is(notNullValue())); when(PacmanUtils.getEBSVolumeWithCheckId(anyString(),anyString(),anyString(),anyString(),anyString())).thenReturn(CommonTestUtils.getEmptyMapString()); assertThat(underutilizedAmazonEBSVolumesRule.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 ")), is(notNullValue())); when(PacmanUtils.getEBSVolumeWithCheckId(anyString(),anyString(),anyString(),anyString(),anyString())).thenThrow(new Exception()); assertThatThrownBy( () -> underutilizedAmazonEBSVolumesRule.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 "))).isInstanceOf(RuleExecutionFailedExeption.class); when(PacmanUtils.doesAllHaveValue(anyString(),anyString(),anyString(),anyString())).thenReturn( false); assertThatThrownBy( () -> underutilizedAmazonEBSVolumesRule.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 "))).isInstanceOf(InvalidInputException.class); }
@Override public RuleResult execute(Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========UnderutilizedAmazonEBSVolumesRule started========="); Annotation annotation = null; String resourceId = null; String region = null; String accountId = null; String checkId = StringUtils.trim(ruleParam.get(PacmanRuleConstants.CHECK_ID)); String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); String serviceEsURL = null; String formattedUrl = PacmanUtils.formatUrl(ruleParam,PacmanRuleConstants.ES_CHECK_SERVICE_SEARCH_URL_PARAM); if(!StringUtils.isNullOrEmpty(formattedUrl)){ serviceEsURL = formattedUrl; } String desc=null; 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(checkId,severity,category,serviceEsURL)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } if (resourceAttributes != null) { region = resourceAttributes.get(PacmanRuleConstants.REGION); accountId = resourceAttributes.get(PacmanSdkConstants.ACCOUNT_ID); resourceId = StringUtils.trim(resourceAttributes.get("_resourceid")); Map<String, String> infoFromTA =null; try { infoFromTA = PacmanUtils.getEBSVolumeWithCheckId(checkId,resourceId,serviceEsURL,region,accountId); } catch (Exception e) { logger.error("unable to determine",e); throw new RuleExecutionFailedExeption("unable to determine"+e); } if (!infoFromTA.isEmpty()) { desc ="Amazon EBS Volume "+resourceId+" Underutilized"; annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,desc); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, desc); issue.put(PacmanRuleConstants.SOURCE_VERIFIED, "Trusted Advisor"); issue.put("info_from_trusted_advisor", new Gson().toJson(infoFromTA)); issueList.add(issue); annotation.put("issueDetails",issueList.toString()); logger.debug("========UnderutilizedAmazonEBSVolumesRule ended with annotation {} :=========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } logger.debug("========UnderutilizedAmazonEBSVolumesRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); }
UnderutilizedAmazonEBSVolumesRule extends BaseRule { @Override public RuleResult execute(Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========UnderutilizedAmazonEBSVolumesRule started========="); Annotation annotation = null; String resourceId = null; String region = null; String accountId = null; String checkId = StringUtils.trim(ruleParam.get(PacmanRuleConstants.CHECK_ID)); String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); String serviceEsURL = null; String formattedUrl = PacmanUtils.formatUrl(ruleParam,PacmanRuleConstants.ES_CHECK_SERVICE_SEARCH_URL_PARAM); if(!StringUtils.isNullOrEmpty(formattedUrl)){ serviceEsURL = formattedUrl; } String desc=null; 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(checkId,severity,category,serviceEsURL)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } if (resourceAttributes != null) { region = resourceAttributes.get(PacmanRuleConstants.REGION); accountId = resourceAttributes.get(PacmanSdkConstants.ACCOUNT_ID); resourceId = StringUtils.trim(resourceAttributes.get("_resourceid")); Map<String, String> infoFromTA =null; try { infoFromTA = PacmanUtils.getEBSVolumeWithCheckId(checkId,resourceId,serviceEsURL,region,accountId); } catch (Exception e) { logger.error("unable to determine",e); throw new RuleExecutionFailedExeption("unable to determine"+e); } if (!infoFromTA.isEmpty()) { desc ="Amazon EBS Volume "+resourceId+" Underutilized"; annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,desc); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, desc); issue.put(PacmanRuleConstants.SOURCE_VERIFIED, "Trusted Advisor"); issue.put("info_from_trusted_advisor", new Gson().toJson(infoFromTA)); issueList.add(issue); annotation.put("issueDetails",issueList.toString()); logger.debug("========UnderutilizedAmazonEBSVolumesRule ended with annotation {} :=========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } logger.debug("========UnderutilizedAmazonEBSVolumesRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); } }
UnderutilizedAmazonEBSVolumesRule extends BaseRule { @Override public RuleResult execute(Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========UnderutilizedAmazonEBSVolumesRule started========="); Annotation annotation = null; String resourceId = null; String region = null; String accountId = null; String checkId = StringUtils.trim(ruleParam.get(PacmanRuleConstants.CHECK_ID)); String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); String serviceEsURL = null; String formattedUrl = PacmanUtils.formatUrl(ruleParam,PacmanRuleConstants.ES_CHECK_SERVICE_SEARCH_URL_PARAM); if(!StringUtils.isNullOrEmpty(formattedUrl)){ serviceEsURL = formattedUrl; } String desc=null; 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(checkId,severity,category,serviceEsURL)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } if (resourceAttributes != null) { region = resourceAttributes.get(PacmanRuleConstants.REGION); accountId = resourceAttributes.get(PacmanSdkConstants.ACCOUNT_ID); resourceId = StringUtils.trim(resourceAttributes.get("_resourceid")); Map<String, String> infoFromTA =null; try { infoFromTA = PacmanUtils.getEBSVolumeWithCheckId(checkId,resourceId,serviceEsURL,region,accountId); } catch (Exception e) { logger.error("unable to determine",e); throw new RuleExecutionFailedExeption("unable to determine"+e); } if (!infoFromTA.isEmpty()) { desc ="Amazon EBS Volume "+resourceId+" Underutilized"; annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,desc); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, desc); issue.put(PacmanRuleConstants.SOURCE_VERIFIED, "Trusted Advisor"); issue.put("info_from_trusted_advisor", new Gson().toJson(infoFromTA)); issueList.add(issue); annotation.put("issueDetails",issueList.toString()); logger.debug("========UnderutilizedAmazonEBSVolumesRule ended with annotation {} :=========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } logger.debug("========UnderutilizedAmazonEBSVolumesRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); }  }
UnderutilizedAmazonEBSVolumesRule extends BaseRule { @Override public RuleResult execute(Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========UnderutilizedAmazonEBSVolumesRule started========="); Annotation annotation = null; String resourceId = null; String region = null; String accountId = null; String checkId = StringUtils.trim(ruleParam.get(PacmanRuleConstants.CHECK_ID)); String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); String serviceEsURL = null; String formattedUrl = PacmanUtils.formatUrl(ruleParam,PacmanRuleConstants.ES_CHECK_SERVICE_SEARCH_URL_PARAM); if(!StringUtils.isNullOrEmpty(formattedUrl)){ serviceEsURL = formattedUrl; } String desc=null; 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(checkId,severity,category,serviceEsURL)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } if (resourceAttributes != null) { region = resourceAttributes.get(PacmanRuleConstants.REGION); accountId = resourceAttributes.get(PacmanSdkConstants.ACCOUNT_ID); resourceId = StringUtils.trim(resourceAttributes.get("_resourceid")); Map<String, String> infoFromTA =null; try { infoFromTA = PacmanUtils.getEBSVolumeWithCheckId(checkId,resourceId,serviceEsURL,region,accountId); } catch (Exception e) { logger.error("unable to determine",e); throw new RuleExecutionFailedExeption("unable to determine"+e); } if (!infoFromTA.isEmpty()) { desc ="Amazon EBS Volume "+resourceId+" Underutilized"; annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,desc); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, desc); issue.put(PacmanRuleConstants.SOURCE_VERIFIED, "Trusted Advisor"); issue.put("info_from_trusted_advisor", new Gson().toJson(infoFromTA)); issueList.add(issue); annotation.put("issueDetails",issueList.toString()); logger.debug("========UnderutilizedAmazonEBSVolumesRule ended with annotation {} :=========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } logger.debug("========UnderutilizedAmazonEBSVolumesRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); }  @Override RuleResult execute(Map<String, String> ruleParam, Map<String, String> resourceAttributes); @Override String getHelpText(); }
UnderutilizedAmazonEBSVolumesRule extends BaseRule { @Override public RuleResult execute(Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========UnderutilizedAmazonEBSVolumesRule started========="); Annotation annotation = null; String resourceId = null; String region = null; String accountId = null; String checkId = StringUtils.trim(ruleParam.get(PacmanRuleConstants.CHECK_ID)); String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); String serviceEsURL = null; String formattedUrl = PacmanUtils.formatUrl(ruleParam,PacmanRuleConstants.ES_CHECK_SERVICE_SEARCH_URL_PARAM); if(!StringUtils.isNullOrEmpty(formattedUrl)){ serviceEsURL = formattedUrl; } String desc=null; 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(checkId,severity,category,serviceEsURL)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } if (resourceAttributes != null) { region = resourceAttributes.get(PacmanRuleConstants.REGION); accountId = resourceAttributes.get(PacmanSdkConstants.ACCOUNT_ID); resourceId = StringUtils.trim(resourceAttributes.get("_resourceid")); Map<String, String> infoFromTA =null; try { infoFromTA = PacmanUtils.getEBSVolumeWithCheckId(checkId,resourceId,serviceEsURL,region,accountId); } catch (Exception e) { logger.error("unable to determine",e); throw new RuleExecutionFailedExeption("unable to determine"+e); } if (!infoFromTA.isEmpty()) { desc ="Amazon EBS Volume "+resourceId+" Underutilized"; annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,desc); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, desc); issue.put(PacmanRuleConstants.SOURCE_VERIFIED, "Trusted Advisor"); issue.put("info_from_trusted_advisor", new Gson().toJson(infoFromTA)); issueList.add(issue); annotation.put("issueDetails",issueList.toString()); logger.debug("========UnderutilizedAmazonEBSVolumesRule ended with annotation {} :=========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } logger.debug("========UnderutilizedAmazonEBSVolumesRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); }  @Override RuleResult execute(Map<String, String> ruleParam, Map<String, String> resourceAttributes); @Override String getHelpText();  }