-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a TopicNameRegexValidation to validate topic names
- Loading branch information
1 parent
83afe9a
commit dddf8b2
Showing
2 changed files
with
72 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -112,4 +112,4 @@ private Class<?> getValidationClazz(String validationClass) { | |
return null; | ||
} | ||
} | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
src/main/java/com/purbon/kafka/topology/validation/topology/TopicNameRegexValidation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.purbon.kafka.topology.validation.topology; | ||
|
||
import java.util.regex.Pattern; | ||
import java.util.regex.PatternSyntaxException; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
import com.purbon.kafka.topology.exceptions.ConfigurationException; | ||
import com.purbon.kafka.topology.exceptions.ValidationException; | ||
import com.purbon.kafka.topology.model.Project; | ||
import com.purbon.kafka.topology.model.Topic; | ||
import com.purbon.kafka.topology.model.Topology; | ||
import com.purbon.kafka.topology.validation.TopologyValidation; | ||
import com.typesafe.config.Config; | ||
import com.typesafe.config.ConfigException; | ||
import com.typesafe.config.ConfigFactory; | ||
|
||
public class TopicNameRegexValidation implements TopologyValidation { | ||
|
||
private static final Logger LOGGER = LogManager.getLogger(TopicNameRegexValidation.class); | ||
|
||
private String topicNamePattern; | ||
|
||
public TopicNameRegexValidation() throws ConfigurationException { | ||
Config config = ConfigFactory.load(); | ||
try { | ||
this.topicNamePattern = config.getString("topology.validations.regexp"); | ||
} catch (ConfigException e) { | ||
throw new ConfigurationException( | ||
"TopicNameRegexValidation requires you to define your regex in config 'topology.validations.regexp'"); | ||
} | ||
|
||
if (StringUtils.isBlank(topicNamePattern)) { | ||
throw new ConfigurationException( | ||
"TopicNameRegexValidation is configured without specifying a topic name pattern. Use config 'topology.validations.regexp'"); | ||
} | ||
|
||
try { | ||
Pattern.compile(topicNamePattern); | ||
} catch (PatternSyntaxException exception) { | ||
throw new ConfigurationException( | ||
String.format("TopicNameRegexValidation configured with unvalid regex '%s'", topicNamePattern)); | ||
} | ||
} | ||
|
||
public TopicNameRegexValidation(String pattern) { | ||
this.topicNamePattern = pattern; | ||
LOGGER.info(String.format("Applying Topic Name Regex Validation [%s]", pattern)); | ||
} | ||
|
||
@Override | ||
public void valid(Topology topology) throws ValidationException { | ||
|
||
for (Project project : topology.getProjects()) { | ||
for (Topic topic : project.getTopics()) { | ||
matches(topic.toString()); | ||
} | ||
} | ||
} | ||
|
||
public void matches(String topicName) throws ValidationException { | ||
if (!topicName.matches(topicNamePattern)) { | ||
String msg = | ||
String.format( | ||
"Topic name '%s' does not follow regex: %s", topicName, topicNamePattern); | ||
throw new ValidationException(msg); | ||
} | ||
} | ||
} |