2023-07-03 01:41:13 +02:00
|
|
|
package projects
|
|
|
|
|
|
|
|
import (
|
|
|
|
// "strconv"
|
|
|
|
"fmt"
|
2024-01-11 14:03:09 +01:00
|
|
|
"net/http"
|
|
|
|
|
2023-07-03 01:41:13 +02:00
|
|
|
"github.com/ansible-semaphore/semaphore/api/helpers"
|
|
|
|
"github.com/ansible-semaphore/semaphore/db"
|
|
|
|
"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) {
|
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-02-11 20:52:14 +01:00
|
|
|
extractor := context.Get(r, "extractor").(db.IntegrationExtractor)
|
|
|
|
var matcher db.IntegrationMatcher
|
2024-03-04 12:36:24 +01:00
|
|
|
matcher, err = helpers.Store(r).GetIntegrationMatcher(0, matcher_id, extractor.ID)
|
2024-02-10 18:18:56 +01:00
|
|
|
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) {
|
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-02-11 20:52:14 +01:00
|
|
|
extractor := context.Get(r, "extractor").(db.IntegrationExtractor)
|
|
|
|
var matcher db.IntegrationMatcher
|
2024-03-04 12:36:24 +01:00
|
|
|
matcher, err = helpers.Store(r).GetIntegrationMatcher(0, matcher_id, extractor.ID)
|
2024-02-10 18:18:56 +01:00
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
2023-07-03 01:41:13 +02:00
|
|
|
|
2024-03-04 12:36:24 +01:00
|
|
|
refs, err := helpers.Store(r).GetIntegrationMatcherRefs(0, matcher.ID, matcher.ExtractorID)
|
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) {
|
|
|
|
extractor := context.Get(r, "extractor").(db.IntegrationExtractor)
|
2023-07-03 01:41:13 +02:00
|
|
|
|
2024-03-04 12:36:24 +01:00
|
|
|
matchers, err := helpers.Store(r).GetIntegrationMatchers(0, helpers.QueryParams(r.URL), extractor.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) {
|
|
|
|
extractor := context.Get(r, "extractor").(db.IntegrationExtractor)
|
|
|
|
var matcher db.IntegrationMatcher
|
2023-07-03 01:41:13 +02:00
|
|
|
if !helpers.Bind(w, r, &matcher) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if matcher.ExtractorID != extractor.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 12:36:24 +01:00
|
|
|
newMatcher, err := helpers.Store(r).CreateIntegrationMatcher(0, 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) {
|
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-02-11 20:52:14 +01:00
|
|
|
extractor := context.Get(r, "extractor").(db.IntegrationExtractor)
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info(fmt.Sprintf("Updating API Matcher %v for Extractor %v, matcher ID: %v", matcher_id, extractor.ID, matcher.ID))
|
|
|
|
|
2024-03-04 12:36:24 +01:00
|
|
|
err = helpers.Store(r).UpdateIntegrationMatcher(0, 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) {
|
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-02-11 20:52:14 +01:00
|
|
|
extractor := context.Get(r, "extractor").(db.IntegrationExtractor)
|
|
|
|
var matcher db.IntegrationMatcher
|
2024-03-04 12:36:24 +01:00
|
|
|
matcher, err = helpers.Store(r).GetIntegrationMatcher(0, matcher_id, extractor.ID)
|
2024-02-10 18:18:56 +01:00
|
|
|
if err != nil {
|
|
|
|
helpers.WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
2023-07-03 01:41:13 +02:00
|
|
|
|
2024-03-04 12:36:24 +01:00
|
|
|
err = helpers.Store(r).DeleteIntegrationMatcher(0, matcher.ID, extractor.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)
|
|
|
|
}
|