Skip to content

Commit

Permalink
Merge pull request #1426 from tailwarden/develop
Browse files Browse the repository at this point in the history
v3.1.17 release 🚀
  • Loading branch information
Azanul authored May 24, 2024
2 parents 6732ea4 + 438711e commit 3edf7c6
Show file tree
Hide file tree
Showing 32 changed files with 3,055 additions and 2,263 deletions.
51 changes: 51 additions & 0 deletions controller/accounts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package controller

import (
"context"

"github.com/tailwarden/komiser/models"
"github.com/tailwarden/komiser/repository"
)

func (ctrl *Controller) ListAccounts(c context.Context) (accounts []models.Account, err error) {
_, err = ctrl.repo.HandleQuery(c, repository.ListKey, &accounts, nil)
return
}

func (ctrl *Controller) CountResources(c context.Context, provider, name string) (output totalOutput, err error) {
conditions := [][3]string{}
if provider != "" {
conditions = append(conditions, [3]string{"provider", "=", provider})
}
if name != "" {
conditions = append(conditions, [3]string{"account", "=", name})
}
_, err = ctrl.repo.HandleQuery(c, repository.ResourceCountKey, &output, conditions)
return
}

func (ctrl *Controller) InsertAccount(c context.Context, account models.Account) (lastId int64, err error) {
result, err := ctrl.repo.HandleQuery(c, repository.InsertKey, &account, nil)
if err != nil {
return
}
return result.LastInsertId()
}

func (ctrl *Controller) RescanAccount(c context.Context, account *models.Account, accountId string) (rows int64, err error) {
res, err := ctrl.repo.HandleQuery(c, repository.ReScanAccountKey, account, [][3]string{{"id", "=", accountId}, {"status", "=", "CONNECTED"}})
if err != nil {
return 0, err
}
return res.RowsAffected()
}

func (ctrl *Controller) DeleteAccount(c context.Context, accountId string) (err error) {
_, err = ctrl.repo.HandleQuery(c, repository.DeleteKey, new(models.Account), [][3]string{{"id", "=", accountId}})
return
}

func (ctrl *Controller) UpdateAccount(c context.Context, account models.Account, accountId string) (err error) {
_, err = ctrl.repo.HandleQuery(c, repository.UpdateAccountKey, &account, [][3]string{{"id", "=", accountId}})
return
}
26 changes: 26 additions & 0 deletions controller/alerts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package controller

import (
"context"

"github.com/tailwarden/komiser/models"
"github.com/tailwarden/komiser/repository"
)

func (ctrl *Controller) InsertAlert(c context.Context, alert models.Alert) (alertId int64, err error) {
result, err := ctrl.repo.HandleQuery(c, repository.InsertKey, &alert, nil)
if err != nil {
return
}
return result.LastInsertId()
}

func (ctrl *Controller) UpdateAlert(c context.Context, alert models.Alert, alertId string) (err error) {
_, err = ctrl.repo.HandleQuery(c, repository.UpdateAlertKey, &alert, [][3]string{{"id", "=", alertId}})
return
}

func (ctrl *Controller) DeleteAlert(c context.Context, alertId string) (err error) {
_, err = ctrl.repo.HandleQuery(c, repository.DeleteKey, new(models.Alert), [][3]string{{"id", "=", alertId}})
return
}
42 changes: 42 additions & 0 deletions controller/controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package controller

import (
"context"
"database/sql"
)

type totalOutput struct {
Total int `bun:"total" json:"total"`
}

type costOutput struct {
Total float64 `bun:"sum" json:"total"`
}

type regionOutput struct {
Region string `bun:"region" json:"region"`
}

type providerOutput struct {
Provider string `bun:"provider" json:"provider"`
}

type serviceOutput struct {
Service string `bun:"service" json:"service"`
}

type accountOutput struct {
Account string `bun:"account" json:"account"`
}

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

type Controller struct {
repo Repository
}

func New(repo Repository) *Controller {
return &Controller{repo}
}
39 changes: 39 additions & 0 deletions controller/resources.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package controller

import (
"context"
"strings"

"github.com/tailwarden/komiser/models"
"github.com/tailwarden/komiser/repository"
)

func (ctrl *Controller) GetResource(c context.Context, resourceId string) (resource models.Resource, err error) {
_, err = ctrl.repo.HandleQuery(c, repository.ListKey, &resource, [][3]string{{"resource_id", "=", resourceId}})
return
}

func (ctrl *Controller) GetResources(c context.Context, idList string) (resources []models.Resource, err error) {
_, err = ctrl.repo.HandleQuery(c, repository.ListKey, &resources, [][3]string{{"id", "IN", "(" + strings.Trim(idList, "[]") + ")"}})
return
}

