Skip to content

Commit

Permalink
feat: implement excludes for namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
yanlibert committed Dec 7, 2023
1 parent f1e7c7f commit 2479bf2
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 25 deletions.
10 changes: 9 additions & 1 deletion api/src/main/java/marquez/MarquezApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import marquez.api.filter.JobRedirectFilter;
import marquez.api.filter.exclusions.ExclusionsConfig;
import marquez.api.filter.exclusions.ExclusionsFilter;
import marquez.cli.DbMigrationCommand;
import marquez.cli.DbRetentionCommand;
import marquez.cli.MetadataCommand;
Expand Down Expand Up @@ -141,7 +143,13 @@ public void run(@NonNull MarquezConfig config, @NonNull Environment env) {
}

// set namespaceFilter
Utils.setNamespaceFilter(config.getNamespaceFilter());
if (config.hasExcludingPatterns()) {
ExclusionsConfig exclusions = config.getExclude();
if (exclusions.namespaces.onRead) {
ExclusionsFilter.setNamespacesReadFilter(exclusions.namespaces.patterns);
}
;
}
}

private boolean isSentryEnabled(MarquezConfig config) {
Expand Down
10 changes: 8 additions & 2 deletions api/src/main/java/marquez/MarquezConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import marquez.api.filter.exclusions.ExclusionsConfig;
import marquez.db.FlywayFactory;
import marquez.graphql.GraphqlConfig;
import marquez.jobs.DbRetentionConfig;
Expand Down Expand Up @@ -49,11 +50,16 @@ public class MarquezConfig extends Configuration {
private DbRetentionConfig dbRetention; // OPTIONAL

@Getter
@JsonProperty("namespaceFilter")
private String namespaceFilter; // OPTIONAL
@JsonProperty("exclude")
private ExclusionsConfig exclude; // OPTIONAL

/** Returns {@code true} if a data retention policy has been configured. */
public boolean hasDbRetentionPolicy() {
return (dbRetention != null);
}

/** Returns {@code true} if an exclude pattern has been configured. */
public boolean hasExcludingPatterns() {
return (exclude != null);
}
}
10 changes: 4 additions & 6 deletions api/src/main/java/marquez/api/NamespaceResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import lombok.NonNull;
import lombok.Value;
import marquez.api.exceptions.NamespaceNotFoundException;
import marquez.common.Utils;
import marquez.api.filter.exclusions.ExclusionsFilter;
import marquez.common.models.NamespaceName;
import marquez.service.ServiceFactory;
import marquez.service.models.Namespace;
Expand Down Expand Up @@ -75,16 +75,14 @@ public Response get(@PathParam("namespace") NamespaceName name) {
public Response list(
@QueryParam("limit") @DefaultValue("100") @Min(value = 0) int limit,
@QueryParam("offset") @DefaultValue("0") @Min(value = 0) int offset) {
final String namespaceFilter = Utils.getNamespaceFilter();
final String namespaceFilter = ExclusionsFilter.getNamespacesReadFilter();
final List<Namespace> allNamespaces = namespaceService.findAll(limit, offset);
// If the key: namespaceFilter is in the config file, exclude the corresponding namespaces

if (namespaceFilter != null) {
final List<Namespace> FilterNamespaces =
namespaceService.findAllFilter(namespaceFilter, limit, offset);
return Response.ok(new Namespaces(FilterNamespaces)).build();
}
// Else return all namespace
else {
} else {
return Response.ok(new Namespaces(allNamespaces)).build();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package marquez.api.filter.exclusions;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;

public class ExclusionsConfig {
@Getter @JsonProperty public NamespaceExclusion namespaces;

public static class NamespaceExclusion {
@Getter @JsonProperty public boolean onRead;
@Getter @JsonProperty public boolean onWrite;
@Getter @JsonProperty public String patterns;
}
;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package marquez.api.filter.exclusions;

public class ExclusionsFilter {
private static String namespacesFilter;

public static void setNamespacesReadFilter(String filter) {
namespacesFilter = filter;
}

public static String getNamespacesReadFilter() {
return namespacesFilter;
}
}
10 changes: 0 additions & 10 deletions api/src/main/java/marquez/common/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -434,14 +434,4 @@ private <T> void setFields(
}
}
}

private static String namespaceFilter;

public static void setNamespaceFilter(String filter) {
namespaceFilter = filter;
}

public static String getNamespaceFilter() {
return namespaceFilter;
}
}
7 changes: 1 addition & 6 deletions api/src/main/java/marquez/db/NamespaceDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,7 @@ default Namespace upsertNamespaceMeta(
List<Namespace> findAll(int limit, int offset);

@SqlQuery(
"""
SELECT * FROM namespaces
WHERE name !~ :excluded
ORDER BY name
LIMIT :limit OFFSET :offset
""")
"SELECT * FROM namespaces WHERE name !~ :excluded ORDER BY name LIMIT :limit OFFSET :offset")
List<Namespace> findAllFilter(String excluded, int limit, int offset);

@SqlQuery("UPDATE namespaces SET is_hidden=false WHERE name = :name RETURNING *")
Expand Down
19 changes: 19 additions & 0 deletions api/src/test/java/marquez/db/NamespaceDaoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@

package marquez.db;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.List;
import marquez.common.models.NamespaceName;
import marquez.common.models.OwnerName;
import marquez.jdbi.MarquezJdbiExternalPostgresExtension;
import marquez.service.models.Namespace;
import marquez.service.models.NamespaceMeta;
import org.jdbi.v3.core.Jdbi;
import org.junit.jupiter.api.BeforeAll;
Expand All @@ -34,4 +37,20 @@ void testWriteAndReadNamespace() {

assertTrue(namespaceDao.exists(namespaceName.getValue()));
}

@Test
void testFindAllFilter() {
var namespaceName1 = NamespaceName.of("postgres://localhost:5432");
var namespaceMeta1 = new NamespaceMeta(new OwnerName("marquez"), null);
namespaceDao.upsertNamespaceMeta(namespaceName1, namespaceMeta1);

var namespaceName2 = NamespaceName.of("s3://bucket");
var namespaceMeta2 = new NamespaceMeta(new OwnerName("yannick"), null);
namespaceDao.upsertNamespaceMeta(namespaceName2, namespaceMeta2);

List<Namespace> namespaces = namespaceDao.findAllFilter("s3.*", 10, 0);

assertEquals(1, namespaces.size());
assertEquals(namespaceName1, namespaces.get(0).getName());
}
}

0 comments on commit 2479bf2

Please sign in to comment.