Skip to content

Commit be27bb6

Browse files
chore: add rule id to deploydecision and get rules oapi
1 parent f64974e commit be27bb6

File tree

8 files changed

+186
-7
lines changed

8 files changed

+186
-7
lines changed

apps/workspace-engine/oapi/openapi.json

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,9 +1137,14 @@
11371137
"message": {
11381138
"description": "Human-readable explanation of the rule result",
11391139
"type": "string"
1140+
},
1141+
"ruleId": {
1142+
"description": "The ID of the rule that was evaluated",
1143+
"type": "string"
11401144
}
11411145
},
11421146
"required": [
1147+
"ruleId",
11431148
"allowed",
11441149
"actionRequired",
11451150
"message",
@@ -3136,6 +3141,74 @@
31363141
"summary": "Get release targets for a policy"
31373142
}
31383143
},
3144+
"/v1/workspaces/{workspaceId}/policies/{policyId}/rules/{ruleId}": {
3145+
"get": {
3146+
"description": "Returns a specific rule by ID.",
3147+
"operationId": "getRule",
3148+
"parameters": [
3149+
{
3150+
"description": "ID of the workspace",
3151+
"in": "path",
3152+
"name": "workspaceId",
3153+
"required": true,
3154+
"schema": {
3155+
"type": "string"
3156+
}
3157+
},
3158+
{
3159+
"description": "ID of the policy",
3160+
"in": "path",
3161+
"name": "policyId",
3162+
"required": true,
3163+
"schema": {
3164+
"type": "string"
3165+
}
3166+
},
3167+
{
3168+
"description": "ID of the rule",
3169+
"in": "path",
3170+
"name": "ruleId",
3171+
"required": true,
3172+
"schema": {
3173+
"type": "string"
3174+
}
3175+
}
3176+
],
3177+
"responses": {
3178+
"200": {
3179+
"content": {
3180+
"application/json": {
3181+
"schema": {
3182+
"$ref": "#/components/schemas/PolicyRule"
3183+
}
3184+
}
3185+
},
3186+
"description": "OK response"
3187+
},
3188+
"400": {
3189+
"content": {
3190+
"application/json": {
3191+
"schema": {
3192+
"$ref": "#/components/schemas/ErrorResponse"
3193+
}
3194+
}
3195+
},
3196+
"description": "Invalid request"
3197+
},
3198+
"404": {
3199+
"content": {
3200+
"application/json": {
3201+
"schema": {
3202+
"$ref": "#/components/schemas/ErrorResponse"
3203+
}
3204+
}
3205+
},
3206+
"description": "Resource not found"
3207+
}
3208+
},
3209+
"summary": "Get rule"
3210+
}
3211+
},
31393212
"/v1/workspaces/{workspaceId}/relationship-rules": {
31403213
"get": {
31413214
"description": "Returns all relationship rules for the specified workspace.",

apps/workspace-engine/oapi/spec/lib/openapi.libsonnet

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
// Common parameters
2020
workspaceIdParam():: self.stringParam('workspaceId', 'ID of the workspace'),
2121
policyIdParam():: self.stringParam('policyId', 'ID of the policy'),
22+
ruleIdParam():: self.stringParam('ruleId', 'ID of the rule'),
2223
resourceIdParam():: self.stringParam('resourceId', 'ID of the resource'),
2324
resourceIdentifierParam():: self.stringParam('resourceIdentifier', 'Identifier of the resource'),
2425
deploymentIdParam():: self.stringParam('deploymentId', 'ID of the deployment'),

apps/workspace-engine/oapi/spec/paths/policy.jsonnet

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,20 @@ local openapi = import '../lib/openapi.libsonnet';
6565
) + openapi.notFoundResponse(),
6666
},
6767
},
68+
69+
'/v1/workspaces/{workspaceId}/policies/{policyId}/rules/{ruleId}': {
70+
get: {
71+
summary: 'Get rule',
72+
operationId: 'getRule',
73+
description: 'Returns a specific rule by ID.',
74+
parameters: [
75+
openapi.workspaceIdParam(),
76+
openapi.policyIdParam(),
77+
openapi.ruleIdParam(),
78+
],
79+
responses: openapi.okResponse(openapi.schemaRef('PolicyRule'))
80+
+ openapi.notFoundResponse()
81+
+ openapi.badRequestResponse(),
82+
},
83+
},
6884
}

apps/workspace-engine/oapi/spec/schemas/policy.jsonnet

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,12 @@ local openapi = import '../lib/openapi.libsonnet';
137137