func (ctrl *Controller) ListResources(c context.Context) (resources []models.Resource, err error) {
_, err = ctrl.repo.HandleQuery(c, repository.ListKey, &resources, [][3]string{})
return
}

func (ctrl *Controller) CountRegionsFromResources(c context.Context) (regions totalOutput, err error) {
_, err = ctrl.repo.HandleQuery(c, repository.RegionResourceCountKey, &regions, [][3]string{})
return
}

func (ctrl *Controller) CountRegionsFromAccounts(c context.Context) (accounts totalOutput, err error) {
_, err = ctrl.repo.HandleQuery(c, repository.AccountsResourceCountKey, &accounts, [][3]string{})
return
}

func (ctrl *Controller) SumResourceCost(c context.Context) (cost costOutput, err error) {
_, err = ctrl.repo.HandleQuery(c, repository.ResourceCostSumKey, &cost, [][3]string{})
return
}
33 changes: 33 additions & 0 deletions controller/stats.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package controller

import (
"context"

"github.com/tailwarden/komiser/models"
"github.com/tailwarden/komiser/repository"
)

func (ctrl *Controller) LocationStatsBreakdown(c context.Context) (groups []models.OutputResources, err error) {
_, err = ctrl.repo.HandleQuery(c, repository.LocationBreakdownStatKey, &groups, [][3]string{})
return
}

func (ctrl *Controller) ListRegions(c context.Context) (regions []regionOutput, err error) {
_, err = ctrl.repo.HandleQuery(c, repository.ListRegionsKey, &regions, nil)
return
}

func (ctrl *Controller) ListProviders(c context.Context) (providers []providerOutput, err error) {
_, err = ctrl.repo.HandleQuery(c, repository.ListProvidersKey, &providers, nil)
return
}

func (ctrl *Controller) ListServices(c context.Context) (services []serviceOutput, err error) {
_, err = ctrl.repo.HandleQuery(c, repository.ListServicesKey, &services, nil)
return
}

func (ctrl *Controller) ListAccountNames(c context.Context) (accounts []accountOutput, err error) {
_, err = ctrl.repo.HandleQuery(c, repository.ListAccountsKey, &accounts, nil)
return
}
15 changes: 15 additions & 0 deletions controller/tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package controller

import (
"context"
"fmt"

"github.com/tailwarden/komiser/models"
"github.com/tailwarden/komiser/repository"
)

func (ctrl *Controller) UpdateTags(c context.Context, tags []models.Tag, resourceId string) (resource models.Resource, err error) {
resource.Tags = tags
_, err = ctrl.repo.HandleQuery(c, repository.UpdateTagsKey, &resource, [][3]string{{"id", "=", fmt.Sprint(resourceId)}})
return
}
46 changes: 46 additions & 0 deletions controller/views.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package controller

import (
"context"

"github.com/tailwarden/komiser/models"
"github.com/tailwarden/komiser/repository"
)

func (ctrl *Controller) GetView(c context.Context, viewId string) (view models.View, err error) {
_, err = ctrl.repo.HandleQuery(c, repository.ListKey, &view, [][3]string{{"id", "=", viewId}})
return
}

func (ctrl *Controller) ListViews(c context.Context) (views []models.View, err error) {
_, err = ctrl.repo.HandleQuery(c, repository.ListKey, &views, [][3]string{})
return
}

func (ctrl *Controller) InsertView(c context.Context, view models.View) (viewId int64, err error) {
result, err := ctrl.repo.HandleQuery(c, repository.InsertKey, &view, nil)
if err != nil {
return
}
return result.LastInsertId()
}

func (ctrl *Controller) UpdateView(c context.Context, view models.View, viewId string) (err error) {
_, err = ctrl.repo.HandleQuery(c, repository.UpdateViewKey, &view, [][3]string{{"id", "=", viewId}})
return
}

func (ctrl *Controller) DeleteView(c context.Context, viewId string) (err error) {
_, err = ctrl.repo.HandleQuery(c, repository.DeleteKey, new(models.View), [][3]string{{"id", "=", viewId}})
return
}

func (ctrl *Controller) UpdateViewExclude(c context.Context, view models.View, viewId string) (err error) {
_, err = ctrl.repo.HandleQuery(c, repository.UpdateViewExcludeKey, &view, [][3]string{{"id", "=", viewId}})
return
}

func (ctrl *Controller) ListViewAlerts(c context.Context, viewId string) (alerts []models.Alert, err error) {
_, err = ctrl.repo.HandleQuery(c, repository.ListKey, &alerts, [][3]string{{"view_id", "=", viewId}})
return
}
Loading

0 comments on commit 3edf7c6

Please sign in to comment.