Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/main/java/com/netflix/frigga/Names.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,21 @@ public static Names parseName(String name) {
return new Names(name);
}

/**
* Same as {@code parseName}, but validates the format of name before constructing
* the returned Names, throwing if it's invalid.
*
* @param name the name of an infrastructure resource
* @return bean containing the component parts of the compound name
* @throws IllegalArgumentException if name is not valid
*/
public static Names parseNameOrThrow(String name) {
if (!isValidName(name)) {
throw new IllegalArgumentException(String.format("Invalid name '%s'", name));
}
return new Names(name);
}

private String checkEmpty(String input) {
return (input != null && !input.isEmpty()) ? input : null;
}
Expand Down Expand Up @@ -174,6 +189,14 @@ private <T> T getLabeledVariable(Function<LabeledVariables, T> extractor) {
return result.getResult().map(extractor).orElse(null);
}

private static boolean isValidName(String name) {
if (name == null || name.trim().isEmpty()) {
return false;
}

return PUSH_PATTERN.matcher(name).matches() || NAME_PATTERN.matcher(name).matches();
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
19 changes: 19 additions & 0 deletions src/test/groovy/com/netflix/frigga/NamesSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,25 @@ class NamesSpec extends Specification {
889 == names.sequence
}

def 'should throw for invalid name when calling parseNameOrThrow'() {
when:
Names.parseNameOrThrow('nccp-moviecontrol%27')

then:
thrown(IllegalArgumentException)
}

def 'should not throw for valid name when calling parseNameOrThrow'() {
when:
Names names = Names.parseNameOrThrow('app-stack-detail-v001')

then:
"app" == names.app
"stack" == names.stack
"detail" == names.detail
"v001" == names.push
}

def 'should return empty object for invalid'() {
when:
Names names = Names.parseName('nccp-moviecontrol%27')
Expand Down