Skip to content

Commit f9fcc4c

Browse files
committed
add relations
1 parent c7d2d3a commit f9fcc4c

File tree

14 files changed

+686
-296
lines changed

14 files changed

+686
-296
lines changed

apps/workspace-engine/oapi/spec.yaml

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,42 @@ paths:
162162
schema:
163163
$ref: "#/components/schemas/NotFoundError"
164164

165+
/v1/workspaces/{workspaceId}/entities/{entityType}/{entityId}/relationships:
166+
get:
167+
summary: Get related entities for a given entity
168+
operationId: getRelatedEntities
169+
description: Returns all entities related to the specified entity (deployment, environment, or resource) based on relationship rules. Relationships are grouped by relationship reference.
170+
parameters:
171+
- $ref: "#/components/parameters/workspaceId"
172+
- $ref: "#/components/parameters/entityType"
173+
- $ref: "#/components/parameters/entityId"
174+
responses:
175+
"200":
176+
description: Related entities grouped by relationship reference.
177+
content:
178+
application/json:
179+
schema:
180+
type: object
181+
properties:
182+
relationships:
183+
type: object
184+
additionalProperties:
185+
type: array
186+
items:
187+
$ref: "#/components/schemas/RelatedEntityGroup"
188+
"404":
189+
description: Entity not found.
190+
content:
191+
application/json:
192+
schema:
193+
$ref: "#/components/schemas/NotFoundError"
194+
"400":
195+
description: Invalid entity type.
196+
content:
197+
application/json:
198+
schema:
199+
$ref: "#/components/schemas/NotFoundError"
200+
165201
components:
166202
parameters:
167203
policyId:
@@ -206,6 +242,24 @@ components:
206242
description: ID of the release target.
207243
schema:
208244
type: string
245+
entityType:
246+
name: entityType
247+
in: path
248+
required: true
249+
description: Type of the entity (deployment, environment, or resource).
250+
schema:
251+
type: string
252+
enum:
253+
- deployment
254+
- environment
255+
- resource
256+
entityId:
257+
name: entityId
258+
in: path
259+
required: true
260+
description: ID of the entity.
261+
schema:
262+
type: string
209263
schemas:
210264
DeployDecision:
211265
type: object
@@ -834,6 +888,12 @@ components:
834888
items:
835889
$ref: "#/components/schemas/PropertyMatcher"
836890

891+
RelationDirection:
892+
type: string
893+
enum:
894+
- from
895+
- to
896+
837897
RelationshipRule:
838898
type: object
839899
required:
@@ -856,11 +916,11 @@ components:
856916
reference:
857917
type: string
858918
fromType:
859-
type: string
919+
$ref: "#/components/schemas/RelatableEntityType"
860920
fromSelector:
861921
$ref: "#/components/schemas/Selector"
862922
toType:
863-
type: string
923+
$ref: "#/components/schemas/RelatableEntityType"
864924
toSelector:
865925
$ref: "#/components/schemas/Selector"
866926
matcher:
@@ -876,6 +936,40 @@ components:
876936
workspaceId:
877937
type: string
878938

939+
RelatedEntityGroup:
940+
type: object
941+
required:
942+
- relationshipRule
943+
- direction
944+
- entityType
945+
- entityId
946+
- entity
947+
properties:
948+
rule:
949+
$ref: "#/components/schemas/RelationshipRule"
950+
direction:
951+
$ref: "#/components/schemas/RelationDirection"
952+
entityType:
953+
$ref: "#/components/schemas/RelatableEntityType"
954+
entityId:
955+
type: string
956+
description: ID of the related entity
957+
entity:
958+
$ref: "#/components/schemas/RelatableEntity"
959+
960+
RelatableEntity:
961+
oneOf:
962+
- $ref: "#/components/schemas/Deployment"
963+
- $ref: "#/components/schemas/Environment"
964+
- $ref: "#/components/schemas/Resource"
965+
966+
RelatableEntityType:
967+
type: string
968+
enum:
969+
- deployment
970+
- environment
971+
- resource
972+
879973
EvaluateReleaseTargetRequest:
880974
type: object
881975
required:
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package oapi
2+
3+
import "encoding/json"
4+
5+
func (e *RelatableEntity) GetType() RelatableEntityType {
6+
// Try to unmarshal into a map to check for distinguishing fields
7+
var data map[string]any
8+
if err := json.Unmarshal(e.union, &data); err != nil {
9+
return ""
10+
}
11+
12+
// Check for distinguishing fields
13+
// Deployment has "slug" field
14+
if _, hasSlug := data["slug"]; hasSlug {
15+
return "deployment"
16+
}
17+
18+
// Resource has "kind" field
19+
if _, hasKind := data["kind"]; hasKind {
20+
return "resource"
21+
}
22+
23+
// Environment has "systemId" but no "slug" or "kind"
24+
if _, hasSystemId := data["systemId"]; hasSystemId {
25+
return "environment"
26+
}
27+
28+
return ""
29+
}
30+
31+
func (e *RelatableEntity) GetDeployment() *Deployment {
32+
if e.GetType() == "deployment" {
33+
deployment, err := e.AsDeployment()
34+
if err != nil {
35+
return nil
36+
}
37+
return &deployment
38+
}
39+
return nil
40+
}
41+
42+
func (e *RelatableEntity) GetEnvironment() *Environment {
43+
if e.GetType() == "environment" {
44+
environment, err := e.AsEnvironment()
45+
if err != nil {
46+
return nil
47+
}
48+
return &environment
49+
}
50+
return nil
51+
}
52+
53+
func (e *RelatableEntity) GetResource() *Resource {
54+
if e.GetType() == "resource" {
55+
resource, err := e.AsResource()
56+
if err != nil {
57+
return nil
58+
}
59+
return &resource
60+
}
61+
return nil
62+
}
63+
64+
func (e *RelatableEntity) GetID() string {
65+
switch e.GetType() {
66+
case "deployment":
67+
d := e.GetDeployment()
68+
if d != nil {
69+
return d.Id
70+
}
71+
case "environment":
72+
env := e.GetEnvironment()
73+
if env != nil {
74+
return env.Id
75+
}
76+
case "resource":
77+
r := e.GetResource()
78+
if r != nil {
79+
return r.Id
80+
}
81+
}
82+
return ""
83+
}

0 commit comments

Comments
 (0)