138138
RuleEvaluation: {
139139
type: 'object',
140-
required: ['allowed', 'actionRequired', 'message', 'details'],
140+
required: ['ruleId', 'allowed', 'actionRequired', 'message', 'details'],
141141
properties: {
142+
ruleId: {
143+
type: 'string',
144+
description: 'The ID of the rule that was evaluated',
145+
},
142146
allowed: {
143147
type: 'boolean',
144148
description: 'Whether the rule allows the deployment',

apps/workspace-engine/pkg/oapi/evaluation.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package oapi
22

33
func NewRuleEvaluation() *RuleEvaluation {
44
return &RuleEvaluation{
5+
RuleId: "",
56
Allowed: false,
67
ActionRequired: false,
78
ActionType: nil,
@@ -20,6 +21,11 @@ func (r *RuleEvaluation) Deny() *RuleEvaluation {
2021
return r
2122
}
2223

24+
func (r *RuleEvaluation) WithRuleId(ruleId string) *RuleEvaluation {
25+
r.RuleId = ruleId
26+
return r
27+
}
28+
2329
func (r *RuleEvaluation) WithActionRequired(actionType RuleEvaluationActionType) *RuleEvaluation {
2430
r.ActionRequired = true
2531
r.ActionType = &actionType

apps/workspace-engine/pkg/oapi/oapi.gen.go

Lines changed: 49 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/workspace-engine/pkg/server/openapi/policies/policies.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package policies
22

33
import (
4+
"fmt"
45
"net/http"
56
"workspace-engine/pkg/oapi"
67
"workspace-engine/pkg/selector"
@@ -100,3 +101,32 @@ func (p *Policies) GetReleaseTargetsForPolicy(c *gin.Context, workspaceId string
100101
"releaseTargets": matchingReleaseTargets,
101102
})
102103
}
104+
105+
func (p *Policies) GetRule(c *gin.Context, workspaceId string, policyId string, ruleId string) {
106+
ws, err := utils.GetWorkspace(c, workspaceId)
107+
if err != nil {
108+
c.JSON(http.StatusInternalServerError, gin.H{
109+
"error": "Failed to get workspace: " + err.Error(),
110+
})
111+
return
112+
}
113+
114+
policy, ok := ws.Policies().Get(policyId)
115+
if !ok {
116+
c.JSON(http.StatusNotFound, gin.H{
117+
"error": "Policy not found",
118+
})
119+
return
120+
}
121+
122+
for _, rule := range policy.Rules {
123+
if rule.Id == ruleId {
124+
c.JSON(http.StatusOK, rule)
125+
return
126+
}
127+
}
128+
129+
c.JSON(http.StatusNotFound, gin.H{
130+
"error": fmt.Sprintf("Rule %s not found in policy %s", ruleId, policyId),
131+
})
132+
}

apps/workspace-engine/pkg/workspace/releasemanager/policy/factory.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (f *EvaluatorFactory) EvaluateEnvironmentAndVersionAndTargetScopedPolicyRul
3939
if err != nil {
4040
return nil, err
4141
}
42-
ruleResults = append(ruleResults, result)
42+
ruleResults = append(ruleResults, result.WithRuleId(rule.Id))
4343
}
4444
return ruleResults, nil
4545
})
@@ -62,7 +62,7 @@ func (f *EvaluatorFactory) EvaluateEnvironmentAndVersionScopedPolicyRules(
6262
if err != nil {
6363
return nil, err
6464
}
65-
ruleResults = append(ruleResults, result)
65+
ruleResults = append(ruleResults, result.WithRuleId(rule.Id))
6666
}
6767
return ruleResults, nil
6868
})
@@ -86,7 +86,7 @@ func (f *EvaluatorFactory) EvaluateVersionScopedPolicyRules(
8686
if err != nil {
8787
return nil, err
8888
}
89-
ruleResults = append(ruleResults, result)
89+
ruleResults = append(ruleResults, result.WithRuleId(rule.Id))
9090
}
9191
return ruleResults, nil
9292
})
@@ -110,7 +110,7 @@ func (f *EvaluatorFactory) EvaluateTargetScopedPolicyRules(
110110
if err != nil {
111111
return nil, err
112112
}
113-
ruleResults = append(ruleResults, result)
113+
ruleResults = append(ruleResults, result.WithRuleId(rule.Id))
114114
}
115115
return ruleResults, nil
116116
})
@@ -134,7 +134,7 @@ func (f *EvaluatorFactory) EvaluateReleaseScopedPolicyRules(
134134
if err != nil {
135135
return nil, err
136136
}
137-
ruleResults = append(ruleResults, result)
137+
ruleResults = append(ruleResults, result.WithRuleId(rule.Id))
138138
}
139139
return ruleResults, nil
140140
})
@@ -157,7 +157,7 @@ func (f *EvaluatorFactory) EvaluateWorkspaceScopedPolicyRules(
157157
if err != nil {
158158
return nil, err
159159
}
160-
ruleResults = append(ruleResults, result)
160+
ruleResults = append(ruleResults, result.WithRuleId(rule.Id))
161161
}
162162
return ruleResults, nil
163163
})

0 commit comments

Comments
 (0)