From ca7e4dd66e36d965135f4da90eaced00e4d2ce15 Mon Sep 17 00:00:00 2001 From: Oscar Zhou <100548325+oscarzhou-portainer@users.noreply.github.com> Date: Tue, 5 Nov 2024 09:32:25 +1300 Subject: [PATCH] fix(edge/async): onboarding agent without predefined group cannot be associated [BE-11281] (#83) --- .../handler/endpoints/endpoint_update_relations.go | 6 ++---- api/http/handler/endpoints/update_edge_relations.go | 11 ++++++++++- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/api/http/handler/endpoints/endpoint_update_relations.go b/api/http/handler/endpoints/endpoint_update_relations.go index 1557c3338..fb2787f6a 100644 --- a/api/http/handler/endpoints/endpoint_update_relations.go +++ b/api/http/handler/endpoints/endpoint_update_relations.go @@ -88,13 +88,11 @@ func (handler *Handler) updateRelations(w http.ResponseWriter, r *http.Request) } if updateRelations { - err := tx.Endpoint().UpdateEndpoint(endpoint.ID, endpoint) - if err != nil { + if err := tx.Endpoint().UpdateEndpoint(endpoint.ID, endpoint); err != nil { return errors.WithMessage(err, "Unable to update environment") } - err = handler.updateEdgeRelations(tx, endpoint) - if err != nil { + if err := handler.updateEdgeRelations(tx, endpoint); err != nil { return errors.WithMessage(err, "Unable to update environment relations") } } diff --git a/api/http/handler/endpoints/update_edge_relations.go b/api/http/handler/endpoints/update_edge_relations.go index 6e1aa1861..8bebadedd 100644 --- a/api/http/handler/endpoints/update_edge_relations.go +++ b/api/http/handler/endpoints/update_edge_relations.go @@ -17,7 +17,16 @@ func (handler *Handler) updateEdgeRelations(tx dataservices.DataStoreTx, endpoin relation, err := tx.EndpointRelation().EndpointRelation(endpoint.ID) if err != nil { - return errors.WithMessage(err, "Unable to find environment relation inside the database") + if !tx.IsErrObjectNotFound(err) { + return errors.WithMessage(err, "Unable to retrieve environment relation inside the database") + } + + relation = &portainer.EndpointRelation{ + EndpointID: endpoint.ID, + } + if err := tx.EndpointRelation().Create(relation); err != nil { + return errors.WithMessage(err, "Unable to create environment relation inside the database") + } } endpointGroup, err := tx.EndpointGroup().Read(endpoint.GroupID)