Skip to content

Commit 17f28a9

Browse files
authored
Implement PolicyCatalog Stage 1: CRUD + ListPolicies (#1294)
1 parent 9714ef2 commit 17f28a9

File tree

10 files changed

+1020
-4
lines changed

10 files changed

+1020
-4
lines changed

api/polaris-catalog-service/build.gradle.kts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ val policyManagementModels =
3636
"CatalogIdentifier",
3737
"CreatePolicyRequest",
3838
"LoadPolicyResponse",
39-
"PolicyIdentifier",
4039
"Policy",
4140
"PolicyAttachmentTarget",
4241
"AttachPolicyRequest",
@@ -59,6 +58,7 @@ dependencies {
5958
implementation(libs.jakarta.inject.api)
6059
implementation(libs.jakarta.validation.api)
6160
implementation(libs.swagger.annotations)
61+
implementation(libs.guava)
6262

6363
implementation(libs.jakarta.servlet.api)
6464
implementation(libs.jakarta.ws.rs.api)
@@ -103,6 +103,9 @@ openApiGenerate {
103103
"ErrorModel" to "org.apache.iceberg.rest.responses.ErrorResponse",
104104
"IcebergErrorResponse" to "org.apache.iceberg.rest.responses.ErrorResponse",
105105
"TableIdentifier" to "org.apache.iceberg.catalog.TableIdentifier",
106+
107+
// Custom types defined below
108+
"PolicyIdentifier" to "org.apache.polaris.service.types.PolicyIdentifier",
106109
)
107110
}
108111

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.polaris.service.types;
20+
21+
import com.fasterxml.jackson.annotation.JsonCreator;
22+
import com.fasterxml.jackson.annotation.JsonProperty;
23+
import io.swagger.annotations.ApiModelProperty;
24+
import java.util.Objects;
25+
import org.apache.iceberg.catalog.Namespace;
26+
27+
/**
28+
* Represents a modified version of the PolicyIdentifier that is different from the one generated by
29+
* the OpenAPI generator
30+
*
31+
* <p>the open api generation inlines the namespace definition, generates a {@code List<String>}
32+
* directly, instead of generating a Namespace class. This version uses {@link
33+
* org.apache.iceberg.catalog.Namespace} for namespace field.
34+
*
35+
* <p>TODO: make code generation use {@link org.apache.iceberg.catalog.Namespace} directly
36+
*/
37+
public class PolicyIdentifier {
38+
39+
private final Namespace namespace;
40+
private final String name;
41+
42+
/** Reference to one or more levels of a namespace */
43+
@ApiModelProperty(
44+
example = "[\"accounting\",\"tax\"]",
45+
required = true,
46+
value = "Reference to one or more levels of a namespace")
47+
@JsonProperty(value = "namespace", required = true)
48+
public Namespace getNamespace() {
49+
return namespace;
50+
}
51+
52+
/** */
53+
@ApiModelProperty(required = true, value = "")
54+
@JsonProperty(value = "name", required = true)
55+
public String getName() {
56+
return name;
57+
}
58+
59+
@JsonCreator
60+
public PolicyIdentifier(
61+
@JsonProperty(value = "namespace", required = true) Namespace namespace,
62+
@JsonProperty(value = "name", required = true) String name) {
63+
this.namespace = Objects.requireNonNullElse(namespace, Namespace.empty());
64+
this.name = name;
65+
}
66+
67+
public static Builder builder() {
68+
return new Builder();
69+
}
70+
71+
public static Builder builder(Namespace namespace, String name) {
72+
return new Builder(namespace, name);
73+
}
74+
75+
public static final class Builder {
76+
private Namespace namespace;
77+
private String name;
78+
79+
private Builder() {}
80+
81+
private Builder(Namespace namespace, String name) {
82+
this.namespace = Objects.requireNonNullElse(namespace, Namespace.empty());
83+
this.name = name;
84+
}
85+
86+
public Builder setNamespace(Namespace namespace) {
87+
this.namespace = namespace;
88+
return this;
89+
}
90+
91+
public Builder setName(String name) {
92+
this.name = name;
93+
return this;
94+
}
95+
96+
public PolicyIdentifier build() {
97+
PolicyIdentifier inst = new PolicyIdentifier(namespace, name);
98+
return inst;
99+
}
100+
}
101+
102+
@Override
103+
public boolean equals(Object o) {
104+
if (this == o) {
105+
return true;
106+
}
107+
if (o == null || getClass() != o.getClass()) {
108+
return false;
109+
}
110+
PolicyIdentifier policyIdentifier = (PolicyIdentifier) o;
111+
return Objects.equals(this.namespace, policyIdentifier.namespace)
112+
&& Objects.equals(this.name, policyIdentifier.name);
113+
}
114+
115+
@Override
116+
public int hashCode() {
117+
return Objects.hash(namespace, name);
118+
}
119+
120+
@Override
121+
public String toString() {
122+
StringBuilder sb = new StringBuilder();
123+
sb.append("class PolicyIdentifier {\n");
124+
125+
sb.append(" namespace: ").append(toIndentedString(namespace)).append("\n");
126+
sb.append(" name: ").append(toIndentedString(name)).append("\n");
127+
sb.append("}");
128+
return sb.toString();
129+
}
130+
131+
/**
132+
* Convert the given object to string with each line indented by 4 spaces (except the first line).
133+
*/
134+
private String toIndentedString(Object o) {
135+
if (o == null) {
136+
return "null";
137+
}
138+
return o.toString().replace("\n", "\n ");
139+
}
140+
}

polaris-core/src/main/java/org/apache/polaris/core/catalog/PolarisCatalogHelpers.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,14 @@ public class PolarisCatalogHelpers {
3636
private PolarisCatalogHelpers() {}
3737

3838
public static List<String> tableIdentifierToList(TableIdentifier identifier) {
39+
return identifierToList(identifier.namespace(), identifier.name());
40+
}
41+
42+
public static List<String> identifierToList(Namespace namespace, String name) {
3943
ImmutableList.Builder<String> fullList =
40-
ImmutableList.builderWithExpectedSize(identifier.namespace().length() + 1);
41-
fullList.addAll(Arrays.asList(identifier.namespace().levels()));
42-
fullList.add(identifier.name());
44+
ImmutableList.builderWithExpectedSize(namespace.length() + 1);
45+
fullList.addAll(Arrays.asList(namespace.levels()));
46+
fullList.add(name);
4347
return fullList.build();
4448
}
4549

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.polaris.core.policy.exceptions;
20+
21+
import org.apache.polaris.core.exceptions.PolarisException;
22+
23+
public class NoSuchPolicyException extends PolarisException {
24+
25+
public NoSuchPolicyException(String message) {
26+
super(message);
27+
}
28+
29+
public NoSuchPolicyException(String message, Throwable cause) {
30+
super(message, cause);
31+
}
32+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.polaris.core.policy.exceptions;
20+
21+
import org.apache.polaris.core.exceptions.PolarisException;
22+
23+
public class PolicyVersionMismatchException extends PolarisException {
24+
public PolicyVersionMismatchException(String message) {
25+
super(message);
26+
}
27+
28+
public PolicyVersionMismatchException(String message, Throwable cause) {
29+
super(message, cause);
30+
}
31+
}

0 commit comments

Comments
 (0)