feat(customtemplates): use Sources for CustomTemplates BE-12919 (#2759)

This commit is contained in:
andres-portainer
2026-06-05 01:51:18 -03:00
committed by GitHub
parent a779c839b7
commit 8daf0bb2a9
22 changed files with 2580 additions and 64 deletions
+15 -4
View File
@@ -13,11 +13,11 @@ import (
"github.com/portainer/portainer/pkg/libhttp/response"
)
var ErrSourceInUse = errors.New("source is used by one or more workflows")
var ErrSourceInUse = errors.New("source is used by one or more workflows or custom templates")
// @id GitOpsSourcesDelete
// @summary Delete a source
// @description Deletes an existing GitOps source. Returns 409 if the source is referenced by any workflow.
// @description Deletes an existing GitOps source. Returns 409 if the source is referenced by any workflow or custom template.
// @description **Access policy**: admin
// @tags gitops
// @security ApiKeyAuth
@@ -27,7 +27,7 @@ var ErrSourceInUse = errors.New("source is used by one or more workflows")
// @failure 400 "Invalid request"
// @failure 403 "Access denied"
// @failure 404 "Source not found"
// @failure 409 "Source is in use by one or more workflows"
// @failure 409 "Source is in use by one or more workflows or custom templates"
// @failure 500 "Server error"
// @router /gitops/sources/{id} [delete]
func (h *Handler) sourceDelete(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
@@ -56,11 +56,22 @@ func (h *Handler) sourceDelete(w http.ResponseWriter, r *http.Request) *httperro
}
}
templates, err := tx.CustomTemplate().ReadAll(func(t portainer.CustomTemplate) bool {
return t.ArtifactSources != nil && slices.Contains(t.ArtifactSources.SourceIDs, portainer.SourceID(sourceID))
})
if err != nil {
return err
}
if len(templates) > 0 {
return ErrSourceInUse
}
return tx.Source().Delete(portainer.SourceID(sourceID))
}); h.dataStore.IsErrObjectNotFound(err) {
return httperror.NotFound("Unable to find a source with the specified identifier", err)
} else if errors.Is(err, ErrSourceInUse) {
return httperror.Conflict("Source is used by one or more workflows", err)
return httperror.Conflict("Source is used by one or more workflows or custom templates", err)
} else if err != nil {
return httperror.InternalServerError("Unable to delete source", err)
}