diff --git a/api/http/handler/endpoints/endpoint_create_global_key.go b/api/http/handler/endpoints/endpoint_create_global_key.go new file mode 100644 index 000000000..c37590960 --- /dev/null +++ b/api/http/handler/endpoints/endpoint_create_global_key.go @@ -0,0 +1,39 @@ +package endpoints + +import ( + "net/http" + + httperror "github.com/portainer/libhttp/error" + "github.com/portainer/libhttp/response" + portainer "github.com/portainer/portainer/api" +) + +type endpointCreateGlobalKeyResponse struct { + EndpointID portainer.EndpointID `json:"endpointID"` +} + +// @id EndpointCreateGlobalKey +// @summary Create or retrieve the endpoint for an EdgeID +// @tags endpoints +// @success 200 {object} endpointCreateGlobalKeyResponse "Success" +// @failure 400 "Invalid request" +// @failure 500 "Server error" +// @router /endpoints/global-key [post] +func (handler *Handler) endpointCreateGlobalKey(w http.ResponseWriter, r *http.Request) *httperror.HandlerError { + edgeID := r.Header.Get(portainer.PortainerAgentEdgeIDHeader) + + // Search for existing endpoints for the given edgeID + + endpoints, err := handler.DataStore.Endpoint().Endpoints() + if err != nil { + return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve the endpoints from the database", err} + } + + for _, endpoint := range endpoints { + if endpoint.EdgeID == edgeID { + return response.JSON(w, endpointCreateGlobalKeyResponse{endpoint.ID}) + } + } + + return &httperror.HandlerError{http.StatusNotFound, "Unable to find the endpoint in the database", err} +} diff --git a/api/http/handler/endpoints/handler.go b/api/http/handler/endpoints/handler.go index 6c8adc093..7a0175168 100644 --- a/api/http/handler/endpoints/handler.go +++ b/api/http/handler/endpoints/handler.go @@ -69,6 +69,8 @@ func NewHandler(bouncer *security.RequestBouncer) *Handler { h.Handle("/endpoints/{id}/registries/{registryId}", bouncer.AuthenticatedAccess(httperror.LoggerHandler(h.endpointRegistryAccess))).Methods(http.MethodPut) + h.Handle("/endpoints/global-key", httperror.LoggerHandler(h.endpointCreateGlobalKey)).Methods(http.MethodPost) + // DEPRECATED h.Handle("/endpoints/{id}/status", httperror.LoggerHandler(h.endpointStatusInspect)).Methods(http.MethodGet)