2018-06-11 13:13:19 +00:00
|
|
|
package endpoints
|
|
|
|
|
|
|
|
import (
|
2023-07-17 20:36:00 +00:00
|
|
|
"errors"
|
2018-06-11 13:13:19 +00:00
|
|
|
"net/http"
|
2023-09-06 01:35:16 +00:00
|
|
|
"slices"
|
2018-06-11 13:13:19 +00:00
|
|
|
"strconv"
|
|
|
|
|
2021-02-23 03:21:39 +00:00
|
|
|
portainer "github.com/portainer/portainer/api"
|
2023-07-17 20:36:00 +00:00
|
|
|
"github.com/portainer/portainer/api/dataservices"
|
2022-05-22 05:34:09 +00:00
|
|
|
httperrors "github.com/portainer/portainer/api/http/errors"
|
2023-01-17 12:47:23 +00:00
|
|
|
"github.com/portainer/portainer/api/internal/endpointutils"
|
2023-09-01 22:27:02 +00:00
|
|
|
httperror "github.com/portainer/portainer/pkg/libhttp/error"
|
|
|
|
"github.com/portainer/portainer/pkg/libhttp/request"
|
|
|
|
"github.com/portainer/portainer/pkg/libhttp/response"
|
2023-07-17 20:36:00 +00:00
|
|
|
|
2023-07-12 01:20:13 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2018-06-11 13:13:19 +00:00
|
|
|
)
|
|
|
|
|
2021-02-23 03:21:39 +00:00
|
|
|
// @id EndpointDelete
|
2021-09-20 00:14:22 +00:00
|
|
|
// @summary Remove an environment(endpoint)
|
|
|
|
// @description Remove an environment(endpoint).
|
2021-02-23 03:21:39 +00:00
|
|
|
// @description **Access policy**: administrator
|
|
|
|
// @tags endpoints
|
2021-11-30 02:31:16 +00:00
|
|
|
// @security ApiKeyAuth
|
2021-02-23 03:21:39 +00:00
|
|
|
// @security jwt
|
2021-09-20 00:14:22 +00:00
|
|
|
// @param id path int true "Environment(Endpoint) identifier"
|
2021-02-23 03:21:39 +00:00
|
|
|
// @success 204 "Success"
|
|
|
|
// @failure 400 "Invalid request"
|
2023-07-12 01:20:13 +00:00
|
|
|
// @failure 403 "Permission denied"
|
2021-09-20 00:14:22 +00:00
|
|
|
// @failure 404 "Environment(Endpoint) not found"
|
2021-02-23 03:21:39 +00:00
|
|
|
// @failure 500 "Server error"
|
|
|
|
// @router /endpoints/{id} [delete]
|
2018-06-11 13:13:19 +00:00
|
|
|
func (handler *Handler) endpointDelete(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
|
|
|
|
endpointID, err := request.RetrieveNumericRouteVariableValue(r, "id")
|
|
|
|
if err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.BadRequest("Invalid environment identifier route variable", err)
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
2023-07-17 20:36:00 +00:00
|
|
|
// This is a Portainer provisioned cloud environment
|
|
|
|
deleteCluster, err := request.RetrieveBooleanQueryParameter(r, "deleteCluster", true)
|
|
|
|
if err != nil {
|
|
|
|
return httperror.BadRequest("Invalid boolean query parameter", err)
|
|
|
|
}
|
|
|
|
|
2022-05-22 05:34:09 +00:00
|
|
|
if handler.demoService.IsDemoEnvironment(portainer.EndpointID(endpointID)) {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.Forbidden(httperrors.ErrNotAvailableInDemo.Error(), httperrors.ErrNotAvailableInDemo)
|
2022-05-22 05:34:09 +00:00
|
|
|
}
|
|
|
|
|
2023-09-05 23:27:20 +00:00
|
|
|
err = handler.DataStore.UpdateTx(func(tx dataservices.DataStoreTx) error {
|
|
|
|
return handler.deleteEndpoint(tx, portainer.EndpointID(endpointID), deleteCluster)
|
|
|
|
})
|
2023-07-17 20:36:00 +00:00
|
|
|
if err != nil {
|
|
|
|
var handlerError *httperror.HandlerError
|
|
|
|
if errors.As(err, &handlerError) {
|
|
|
|
return handlerError
|
|
|
|
}
|
|
|
|
|
|
|
|
return httperror.InternalServerError("Unexpected error", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.Empty(w)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (handler *Handler) deleteEndpoint(tx dataservices.DataStoreTx, endpointID portainer.EndpointID, deleteCluster bool) error {
|
|
|
|
endpoint, err := tx.Endpoint().Endpoint(portainer.EndpointID(endpointID))
|
|
|
|
if tx.IsErrObjectNotFound(err) {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.NotFound("Unable to find an environment with the specified identifier inside the database", err)
|
2018-06-11 13:13:19 +00:00
|
|
|
} else if err != nil {
|
2023-07-12 01:20:13 +00:00
|
|
|
return httperror.InternalServerError("Unable to read the environment record from the database", err)
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if endpoint.TLSConfig.TLS {
|
2023-07-17 20:36:00 +00:00
|
|
|
folder := strconv.Itoa(int(endpointID))
|
2018-06-11 13:13:19 +00:00
|
|
|
err = handler.FileService.DeleteTLSFiles(folder)
|
|
|
|
if err != nil {
|
2023-07-12 01:20:13 +00:00
|
|
|
log.Error().Err(err).Msgf("Unable to remove TLS files from disk when deleting endpoint %d", endpointID)
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-17 20:36:00 +00:00
|
|
|
err = tx.Snapshot().Delete(endpointID)
|
2022-11-07 23:28:18 +00:00
|
|
|
if err != nil {
|
2023-07-12 01:20:13 +00:00
|
|
|
log.Warn().Err(err).Msgf("Unable to remove the snapshot from the database")
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
2021-10-15 05:13:20 +00:00
|
|
|
handler.ProxyManager.DeleteEndpointProxy(endpoint.ID)
|
2018-06-11 13:13:19 +00:00
|
|
|
|
2023-07-17 20:36:00 +00:00
|
|
|
if len(endpoint.UserAccessPolicies) > 0 || len(endpoint.TeamAccessPolicies) > 0 {
|
|
|
|
err = handler.AuthorizationService.UpdateUsersAuthorizationsTx(tx)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn().Err(err).Msgf("Unable to update user authorizations")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tx.EndpointRelation().DeleteEndpointRelation(endpoint.ID)
|
2020-05-14 02:14:28 +00:00
|
|
|
if err != nil {
|
2023-07-12 01:20:13 +00:00
|
|
|
log.Warn().Err(err).Msgf("Unable to remove environment relation from the database")
|
2020-05-14 02:14:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tagID := range endpoint.TagIDs {
|
2023-09-05 23:27:20 +00:00
|
|
|
var tag *portainer.Tag
|
|
|
|
tag, err = tx.Tag().Read(tagID)
|
|
|
|
if err == nil {
|
|
|
|
delete(tag.Endpoints, endpoint.ID)
|
|
|
|
err = tx.Tag().Update(tagID, tag)
|
2023-07-17 20:36:00 +00:00
|
|
|
}
|
2020-05-14 02:14:28 +00:00
|
|
|
|
2022-10-13 14:12:12 +00:00
|
|
|
if handler.DataStore.IsErrObjectNotFound(err) {
|
2023-07-12 01:20:13 +00:00
|
|
|
log.Warn().Err(err).Msgf("Unable to find tag inside the database")
|
2022-10-13 14:12:12 +00:00
|
|
|
} else if err != nil {
|
2023-07-12 01:20:13 +00:00
|
|
|
log.Warn().Err(err).Msgf("Unable to delete tag relation from the database")
|
2020-05-14 02:14:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-17 20:36:00 +00:00
|
|
|
edgeGroups, err := tx.EdgeGroup().ReadAll()
|
2020-05-14 02:14:28 +00:00
|
|
|
if err != nil {
|
2023-07-12 01:20:13 +00:00
|
|
|
log.Warn().Err(err).Msgf("Unable to retrieve edge groups from the database")
|
2020-05-14 02:14:28 +00:00
|
|
|
}
|
|
|
|
|
2022-11-24 00:36:17 +00:00
|
|
|
for _, edgeGroup := range edgeGroups {
|
2023-09-06 01:35:16 +00:00
|
|
|
edgeGroup.Endpoints = slices.DeleteFunc(edgeGroup.Endpoints, func(e portainer.EndpointID) bool {
|
|
|
|
return e == endpoint.ID
|
|
|
|
})
|
2023-07-17 20:36:00 +00:00
|
|
|
|
2023-09-06 01:35:16 +00:00
|
|
|
err = tx.EdgeGroup().Update(edgeGroup.ID, &edgeGroup)
|
2022-11-24 00:36:17 +00:00
|
|
|
if err != nil {
|
2023-07-12 01:20:13 +00:00
|
|
|
log.Warn().Err(err).Msgf("Unable to update edge group")
|
2020-05-14 02:14:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-17 20:36:00 +00:00
|
|
|
edgeStacks, err := tx.EdgeStack().EdgeStacks()
|
2020-05-14 02:14:28 +00:00
|
|
|
if err != nil {
|
2023-07-12 01:20:13 +00:00
|
|
|
log.Warn().Err(err).Msgf("Unable to retrieve edge stacks from the database")
|
2020-05-14 02:14:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for idx := range edgeStacks {
|
|
|
|
edgeStack := &edgeStacks[idx]
|
|
|
|
if _, ok := edgeStack.Status[endpoint.ID]; ok {
|
|
|
|
delete(edgeStack.Status, endpoint.ID)
|
2023-07-17 20:36:00 +00:00
|
|
|
err = tx.EdgeStack().UpdateEdgeStack(edgeStack.ID, edgeStack)
|
2020-05-14 02:14:28 +00:00
|
|
|
if err != nil {
|
2023-07-12 01:20:13 +00:00
|
|
|
log.Warn().Err(err).Msgf("Unable to update edge stack")
|
2020-05-14 02:14:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-17 20:36:00 +00:00
|
|
|
registries, err := tx.Registry().ReadAll()
|
2021-07-14 09:15:21 +00:00
|
|
|
if err != nil {
|
2023-07-12 01:20:13 +00:00
|
|
|
log.Warn().Err(err).Msgf("Unable to retrieve registries from the database")
|
2021-07-14 09:15:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for idx := range registries {
|
|
|
|
registry := ®istries[idx]
|
|
|
|
if _, ok := registry.RegistryAccesses[endpoint.ID]; ok {
|
|
|
|
delete(registry.RegistryAccesses, endpoint.ID)
|
2023-07-17 20:36:00 +00:00
|
|
|
err = tx.Registry().Update(registry.ID, registry)
|
2021-07-14 09:15:21 +00:00
|
|
|
if err != nil {
|
2023-07-12 01:20:13 +00:00
|
|
|
log.Warn().Err(err).Msgf("Unable to update registry accesses")
|
2021-07-14 09:15:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-12 01:20:13 +00:00
|
|
|
if endpointutils.IsEdgeEndpoint(endpoint) {
|
|
|
|
edgeJobs, err := handler.DataStore.EdgeJob().ReadAll()
|
|
|
|
if err != nil {
|
|
|
|
log.Warn().Err(err).Msgf("Unable to retrieve edge jobs from the database")
|
|
|
|
}
|
2023-01-17 12:47:23 +00:00
|
|
|
|
2023-07-12 01:20:13 +00:00
|
|
|
for idx := range edgeJobs {
|
|
|
|
edgeJob := &edgeJobs[idx]
|
|
|
|
if _, ok := edgeJob.Endpoints[endpoint.ID]; ok {
|
2023-09-05 23:27:20 +00:00
|
|
|
delete(edgeJob.Endpoints, endpoint.ID)
|
2023-01-17 12:47:23 +00:00
|
|
|
|
2023-09-05 23:27:20 +00:00
|
|
|
err = tx.EdgeJob().Update(edgeJob.ID, edgeJob)
|
2023-07-12 01:20:13 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Warn().Err(err).Msgf("Unable to update edge job")
|
|
|
|
}
|
2023-01-17 12:47:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-17 20:36:00 +00:00
|
|
|
err = tx.Endpoint().DeleteEndpoint(portainer.EndpointID(endpointID))
|
2023-07-12 01:20:13 +00:00
|
|
|
if err != nil {
|
2023-07-17 20:36:00 +00:00
|
|
|
return httperror.InternalServerError("Unable to delete the environment from the database", err)
|
2023-07-12 01:20:13 +00:00
|
|
|
}
|
|
|
|
|
2023-07-17 20:36:00 +00:00
|
|
|
return nil
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|