Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions aap_policy_examples/jt_naming_validation.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package aap_policy_examples

import rego.v1

# Default policy response indicating allowed status with no violations
default jt_naming_validation := {
"allowed": true,
"violations": [],
}

# Validate that job template name has correct organization and project name prefixes
jt_naming_validation := result if {
# Extract values from input
org_name := object.get(input, ["organization", "name"], "")
project_name := object.get(input, ["project", "name"], "")
jt_name := object.get(input, ["job_template", "name"], "")

# Construct the expected prefix
expected_prefix := concat("_", [org_name, project_name])

# Check if job template name starts with expected prefix
not startswith(jt_name, expected_prefix)

result := {
"allowed": false,
"violations": [sprintf("Job template naming for '%v' does not comply with standards", [jt_name])]
}
}
30 changes: 30 additions & 0 deletions test_aap_policy_examples/jt_naming_validation_test.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package test_aap_policy_examples

import data.aap_policy_examples

test_valid_jt_naming_allowed if {
test_input := {
"organization": {"name": "org1"},
"project": {"name": "proj1"},
"job_template": {"name": "org1_proj1_my_template"},
}
aap_policy_examples.jt_naming_validation.allowed == true with input as test_input
}

test_invalid_jt_naming_not_allowed if {
test_input := {
"organization": {"name": "org1"},
"project": {"name": "proj1"},
"job_template": {"name": "my_template"},
}
aap_policy_examples.jt_naming_validation.allowed == false with input as test_input
}

test_violation_message if {
test_input := {
"organization": {"name": "org1"},
"project": {"name": "proj1"},
"job_template": {"name": "my_template"},
}
aap_policy_examples.jt_naming_validation.violations[0] == "Job template naming for 'my_template' does not comply with standards" with input as test_input
}