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
2 changes: 2 additions & 0 deletions apps/workspace-engine/pkg/config/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,6 @@ type Config struct {
RegisterAddress string `envconfig:"REGISTER_ADDRESS" default:""`

TraceTokenSecret string `envconfig:"TRACE_TOKEN_SECRET" default:"secret"`

MigrateLegacyEntities bool `envconfig:"MIGRATE_LEGACY_ENTITIES" default:"true"`
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package convert
package db

import (
"encoding/json"

"workspace-engine/pkg/db"
"workspace-engine/pkg/oapi"

"github.com/google/uuid"
)

func Deployment(row db.Deployment) *oapi.Deployment {
func ToOapiDeployment(row Deployment) *oapi.Deployment {
d := &oapi.Deployment{
Id: row.ID.String(),
Name: row.Name,
Expand All @@ -26,7 +25,7 @@ func Deployment(row db.Deployment) *oapi.Deployment {
return d
}

func Environment(row db.Environment) *oapi.Environment {
func ToOapiEnvironment(row Environment) *oapi.Environment {
e := &oapi.Environment{
Id: row.ID.String(),
Name: row.Name,
Expand All @@ -41,7 +40,7 @@ func Environment(row db.Environment) *oapi.Environment {
return e
}

func Resource(row db.GetResourceByIDRow) *oapi.Resource {
func ToOapiResource(row GetResourceByIDRow) *oapi.Resource {
r := &oapi.Resource{
Id: row.ID.String(),
Name: row.Name,
Expand Down Expand Up @@ -70,7 +69,7 @@ func Resource(row db.GetResourceByIDRow) *oapi.Resource {
return r
}

func Policy(row db.Policy) *oapi.Policy {
func ToOapiPolicy(row Policy) *oapi.Policy {
p := &oapi.Policy{
Id: row.ID.String(),
Name: row.Name,
Expand All @@ -89,7 +88,7 @@ func Policy(row db.Policy) *oapi.Policy {
return p
}

func UserApprovalRecord(row db.UserApprovalRecord) *oapi.UserApprovalRecord {
func ToOapiUserApprovalRecord(row UserApprovalRecord) *oapi.UserApprovalRecord {
r := &oapi.UserApprovalRecord{
VersionId: row.VersionID.String(),
UserId: row.UserID.String(),
Expand All @@ -105,7 +104,7 @@ func UserApprovalRecord(row db.UserApprovalRecord) *oapi.UserApprovalRecord {
return r
}

func PolicySkip(row db.PolicySkip) *oapi.PolicySkip {
func ToOapiPolicySkip(row PolicySkip) *oapi.PolicySkip {
s := &oapi.PolicySkip{
Id: row.ID.String(),
CreatedBy: row.CreatedBy,
Expand All @@ -131,7 +130,7 @@ func PolicySkip(row db.PolicySkip) *oapi.PolicySkip {
return s
}

func DeploymentVariable(row db.DeploymentVariable) oapi.DeploymentVariable {
func ToOapiDeploymentVariable(row DeploymentVariable) oapi.DeploymentVariable {
v := oapi.DeploymentVariable{
Id: row.ID.String(),
DeploymentId: row.DeploymentID.String(),
Expand All @@ -149,7 +148,7 @@ func DeploymentVariable(row db.DeploymentVariable) oapi.DeploymentVariable {
return v
}

func DeploymentVariableValue(row db.DeploymentVariableValue) oapi.DeploymentVariableValue {
func ToOapiDeploymentVariableValue(row DeploymentVariableValue) oapi.DeploymentVariableValue {
v := oapi.DeploymentVariableValue{
Id: row.ID.String(),
DeploymentVariableId: row.DeploymentVariableID.String(),
Expand All @@ -167,7 +166,7 @@ func DeploymentVariableValue(row db.DeploymentVariableValue) oapi.DeploymentVari
return v
}

func ResourceVariable(row db.ResourceVariable) oapi.ResourceVariable {
func ToOapiResourceVariable(row ResourceVariable) oapi.ResourceVariable {
v := oapi.ResourceVariable{
ResourceId: row.ResourceID.String(),
Key: row.Key,
Expand All @@ -178,7 +177,7 @@ func ResourceVariable(row db.ResourceVariable) oapi.ResourceVariable {
return v
}

func DeploymentVersion(row db.DeploymentVersion) *oapi.DeploymentVersion {
func ToOapiDeploymentVersion(row DeploymentVersion) *oapi.DeploymentVersion {
v := &oapi.DeploymentVersion{
Id: row.ID.String(),
Name: row.Name,
Expand Down
50 changes: 50 additions & 0 deletions apps/workspace-engine/pkg/getters/deployment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package getters

import (
"context"
"fmt"
"workspace-engine/pkg/db"
"workspace-engine/pkg/oapi"
"workspace-engine/pkg/workspace/store"

"github.com/google/uuid"
)

type DeploymentGetter interface {
GetDeployment(ctx context.Context, deploymentID string) (*oapi.Deployment, error)
}

var _ DeploymentGetter = (*PostgresDeploymentGetter)(nil)

type PostgresDeploymentGetter struct {
queries *db.Queries
}

func NewPostgresDeploymentGetter(queries *db.Queries) *PostgresDeploymentGetter {
return &PostgresDeploymentGetter{queries: queries}
}

// GetDeployment implements [DeploymentGetter].
func (d *PostgresDeploymentGetter) GetDeployment(ctx context.Context, deploymentID string) (*oapi.Deployment, error) {
deployment, err := d.queries.GetDeploymentByID(ctx, uuid.MustParse(deploymentID))
if err != nil {
return nil, err
}
return db.ToOapiDeployment(deployment), nil
}

type StoreDeploymentGetter struct {
store *store.Store
}

func NewStoreDeploymentGetter(store *store.Store) *StoreDeploymentGetter {
return &StoreDeploymentGetter{store: store}
}

func (s *StoreDeploymentGetter) GetDeployment(ctx context.Context, deploymentID string) (*oapi.Deployment, error) {
deployment, ok := s.store.Deployments.Get(deploymentID)
if !ok {
return nil, fmt.Errorf("deployment not found")
}
return deployment, nil
}
50 changes: 50 additions & 0 deletions apps/workspace-engine/pkg/getters/environment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package getters

import (
"context"
"fmt"
"workspace-engine/pkg/db"
"workspace-engine/pkg/oapi"
"workspace-engine/pkg/workspace/store"

"github.com/google/uuid"
)

type EnvironmentGetter interface {
GetEnvironment(ctx context.Context, environmentID string) (*oapi.Environment, error)
}

var _ EnvironmentGetter = (*PostgresEnvironmentGetter)(nil)

type PostgresEnvironmentGetter struct {
queries *db.Queries
}

func NewPostgresEnvironmentGetter(queries *db.Queries) *PostgresEnvironmentGetter {
return &PostgresEnvironmentGetter{queries: queries}
}

// GetEnvironment implements [EnvironmentGetter].
func (e *PostgresEnvironmentGetter) GetEnvironment(ctx context.Context, environmentID string) (*oapi.Environment, error) {
environment, err := e.queries.GetEnvironmentByID(ctx, uuid.MustParse(environmentID))
if err != nil {
return nil, err
}
return db.ToOapiEnvironment(environment), nil
}

type StoreEnvironmentGetter struct {
store *store.Store
}

func NewStoreEnvironmentGetter(store *store.Store) *StoreEnvironmentGetter {
return &StoreEnvironmentGetter{store: store}
}

func (s *StoreEnvironmentGetter) GetEnvironment(ctx context.Context, environmentID string) (*oapi.Environment, error) {
environment, ok := s.store.Environments.Get(environmentID)
if !ok {
return nil, fmt.Errorf("environment not found")
}
return environment, nil
}
50 changes: 50 additions & 0 deletions apps/workspace-engine/pkg/getters/resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package getters

import (
"context"
"fmt"
"workspace-engine/pkg/db"
"workspace-engine/pkg/oapi"
"workspace-engine/pkg/workspace/store"

"github.com/google/uuid"
)

type ResourceGetter interface {
GetResource(ctx context.Context, resourceID string) (*oapi.Resource, error)
}

var _ ResourceGetter = (*PostgresResourceGetter)(nil)

type PostgresResourceGetter struct {
queries *db.Queries
}

func NewPostgresResourceGetter(queries *db.Queries) *PostgresResourceGetter {
return &PostgresResourceGetter{queries: queries}
}

// GetResource implements [ResourceGetter].
func (r *PostgresResourceGetter) GetResource(ctx context.Context, resourceID string) (*oapi.Resource, error) {
resource, err := r.queries.GetResourceByID(ctx, uuid.MustParse(resourceID))
if err != nil {
return nil, err
}
return db.ToOapiResource(resource), nil
}

type StoreResourceGetter struct {
store *store.Store
}

func NewStoreResourceGetter(store *store.Store) *StoreResourceGetter {
return &StoreResourceGetter{store: store}
}

func (s *StoreResourceGetter) GetResource(ctx context.Context, resourceID string) (*oapi.Resource, error) {
resource, ok := s.store.Resources.Get(resourceID)
if !ok {
return nil, fmt.Errorf("resource not found")
}
return resource, nil
}
44 changes: 2 additions & 42 deletions apps/workspace-engine/pkg/workspace/relationships/eval/evaluate.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,27 +119,18 @@ func EvaluateRule(
// candidates via the provided loader. Returns all matches across all rules.
func EvaluateRules(
ctx context.Context,
loader CandidateLoader,
entity *EntityData,
candidates []EntityData,
rules []Rule,
) ([]Match, error) {
ctx, span := tracer.Start(ctx, "eval.EvaluateRules")
defer span.End()

var allCandidates []EntityData
for _, entityType := range knownEntityTypes {
candidates, err := loader.LoadCandidates(ctx, entity.WorkspaceID, entityType)
if err != nil {
return nil, fmt.Errorf("load candidates for entity %s (type %s): %w", entity.ID, entityType, err)
}
allCandidates = append(allCandidates, candidates...)
}

var allMatches []Match
for i := range rules {
rule := &rules[i]

matches, err := EvaluateRule(ctx, entity, rule, allCandidates)
matches, err := EvaluateRule(ctx, entity, rule, candidates)
if err != nil {
return nil, fmt.Errorf("evaluate rule %s: %w", rule.ID, err)
}
Expand All @@ -149,34 +140,3 @@ func EvaluateRules(
span.SetAttributes(attribute.Int("matches.total", len(allMatches)))
return allMatches, nil
}

// ResolveForReference finds all entities related to entity through rules
// matching the given reference name. Only rules whose Reference field
// matches are evaluated, keeping the candidate search targeted.
func ResolveForReference(
ctx context.Context,
loader CandidateLoader,
entity *EntityData,
rules []Rule,
reference string,
) ([]Match, error) {
ctx, span := tracer.Start(ctx, "eval.ResolveForReference")
defer span.End()
span.SetAttributes(
attribute.String("reference", reference),
attribute.String("entity.id", entity.ID.String()),
)

filtered := make([]Rule, 0, len(rules))
for _, r := range rules {
if r.Reference == reference {
filtered = append(filtered, r)
}
}

if len(filtered) == 0 {
return nil, nil
}

return EvaluateRules(ctx, loader, entity, filtered)
}
Loading
Loading