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

feat: added db utils to account and alert #1404

Merged
merged 18 commits into from
May 2, 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
22 changes: 13 additions & 9 deletions handlers/accounts_handler.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package handlers

import (
"context"
"database/sql"
"encoding/json"
"fmt"
Expand All @@ -12,6 +11,7 @@ import (
"github.com/go-co-op/gocron"
log "github.com/sirupsen/logrus"
"github.com/tailwarden/komiser/models"
"github.com/tailwarden/komiser/repository"
"github.com/tailwarden/komiser/utils"
"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect/pgdialect"
Expand All @@ -38,7 +38,8 @@ func (handler *ApiHandler) IsOnboardedHandler(c *gin.Context) {
}

accounts := make([]models.Account, 0)
err := handler.db.NewRaw("SELECT * FROM accounts").Scan(handler.ctx, &accounts)

_, err := handler.repo.HandleQuery(c, repository.ListKey, &accounts, nil)
if err != nil {
log.WithError(err).Error("scan failed")
c.JSON(http.StatusInternalServerError, gin.H{"error": "scan failed"})
Expand All @@ -62,7 +63,7 @@ func (handler *ApiHandler) ListCloudAccountsHandler(c *gin.Context) {
return
}

err := handler.db.NewRaw("SELECT * FROM accounts").Scan(handler.ctx, &accounts)
_, err := handler.repo.HandleQuery(c, repository.ListKey, &accounts, nil)
if err != nil {
log.WithError(err).Error("scan failed")
c.JSON(http.StatusInternalServerError, gin.H{"error": "scan failed"})
Expand All @@ -73,8 +74,10 @@ func (handler *ApiHandler) ListCloudAccountsHandler(c *gin.Context) {
output := struct {
Total int `bun:"total" json:"total"`
}{}
err = handler.db.NewRaw(fmt.Sprintf("SELECT COUNT(*) as total FROM resources WHERE provider='%s' AND account='%s'", account.Provider, account.Name)).Scan(handler.ctx, &output)

_, err := handler.repo.HandleQuery(c, repository.ResourceCountKey, &output, [][3]string{{"provider", "=", account.Provider}, {"account", "=", account.Name}})
if err != nil {
fmt.Println(err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "scan failed"})
return
}
Expand Down Expand Up @@ -104,12 +107,12 @@ func (handler *ApiHandler) NewCloudAccountHandler(c *gin.Context) {

unsavedAccounts = append(unsavedAccounts, account)
} else {
result, err := handler.db.NewInsert().Model(&account).Exec(context.Background())

result, err := handler.repo.HandleQuery(c, repository.InsertKey, &account, nil)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}

accountId, _ := result.LastInsertId()
account.Id = accountId

Expand Down Expand Up @@ -152,7 +155,8 @@ func (handler *ApiHandler) ReScanAccount(c *gin.Context) {
accountId := c.Param("id")

account := new(models.Account)
res, err := handler.db.NewUpdate().Model(account).Set("status = ? ", "SCANNING").Where("id = ?", accountId).Where("status = ?", "CONNECTED").Returning("*").Exec(handler.ctx)
account.Status = "SCANNING"
res, err := handler.repo.HandleQuery(c, repository.ReScanAccountKey, account, [][3]string{{"id", "=", accountId}, {"status", "=", "CONNECTED"}})
if err != nil {
log.Error("Couldn't set status", err)
return
Expand All @@ -169,7 +173,7 @@ func (handler *ApiHandler) DeleteCloudAccountHandler(c *gin.Context) {
accountId := c.Param("id")

account := new(models.Account)
_, err := handler.db.NewDelete().Model(account).Where("id = ?", accountId).Exec(handler.ctx)
_, err := handler.repo.HandleQuery(c, repository.DeleteKey, account, [][3]string{{"id", "=", accountId}})
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
Expand All @@ -188,7 +192,7 @@ func (handler *ApiHandler) UpdateCloudAccountHandler(c *gin.Context) {
return
}

_, err = handler.db.NewUpdate().Model(&account).Column("name", "provider", "credentials").Where("id = ?", accountId).Exec(handler.ctx)
_, err = handler.repo.HandleQuery(c, repository.UpdateAccountKey, &account, [][3]string{{"id", "=", accountId}})
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
Expand Down
9 changes: 5 additions & 4 deletions handlers/alerts_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package handlers

import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"time"

"github.com/gin-gonic/gin"
"github.com/tailwarden/komiser/models"
"github.com/tailwarden/komiser/repository"
)

func (handler *ApiHandler) IsSlackEnabledHandler(c *gin.Context) {
Expand All @@ -33,7 +34,7 @@ func (handler *ApiHandler) NewAlertHandler(c *gin.Context) {
return
}

result, err := handler.db.NewInsert().Model(&alert).Exec(context.Background())
result, err := handler.repo.HandleQuery(c, repository.InsertKey, &alert, nil)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
Expand Down Expand Up @@ -62,7 +63,7 @@ func (handler *ApiHandler) UpdateAlertHandler(c *gin.Context) {
return
}

_, err = handler.db.NewUpdate().Model(&alert).Column("name", "type", "budget", "usage", "endpoint", "secret").Where("id = ?", alertId).Exec(handler.ctx)
_, err = handler.repo.HandleQuery(c, repository.UpdateAlertKey, &alert, [][3]string{{"id", "=", fmt.Sprint(alertId)}})
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
Expand All @@ -75,7 +76,7 @@ func (handler *ApiHandler) DeleteAlertHandler(c *gin.Context) {
alertId := c.Param("id")

alert := new(models.Alert)
_, err := handler.db.NewDelete().Model(alert).Where("id = ?", alertId).Exec(handler.ctx)
_, err := handler.repo.HandleQuery(c, repository.DeleteKey, alert, [][3]string{{"id", "=", fmt.Sprint(alertId)}})
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
Expand Down
5 changes: 3 additions & 2 deletions handlers/csv_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ import (
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"github.com/tailwarden/komiser/models"
"github.com/tailwarden/komiser/repository"
"github.com/uptrace/bun/dialect"
)

func (handler *ApiHandler) DownloadInventoryCSV(c *gin.Context) {
resources := make([]models.Resource, 0)
err := handler.db.NewSelect().Table("resources").Scan(handler.ctx, &resources)
_, err := handler.repo.HandleQuery(c, repository.ListKey, &resources, [][3]string{})
if err != nil {
logrus.WithError(err).Error("Could not read from DB")
c.JSON(http.StatusInternalServerError, gin.H{"error": "cloud not read from DB"})
Expand All @@ -37,7 +38,7 @@ func (handler *ApiHandler) DownloadInventoryCSVForView(c *gin.Context) {
viewId := c.Param("viewId")

view := new(models.View)
err := handler.db.NewSelect().Model(view).Where("id = ?", viewId).Scan(handler.ctx)
_, err := handler.repo.HandleQuery(c, repository.ListKey, view, [][3]string{{"id", "=", fmt.Sprint(viewId)}})
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
Expand Down
14 changes: 7 additions & 7 deletions handlers/dashboard_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"github.com/tailwarden/komiser/models"
"github.com/tailwarden/komiser/repository"
"github.com/tailwarden/komiser/utils"
"github.com/uptrace/bun"
)
Expand All @@ -32,16 +33,16 @@ func (handler *ApiHandler) DashboardStatsHandler(c *gin.Context) {
Count int `bun:"count" json:"total"`
}{}

err := handler.db.NewRaw("SELECT COUNT(*) as count FROM (SELECT DISTINCT region FROM resources) AS temp").Scan(handler.ctx, &regions)
_, err := handler.repo.HandleQuery(c, repository.RegionResourceCountKey, &regions, [][3]string{})
if err != nil {
logrus.WithError(err).Error("scan failed")
}

resources := struct {
Count int `bun:"count" json:"total"`
Count int `bun:"total" json:"total"`
}{}

err = handler.db.NewRaw("SELECT COUNT(*) as count FROM resources").Scan(handler.ctx, &resources)
_, err = handler.repo.HandleQuery(c, repository.ResourceCountKey, &resources, [][3]string{})
if err != nil {
logrus.WithError(err).Error("scan failed")
}
Expand All @@ -50,7 +51,7 @@ func (handler *ApiHandler) DashboardStatsHandler(c *gin.Context) {
Sum float64 `bun:"sum" json:"total"`
}{}

err = handler.db.NewRaw("SELECT SUM(cost) as sum FROM resources").Scan(handler.ctx, &cost)
_, err = handler.repo.HandleQuery(c, repository.ResourceCostSumKey, &cost, [][3]string{})
if err != nil {
logrus.WithError(err).Error("scan failed")
}
Expand All @@ -59,7 +60,7 @@ func (handler *ApiHandler) DashboardStatsHandler(c *gin.Context) {
Count int `bun:"count" json:"total"`
}{}

err = handler.db.NewRaw("SELECT COUNT(*) as count FROM (SELECT DISTINCT account FROM resources) AS temp").Scan(handler.ctx, &accounts)
_, err = handler.repo.HandleQuery(c, repository.RegionResourceCountKey, &regions, [][3]string{})
if err != nil {
logrus.WithError(err).Error("scan failed")
}
Expand Down Expand Up @@ -143,8 +144,7 @@ func (handler *ApiHandler) LocationBreakdownStatsHandler(c *gin.Context) {
c.JSON(http.StatusInternalServerError, []models.OutputLocations{})
return
}

err := handler.db.NewRaw("SELECT region as label, COUNT(*) as total FROM resources GROUP BY region ORDER by total desc;").Scan(handler.ctx, &groups)
_, err := handler.repo.HandleQuery(c, repository.LocationBreakdownStatKey, &groups, [][3]string{})
if err != nil {
logrus.WithError(err).Error("scan failed")
}
Expand Down
46 changes: 26 additions & 20 deletions handlers/resources_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package handlers

import (
"context"
"database/sql"
"encoding/json"
"fmt"
"net/http"
Expand All @@ -11,44 +12,49 @@ import (
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"github.com/tailwarden/komiser/models"
. "github.com/tailwarden/komiser/models"
"github.com/tailwarden/komiser/repository"
"github.com/tailwarden/komiser/utils"
"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect"
)

type ApiHandler struct {
db *bun.DB
ctx context.Context
telemetry bool
cfg models.Config
db *bun.DB
repo Repository
ctx context.Context
telemetry bool
cfg models.Config
configPath string
analytics utils.Analytics
accounts []models.Account
analytics utils.Analytics
accounts []models.Account
}

type Repository interface {
HandleQuery(context.Context, repository.QueryType, interface{}, [][3]string) (sql.Result, error)
}

func NewApiHandler(ctx context.Context, telemetry bool, analytics utils.Analytics, db *bun.DB, cfg models.Config, configPath string, accounts []models.Account) *ApiHandler {
handler := ApiHandler{
db: db,
ctx: ctx,
telemetry: telemetry,
cfg: cfg,
db: db,
ctx: ctx,
telemetry: telemetry,
cfg: cfg,
configPath: configPath,
analytics: analytics,
accounts: accounts,
analytics: analytics,
accounts: accounts,
}
return &handler
}

func (handler *ApiHandler) FilterResourcesHandler(c *gin.Context) {
var filters []Filter
var filters []models.Filter

limitRaw := c.Query("limit")
skipRaw := c.Query("skip")
query := c.Query("query")
viewId := c.Query("view")

view := new(View)
view := new(models.View)
if viewId != "" {
err := handler.db.NewSelect().Model(view).Where("id = ?", viewId).Scan(handler.ctx)
if err != nil {
Expand Down Expand Up @@ -240,7 +246,7 @@ func (handler *ApiHandler) FilterResourcesHandler(c *gin.Context) {

whereClause := strings.Join(whereQueries, " AND ")

resources := make([]Resource, 0)
resources := make([]models.Resource, 0)

if len(filters) == 0 {
if len(query) > 0 {
Expand Down Expand Up @@ -305,7 +311,7 @@ func (handler *ApiHandler) FilterResourcesHandler(c *gin.Context) {
}

func (handler *ApiHandler) RelationStatsHandler(c *gin.Context) {
var filters []Filter
var filters []models.Filter

err := json.NewDecoder(c.Request.Body).Decode(&filters)
if err != nil {
Expand Down Expand Up @@ -428,17 +434,17 @@ func (handler *ApiHandler) RelationStatsHandler(c *gin.Context) {
Provider: ele.Provider,
})
}

c.JSON(http.StatusOK, out)

}

func (handler *ApiHandler) GetResourceByIdHandler(c *gin.Context) {
resourceId := c.Query("resourceId")

var resource Resource
var resource models.Resource

err := handler.db.NewSelect().Model(&resource).Where("resource_id = ?", resourceId).Scan(handler.ctx)
_, err := handler.repo.HandleQuery(c, repository.ListKey, &resource, [][3]string{{"resource_id", "=", resourceId}})
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "Resource not found"})
}
Expand Down
Loading
Loading