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

SOLID #1411

Merged
merged 31 commits into from
May 10, 2024
Merged

SOLID #1411

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
ae086e4
feat: mark successful triggering of workflow even without any support…
Azanul Feb 27, 2024
e32299e
feat: engines entry
Azanul Mar 2, 2024
79315da
Merge branch 'develop' into develop
Azanul Mar 4, 2024
64650a2
feat: reduce code duplication
Azanul Mar 5, 2024
707ba40
Merge branch 'develop' into develop
Azanul Mar 8, 2024
252bf73
feat: update codeowners
Azanul Mar 8, 2024
22ff31b
Merge branch 'develop' into develop
Azanul Mar 8, 2024
6ccc4ac
feat: Azure cost PoC
Azanul Mar 13, 2024
e57e527
Merge branch 'develop' into develop
Azanul Mar 13, 2024
98186b5
feat: log schema setup err
Azanul Mar 13, 2024
5c7abbf
feat: log usage err
Azanul Mar 15, 2024
a521582
Merge branch 'develop' into develop
Azanul Mar 22, 2024
180eee7
fix: aws config env vars support
Azanul Mar 28, 2024
e31cbcc
Merge branch 'develop' into develop
Azanul Mar 28, 2024
e8e57bd
feat: log schema setup err
Azanul Apr 2, 2024
14be2e1
feat: separate create config
Azanul Apr 5, 2024
a9723eb
feat: use util to create config
Azanul Apr 5, 2024
f1f91f8
Merge branch 'develop' into develop
Azanul Apr 6, 2024
f3d351e
refac: clean code
Azanul Apr 20, 2024
cd1d7d2
refac: seperate errors
Azanul Apr 20, 2024
e5084b4
refac: seperate jobs
Azanul Apr 20, 2024
c68bb50
Merge branch 'develop' into develop
Azanul May 3, 2024
f0a9492
feat: accounts controller
Azanul May 6, 2024
cfe1657
feat: resources controller
Azanul May 6, 2024
6b70baa
feat: tags controller
Azanul May 6, 2024
b99742f
feat: csv controller
Azanul May 8, 2024
72e8aa1
feat: alerts controller
Azanul May 8, 2024
7987186
feat: dashboard controller
Azanul May 8, 2024
724ebb6
feat: stats controller
Azanul May 9, 2024
4d035b0
feat: views controller
Azanul May 9, 2024
29c5bcd
feat: sql -> postgres
Azanul May 9, 2024
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
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
}
44 changes: 44 additions & 0 deletions controller/controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package controller

import (
"context"
"database/sql"

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

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, repository.QueryType, 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
Loading