Skip to content

Commit

Permalink
feat: update spec (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrejpetras authored Jan 16, 2024
1 parent a0c0e98 commit 38db49f
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 110 deletions.
77 changes: 35 additions & 42 deletions src/main/helm/crds/permissions.onecx.github.io-v1.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,47 +11,40 @@ spec:
singular: permission
scope: Namespaced
versions:
- name: v1
schema:
openAPIV3Schema:
properties:
spec:
properties:
appId:
type: string
permissions:
items:
properties:
action:
type: string
description:
type: string
name:
type: string
resource:
- name: v1
schema:
openAPIV3Schema:
properties:
spec:
properties:
appId:
type: string
permissions:
additionalProperties:
additionalProperties:
type: string
type: object
type: object
type: array
type: object
status:
properties:
appId:
type: string
message:
type: string
observedGeneration:
type: integer
response-code:
type: integer
status:
enum:
- ERROR
- UNDEFINED
- UPDATED
type: string
type: object
type: object
served: true
storage: true
subresources:
status: {}
type: object
status:
properties:
appId:
type: string
message:
type: string
observedGeneration:
type: integer
response-code:
type: integer
status:
enum:
- ERROR
- UNDEFINED
- UPDATED
type: string
type: object
type: object
served: true
storage: true
subresources:
status: {}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.github.onecx.permission.operator;

import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand All @@ -12,7 +12,7 @@ public class PermissionSpec {
private String appId;

@JsonProperty("permissions")
private List<PermissionItemSpec> permissions;
private Map<String, Map<String, String>> permissions;

public String getAppId() {
return appId;
Expand All @@ -22,54 +22,12 @@ public void setAppId(String appId) {
this.appId = appId;
}

public List<PermissionItemSpec> getPermissions() {
public Map<String, Map<String, String>> getPermissions() {
return permissions;
}

public void setPermissions(List<PermissionItemSpec> permissions) {
public void setPermissions(Map<String, Map<String, String>> permissions) {
this.permissions = permissions;
}

public static class PermissionItemSpec {
@JsonProperty(value = "name")
private String name;
@JsonProperty(value = "resource", required = true)
private String resource;
@JsonProperty(value = "action", required = true)
private String action;
@JsonProperty(value = "description")
private String description;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getResource() {
return resource;
}

public void setResource(String resource) {
this.resource = resource;
}

public String getAction() {
return action;
}

public void setAction(String action) {
this.action = action;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.github.onecx.permission.operator.client.mappers;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.mapstruct.Mapper;

Expand All @@ -13,5 +15,27 @@ public interface PermissionClientMapper {

PermissionRequest map(PermissionSpec spec);

List<Permission> map(List<PermissionSpec.PermissionItemSpec> spec);
default List<Permission> map(Map<String, Map<String, String>> permissions) {
if (permissions == null || permissions.isEmpty()) {
return List.of();
}
List<Permission> result = new ArrayList<>();
for (Map.Entry<String, Map<String, String>> permission : permissions.entrySet()) {
result.addAll(map(permission.getKey(), permission.getValue()));
}
return result;
}

default List<Permission> map(String resource, Map<String, String> actions) {
if (actions == null || actions.isEmpty()) {
return List.of();
}
List<Permission> result = new ArrayList<>();
for (Map.Entry<String, String> action : actions.entrySet()) {
result.add(map(resource, action.getKey(), action.getValue()));
}
return result;
}

Permission map(String resource, String action, String description);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;

import java.util.List;
import java.util.HashMap;
import java.util.Map;

import jakarta.inject.Inject;

Expand Down Expand Up @@ -33,16 +34,12 @@ void beforeAll() {

@Test
void testWrongResponse() throws Exception {

var p1 = new PermissionSpec.PermissionItemSpec();
p1.setName("n2");
p1.setAction("a2");
p1.setDescription("d1");
p1.setResource("r1");
Map<String, Map<String, String>> p1 = new HashMap<>();
p1.put("r1", Map.of("a2", "d1"));

var s = new PermissionSpec();
s.setAppId("test-3");
s.setPermissions(List.of(p1));
s.setPermissions(p1);

Permission m = new Permission();
m.setSpec(s);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import static java.util.concurrent.TimeUnit.SECONDS;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.catchThrowableOfType;
import static org.awaitility.Awaitility.await;

import java.util.List;
import java.util.HashMap;
import java.util.Map;

import jakarta.inject.Inject;

Expand All @@ -16,6 +18,7 @@

import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.github.onecx.permission.test.AbstractTest;
import io.javaoperatorsdk.operator.Operator;
import io.quarkus.test.junit.QuarkusTest;
Expand Down Expand Up @@ -85,15 +88,12 @@ void productUpdateEmptySpecTest() {

operator.start();

var p1 = new PermissionSpec.PermissionItemSpec();
p1.setName("n2");
p1.setAction("a2");
p1.setDescription("d1");
p1.setResource("r1");
Map<String, Map<String, String>> p1 = new HashMap<>();
p1.put("r1", Map.of("a2", "d1"));

var m = new PermissionSpec();
m.setAppId("test-3");
m.setPermissions(List.of(p1));
m.setPermissions(p1);

var data = new Permission();
data
Expand Down Expand Up @@ -124,20 +124,42 @@ void productUpdateEmptySpecTest() {
});
}

@Test
void productUpdateNoDescriptionTest() {

operator.start();

Map<String, Map<String, String>> p1 = new HashMap<>();
var a = new HashMap<String, String>();
a.put("a2", null);
p1.put("r1", a);

var m = new PermissionSpec();
m.setAppId("test-3");
m.setPermissions(p1);

var data = new Permission();
data
.setMetadata(new ObjectMetaBuilder().withName("to-update-spec").withNamespace(client.getNamespace()).build());
data.setSpec(m);

log.info("Creating test permission object: {}", data);
var exception = catchThrowableOfType(() -> client.resource(data).serverSideApply(), KubernetesClientException.class);
assertThat(exception).isNotNull();

}

@Test
void productRestClientExceptionTest() {

operator.start();

var p1 = new PermissionSpec.PermissionItemSpec();
p1.setName("n2");
p1.setAction("a2");
p1.setDescription("d1");
p1.setResource("r1");
Map<String, Map<String, String>> p1 = new HashMap<>();
p1.put("r1", Map.of("a2", "d1"));

var m = new PermissionSpec();
m.setAppId("test-error-1");
m.setPermissions(List.of(p1));
m.setPermissions(p1);

var data = new Permission();
data
Expand Down

0 comments on commit 38db49f

Please sign in to comment.