@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.getIdleLoadBalancerDetails(anyString(),anyString(),anyString(),anyString(),anyString())).thenReturn(CommonTestUtils.getMapString("r_123 ")); assertThat(checkForIdleLoadBalancersRule.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 ")), is(notNullValue())); when(PacmanUtils.getIdleLoadBalancerDetails(anyString(),anyString(),anyString(),anyString(),anyString())).thenReturn(CommonTestUtils.getEmptyMapString()); assertThat(checkForIdleLoadBalancersRule.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 ")), is(notNullValue())); when(PacmanUtils.getIdleLoadBalancerDetails(anyString(),anyString(),anyString(),anyString(),anyString())).thenThrow(new Exception()); assertThatThrownBy( () -> checkForIdleLoadBalancersRule.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 "))).isInstanceOf(RuleExecutionFailedExeption.class); when(PacmanUtils.doesAllHaveValue(anyString(),anyString(),anyString(),anyString())).thenReturn( false); assertThatThrownBy( () -> checkForIdleLoadBalancersRule.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("========CheckForIdleLoadBalancersRule started========="); Annotation annotation = null; String loadBalancerId = 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; } 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); loadBalancerId = StringUtils.trim(resourceAttributes.get(PacmanRuleConstants.LOAD_BALANCER_ID_ATTRIBUTE)); Map<String, String> idleLoadBalancerMap = new HashMap<>(); try { idleLoadBalancerMap = PacmanUtils.getIdleLoadBalancerDetails(checkId,loadBalancerId,serviceEsURL,region,accountId); } catch (Exception e) { logger.error("unable to determine",e); throw new RuleExecutionFailedExeption("unable to determine"+e); } if (!idleLoadBalancerMap.isEmpty()) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,"Idle load balancer found !"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); annotation.put(PacmanRuleConstants.EST_MONTHLY_SAVINGS, idleLoadBalancerMap.get(PacmanRuleConstants.EST_MONTHLY_SAVINGS)); annotation.put(PacmanRuleConstants.REASON, idleLoadBalancerMap.get(PacmanRuleConstants.REASON)); issue.put(PacmanRuleConstants.VIOLATION_REASON, "Idle load balancer found!!"); issue.put(PacmanRuleConstants.CHECKID, checkId); issue.put(PacmanRuleConstants.SOURCE_VERIFIED, "trusted advisor"); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); logger.debug("========CheckForIdleLoadBalancersRule ended with annotaion {} :=========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } logger.debug("========CheckForIdleLoadBalancersRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); }
CheckForIdleLoadBalancersRule extends BaseRule { @Override public RuleResult execute(Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========CheckForIdleLoadBalancersRule started========="); Annotation annotation = null; String loadBalancerId = 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; } 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); loadBalancerId = StringUtils.trim(resourceAttributes.get(PacmanRuleConstants.LOAD_BALANCER_ID_ATTRIBUTE)); Map<String, String> idleLoadBalancerMap = new HashMap<>(); try { idleLoadBalancerMap = PacmanUtils.getIdleLoadBalancerDetails(checkId,loadBalancerId,serviceEsURL,region,accountId); } catch (Exception e) { logger.error("unable to determine",e); throw new RuleExecutionFailedExeption("unable to determine"+e); } if (!idleLoadBalancerMap.isEmpty()) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,"Idle load balancer found !"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); annotation.put(PacmanRuleConstants.EST_MONTHLY_SAVINGS, idleLoadBalancerMap.get(PacmanRuleConstants.EST_MONTHLY_SAVINGS)); annotation.put(PacmanRuleConstants.REASON, idleLoadBalancerMap.get(PacmanRuleConstants.REASON)); issue.put(PacmanRuleConstants.VIOLATION_REASON, "Idle load balancer found!!"); issue.put(PacmanRuleConstants.CHECKID, checkId); issue.put(PacmanRuleConstants.SOURCE_VERIFIED, "trusted advisor"); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); logger.debug("========CheckForIdleLoadBalancersRule ended with annotaion {} :=========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } logger.debug("========CheckForIdleLoadBalancersRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); } }
CheckForIdleLoadBalancersRule extends BaseRule { @Override public RuleResult execute(Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========CheckForIdleLoadBalancersRule started========="); Annotation annotation = null; String loadBalancerId = 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; } 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); loadBalancerId = StringUtils.trim(resourceAttributes.get(PacmanRuleConstants.LOAD_BALANCER_ID_ATTRIBUTE)); Map<String, String> idleLoadBalancerMap = new HashMap<>(); try { idleLoadBalancerMap = PacmanUtils.getIdleLoadBalancerDetails(checkId,loadBalancerId,serviceEsURL,region,accountId); } catch (Exception e) { logger.error("unable to determine",e); throw new RuleExecutionFailedExeption("unable to determine"+e); } if (!idleLoadBalancerMap.isEmpty()) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,"Idle load balancer found !"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); annotation.put(PacmanRuleConstants.EST_MONTHLY_SAVINGS, idleLoadBalancerMap.get(PacmanRuleConstants.EST_MONTHLY_SAVINGS)); annotation.put(PacmanRuleConstants.REASON, idleLoadBalancerMap.get(PacmanRuleConstants.REASON)); issue.put(PacmanRuleConstants.VIOLATION_REASON, "Idle load balancer found!!"); issue.put(PacmanRuleConstants.CHECKID, checkId); issue.put(PacmanRuleConstants.SOURCE_VERIFIED, "trusted advisor"); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); logger.debug("========CheckForIdleLoadBalancersRule ended with annotaion {} :=========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } logger.debug("========CheckForIdleLoadBalancersRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); }  }
CheckForIdleLoadBalancersRule extends BaseRule { @Override public RuleResult execute(Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========CheckForIdleLoadBalancersRule started========="); Annotation annotation = null; String loadBalancerId = 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; } 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); loadBalancerId = StringUtils.trim(resourceAttributes.get(PacmanRuleConstants.LOAD_BALANCER_ID_ATTRIBUTE)); Map<String, String> idleLoadBalancerMap = new HashMap<>(); try { idleLoadBalancerMap = PacmanUtils.getIdleLoadBalancerDetails(checkId,loadBalancerId,serviceEsURL,region,accountId); } catch (Exception e) { logger.error("unable to determine",e); throw new RuleExecutionFailedExeption("unable to determine"+e); } if (!idleLoadBalancerMap.isEmpty()) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,"Idle load balancer found !"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); annotation.put(PacmanRuleConstants.EST_MONTHLY_SAVINGS, idleLoadBalancerMap.get(PacmanRuleConstants.EST_MONTHLY_SAVINGS)); annotation.put(PacmanRuleConstants.REASON, idleLoadBalancerMap.get(PacmanRuleConstants.REASON)); issue.put(PacmanRuleConstants.VIOLATION_REASON, "Idle load balancer found!!"); issue.put(PacmanRuleConstants.CHECKID, checkId); issue.put(PacmanRuleConstants.SOURCE_VERIFIED, "trusted advisor"); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); logger.debug("========CheckForIdleLoadBalancersRule ended with annotaion {} :=========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } logger.debug("========CheckForIdleLoadBalancersRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); }  @Override RuleResult execute(Map<String, String> ruleParam, Map<String, String> resourceAttributes); @Override String getHelpText(); }
CheckForIdleLoadBalancersRule extends BaseRule { @Override public RuleResult execute(Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========CheckForIdleLoadBalancersRule started========="); Annotation annotation = null; String loadBalancerId = 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; } 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); loadBalancerId = StringUtils.trim(resourceAttributes.get(PacmanRuleConstants.LOAD_BALANCER_ID_ATTRIBUTE)); Map<String, String> idleLoadBalancerMap = new HashMap<>(); try { idleLoadBalancerMap = PacmanUtils.getIdleLoadBalancerDetails(checkId,loadBalancerId,serviceEsURL,region,accountId); } catch (Exception e) { logger.error("unable to determine",e); throw new RuleExecutionFailedExeption("unable to determine"+e); } if (!idleLoadBalancerMap.isEmpty()) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,"Idle load balancer found !"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); annotation.put(PacmanRuleConstants.EST_MONTHLY_SAVINGS, idleLoadBalancerMap.get(PacmanRuleConstants.EST_MONTHLY_SAVINGS)); annotation.put(PacmanRuleConstants.REASON, idleLoadBalancerMap.get(PacmanRuleConstants.REASON)); issue.put(PacmanRuleConstants.VIOLATION_REASON, "Idle load balancer found!!"); issue.put(PacmanRuleConstants.CHECKID, checkId); issue.put(PacmanRuleConstants.SOURCE_VERIFIED, "trusted advisor"); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); logger.debug("========CheckForIdleLoadBalancersRule ended with annotaion {} :=========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } logger.debug("========CheckForIdleLoadBalancersRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); }  @Override RuleResult execute(Map<String, String> ruleParam, Map<String, String> resourceAttributes); @Override String getHelpText();  }