Semaphore/api/projects/integrationextractvalue.go

179 lines
4.7 KiB
Go
Raw Normal View History

2023-07-03 01:41:13 +02:00
package projects
import (
"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 GetIntegrationExtractValue(w http.ResponseWriter, r *http.Request) {
2024-03-04 18:53:20 +01:00
project := context.Get(r, "project").(db.Project)
valueId, err := helpers.GetIntParam("value_id", w, r)
2023-07-03 01:41:13 +02:00
if err != nil {
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
2024-02-11 20:52:14 +01:00
"error": "Invalid IntegrationExtractValue ID",
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
}
2024-03-06 14:28:24 +01:00
integration := context.Get(r, "integration").(db.Integration)
2024-02-11 20:52:14 +01:00
var value db.IntegrationExtractValue
2024-03-06 14:28:24 +01:00
value, err = helpers.Store(r).GetIntegrationExtractValue(project.ID, valueId, integration.ID)
2023-07-03 01:41:13 +02:00
if err != nil {
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
2024-02-11 20:52:14 +01:00
"error": fmt.Sprintf("Failed to get IntegrationExtractValue, %v", err),
})
return
}
2024-01-11 14:03:09 +01:00
helpers.WriteJSON(w, http.StatusOK, value)
2023-07-03 01:41:13 +02:00
}
2024-02-11 20:52:14 +01:00
func GetIntegrationExtractValues(w http.ResponseWriter, r *http.Request) {
project := context.Get(r, "project").(db.Project)
2024-03-06 14:28:24 +01:00
integration := context.Get(r, "integration").(db.Integration)
values, err := helpers.Store(r).GetIntegrationExtractValues(project.ID, helpers.QueryParams(r.URL), integration.ID)
2023-07-03 01:41:13 +02:00
2024-01-11 14:03:09 +01:00
if err != nil {
helpers.WriteError(w, err)
return
}
2023-07-03 01:41:13 +02:00
2024-01-11 14:03:09 +01:00
helpers.WriteJSON(w, http.StatusOK, values)
2023-07-03 01:41:13 +02:00
}
2024-02-11 20:52:14 +01:00
func AddIntegrationExtractValue(w http.ResponseWriter, r *http.Request) {
2024-03-04 12:24:47 +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-02-11 20:52:14 +01:00
var value db.IntegrationExtractValue
2024-01-11 14:03:09 +01:00
if !helpers.Bind(w, r, &value) {
return
}
2023-07-03 01:41:13 +02:00
2024-03-06 14:28:24 +01:00
if value.IntegrationID != integration.ID {
2024-01-11 14:03:09 +01:00
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
"error": "Extractor ID in body and URL must be the same",
})
return
}
2023-07-03 01:41:13 +02:00
2024-01-11 14:03:09 +01:00
if err := value.Validate(); err != nil {
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
"error": err.Error(),
})
return
}
2023-07-03 01:41:13 +02:00
2024-03-04 12:24:47 +01:00
newValue, err := helpers.Store(r).CreateIntegrationExtractValue(project.ID, value)
2023-07-03 01:41:13 +02:00
2024-01-11 14:03:09 +01:00
if err != nil {
helpers.WriteError(w, err)
return
}
helpers.WriteJSON(w, http.StatusCreated, newValue)
2024-01-11 14:03:09 +01:00
}
2023-07-03 01:41:13 +02:00
2024-02-11 20:52:14 +01:00
func UpdateIntegrationExtractValue(w http.ResponseWriter, r *http.Request) {
2024-03-04 18:53:20 +01:00
project := context.Get(r, "project").(db.Project)
valueId, err := helpers.GetIntParam("value_id", w, r)
2023-07-03 01:41:13 +02:00
2024-01-11 14:03:09 +01:00
if err != nil {
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
"error": "Invalid Value ID",
})
return
}
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 value db.IntegrationExtractValue
2024-03-06 14:28:24 +01:00
value, err = helpers.Store(r).GetIntegrationExtractValue(project.ID, valueId, integration.ID)
if err != nil {
helpers.WriteError(w, err)
return
}
2023-07-03 01:41:13 +02:00
2024-01-11 14:03:09 +01:00
if !helpers.Bind(w, r, &value) {
return
}
2023-07-03 01:41:13 +02:00
2024-03-04 20:05:18 +01:00
if value.ID != valueId {
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
"error": "Value ID in body and URL must be the same",
})
return
}
2024-03-04 18:53:20 +01:00
err = helpers.Store(r).UpdateIntegrationExtractValue(project.ID, value)
2023-07-03 01:41:13 +02:00
if err != nil {
2024-01-11 14:03:09 +01:00
helpers.WriteError(w, err)
return
2023-07-03 01:41:13 +02:00
}
w.WriteHeader(http.StatusNoContent)
}
2024-02-11 20:52:14 +01:00
func GetIntegrationExtractValueRefs(w http.ResponseWriter, r *http.Request) {
2024-03-04 18:53:20 +01:00
project := context.Get(r, "project").(db.Project)
valueId, err := helpers.GetIntParam("value_id", w, r)
2023-07-03 01:41:13 +02:00
if err != nil {
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
"error": "Invalid Value 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 value db.IntegrationExtractValue
2024-03-06 14:28:24 +01:00
value, err = helpers.Store(r).GetIntegrationExtractValue(project.ID, valueId, 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).GetIntegrationExtractValueRefs(project.ID, value.ID, value.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 DeleteIntegrationExtractValue(w http.ResponseWriter, r *http.Request) {
2024-03-04 18:53:20 +01:00
project := context.Get(r, "project").(db.Project)
valueId, err := helpers.GetIntParam("value_id", w, r)
2023-07-03 01:41:13 +02:00
if err != nil {
helpers.WriteJSON(w, http.StatusBadRequest, map[string]string{
2024-01-11 14:03:09 +01:00
"error": "Invalid Value ID",
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
}
2024-03-06 14:28:24 +01:00
integration := context.Get(r, "integration").(db.Integration)
2023-07-03 01:41:13 +02:00
2024-01-08 13:56:50 +01:00
if err != nil {
log.Error(err)
helpers.WriteJSON(w, http.StatusBadRequest, map[string]interface{}{
2024-02-11 20:52:14 +01:00
"error": "Integration Extract Value failed to be deleted",
2024-01-08 13:56:50 +01:00
})
return
}
2024-01-11 14:03:09 +01:00
2024-03-06 14:28:24 +01:00
err = helpers.Store(r).DeleteIntegrationExtractValue(project.ID, valueId, 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 Extract Value failed to be deleted",
2023-07-03 01:41:13 +02:00
})
2024-01-08 13:56:50 +01:00
return
2023-07-03 01:41:13 +02:00
}
w.WriteHeader(http.StatusNoContent)
}