Skip to content

Commit

Permalink
add location to manual hook
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Siwiec <[email protected]>
  • Loading branch information
rizzza committed Aug 28, 2023
1 parent ba9529f commit 8120073
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 29 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion internal/ent/schema/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ func (Port) Fields() []ent.Field {
Annotations(
entgql.Type("ID"),
entgql.Skip(entgql.SkipWhereInput, entgql.SkipMutationUpdateInput),
// pubsubinfo.EventsHookAdditionalSubject("loadbalancer"),
),
}
}
Expand Down
139 changes: 113 additions & 26 deletions internal/manualhooks/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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))
Expand All @@ -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))
Expand All @@ -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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -1096,7 +1170,6 @@ func PubsubHooks(c *generated.Client) {
c.Pool.Use(PoolHooks()...)

c.Port.Use(PortHooks()...)

}

func eventType(op ent.Op) string {
Expand All @@ -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 ""
}
42 changes: 41 additions & 1 deletion x/pubsubinfo/template/event_hooks.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 }}

0 comments on commit 8120073

Please sign in to comment.