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

Api queries #3

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
81 changes: 74 additions & 7 deletions api/handlers-queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,83 @@ func apiQueriesRunHandler(w http.ResponseWriter, r *http.Request) {
incMetric(metricAPIQueriesErr)
return
}
// Create UUID target
if (q.UUID != "") && nodesmgr.CheckByUUID(q.UUID) {
if err := queriesmgr.CreateTarget(queryName, queries.QueryTargetUUID, q.UUID); err != nil {
apiErrorResponse(w, "error creating query UUID target", http.StatusInternalServerError, err)
incMetric(metricAPICarvesErr)
return

// Temporary list of UUIDs to calculate Expected
var expected []string
// Create targets
if len(q.Environments) > 0 {
for _, e := range q.Environments {
if (e != "") && envs.Exists(e) {
if err := queriesmgr.CreateTarget(newQuery.Name, queries.QueryTargetEnvironment, e); err != nil {
apiErrorResponse(w, "error creating query environment target", http.StatusInternalServerError, err)
incMetric(metricAPIQueriesErr)
return
}
nodes, err := nodesmgr.GetByEnv(e, "active", settingsmgr.InactiveHours(settings.NoEnvironmentID))
if err != nil {
apiErrorResponse(w, "error getting nodes by environment", http.StatusInternalServerError, err)
incMetric(metricAPIQueriesErr)
return
}
for _, n := range nodes {
expected = append(expected, n.UUID)
}
}
}
}
// Create platform target
if len(q.Platforms) > 0 {
platforms, _ := nodesmgr.GetAllPlatforms()
for _, p := range q.Platforms {
if (p != "") && checkValidPlatform(platforms, p) {
if err := queriesmgr.CreateTarget(newQuery.Name, queries.QueryTargetPlatform, p); err != nil {
apiErrorResponse(w, "error creating query platform target", http.StatusInternalServerError, err)
incMetric(metricAPIQueriesErr)
return
}
nodes, err := nodesmgr.GetByPlatform(p, "active", settingsmgr.InactiveHours(settings.NoEnvironmentID))
if err != nil {
apiErrorResponse(w, "error getting nodes by platform", http.StatusInternalServerError, err)
incMetric(metricAPIQueriesErr)
return
}
for _, n := range nodes {
expected = append(expected, n.UUID)
}
}
}
}
// Create UUIDs target
if len(q.UUIDs) > 0 {
for _, u := range q.UUIDs {
if (u != "") && nodesmgr.CheckByUUID(u) {
if err := queriesmgr.CreateTarget(newQuery.Name, queries.QueryTargetUUID, u); err != nil {
apiErrorResponse(w, "error creating query UUID target", http.StatusInternalServerError, err)
incMetric(metricAPIQueriesErr)
return
}
expected = append(expected, u)
}
}
}
// Create hostnames target
if len(q.Hosts) > 0 {
for _, _h := range q.Hosts {
if (_h != "") && nodesmgr.CheckByHost(_h) {
if err := queriesmgr.CreateTarget(newQuery.Name, queries.QueryTargetLocalname, _h); err != nil {
apiErrorResponse(w, "error creating query hostname target", http.StatusInternalServerError, err)
incMetric(metricAPIQueriesErr)
return
}
expected = append(expected, _h)
}
}
}

// Remove duplicates from expected
expectedClear := removeStringDuplicates(expected)
// Update value for expected
if err := queriesmgr.SetExpected(queryName, 1, env.ID); err != nil {
if err := queriesmgr.SetExpected(queryName, len(expectedClear), env.ID); err != nil {
apiErrorResponse(w, "error setting expected", http.StatusInternalServerError, err)
incMetric(metricAPICarvesErr)
return
Expand Down
12 changes: 6 additions & 6 deletions api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,16 +527,16 @@ func osctrlAPIService() {
routerAPI.Handle(_apiPath(apiLoginPath)+"/{env}/", handlerAuthCheck(http.HandlerFunc(apiLoginHandler))).Methods("POST")
// ///////////////////////// AUTHENTICATED
// API: nodes by environment
routerAPI.Handle(_apiPath(apiNodesPath)+"/{env}/node/{node}", handlerAuthCheck(http.HandlerFunc(apiNodeHandler))).Methods("GET")
routerAPI.Handle(_apiPath(apiNodesPath)+"/{env}/node/{node}/", handlerAuthCheck(http.HandlerFunc(apiNodeHandler))).Methods("GET")
routerAPI.Handle(_apiPath(apiNodesPath)+"/{env}/delete", handlerAuthCheck(http.HandlerFunc(apiDeleteNodeHandler))).Methods("POST")
routerAPI.Handle(_apiPath(apiNodesPath)+"/{env}/delete/", handlerAuthCheck(http.HandlerFunc(apiDeleteNodeHandler))).Methods("POST")
routerAPI.Handle(_apiPath(apiNodesPath)+"/{env}/all", handlerAuthCheck(http.HandlerFunc(apiAllNodesHandler))).Methods("GET")
routerAPI.Handle(_apiPath(apiNodesPath)+"/{env}/all/", handlerAuthCheck(http.HandlerFunc(apiAllNodesHandler))).Methods("GET")
routerAPI.Handle(_apiPath(apiNodesPath)+"/{env}/active", handlerAuthCheck(http.HandlerFunc(apiActiveNodesHandler))).Methods("GET")
routerAPI.Handle(_apiPath(apiNodesPath)+"/{env}/active/", handlerAuthCheck(http.HandlerFunc(apiActiveNodesHandler))).Methods("GET")
routerAPI.Handle(_apiPath(apiNodesPath)+"/{env}/inactive", handlerAuthCheck(http.HandlerFunc(apiInactiveNodesHandler))).Methods("GET")
routerAPI.Handle(_apiPath(apiNodesPath)+"/{env}/inactive/", handlerAuthCheck(http.HandlerFunc(apiInactiveNodesHandler))).Methods("GET")
routerAPI.Handle(_apiPath(apiNodesPath)+"/{env}/node/{node}", handlerAuthCheck(http.HandlerFunc(apiNodeHandler))).Methods("GET")
routerAPI.Handle(_apiPath(apiNodesPath)+"/{env}/node/{node}/", handlerAuthCheck(http.HandlerFunc(apiNodeHandler))).Methods("GET")
routerAPI.Handle(_apiPath(apiNodesPath)+"/{env}/delete", handlerAuthCheck(http.HandlerFunc(apiDeleteNodeHandler))).Methods("POST")
routerAPI.Handle(_apiPath(apiNodesPath)+"/{env}/delete/", handlerAuthCheck(http.HandlerFunc(apiDeleteNodeHandler))).Methods("POST")
// API: queries by environment
routerAPI.Handle(_apiPath(apiQueriesPath)+"/{env}", handlerAuthCheck(http.HandlerFunc(apiAllQueriesShowHandler))).Methods("GET")
routerAPI.Handle(_apiPath(apiQueriesPath)+"/{env}/", handlerAuthCheck(http.HandlerFunc(apiAllQueriesShowHandler))).Methods("GET")
Expand All @@ -560,15 +560,15 @@ func osctrlAPIService() {
routerAPI.Handle(_apiPath(apiUsersPath)+"/{username}/", handlerAuthCheck(http.HandlerFunc(apiUserHandler))).Methods("GET")
routerAPI.Handle(_apiPath(apiUsersPath), handlerAuthCheck(http.HandlerFunc(apiUsersHandler))).Methods("GET")
routerAPI.Handle(_apiPath(apiUsersPath)+"/", handlerAuthCheck(http.HandlerFunc(apiUsersHandler))).Methods("GET")
// API: platforms by environment
// API: platforms
routerAPI.Handle(_apiPath(apiPlatformsPath), handlerAuthCheck(http.HandlerFunc(apiPlatformsHandler))).Methods("GET")
routerAPI.Handle(_apiPath(apiPlatformsPath)+"/", handlerAuthCheck(http.HandlerFunc(apiPlatformsHandler))).Methods("GET")
// API: environments by environment
routerAPI.Handle(_apiPath(apiEnvironmentsPath)+"/{env}", handlerAuthCheck(http.HandlerFunc(apiEnvironmentHandler))).Methods("GET")
routerAPI.Handle(_apiPath(apiEnvironmentsPath)+"/{env}/", handlerAuthCheck(http.HandlerFunc(apiEnvironmentHandler))).Methods("GET")
routerAPI.Handle(_apiPath(apiEnvironmentsPath), handlerAuthCheck(http.HandlerFunc(apiEnvironmentsHandler))).Methods("GET")
routerAPI.Handle(_apiPath(apiEnvironmentsPath)+"/", handlerAuthCheck(http.HandlerFunc(apiEnvironmentsHandler))).Methods("GET")
// API: tags by environment
// API: tags
routerAPI.Handle(_apiPath(apiTagsPath), handlerAuthCheck(http.HandlerFunc(apiTagsHandler))).Methods("GET")
routerAPI.Handle(_apiPath(apiTagsPath)+"/", handlerAuthCheck(http.HandlerFunc(apiTagsHandler))).Methods("GET")
// API: settings by environment
Expand Down
6 changes: 1 addition & 5 deletions api/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,7 @@ func _apiPath(target string) string {
}

// Helper to verify if a platform is valid
func checkValidPlatform(platform string) bool {
platforms, err := nodesmgr.GetAllPlatforms()
if err != nil {
return false
}
func checkValidPlatform(platforms []string, platform string) bool {
for _, p := range platforms {
if p == platform {
return true
Expand Down
2 changes: 1 addition & 1 deletion cli/api-query.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (api *OsctrlAPI) CompleteQuery(env, identifier string) error {
// RunQuery to initiate a query in osctrl
func (api *OsctrlAPI) RunQuery(env, uuid, query string, hidden bool) (types.ApiQueriesResponse, error) {
q := types.ApiDistributedQueryRequest{
UUID: uuid,
UUIDs: []string{uuid},
Query: query,
Hidden: hidden,
}
Expand Down
Loading