Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: validate entity type for an urn #1958

Merged
merged 1 commit into from
Oct 23, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public String getFlowIdEntity() {
}

public static AzkabanFlowUrn createFromString(String rawUrn) throws URISyntaxException {
validateUrn(rawUrn, ENTITY_TYPE);
String content = new Urn(rawUrn).getContent();
String[] parts = content.substring(1, content.length() - 1).split(",");
return new AzkabanFlowUrn(parts[0], parts[1], parts[2]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public String getJobIdEntity() {
}

public static AzkabanJobUrn createFromString(String rawUrn) throws URISyntaxException {
validateUrn(rawUrn, ENTITY_TYPE);
String content = new Urn(rawUrn).getContent();
String flowParts = content.substring(1, content.lastIndexOf(",") + 1);
String[] parts = content.substring(1, content.length() - 1).split(",");
Expand Down
5 changes: 2 additions & 3 deletions li-utils/src/main/java/com/linkedin/common/urn/ChartUrn.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ public String getChartIdEntity() {
}

public static ChartUrn createFromString(String rawUrn) throws URISyntaxException {
Urn urn = new Urn(rawUrn);
validateUrn(urn, ENTITY_TYPE);
String[] urnParts = urn.getContent().split(",");
validateUrn(rawUrn, ENTITY_TYPE);
String[] urnParts = new Urn(rawUrn).getContent().split(",");
return new ChartUrn(urnParts[0], urnParts[1]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@ public String getGroupNameEntity() {
}

public static CorpGroupUrn createFromString(String rawUrn) throws URISyntaxException {
validateUrn(rawUrn, ENTITY_TYPE);
String groupName = new Urn(rawUrn).getContent();
return new CorpGroupUrn(groupName);
}

public static CorpGroupUrn createFromUrn(Urn urn) throws URISyntaxException {
if (!ENTITY_TYPE.equals(urn.getEntityType())) {
throw new URISyntaxException(urn.toString(), "Can't cast URN to CorpGroupUrn, not same ENTITY");
}
validateUrn(urn, ENTITY_TYPE);

Matcher matcher = URN_PATTERN.matcher(urn.toString());
if (matcher.find()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,13 @@ public String getUsernameEntity() {
}

public static CorpuserUrn createFromString(String rawUrn) throws URISyntaxException {
validateUrn(rawUrn, ENTITY_TYPE);
String username = new Urn(rawUrn).getContent();
return new CorpuserUrn(username);
}

public static CorpuserUrn createFromUrn(Urn urn) throws URISyntaxException {
if (!ENTITY_TYPE.equals(urn.getEntityType())) {
throw new URISyntaxException(urn.toString(), "Can't cast URN to CorpuserUrn, not same ENTITY");
}
validateUrn(urn, ENTITY_TYPE);

Matcher matcher = URN_PATTERN.matcher(urn.toString());
if (matcher.find()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ public String getDashboardIdEntity() {
}

public static DashboardUrn createFromString(String rawUrn) throws URISyntaxException {
Urn urn = new Urn(rawUrn);
validateUrn(urn, ENTITY_TYPE);
String[] urnParts = urn.getContent().split(",");
validateUrn(rawUrn, ENTITY_TYPE);
String[] urnParts = new Urn(rawUrn).getContent().split(",");
return new DashboardUrn(urnParts[0], urnParts[1]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public String getPlatformNameEntity() {
}

public static DataPlatformUrn createFromString(String rawUrn) throws URISyntaxException {
validateUrn(rawUrn, ENTITY_TYPE);
String platformName = new Urn(rawUrn).getContent();
return new DataPlatformUrn(platformName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public FabricType getOriginEntity() {
}

public static DataProcessUrn createFromString(String rawUrn) throws URISyntaxException {
validateUrn(rawUrn, ENTITY_TYPE);
String content = new Urn(rawUrn).getContent();
String[] parts = content.substring(1, content.length() - 1).split(",");
return new DataProcessUrn(parts[0], parts[1], toFabricType(parts[2]));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public FabricType getOriginEntity() {
}

public static DatasetUrn createFromString(String rawUrn) throws URISyntaxException {
validateUrn(rawUrn, ENTITY_TYPE);
String content = new Urn(rawUrn).getContent();
String[] parts = content.substring(1, content.length() - 1).split(",");
return new DatasetUrn(DataPlatformUrn.createFromString(parts[0]), parts[1], toFabricType(parts[2]));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public FabricType getOriginEntity() {
}

public static MLModelUrn createFromString(String rawUrn) throws URISyntaxException {
validateUrn(rawUrn, ENTITY_TYPE);
String content = new Urn(rawUrn).getContent();
String[] parts = content.substring(1, content.length() - 1).split(",");
return new MLModelUrn(DataPlatformUrn.createFromString(parts[0]), parts[1], toFabricType(parts[2]));
Expand Down
6 changes: 6 additions & 0 deletions li-utils/src/main/java/com/linkedin/common/urn/Urn.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ public static Urn deserialize(String rawUrn) throws URISyntaxException {
return createFromString(rawUrn);
}

public static void validateUrn(@Nonnull String rawUrn, @Nonnull String entityType)
throws URISyntaxException {
final Urn urn = new Urn(rawUrn);
validateUrn(urn, entityType);
}

public static void validateUrn(@Nonnull Urn urn, @Nonnull String entityType)
throws URISyntaxException {
if (!entityType.equals(urn.getEntityType())) {
Expand Down