Semaphore/api/projects/integrationmatcher.go

171 lines
4.4 KiB
Go
Raw Normal View History

2023-07-03 01:41:13 +02:00
package projects
import (
// "strconv"
"fmt"
2024-01-11 14:03:09 +01:00
"net/http"
"github.com/semaphoreui/semaphore/api/helpers"
"github.com/semaphoreui/semaphore/db"
2023-07-03 01:41:13 +02:00
"github.com/gorilla/context"
2024-03-03 11:57:39 +01:00
log "github.com/sirupsen/logrus"
2023-07-03 01:41:13 +02:00
)
2024-02-11 20:52:14 +01:00
func GetIntegrationMatcher(w http.ResponseWriter, r *http.Request) {
2024-03-04 18:44:15 +01:00
project := context.Get(r, "project").(db.Project)
2023-07-03 01:41:13 +02:00
matcher_id, err := helpers.GetIntParam("matcher_id", w, r)
if err != nil {
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
"error": "Invalid Matcher ID",
})
2024-01-11 14:03:09 +01:00
return
2023-07-03 01:41:13 +02:00
}
2024-01-11 14:03:09 +01:00
2024-03-06 14:28:24 +01:00
integration := context.Get(r, "integration").(db.Integration)
2024-02-11 20:52:14 +01:00
var matcher db.IntegrationMatcher
2024-03-06 14:28:24 +01:00
matcher, err = helpers.Store(r).GetIntegrationMatcher(project.ID, matcher_id, integration.ID)
if err != nil {
helpers.WriteError(w, err)
return
}
2023-07-03 01:41:13 +02:00
helpers.WriteJSON(w, http.StatusOK, matcher)
}
2024-02-11 20:52:14 +01:00
func GetIntegrationMatcherRefs(w http.ResponseWriter, r *http.Request) {
2024-03-04 18:44:15 +01:00
project := context.Get(r, "project").(db.Project)
matcherId, err := helpers.GetIntParam("matcher_id", w, r)
2023-07-03 01:41:13 +02:00
if err != nil {
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
"error": "Invalid Matcher ID",
})
2024-01-11 14:03:09 +01:00
return
2023-07-03 01:41:13 +02:00
}
2024-03-06 14:28:24 +01:00
integration := context.Get(r, "integration").(db.Integration)
2024-02-11 20:52:14 +01:00
var matcher db.IntegrationMatcher
2024-03-06 14:28:24 +01:00
matcher, err = helpers.Store(r).GetIntegrationMatcher(project.ID, matcherId, integration.ID)
if err != nil {
helpers.WriteError(w, err)
return
}
2023-07-03 01:41:13 +02:00
2024-03-06 14:28:24 +01:00
refs, err := helpers.Store(r).GetIntegrationMatcherRefs(project.ID, matcher.ID, matcher.IntegrationID)
2023-07-03 01:41:13 +02:00
if err != nil {
helpers.WriteError(w, err)
return
}
helpers.WriteJSON(w, http.StatusOK, refs)
}
2024-02-11 20:52:14 +01:00
func GetIntegrationMatchers(w http.ResponseWriter, r *http.Request) {
2024-03-04 18:44:15 +01:00
project := context.Get(r, "project").(db.Project)
2024-03-06 14:28:24 +01:00
integration := context.Get(r, "integration").(db.Integration)
2023-07-03 01:41:13 +02:00
2024-03-06 14:28:24 +01:00
matchers, err := helpers.Store(r).GetIntegrationMatchers(project.ID, helpers.QueryParams(r.URL), integration.ID)
2023-07-03 01:41:13 +02:00
if err != nil {
helpers.WriteError(w, err)
return
}
helpers.WriteJSON(w, http.StatusOK, matchers)
}
2024-02-11 20:52:14 +01:00
func AddIntegrationMatcher(w http.ResponseWriter, r *http.Request) {
2024-03-04 18:44:15 +01:00
project := context.Get(r, "project").(db.Project)
2024-03-06 14:28:24 +01:00
integration := context.Get(r, "integration").(db.Integration)
2024-03-04 18:44:15 +01:00
2024-02-11 20:52:14 +01:00
var matcher db.IntegrationMatcher
2023-07-03 01:41:13 +02:00
if !helpers.Bind(w, r, &matcher) {
return
}
2024-03-06 14:28:24 +01:00
if matcher.IntegrationID != integration.ID {
2024-01-11 14:03:09 +01:00
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
2023-07-03 01:41:13 +02:00
"error": "Extractor ID in body and URL must be the same",
})
return
}
err := matcher.Validate()
if err != nil {
2024-01-11 14:03:09 +01:00
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
2023-07-03 01:41:13 +02:00
"error": err.Error(),
2024-01-11 14:03:09 +01:00
})
2023-07-03 01:41:13 +02:00
return
}
2024-03-04 18:44:15 +01:00
newMatcher, err := helpers.Store(r).CreateIntegrationMatcher(project.ID, matcher)
2023-07-03 01:41:13 +02:00
if err != nil {
helpers.WriteError(w, err)
return
}
helpers.WriteJSON(w, http.StatusOK, newMatcher)
}
2024-02-11 20:52:14 +01:00
func UpdateIntegrationMatcher(w http.ResponseWriter, r *http.Request) {
2024-03-04 18:44:15 +01:00
project := context.Get(r, "project").(db.Project)
matcherId, err := helpers.GetIntParam("matcher_id", w, r)
2023-07-03 01:41:13 +02:00
if err != nil {
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
"error": "Invalid Matcher ID",
})
2024-01-11 14:03:09 +01:00
return
2023-07-03 01:41:13 +02:00
}
2024-03-06 14:28:24 +01:00
integration := context.Get(r, "integration").(db.Integration)
2023-07-03 01:41:13 +02:00
2024-02-11 20:52:14 +01:00
var matcher db.IntegrationMatcher
2023-07-03 01:41:13 +02:00
if !helpers.Bind(w, r, &matcher) {
return
}
2024-03-06 14:28:24 +01:00
log.Info(fmt.Sprintf("Updating API Matcher %v for Extractor %v, matcher ID: %v", matcherId, integration.ID, matcher.ID))
2023-07-03 01:41:13 +02:00
2024-03-04 18:44:15 +01:00
err = helpers.Store(r).UpdateIntegrationMatcher(project.ID, matcher)
2023-07-03 01:41:13 +02:00
log.Info(fmt.Sprintf("Err %s", err))
if err != nil {
helpers.WriteError(w, err)
return
}
w.WriteHeader(http.StatusNoContent)
}
2024-02-11 20:52:14 +01:00
func DeleteIntegrationMatcher(w http.ResponseWriter, r *http.Request) {
2024-03-04 18:44:15 +01:00
project := context.Get(r, "project").(db.Project)
matcherId, err := helpers.GetIntParam("matcher_id", w, r)
2023-07-03 01:41:13 +02:00
if err != nil {
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
"error": "Invalid Matcher ID",
})
2024-01-11 14:03:09 +01:00
return
2023-07-03 01:41:13 +02:00
}
2024-03-06 14:28:24 +01:00
integration := context.Get(r, "integration").(db.Integration)
2024-02-11 20:52:14 +01:00
var matcher db.IntegrationMatcher
2024-03-06 14:28:24 +01:00
matcher, err = helpers.Store(r).GetIntegrationMatcher(project.ID, matcherId, integration.ID)
if err != nil {
helpers.WriteError(w, err)
return
}
2023-07-03 01:41:13 +02:00
2024-03-06 14:28:24 +01:00
err = helpers.Store(r).DeleteIntegrationMatcher(project.ID, matcher.ID, integration.ID)
2023-07-03 01:41:13 +02:00
if err == db.ErrInvalidOperation {
helpers.WriteJSON(w, http.StatusBadRequest, map[string]interface{}{
2024-02-11 20:52:14 +01:00
"error": "Integration Matcher failed to be deleted",
2023-07-03 01:41:13 +02:00
})
2024-01-11 14:03:09 +01:00
return
2023-07-03 01:41:13 +02:00
}
w.WriteHeader(http.StatusNoContent)
}