Skip to content

Commit a059777

Browse files
chore: add rule id to deploydecision and get rules oapi (#707)
1 parent a279f4d commit a059777

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
@@ -1154,9 +1154,14 @@
11541154
"message": {
11551155
"description": "Human-readable explanation of the rule result",
11561156
"type": "string"
1157+
},
1158+
"ruleId": {
1159+
"description": "The ID of the rule that was evaluated",
1160+
"type": "string"
11571161
}
11581162
},
11591163
"required": [
1164+
"ruleId",
11601165
"allowed",
11611166
"actionRequired",
11621167
"message",
@@ -3223,6 +3228,74 @@
32233228
"summary": "Get release targets for a policy"
32243229
}
32253230
},
3231+
"/v1/workspaces/{workspaceId}/policies/{policyId}/rules/{ruleId}": {
3232+
"get": {
3233+
"description": "Returns a specific rule by ID.",
3234+
"operationId": "getRule",
3235+
"parameters": [
3236+
{
3237+
"description": "ID of the workspace",
3238+
"in": "path",
3239+
"name": "workspaceId",
3240+
"required": true,
3241+
"schema": {
3242+
"type": "string"
3243+
}
3244+
},
3245+
{
3246+
"description": "ID of the policy",
3247+
"in": "path",
3248+
"name": "policyId",
3249+
"required": true,
3250+
"schema": {
3251+
"type": "string"
3252+
}
3253+
},
3254+
{
3255+
"description": "ID of the rule",
3256+
"in": "path",
3257+
"name": "ruleId",
3258+
"required": true,
3259+
"schema": {
3260+
"type": "string"
3261+
}
3262+
}
3263+
],
3264+
"responses": {
3265+
"200": {
3266+
"content": {
3267+
"application/json": {
3268+
"schema": {
3269+
"$ref": "#/components/schemas/PolicyRule"
3270+
}
3271+
}
3272+
},
3273+
"description": "OK response"
3274+
},
3275+
"400": {
3276+
"content": {
3277+
"application/json": {
3278+
"schema": {
3279+
"$ref": "#/components/schemas/ErrorResponse"
3280+
}
3281+
}
3282+
},
3283+
"description": "Invalid request"
3284+
},
3285+
"404": {
3286+
"content": {
3287+
"application/json": {
3288+
"schema": {
3289+
"$ref": "#/components/schemas/ErrorResponse"
3290+
}
3291+
}
3292+
},
3293+
"description": "Resource not found"
3294+
}
3295+
},
3296+
"summary": "Get rule"
3297+
}
3298+
},
32263299
"/v1/workspaces/{workspaceId}/relationship-rules": {
32273300
"get": {
32283301
"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
@@ -145,8 +145,12 @@ local openapi = import '../lib/openapi.libsonnet';
145145

146146
RuleEvaluation: {
147147
type: 'object',
148-
required: ['allowed', 'actionRequired', 'message', 'details'],
148+
required: ['ruleId', 'allowed', 'actionRequired', 'message', 'details'],
149149
properties: {
150+
ruleId: {
151+
type: 'string',
152+
description: 'The ID of the rule that was evaluated',
153+
},
150154
allowed: {
151155
type: 'boolean',
152156
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)