Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using only UUID to identify environment in osctrl-api #513

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions api/handlers/carves.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ func (h *HandlersApi) CarveShowHandler(w http.ResponseWriter, r *http.Request) {
// Extract environment
envVar := r.PathValue("env")
if envVar == "" {
apiErrorResponse(w, "error with environment", http.StatusInternalServerError, nil)
apiErrorResponse(w, "error with environment", http.StatusBadRequest, nil)
h.Inc(metricAPICarvesErr)
return
}
// Get environment
env, err := h.Envs.Get(envVar)
env, err := h.Envs.GetByUUID(envVar)
if err != nil {
apiErrorResponse(w, "error getting environment", http.StatusInternalServerError, nil)
h.Inc(metricAPICarvesErr)
Expand Down Expand Up @@ -72,12 +72,12 @@ func (h *HandlersApi) CarvesRunHandler(w http.ResponseWriter, r *http.Request) {
// Extract environment
envVar := r.PathValue("env")
if envVar == "" {
apiErrorResponse(w, "error with environment", http.StatusInternalServerError, nil)
apiErrorResponse(w, "error with environment", http.StatusBadRequest, nil)
h.Inc(metricAPICarvesErr)
return
}
// Get environment
env, err := h.Envs.Get(envVar)
env, err := h.Envs.GetByUUID(envVar)
if err != nil {
apiErrorResponse(w, "error getting environment", http.StatusInternalServerError, nil)
h.Inc(metricAPICarvesErr)
Expand Down Expand Up @@ -150,12 +150,12 @@ func (h *HandlersApi) apiCarvesShowHandler(w http.ResponseWriter, r *http.Reques
// Extract environment
envVar := r.PathValue("env")
if envVar == "" {
apiErrorResponse(w, "error with environment", http.StatusInternalServerError, nil)
apiErrorResponse(w, "error with environment", http.StatusBadRequest, nil)
h.Inc(metricAPICarvesErr)
return
}
// Get environment
env, err := h.Envs.Get(envVar)
env, err := h.Envs.GetByUUID(envVar)
if err != nil {
apiErrorResponse(w, "error getting environment", http.StatusInternalServerError, nil)
h.Inc(metricAPICarvesErr)
Expand Down
26 changes: 13 additions & 13 deletions api/handlers/environments.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ func (h *HandlersApi) EnvironmentHandler(w http.ResponseWriter, r *http.Request)
// Extract environment
envVar := r.PathValue("env")
if envVar == "" {
apiErrorResponse(w, "error getting environment", http.StatusInternalServerError, nil)
apiErrorResponse(w, "error getting environment", http.StatusBadRequest, nil)
h.Inc(metricAPIEnvsErr)
return
}
// Get environment by name
env, err := h.Envs.Get(envVar)
env, err := h.Envs.GetByUUID(envVar)
if err != nil {
if err.Error() == "record not found" {
apiErrorResponse(w, "environment not found", http.StatusNotFound, err)
Expand Down Expand Up @@ -83,12 +83,12 @@ func (h *HandlersApi) EnvEnrollHandler(w http.ResponseWriter, r *http.Request) {
// Extract environment
envVar := r.PathValue("env")
if envVar == "" {
apiErrorResponse(w, "error getting environment", http.StatusInternalServerError, nil)
apiErrorResponse(w, "error getting environment", http.StatusBadRequest, nil)
h.Inc(metricAPIEnvsErr)
return
}
// Get environment by name
env, err := h.Envs.Get(envVar)
env, err := h.Envs.GetByUUID(envVar)
if err != nil {
if err.Error() == "record not found" {
apiErrorResponse(w, "environment not found", http.StatusNotFound, err)
Expand All @@ -108,7 +108,7 @@ func (h *HandlersApi) EnvEnrollHandler(w http.ResponseWriter, r *http.Request) {
// Extract target
targetVar := r.PathValue("target")
if targetVar == "" {
apiErrorResponse(w, "error getting target", http.StatusInternalServerError, nil)
apiErrorResponse(w, "error getting target", http.StatusBadRequest, nil)
h.Inc(metricAPIEnvsErr)
return
}
Expand Down Expand Up @@ -154,12 +154,12 @@ func (h *HandlersApi) EnvRemoveHandler(w http.ResponseWriter, r *http.Request) {
// Extract environment
envVar := r.PathValue("env")
if envVar == "" {
apiErrorResponse(w, "error getting environment", http.StatusInternalServerError, nil)
apiErrorResponse(w, "error getting environment", http.StatusBadRequest, nil)
h.Inc(metricAPIEnvsErr)
return
}
// Get environment by name
env, err := h.Envs.Get(envVar)
env, err := h.Envs.GetByUUID(envVar)
if err != nil {
if err.Error() == "record not found" {
apiErrorResponse(w, "environment not found", http.StatusNotFound, err)
Expand Down Expand Up @@ -219,12 +219,12 @@ func (h *HandlersApi) EnvEnrollActionsHandler(w http.ResponseWriter, r *http.Req
// Extract environment
envVar := r.PathValue("env")
if envVar == "" {
apiErrorResponse(w, "error getting environment", http.StatusInternalServerError, nil)
apiErrorResponse(w, "error getting environment", http.StatusBadRequest, nil)
h.Inc(metricAPIEnvsErr)
return
}
// Get environment by name
env, err := h.Envs.Get(envVar)
env, err := h.Envs.GetByUUID(envVar)
if err != nil {
if err.Error() == "record not found" {
apiErrorResponse(w, "environment not found", http.StatusNotFound, err)
Expand All @@ -244,7 +244,7 @@ func (h *HandlersApi) EnvEnrollActionsHandler(w http.ResponseWriter, r *http.Req
// Extract action
actionVar := r.PathValue("action")
if actionVar == "" {
apiErrorResponse(w, "error getting action", http.StatusInternalServerError, nil)
apiErrorResponse(w, "error getting action", http.StatusBadRequest, nil)
h.Inc(metricAPIEnvsErr)
return
}
Expand Down Expand Up @@ -330,12 +330,12 @@ func (h *HandlersApi) EnvRemoveActionsHandler(w http.ResponseWriter, r *http.Req
// Extract environment
envVar := r.PathValue("env")
if envVar == "" {
apiErrorResponse(w, "error getting environment", http.StatusInternalServerError, nil)
apiErrorResponse(w, "error getting environment", http.StatusBadRequest, nil)
h.Inc(metricAPIEnvsErr)
return
}
// Get environment by name
env, err := h.Envs.Get(envVar)
env, err := h.Envs.GetByUUID(envVar)
if err != nil {
if err.Error() == "record not found" {
apiErrorResponse(w, "environment not found", http.StatusNotFound, err)
Expand All @@ -355,7 +355,7 @@ func (h *HandlersApi) EnvRemoveActionsHandler(w http.ResponseWriter, r *http.Req
// Extract action
actionVar := r.PathValue("action")
if actionVar == "" {
apiErrorResponse(w, "error getting action", http.StatusInternalServerError, nil)
apiErrorResponse(w, "error getting action", http.StatusBadRequest, nil)
h.Inc(metricAPIEnvsErr)
return
}
Expand Down
6 changes: 3 additions & 3 deletions api/handlers/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ func (h *HandlersApi) LoginHandler(w http.ResponseWriter, r *http.Request) {
// Extract environment
envVar := r.PathValue("env")
if envVar == "" {
apiErrorResponse(w, "error with environment", http.StatusInternalServerError, nil)
apiErrorResponse(w, "error with environment", http.StatusBadRequest, nil)
h.Inc(metricAPILoginErr)
return
}
// Get environment
env, err := h.Envs.Get(envVar)
// Get environment by UUID
env, err := h.Envs.GetByUUID(envVar)
if err != nil {
apiErrorResponse(w, "error getting environment", http.StatusInternalServerError, nil)
h.Inc(metricAPILoginErr)
Expand Down
31 changes: 16 additions & 15 deletions api/handlers/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"net/http"

"github.com/jmpsec/osctrl/nodes"
"github.com/jmpsec/osctrl/settings"
"github.com/jmpsec/osctrl/types"
"github.com/jmpsec/osctrl/users"
Expand All @@ -19,12 +20,12 @@ func (h *HandlersApi) NodeHandler(w http.ResponseWriter, r *http.Request) {
// Extract environment
envVar := r.PathValue("env")
if envVar == "" {
apiErrorResponse(w, "error with environment", http.StatusInternalServerError, nil)
apiErrorResponse(w, "error with environment", http.StatusBadRequest, nil)
h.Inc(metricAPINodesErr)
return
}
// Get environment
env, err := h.Envs.Get(envVar)
env, err := h.Envs.GetByUUID(envVar)
if err != nil {
apiErrorResponse(w, "error getting environment", http.StatusInternalServerError, nil)
h.Inc(metricAPINodesErr)
Expand All @@ -40,7 +41,7 @@ func (h *HandlersApi) NodeHandler(w http.ResponseWriter, r *http.Request) {
// Extract host identifier for node
nodeVar := r.PathValue("node")
if nodeVar == "" {
apiErrorResponse(w, "error getting node", http.StatusInternalServerError, nil)
apiErrorResponse(w, "error getting node", http.StatusBadRequest, nil)
h.Inc(metricAPINodesErr)
return
}
Expand Down Expand Up @@ -71,12 +72,12 @@ func (h *HandlersApi) ActiveNodesHandler(w http.ResponseWriter, r *http.Request)
// Extract environment
envVar := r.PathValue("env")
if envVar == "" {
apiErrorResponse(w, "error with environment", http.StatusInternalServerError, nil)
apiErrorResponse(w, "error with environment", http.StatusBadRequest, nil)
h.Inc(metricAPINodesErr)
return
}
// Get environment
env, err := h.Envs.Get(envVar)
env, err := h.Envs.GetByUUID(envVar)
if err != nil {
apiErrorResponse(w, "error getting environment", http.StatusInternalServerError, nil)
h.Inc(metricAPINodesErr)
Expand All @@ -90,7 +91,7 @@ func (h *HandlersApi) ActiveNodesHandler(w http.ResponseWriter, r *http.Request)
return
}
// Get nodes
nodes, err := h.Nodes.Gets("active", 24)
nodes, err := h.Nodes.Gets(nodes.ActiveNodes, 24)
if err != nil {
apiErrorResponse(w, "error getting nodes", http.StatusInternalServerError, err)
h.Inc(metricAPINodesErr)
Expand All @@ -116,12 +117,12 @@ func (h *HandlersApi) InactiveNodesHandler(w http.ResponseWriter, r *http.Reques
// Extract environment
envVar := r.PathValue("env")
if envVar == "" {
apiErrorResponse(w, "error with environment", http.StatusInternalServerError, nil)
apiErrorResponse(w, "error with environment", http.StatusBadRequest, nil)
h.Inc(metricAPINodesErr)
return
}
// Get environment
env, err := h.Envs.Get(envVar)
env, err := h.Envs.GetByUUID(envVar)
if err != nil {
apiErrorResponse(w, "error getting environment", http.StatusInternalServerError, nil)
h.Inc(metricAPINodesErr)
Expand All @@ -135,7 +136,7 @@ func (h *HandlersApi) InactiveNodesHandler(w http.ResponseWriter, r *http.Reques
return
}
// Get nodes
nodes, err := h.Nodes.Gets("inactive", 24)
nodes, err := h.Nodes.Gets(nodes.InactiveNodes, 24)
if err != nil {
apiErrorResponse(w, "error getting nodes", http.StatusInternalServerError, err)
h.Inc(metricAPINodesErr)
Expand All @@ -161,14 +162,14 @@ func (h *HandlersApi) AllNodesHandler(w http.ResponseWriter, r *http.Request) {
// Extract environment
envVar := r.PathValue("env")
if envVar == "" {
apiErrorResponse(w, "error with environment", http.StatusInternalServerError, nil)
apiErrorResponse(w, "error with environment", http.StatusBadRequest, nil)
h.Inc(metricAPINodesErr)
return
}
// Get environment
env, err := h.Envs.Get(envVar)
env, err := h.Envs.GetByUUID(envVar)
if err != nil {
apiErrorResponse(w, "error getting environment", http.StatusInternalServerError, nil)
apiErrorResponse(w, "error getting environment", http.StatusBadRequest, nil)
h.Inc(metricAPINodesErr)
return
}
Expand All @@ -180,7 +181,7 @@ func (h *HandlersApi) AllNodesHandler(w http.ResponseWriter, r *http.Request) {
return
}
// Get nodes
nodes, err := h.Nodes.Gets("all", 0)
nodes, err := h.Nodes.Gets(nodes.AllNodes, 0)
if err != nil {
apiErrorResponse(w, "error getting nodes", http.StatusInternalServerError, err)
h.Inc(metricAPINodesErr)
Expand All @@ -206,12 +207,12 @@ func (h *HandlersApi) DeleteNodeHandler(w http.ResponseWriter, r *http.Request)
// Extract environment
envVar := r.PathValue("env")
if envVar == "" {
apiErrorResponse(w, "error with environment", http.StatusInternalServerError, nil)
apiErrorResponse(w, "error with environment", http.StatusBadRequest, nil)
h.Inc(metricAPINodesErr)
return
}
// Get environment
env, err := h.Envs.Get(envVar)
env, err := h.Envs.GetByUUID(envVar)
if err != nil {
apiErrorResponse(w, "error getting environment", http.StatusInternalServerError, nil)
h.Inc(metricAPINodesErr)
Expand Down
4 changes: 2 additions & 2 deletions api/handlers/platforms.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ func (h *HandlersApi) PlatformsEnvHandler(w http.ResponseWriter, r *http.Request
// Extract environment
envVar := r.PathValue("env")
if envVar == "" {
apiErrorResponse(w, "error getting environment", http.StatusInternalServerError, nil)
apiErrorResponse(w, "error getting environment", http.StatusBadRequest, nil)
h.Inc(metricAPIEnvsErr)
return
}
// Get environment by name
env, err := h.Envs.Get(envVar)
env, err := h.Envs.GetByUUID(envVar)
if err != nil {
if err.Error() == "record not found" {
apiErrorResponse(w, "environment not found", http.StatusNotFound, err)
Expand Down
24 changes: 12 additions & 12 deletions api/handlers/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ func (h *HandlersApi) QueryShowHandler(w http.ResponseWriter, r *http.Request) {
// Extract name
name := r.PathValue("name")
if name == "" {
apiErrorResponse(w, "error getting name", http.StatusInternalServerError, nil)
apiErrorResponse(w, "error getting name", http.StatusBadRequest, nil)
h.Inc(metricAPIQueriesErr)
return
}
// Extract environment
envVar := r.PathValue("env")
if envVar == "" {
apiErrorResponse(w, "error with environment", http.StatusInternalServerError, nil)
apiErrorResponse(w, "error with environment", http.StatusBadRequest, nil)
h.Inc(metricAPIQueriesErr)
return
}
// Get environment
env, err := h.Envs.Get(envVar)
env, err := h.Envs.GetByUUID(envVar)
if err != nil {
apiErrorResponse(w, "error getting environment", http.StatusInternalServerError, nil)
h.Inc(metricAPIQueriesErr)
Expand Down Expand Up @@ -71,12 +71,12 @@ func (h *HandlersApi) QueriesRunHandler(w http.ResponseWriter, r *http.Request)
// Extract environment
envVar := r.PathValue("env")
if envVar == "" {
apiErrorResponse(w, "error with environment", http.StatusInternalServerError, nil)
apiErrorResponse(w, "error with environment", http.StatusBadRequest, nil)
h.Inc(metricAPIQueriesErr)
return
}
// Get environment
env, err := h.Envs.Get(envVar)
env, err := h.Envs.GetByUUID(envVar)
if err != nil {
apiErrorResponse(w, "error getting environment", http.StatusInternalServerError, nil)
h.Inc(metricAPIQueriesErr)
Expand All @@ -99,7 +99,7 @@ func (h *HandlersApi) QueriesRunHandler(w http.ResponseWriter, r *http.Request)
// FIXME check validity of query
// Query can not be empty
if q.Query == "" {
apiErrorResponse(w, "query can not be empty", http.StatusInternalServerError, nil)
apiErrorResponse(w, "query can not be empty", http.StatusBadRequest, nil)
h.Inc(metricAPIQueriesErr)
return
}
Expand Down Expand Up @@ -216,12 +216,12 @@ func (h *HandlersApi) AllQueriesShowHandler(w http.ResponseWriter, r *http.Reque
// Extract environment
envVar := r.PathValue("env")
if envVar == "" {
apiErrorResponse(w, "error with environment", http.StatusInternalServerError, nil)
apiErrorResponse(w, "error with environment", http.StatusBadRequest, nil)
h.Inc(metricAPIQueriesErr)
return
}
// Get environment
env, err := h.Envs.Get(envVar)
env, err := h.Envs.GetByUUID(envVar)
if err != nil {
apiErrorResponse(w, "error getting environment", http.StatusInternalServerError, nil)
h.Inc(metricAPIQueriesErr)
Expand Down Expand Up @@ -258,12 +258,12 @@ func (h *HandlersApi) HiddenQueriesShowHandler(w http.ResponseWriter, r *http.Re
// Extract environment
envVar := r.PathValue("env")
if envVar == "" {
apiErrorResponse(w, "error with environment", http.StatusInternalServerError, nil)
apiErrorResponse(w, "error with environment", http.StatusBadRequest, nil)
h.Inc(metricAPIQueriesErr)
return
}
// Get environment
env, err := h.Envs.Get(envVar)
env, err := h.Envs.GetByUUID(envVar)
if err != nil {
apiErrorResponse(w, "error getting environment", http.StatusInternalServerError, nil)
h.Inc(metricAPIQueriesErr)
Expand Down Expand Up @@ -300,7 +300,7 @@ func (h *HandlersApi) QueryResultsHandler(w http.ResponseWriter, r *http.Request
// Extract name
name := r.PathValue("name")
if name == "" {
apiErrorResponse(w, "error getting name", http.StatusInternalServerError, nil)
apiErrorResponse(w, "error getting name", http.StatusBadRequest, nil)
h.Inc(metricAPIQueriesErr)
return
}
Expand All @@ -312,7 +312,7 @@ func (h *HandlersApi) QueryResultsHandler(w http.ResponseWriter, r *http.Request
return
}
// Get environment
env, err := h.Envs.Get(envVar)
env, err := h.Envs.GetByUUID(envVar)
if err != nil {
apiErrorResponse(w, "error getting environment", http.StatusInternalServerError, nil)
h.Inc(metricAPIQueriesErr)
Expand Down
Loading
Loading