Skip to content

Commit

Permalink
Merge pull request #2997 from takumakume/fix-create-policy-via-api
Browse files Browse the repository at this point in the history
Fix: allow operator and violationState to be specified when create policy
  • Loading branch information
nscuro authored Aug 30, 2023
2 parents 9ffca65 + 248d610 commit 15a9baa
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,17 @@ public Response createPolicy(Policy jsonPolicy) {
try (QueryManager qm = new QueryManager()) {
Policy policy = qm.getPolicy(StringUtils.trimToNull(jsonPolicy.getName()));
if (policy == null) {
Policy.Operator operator = jsonPolicy.getOperator();
if (operator == null) {
operator = Policy.Operator.ANY;
}
Policy.ViolationState violationState = jsonPolicy.getViolationState();
if (violationState == null) {
violationState = Policy.ViolationState.INFO;
}
policy = qm.createPolicy(
StringUtils.trimToNull(jsonPolicy.getName()),
Policy.Operator.ANY, Policy.ViolationState.INFO);
operator, violationState);
return Response.status(Response.Status.CREATED).entity(policy).build();
} else {
return Response.status(Response.Status.CONFLICT).entity("A policy with the specified name already exists.").build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,50 @@ public void createPolicyTest() {
assertThat(json.getBoolean("includeChildren")).isEqualTo(false);
}

@Test
public void createPolicySpecifyOperatorAndViolationStateTest() {
final Policy policy = new Policy();
policy.setName("policy");
policy.setOperator(Policy.Operator.ALL);
policy.setViolationState(Policy.ViolationState.FAIL);

final Response response = target(V1_POLICY)
.request()
.header(X_API_KEY, apiKey)
.put(Entity.entity(policy, MediaType.APPLICATION_JSON));

assertThat(response.getStatus()).isEqualTo(201);

final JsonObject json = parseJsonObject(response);
assertThat(json).isNotNull();
assertThat(json.getString("name")).isEqualTo("policy");
assertThat(json.getString("operator")).isEqualTo("ALL");
assertThat(json.getString("violationState")).isEqualTo("FAIL");
assertThat(UuidUtil.isValidUUID(json.getString("uuid")));
assertThat(json.getBoolean("includeChildren")).isEqualTo(false);
}

@Test
public void createPolicyUseDefaultValueTest() {
final Policy policy = new Policy();
policy.setName("policy");

final Response response = target(V1_POLICY)
.request()
.header(X_API_KEY, apiKey)
.put(Entity.entity(policy, MediaType.APPLICATION_JSON));

assertThat(response.getStatus()).isEqualTo(201);

final JsonObject json = parseJsonObject(response);
assertThat(json).isNotNull();
assertThat(json.getString("name")).isEqualTo("policy");
assertThat(json.getString("operator")).isEqualTo("ANY");
assertThat(json.getString("violationState")).isEqualTo("INFO");
assertThat(UuidUtil.isValidUUID(json.getString("uuid")));
assertThat(json.getBoolean("includeChildren")).isEqualTo(false);
}

@Test
public void updatePolicyTest() {
final Policy policy = qm.createPolicy("policy", Policy.Operator.ANY, Policy.ViolationState.INFO);
Expand Down

0 comments on commit 15a9baa

Please sign in to comment.