From 67d531c7465256a0468e37fb8ce1cf5dad796a61 Mon Sep 17 00:00:00 2001 From: Julien Perrochet Date: Tue, 13 Aug 2024 09:19:20 +0200 Subject: [PATCH] rename and make subscriptionCanBeRemoved more specific --- pkg/scd/operational_intents_handler.go | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/pkg/scd/operational_intents_handler.go b/pkg/scd/operational_intents_handler.go index f94d31c5c..533b6f5b2 100644 --- a/pkg/scd/operational_intents_handler.go +++ b/pkg/scd/operational_intents_handler.go @@ -16,16 +16,18 @@ import ( "github.com/interuss/stacktrace" ) -// subscriptionCanBeRemoved will check if: -// - a previous subscription was attached, -// - if so, if it was an implicit subscription -// - if so, if we can remove it after creating the new implicit subscription +// subscriptionIsOnlyAttachedToOIR will check if: +// - the subscription exists and is implicit +// - the subscription is attached to the specified operational intent +// - the subscription is not attached to any other operational intent +// +// This is to be used in contexts where an implicit subscription may need to be cleaned up: if true is returned, +// the subscription can be safely removed after the operational intent is deleted or attached to another subscription. // -// This is to be used in contexts where an implicit subscription may need to be cleaned up. // NOTE: this should eventually be pushed down to CRDB as part of the queries being executed in the callers of this method. // // See https://github.com/interuss/dss/issues/1059 for more details -func subscriptionCanBeRemoved(ctx context.Context, r repos.Repository, subscriptionID *dssmodels.ID) (bool, error) { +func subscriptionIsOnlyAttachedToOIR(ctx context.Context, r repos.Repository, oirID, subscriptionID *dssmodels.ID) (bool, error) { // Get the Subscription supporting the OperationalIntent, if one is defined if subscriptionID != nil { sub, err := r.GetSubscription(ctx, *subscriptionID) @@ -44,7 +46,7 @@ func subscriptionCanBeRemoved(ctx context.Context, r repos.Repository, subscript } if len(dependentOps) == 0 { return false, stacktrace.NewError("An implicit Subscription had no dependent OperationalIntents") - } else if len(dependentOps) == 1 { + } else if len(dependentOps) == 1 && dependentOps[0] == *oirID { return true, nil } } @@ -92,7 +94,7 @@ func (a *Server) DeleteOperationalIntentReference(ctx context.Context, req *rest "OperationalIntent owned by %s, but %s attempted to delete", old.Manager, *req.Auth.ClientID) } - removeImplicitSubscription, err := subscriptionCanBeRemoved(ctx, r, old.SubscriptionID) + removeImplicitSubscription, err := subscriptionIsOnlyAttachedToOIR(ctx, r, &id, old.SubscriptionID) if err != nil { return stacktrace.Propagate(err, "Could not determine if Subscription can be removed") } @@ -509,7 +511,7 @@ func (a *Server) upsertOperationalIntentReference(ctx context.Context, authorize } } - removePreviousImplicitSubscription, err = subscriptionCanBeRemoved(ctx, r, previousSubscriptionID) + removePreviousImplicitSubscription, err = subscriptionIsOnlyAttachedToOIR(ctx, r, &id, previousSubscriptionID) if err != nil { return stacktrace.Propagate(err, "Could not determine if previous Subscription can be removed") }