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)