Skip to content

Commit 18cec73

Browse files
committed
implement the GrantCreate testdata creator
1 parent d8e7d1a commit 18cec73

2 files changed

Lines changed: 204 additions & 0 deletions

File tree

operator/src/test/java/it/aboutbits/postgresql/_support/testdata/persisted/Given.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io.fabric8.kubernetes.client.KubernetesClient;
44
import it.aboutbits.postgresql._support.testdata.persisted.creator.ClusterConnectionCreate;
55
import it.aboutbits.postgresql._support.testdata.persisted.creator.DatabaseCreate;
6+
import it.aboutbits.postgresql._support.testdata.persisted.creator.GrantCreate;
67
import it.aboutbits.postgresql._support.testdata.persisted.creator.RoleCreate;
78
import it.aboutbits.postgresql._support.testdata.persisted.creator.SchemaCreate;
89
import it.aboutbits.postgresql._support.testdata.persisted.creator.SecretRefCreate;
@@ -107,6 +108,14 @@ public SchemaCreate schema() {
107108
kubernetesClient
108109
);
109110
}
111+
112+
public GrantCreate grant() {
113+
return new GrantCreate(
114+
numberOfItems,
115+
given,
116+
kubernetesClient
117+
);
118+
}
110119
}
111120

112121
public record DBConnectionDetails(
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
package it.aboutbits.postgresql._support.testdata.persisted.creator;
2+
3+
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
4+
import io.fabric8.kubernetes.client.KubernetesClient;
5+
import it.aboutbits.postgresql._support.testdata.base.TestDataCreator;
6+
import it.aboutbits.postgresql._support.testdata.persisted.Given;
7+
import it.aboutbits.postgresql.core.ClusterReference;
8+
import it.aboutbits.postgresql.crd.grant.Grant;
9+
import it.aboutbits.postgresql.crd.grant.GrantObjectType;
10+
import it.aboutbits.postgresql.crd.grant.GrantPrivilege;
11+
import it.aboutbits.postgresql.crd.grant.GrantSpec;
12+
import lombok.AccessLevel;
13+
import lombok.Setter;
14+
import lombok.experimental.Accessors;
15+
import org.jspecify.annotations.NullMarked;
16+
import org.jspecify.annotations.Nullable;
17+
18+
import java.util.ArrayList;
19+
import java.util.List;
20+
import java.util.Objects;
21+
import java.util.concurrent.TimeUnit;
22+
23+
@NullMarked
24+
@Setter
25+
@Accessors(fluent = true, chain = true)
26+
public class GrantCreate extends TestDataCreator<Grant> {
27+
private final Given given;
28+
29+
private final KubernetesClient kubernetesClient;
30+
31+
@Nullable
32+
private String withNamespace;
33+
@Setter(AccessLevel.NONE)
34+
private boolean withoutNamespace = false;
35+
36+
@Nullable
37+
private String withName;
38+
39+
@Nullable
40+
private String withClusterConnectionName;
41+
42+
@Nullable
43+
private String withClusterConnectionNamespace;
44+
45+
@Nullable
46+
private String withDatabase;
47+
48+
@Nullable
49+
private String withRole;
50+
51+
@Nullable
52+
private String withSchema;
53+
54+
private GrantObjectType withObjectType = GrantObjectType.DATABASE;
55+
56+
private List<String> withObjects = new ArrayList<>();
57+
58+
private List<GrantPrivilege> withPrivileges = new ArrayList<>();
59+
60+
public GrantCreate(
61+
int numberOfItems,
62+
Given given,
63+
KubernetesClient kubernetesClient
64+
) {
65+
super(numberOfItems);
66+
this.given = given;
67+
this.kubernetesClient = kubernetesClient;
68+
}
69+
70+
@SuppressWarnings("unused")
71+
public GrantCreate withObjects(String... objects) {
72+
this.withObjects = List.of(objects);
73+
return this;
74+
}
75+
76+
@SuppressWarnings("unused")
77+
public GrantCreate withPrivileges(GrantPrivilege... privileges) {
78+
this.withPrivileges = List.of(privileges);
79+
return this;
80+
}
81+
82+
@SuppressWarnings("unused")
83+
public GrantCreate withoutNamespace() {
84+
withoutNamespace = true;
85+
return this;
86+
}
87+
88+
@Override
89+
protected Grant create(int index) {
90+
var namespace = getNamespace();
91+
var name = getName();
92+
93+
var item = new Grant();
94+
95+
item.setMetadata(new ObjectMetaBuilder()
96+
.withName(name)
97+
.withNamespace(namespace)
98+
.build()
99+
);
100+
101+
var spec = new GrantSpec();
102+
spec.setDatabase(getDatabase());
103+
spec.setRole(getRole());
104+
spec.setSchema(getSchema());
105+
spec.setObjectType(withObjectType);
106+
spec.setObjects(withObjects);
107+
spec.setPrivileges(withPrivileges);
108+
109+
var clusterRef = new ClusterReference();
110+
clusterRef.setName(getClusterConnectionName());
111+
clusterRef.setNamespace(withClusterConnectionNamespace);
112+
spec.setClusterRef(clusterRef);
113+
114+
item.setSpec(spec);
115+
116+
kubernetesClient.resources(Grant.class)
117+
.inNamespace(namespace)
118+
.resource(item)
119+
.serverSideApply();
120+
121+
//noinspection ConstantConditions
122+
return kubernetesClient.resources(Grant.class)
123+
.inNamespace(namespace)
124+
.withName(name)
125+
.waitUntilCondition(
126+
grant -> grant.getStatus() != null,
127+
10,
128+
TimeUnit.SECONDS
129+
);
130+
}
131+
132+
@Nullable
133+
private String getNamespace() {
134+
if (withoutNamespace) {
135+
return null;
136+
}
137+
138+
if (withNamespace != null) {
139+
return withNamespace;
140+
}
141+
142+
return kubernetesClient.getNamespace();
143+
}
144+
145+
private String getName() {
146+
if (withName != null) {
147+
return withName;
148+
}
149+
150+
return randomKubernetesNameSuffix("test-grant");
151+
}
152+
153+
private String getClusterConnectionName() {
154+
return Objects.requireNonNullElse(
155+
withClusterConnectionName,
156+
"test-cluster-connection"
157+
);
158+
}
159+
160+
private String getDatabase() {
161+
if (withDatabase != null) {
162+
return withDatabase;
163+
}
164+
165+
return given.one()
166+
.database()
167+
.returnFirst()
168+
.getSpec()
169+
.getName();
170+
}
171+
172+
private String getRole() {
173+
if (withRole != null) {
174+
return withRole;
175+
}
176+
177+
return given.one()
178+
.role()
179+
.returnFirst()
180+
.getSpec()
181+
.getName();
182+
}
183+
184+
private String getSchema() {
185+
if (withSchema != null) {
186+
return withSchema;
187+
}
188+
189+
return given.one()
190+
.schema()
191+
.returnFirst()
192+
.getSpec()
193+
.getName();
194+
}
195+
}

0 commit comments

Comments
 (0)