2018-07-25 19:52:17 +00:00
|
|
|
package endpoints
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2018-09-10 10:01:38 +00:00
|
|
|
httperror "github.com/portainer/libhttp/error"
|
2018-11-13 03:02:49 +00:00
|
|
|
"github.com/portainer/libhttp/request"
|
2018-09-10 10:01:38 +00:00
|
|
|
"github.com/portainer/libhttp/response"
|
2019-03-21 01:20:14 +00:00
|
|
|
"github.com/portainer/portainer/api"
|
2018-07-25 19:52:17 +00:00
|
|
|
)
|
|
|
|
|
2018-11-13 03:02:49 +00:00
|
|
|
// POST request on /api/endpoints/:id/snapshot
|
2018-07-25 19:52:17 +00:00
|
|
|
func (handler *Handler) endpointSnapshot(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
|
2018-11-13 03:02:49 +00:00
|
|
|
endpointID, err := request.RetrieveNumericRouteVariableValue(r, "id")
|
2018-07-25 19:52:17 +00:00
|
|
|
if err != nil {
|
2018-11-13 03:02:49 +00:00
|
|
|
return &httperror.HandlerError{http.StatusBadRequest, "Invalid endpoint identifier route variable", err}
|
2018-07-25 19:52:17 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 05:23:15 +00:00
|
|
|
endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID))
|
2018-11-13 03:02:49 +00:00
|
|
|
if err == portainer.ErrObjectNotFound {
|
|
|
|
return &httperror.HandlerError{http.StatusNotFound, "Unable to find an endpoint with the specified identifier inside the database", err}
|
|
|
|
} else if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find an endpoint with the specified identifier inside the database", err}
|
|
|
|
}
|
|
|
|
|
|
|
|
snapshot, snapshotError := handler.Snapshotter.CreateSnapshot(endpoint)
|
|
|
|
|
2020-05-20 05:23:15 +00:00
|
|
|
latestEndpointReference, err := handler.DataStore.Endpoint().Endpoint(endpoint.ID)
|
2018-11-13 03:02:49 +00:00
|
|
|
if latestEndpointReference == nil {
|
|
|
|
return &httperror.HandlerError{http.StatusNotFound, "Unable to find an endpoint with the specified identifier inside the database", err}
|
|
|
|
}
|
|
|
|
|
|
|
|
latestEndpointReference.Status = portainer.EndpointStatusUp
|
|
|
|
if snapshotError != nil {
|
|
|
|
latestEndpointReference.Status = portainer.EndpointStatusDown
|
|
|
|
}
|
|
|
|
|
|
|
|
if snapshot != nil {
|
|
|
|
latestEndpointReference.Snapshots = []portainer.Snapshot{*snapshot}
|
|
|
|
}
|
|
|
|
|
2020-05-20 05:23:15 +00:00
|
|
|
err = handler.DataStore.Endpoint().UpdateEndpoint(latestEndpointReference.ID, latestEndpointReference)
|
2018-11-13 03:02:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist endpoint changes inside the database", err}
|
2018-07-25 19:52:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return response.Empty(w)
|
|
|
|
}
|