@Test public void executeTest() throws Exception { mockStatic(PacmanUtils.class); when(PacmanUtils.getEnvironmentVariable(anyString())).thenReturn("env"); when(PacmanUtils.getHeader(anyString())).thenReturn(CommonTestUtils.getMapString("id")); when(PacmanUtils.getConfigurationsFromConfigApi(anyString(), anyObject())).thenReturn(CommonTestUtils.getJsonObject()); mockStatic(ConfigUtils.class); when(ConfigUtils.getPropValue(anyString())).thenReturn("tag"); when(PacmanUtils.doesAllHaveValue(anyString(),anyString(),anyString(),anyString(),anyString())).thenReturn( true); when(PacmanUtils.splitStringToAList(anyString(),anyString())).thenReturn(CommonTestUtils.getListString()); assertThat(taggingRule.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 ")), is(notNullValue())); when(PacmanUtils.getMissingTagsfromResourceAttribute(anyObject(),anyObject())).thenReturn(CommonTestUtils.getSetString("123")); assertThat(taggingRule.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 ")), is(notNullValue())); when(PacmanUtils.createAnnotaion(anyObject(),anyString(),anyObject(),anyString(),anyString(),anyString())).thenReturn(CommonTestUtils.getAnnotation("123")); assertThat(taggingRule.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 ")), is(notNullValue())); assertThat(taggingRule.execute(CommonTestUtils.getOneMoreMapString("r_123 "),CommonTestUtils.getMapString("r_123 ")), is(notNullValue())); when(PacmanUtils.doesAllHaveValue(anyString(),anyString(),anyString(),anyString(),anyString())).thenReturn( false); assertThatThrownBy( () -> taggingRule.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("========TaggingRule started========="); Set<String> missingTags = new HashSet<>(); String mandatoryTags = ConfigUtils.getPropValue(PacmanSdkConstants.TAGGING_MANDATORY_TAGS); String tagsSplitter = ruleParam.get(PacmanSdkConstants.SPLITTER_CHAR); String entityId = ruleParam.get(PacmanSdkConstants.RESOURCE_ID); String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); String targetType = ruleParam.get(PacmanRuleConstants.TARGET_TYPE); MDC.put("executionId", ruleParam.get("executionId")); MDC.put("ruleId", ruleParam.get(PacmanSdkConstants.RULE_ID)); if (!PacmanUtils.doesAllHaveValue(mandatoryTags,tagsSplitter,severity,category,targetType)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } String missingTagsStr = null; List<String> mandatoryTagsList = PacmanUtils.splitStringToAList(mandatoryTags, tagsSplitter); if (resourceAttributes!=null) { if(targetType.equalsIgnoreCase(PacmanRuleConstants.TARGET_TYPE_EC2)){ if(resourceAttributes.get(PacmanRuleConstants.STATE_NAME).equalsIgnoreCase(PacmanRuleConstants.RUNNING_STATE)||resourceAttributes.get(PacmanRuleConstants.STATE_NAME).equalsIgnoreCase(PacmanRuleConstants.STOPPED_STATE)){ missingTags = PacmanUtils.getMissingTagsfromResourceAttribute(mandatoryTagsList,resourceAttributes); } }else{ missingTags = PacmanUtils.getMissingTagsfromResourceAttribute(mandatoryTagsList,resourceAttributes); } } missingTagsStr = Joiner.on(", ").join(missingTags); if (!missingTags.isEmpty()) { String description = "Missed tags for "+targetType+" are "+ missingTagsStr; return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE, PacmanUtils.createAnnotaion(ruleParam, missingTagsStr, mandatoryTagsList, description,severity,category,targetType)); } else { logger.info(targetType ," ", entityId , " has all manadatory tags"); } logger.debug("========TaggingRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); }
TaggingRule extends BaseRule { public RuleResult execute(final Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========TaggingRule started========="); Set<String> missingTags = new HashSet<>(); String mandatoryTags = ConfigUtils.getPropValue(PacmanSdkConstants.TAGGING_MANDATORY_TAGS); String tagsSplitter = ruleParam.get(PacmanSdkConstants.SPLITTER_CHAR); String entityId = ruleParam.get(PacmanSdkConstants.RESOURCE_ID); String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); String targetType = ruleParam.get(PacmanRuleConstants.TARGET_TYPE); MDC.put("executionId", ruleParam.get("executionId")); MDC.put("ruleId", ruleParam.get(PacmanSdkConstants.RULE_ID)); if (!PacmanUtils.doesAllHaveValue(mandatoryTags,tagsSplitter,severity,category,targetType)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } String missingTagsStr = null; List<String> mandatoryTagsList = PacmanUtils.splitStringToAList(mandatoryTags, tagsSplitter); if (resourceAttributes!=null) { if(targetType.equalsIgnoreCase(PacmanRuleConstants.TARGET_TYPE_EC2)){ if(resourceAttributes.get(PacmanRuleConstants.STATE_NAME).equalsIgnoreCase(PacmanRuleConstants.RUNNING_STATE)||resourceAttributes.get(PacmanRuleConstants.STATE_NAME).equalsIgnoreCase(PacmanRuleConstants.STOPPED_STATE)){ missingTags = PacmanUtils.getMissingTagsfromResourceAttribute(mandatoryTagsList,resourceAttributes); } }else{ missingTags = PacmanUtils.getMissingTagsfromResourceAttribute(mandatoryTagsList,resourceAttributes); } } missingTagsStr = Joiner.on(", ").join(missingTags); if (!missingTags.isEmpty()) { String description = "Missed tags for "+targetType+" are "+ missingTagsStr; return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE, PacmanUtils.createAnnotaion(ruleParam, missingTagsStr, mandatoryTagsList, description,severity,category,targetType)); } else { logger.info(targetType ," ", entityId , " has all manadatory tags"); } logger.debug("========TaggingRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); } }
TaggingRule extends BaseRule { public RuleResult execute(final Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========TaggingRule started========="); Set<String> missingTags = new HashSet<>(); String mandatoryTags = ConfigUtils.getPropValue(PacmanSdkConstants.TAGGING_MANDATORY_TAGS); String tagsSplitter = ruleParam.get(PacmanSdkConstants.SPLITTER_CHAR); String entityId = ruleParam.get(PacmanSdkConstants.RESOURCE_ID); String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); String targetType = ruleParam.get(PacmanRuleConstants.TARGET_TYPE); MDC.put("executionId", ruleParam.get("executionId")); MDC.put("ruleId", ruleParam.get(PacmanSdkConstants.RULE_ID)); if (!PacmanUtils.doesAllHaveValue(mandatoryTags,tagsSplitter,severity,category,targetType)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } String missingTagsStr = null; List<String> mandatoryTagsList = PacmanUtils.splitStringToAList(mandatoryTags, tagsSplitter); if (resourceAttributes!=null) { if(targetType.equalsIgnoreCase(PacmanRuleConstants.TARGET_TYPE_EC2)){ if(resourceAttributes.get(PacmanRuleConstants.STATE_NAME).equalsIgnoreCase(PacmanRuleConstants.RUNNING_STATE)||resourceAttributes.get(PacmanRuleConstants.STATE_NAME).equalsIgnoreCase(PacmanRuleConstants.STOPPED_STATE)){ missingTags = PacmanUtils.getMissingTagsfromResourceAttribute(mandatoryTagsList,resourceAttributes); } }else{ missingTags = PacmanUtils.getMissingTagsfromResourceAttribute(mandatoryTagsList,resourceAttributes); } } missingTagsStr = Joiner.on(", ").join(missingTags); if (!missingTags.isEmpty()) { String description = "Missed tags for "+targetType+" are "+ missingTagsStr; return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE, PacmanUtils.createAnnotaion(ruleParam, missingTagsStr, mandatoryTagsList, description,severity,category,targetType)); } else { logger.info(targetType ," ", entityId , " has all manadatory tags"); } logger.debug("========TaggingRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); }  }
TaggingRule extends BaseRule { public RuleResult execute(final Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========TaggingRule started========="); Set<String> missingTags = new HashSet<>(); String mandatoryTags = ConfigUtils.getPropValue(PacmanSdkConstants.TAGGING_MANDATORY_TAGS); String tagsSplitter = ruleParam.get(PacmanSdkConstants.SPLITTER_CHAR); String entityId = ruleParam.get(PacmanSdkConstants.RESOURCE_ID); String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); String targetType = ruleParam.get(PacmanRuleConstants.TARGET_TYPE); MDC.put("executionId", ruleParam.get("executionId")); MDC.put("ruleId", ruleParam.get(PacmanSdkConstants.RULE_ID)); if (!PacmanUtils.doesAllHaveValue(mandatoryTags,tagsSplitter,severity,category,targetType)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } String missingTagsStr = null; List<String> mandatoryTagsList = PacmanUtils.splitStringToAList(mandatoryTags, tagsSplitter); if (resourceAttributes!=null) { if(targetType.equalsIgnoreCase(PacmanRuleConstants.TARGET_TYPE_EC2)){ if(resourceAttributes.get(PacmanRuleConstants.STATE_NAME).equalsIgnoreCase(PacmanRuleConstants.RUNNING_STATE)||resourceAttributes.get(PacmanRuleConstants.STATE_NAME).equalsIgnoreCase(PacmanRuleConstants.STOPPED_STATE)){ missingTags = PacmanUtils.getMissingTagsfromResourceAttribute(mandatoryTagsList,resourceAttributes); } }else{ missingTags = PacmanUtils.getMissingTagsfromResourceAttribute(mandatoryTagsList,resourceAttributes); } } missingTagsStr = Joiner.on(", ").join(missingTags); if (!missingTags.isEmpty()) { String description = "Missed tags for "+targetType+" are "+ missingTagsStr; return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE, PacmanUtils.createAnnotaion(ruleParam, missingTagsStr, mandatoryTagsList, description,severity,category,targetType)); } else { logger.info(targetType ," ", entityId , " has all manadatory tags"); } logger.debug("========TaggingRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); }  RuleResult execute(final Map<String, String> ruleParam, Map<String, String> resourceAttributes); String getHelpText(); }
TaggingRule extends BaseRule { public RuleResult execute(final Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========TaggingRule started========="); Set<String> missingTags = new HashSet<>(); String mandatoryTags = ConfigUtils.getPropValue(PacmanSdkConstants.TAGGING_MANDATORY_TAGS); String tagsSplitter = ruleParam.get(PacmanSdkConstants.SPLITTER_CHAR); String entityId = ruleParam.get(PacmanSdkConstants.RESOURCE_ID); String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); String targetType = ruleParam.get(PacmanRuleConstants.TARGET_TYPE); MDC.put("executionId", ruleParam.get("executionId")); MDC.put("ruleId", ruleParam.get(PacmanSdkConstants.RULE_ID)); if (!PacmanUtils.doesAllHaveValue(mandatoryTags,tagsSplitter,severity,category,targetType)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } String missingTagsStr = null; List<String> mandatoryTagsList = PacmanUtils.splitStringToAList(mandatoryTags, tagsSplitter); if (resourceAttributes!=null) { if(targetType.equalsIgnoreCase(PacmanRuleConstants.TARGET_TYPE_EC2)){ if(resourceAttributes.get(PacmanRuleConstants.STATE_NAME).equalsIgnoreCase(PacmanRuleConstants.RUNNING_STATE)||resourceAttributes.get(PacmanRuleConstants.STATE_NAME).equalsIgnoreCase(PacmanRuleConstants.STOPPED_STATE)){ missingTags = PacmanUtils.getMissingTagsfromResourceAttribute(mandatoryTagsList,resourceAttributes); } }else{ missingTags = PacmanUtils.getMissingTagsfromResourceAttribute(mandatoryTagsList,resourceAttributes); } } missingTagsStr = Joiner.on(", ").join(missingTags); if (!missingTags.isEmpty()) { String description = "Missed tags for "+targetType+" are "+ missingTagsStr; return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE, PacmanUtils.createAnnotaion(ruleParam, missingTagsStr, mandatoryTagsList, description,severity,category,targetType)); } else { logger.info(targetType ," ", entityId , " has all manadatory tags"); } logger.debug("========TaggingRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); }  RuleResult execute(final Map<String, String> ruleParam, Map<String, String> resourceAttributes); String getHelpText();  }