From 81200731040fe6f4173bea96842e60b5854bcf0f Mon Sep 17 00:00:00 2001 From: Matt Siwiec Date: Mon, 28 Aug 2023 16:25:07 +0000 Subject: [PATCH] add location to manual hook Signed-off-by: Matt Siwiec --- go.mod | 2 +- internal/ent/schema/port.go | 1 - internal/manualhooks/hooks.go | 139 ++++++++++++++++++++----- x/pubsubinfo/template/event_hooks.tmpl | 42 +++++++- 4 files changed, 155 insertions(+), 29 deletions(-) diff --git a/go.mod b/go.mod index ffa2caa0c..3e6beee75 100644 --- a/go.mod +++ b/go.mod @@ -25,6 +25,7 @@ require ( go.infratographer.com/permissions-api v0.2.4 go.infratographer.com/x v0.3.7 go.uber.org/zap v1.25.0 + golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63 ) require ( @@ -137,7 +138,6 @@ require ( go.step.sm/crypto v0.31.2 // indirect go.uber.org/multierr v1.11.0 // indirect golang.org/x/crypto v0.12.0 // indirect - golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63 // indirect golang.org/x/mod v0.12.0 // indirect golang.org/x/net v0.14.0 // indirect golang.org/x/oauth2 v0.10.0 // indirect diff --git a/internal/ent/schema/port.go b/internal/ent/schema/port.go index 96e8a2a42..b13e64c2f 100644 --- a/internal/ent/schema/port.go +++ b/internal/ent/schema/port.go @@ -57,7 +57,6 @@ func (Port) Fields() []ent.Field { Annotations( entgql.Type("ID"), entgql.Skip(entgql.SkipWhereInput, entgql.SkipMutationUpdateInput), - // pubsubinfo.EventsHookAdditionalSubject("loadbalancer"), ), } } diff --git a/internal/manualhooks/hooks.go b/internal/manualhooks/hooks.go index ccf06c4d3..f756175e6 100644 --- a/internal/manualhooks/hooks.go +++ b/internal/manualhooks/hooks.go @@ -17,6 +17,7 @@ import ( "go.infratographer.com/load-balancer-api/internal/ent/generated/origin" "go.infratographer.com/load-balancer-api/internal/ent/generated/pool" "go.infratographer.com/load-balancer-api/internal/ent/generated/port" + "go.infratographer.com/load-balancer-api/internal/ent/schema" ) func LoadBalancerHooks() []ent.Hook { @@ -146,14 +147,6 @@ func LoadBalancerHooks() []ent.Hook { cv_location_id := "" location_id, ok := m.LocationID() - if !ok && !m.Op().Is(ent.OpCreate) { - // since we are doing an update or delete and these fields didn't change, load the "old" value - location_id, err = m.OldLocationID(ctx) - if err != nil { - return nil, err - } - } - additionalSubjects = append(additionalSubjects, location_id) if ok { cv_location_id = fmt.Sprintf("%s", fmt.Sprint(location_id)) @@ -176,14 +169,6 @@ func LoadBalancerHooks() []ent.Hook { cv_provider_id := "" provider_id, ok := m.ProviderID() - if !ok && !m.Op().Is(ent.OpCreate) { - // since we are doing an update or delete and these fields didn't change, load the "old" value - provider_id, err = m.OldProviderID(ctx) - if err != nil { - return nil, err - } - } - additionalSubjects = append(additionalSubjects, provider_id) if ok { cv_provider_id = fmt.Sprintf("%s", fmt.Sprint(provider_id)) @@ -204,6 +189,18 @@ func LoadBalancerHooks() []ent.Hook { }) } + lb_lookup := getLoadBalancerID(ctx, objID, additionalSubjects) + if lb_lookup != "" { + lb, err := m.Client().LoadBalancer.Get(ctx, lb_lookup) + if err != nil { + return nil, fmt.Errorf("unable to lookup location %s", lb_lookup) + } + + if !slices.Contains(additionalSubjects, lb.LocationID) { + additionalSubjects = append(additionalSubjects, lb.LocationID) + } + } + msg := events.ChangeMessage{ EventType: eventType(m.Op()), SubjectID: objID, @@ -251,15 +248,13 @@ func LoadBalancerHooks() []ent.Hook { return nil, fmt.Errorf("failed to load object to get values for event, err %w", err) } + additionalSubjects = append(additionalSubjects, dbObj.OwnerID) + relationships = append(relationships, events.AuthRelationshipRelation{ Relation: "owner", SubjectID: dbObj.OwnerID, }) - additionalSubjects = append(additionalSubjects, dbObj.OwnerID) - additionalSubjects = append(additionalSubjects, dbObj.LocationID) - additionalSubjects = append(additionalSubjects, dbObj.ProviderID) - // we have all the info we need, now complete the mutation before we process the event retValue, err := next.Mutate(ctx, m) if err != nil { @@ -272,6 +267,18 @@ func LoadBalancerHooks() []ent.Hook { } } + lb_lookup := getLoadBalancerID(ctx, objID, additionalSubjects) + if lb_lookup != "" { + lb, err := m.Client().LoadBalancer.Get(ctx, lb_lookup) + if err != nil { + return nil, fmt.Errorf("unable to lookup location %s", lb_lookup) + } + + if !slices.Contains(additionalSubjects, lb.LocationID) { + additionalSubjects = append(additionalSubjects, lb.LocationID) + } + } + msg := events.ChangeMessage{ EventType: eventType(m.Op()), SubjectID: objID, @@ -492,6 +499,18 @@ func OriginHooks() []ent.Hook { }) } + lb_lookup := getLoadBalancerID(ctx, objID, additionalSubjects) + if lb_lookup != "" { + lb, err := m.Client().LoadBalancer.Get(ctx, lb_lookup) + if err != nil { + return nil, fmt.Errorf("unable to lookup location %s", lb_lookup) + } + + if !slices.Contains(additionalSubjects, lb.LocationID) { + additionalSubjects = append(additionalSubjects, lb.LocationID) + } + } + msg := events.ChangeMessage{ EventType: eventType(m.Op()), SubjectID: objID, @@ -558,6 +577,18 @@ func OriginHooks() []ent.Hook { } } + lb_lookup := getLoadBalancerID(ctx, objID, additionalSubjects) + if lb_lookup != "" { + lb, err := m.Client().LoadBalancer.Get(ctx, lb_lookup) + if err != nil { + return nil, fmt.Errorf("unable to lookup location %s", lb_lookup) + } + + if !slices.Contains(additionalSubjects, lb.LocationID) { + additionalSubjects = append(additionalSubjects, lb.LocationID) + } + } + msg := events.ChangeMessage{ EventType: eventType(m.Op()), SubjectID: objID, @@ -737,6 +768,18 @@ func PoolHooks() []ent.Hook { }) } + lb_lookup := getLoadBalancerID(ctx, objID, additionalSubjects) + if lb_lookup != "" { + lb, err := m.Client().LoadBalancer.Get(ctx, lb_lookup) + if err != nil { + return nil, fmt.Errorf("unable to lookup location %s", lb_lookup) + } + + if !slices.Contains(additionalSubjects, lb.LocationID) { + additionalSubjects = append(additionalSubjects, lb.LocationID) + } + } + msg := events.ChangeMessage{ EventType: eventType(m.Op()), SubjectID: objID, @@ -810,6 +853,18 @@ func PoolHooks() []ent.Hook { } } + lb_lookup := getLoadBalancerID(ctx, objID, additionalSubjects) + if lb_lookup != "" { + lb, err := m.Client().LoadBalancer.Get(ctx, lb_lookup) + if err != nil { + return nil, fmt.Errorf("unable to lookup location %s", lb_lookup) + } + + if !slices.Contains(additionalSubjects, lb.LocationID) { + additionalSubjects = append(additionalSubjects, lb.LocationID) + } + } + msg := events.ChangeMessage{ EventType: eventType(m.Op()), SubjectID: objID, @@ -987,6 +1042,18 @@ func PortHooks() []ent.Hook { }) } + lb_lookup := getLoadBalancerID(ctx, objID, additionalSubjects) + if lb_lookup != "" { + lb, err := m.Client().LoadBalancer.Get(ctx, lb_lookup) + if err != nil { + return nil, fmt.Errorf("unable to lookup location %s", lb_lookup) + } + + if !slices.Contains(additionalSubjects, lb.LocationID) { + additionalSubjects = append(additionalSubjects, lb.LocationID) + } + } + msg := events.ChangeMessage{ EventType: eventType(m.Op()), SubjectID: objID, @@ -1036,11 +1103,6 @@ func PortHooks() []ent.Hook { additionalSubjects = append(additionalSubjects, dbObj.LoadBalancerID) - // relationships = append(relationships, events.AuthRelationshipRelation{ - // Relation: "owner", - // SubjectID: dbObj, - // }) - addSubjLoadBalancer, err := m.Client().LoadBalancer.Get(ctx, dbObj.LoadBalancerID) if err != nil { if !slices.Contains(additionalSubjects, addSubjLoadBalancer.LocationID) { @@ -1068,6 +1130,18 @@ func PortHooks() []ent.Hook { } } + lb_lookup := getLoadBalancerID(ctx, objID, additionalSubjects) + if lb_lookup != "" { + lb, err := m.Client().LoadBalancer.Get(ctx, lb_lookup) + if err != nil { + return nil, fmt.Errorf("unable to lookup location %s", lb_lookup) + } + + if !slices.Contains(additionalSubjects, lb.LocationID) { + additionalSubjects = append(additionalSubjects, lb.LocationID) + } + } + msg := events.ChangeMessage{ EventType: eventType(m.Op()), SubjectID: objID, @@ -1096,7 +1170,6 @@ func PubsubHooks(c *generated.Client) { c.Pool.Use(PoolHooks()...) c.Port.Use(PortHooks()...) - } func eventType(op ent.Op) string { @@ -1111,3 +1184,17 @@ func eventType(op ent.Op) string { return "unknown" } } + +func getLoadBalancerID(ctx context.Context, id gidx.PrefixedID, addID []gidx.PrefixedID) gidx.PrefixedID { + if id.Prefix() == schema.LoadBalancerPrefix { + return id + } + + for _, id := range addID { + if id.Prefix() == schema.LoadBalancerPrefix { + return id + } + } + + return "" +} diff --git a/x/pubsubinfo/template/event_hooks.tmpl b/x/pubsubinfo/template/event_hooks.tmpl index a3cb55f33..7b419899f 100644 --- a/x/pubsubinfo/template/event_hooks.tmpl +++ b/x/pubsubinfo/template/event_hooks.tmpl @@ -7,7 +7,10 @@ {{ $genPackage := base $.Config.Package }} - import "go.infratographer.com/permissions-api/pkg/permissions" + import ( + "go.infratographer.com/permissions-api/pkg/permissions" + "golang.org/x/exp/slices" + ) {{- range $node := $.Nodes }} {{- if $nodeAnnotation := $node.Annotations.INFRA9_EVENTHOOKS }} @@ -113,6 +116,18 @@ {{ end }} {{ end }} + lb_lookup := getLoadBalancerID(ctx, objID, additionalSubjects) + if lb_lookup != "" { + lb, err := m.Client().LoadBalancer.Get(ctx, lb_lookup) + if err != nil { + return nil, fmt.Errorf("unable to lookup location %s", lb_lookup) + } + + if !slices.Contains(additionalSubjects, lb.LocationID) { + additionalSubjects = append(additionalSubjects, lb.LocationID) + } + } + msg := events.ChangeMessage{ EventType: eventType(m.Op()), SubjectID: objID, @@ -196,6 +211,18 @@ } } + lb_lookup := getLoadBalancerID(ctx, objID, additionalSubjects) + if lb_lookup != "" { + lb, err := m.Client().LoadBalancer.Get(ctx, lb_lookup) + if err != nil { + return nil, fmt.Errorf("unable to lookup location %s", lb_lookup) + } + + if !slices.Contains(additionalSubjects, lb.LocationID) { + additionalSubjects = append(additionalSubjects, lb.LocationID) + } + } + msg := events.ChangeMessage{ EventType: eventType(m.Op()), SubjectID: objID, @@ -240,5 +267,18 @@ } } + func getLoadBalancerID(ctx context.Context, id gidx.PrefixedID, addID []gidx.PrefixedID) (gidx.PrefixedID) { + if id.Prefix() == schema.LoadBalancerPrefix { + return id + } + + for _, id := range addID { + if id.Prefix() == schema.LoadBalancerPrefix { + return id + } + } + + return "" + } {{ end }}