Skip to content

Commit 3edf7c6

Browse files
authored
Merge pull request #1426 from tailwarden/develop
v3.1.17 release 🚀
2 parents 6732ea4 + 438711e commit 3edf7c6

32 files changed

+3055
-2263
lines changed

controller/accounts.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package controller
2+
3+
import (
4+
"context"
5+
6+
"github.com/tailwarden/komiser/models"
7+
"github.com/tailwarden/komiser/repository"
8+
)
9+
10+
func (ctrl *Controller) ListAccounts(c context.Context) (accounts []models.Account, err error) {
11+
_, err = ctrl.repo.HandleQuery(c, repository.ListKey, &accounts, nil)
12+
return
13+
}
14+
15+
func (ctrl *Controller) CountResources(c context.Context, provider, name string) (output totalOutput, err error) {
16+
conditions := [][3]string{}
17+
if provider != "" {
18+
conditions = append(conditions, [3]string{"provider", "=", provider})
19+
}
20+
if name != "" {
21+
conditions = append(conditions, [3]string{"account", "=", name})
22+
}
23+
_, err = ctrl.repo.HandleQuery(c, repository.ResourceCountKey, &output, conditions)
24+
return
25+
}
26+
27+
func (ctrl *Controller) InsertAccount(c context.Context, account models.Account) (lastId int64, err error) {
28+
result, err := ctrl.repo.HandleQuery(c, repository.InsertKey, &account, nil)
29+
if err != nil {
30+
return
31+
}
32+
return result.LastInsertId()
33+
}
34+
35+
func (ctrl *Controller) RescanAccount(c context.Context, account *models.Account, accountId string) (rows int64, err error) {
36+
res, err := ctrl.repo.HandleQuery(c, repository.ReScanAccountKey, account, [][3]string{{"id", "=", accountId}, {"status", "=", "CONNECTED"}})
37+
if err != nil {
38+
return 0, err
39+
}
40+
return res.RowsAffected()
41+
}
42+
43+
func (ctrl *Controller) DeleteAccount(c context.Context, accountId string) (err error) {
44+
_, err = ctrl.repo.HandleQuery(c, repository.DeleteKey, new(models.Account), [][3]string{{"id", "=", accountId}})
45+
return
46+
}
47+
48+
func (ctrl *Controller) UpdateAccount(c context.Context, account models.Account, accountId string) (err error) {
49+
_, err = ctrl.repo.HandleQuery(c, repository.UpdateAccountKey, &account, [][3]string{{"id", "=", accountId}})
50+
return
51+
}

controller/alerts.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package controller
2+
3+
import (
4+
"context"
5+
6+
"github.com/tailwarden/komiser/models"
7+
"github.com/tailwarden/komiser/repository"
8+
)
9+
10+
func (ctrl *Controller) InsertAlert(c context.Context, alert models.Alert) (alertId int64, err error) {
11+
result, err := ctrl.repo.HandleQuery(c, repository.InsertKey, &alert, nil)
12+
if err != nil {
13+
return
14+
}
15+
return result.LastInsertId()
16+
}
17+
18+
func (ctrl *Controller) UpdateAlert(c context.Context, alert models.Alert, alertId string) (err error) {
19+
_, err = ctrl.repo.HandleQuery(c, repository.UpdateAlertKey, &alert, [][3]string{{"id", "=", alertId}})
20+
return
21+
}
22+
23+
func (ctrl *Controller) DeleteAlert(c context.Context, alertId string) (err error) {
24+
_, err = ctrl.repo.HandleQuery(c, repository.DeleteKey, new(models.Alert), [][3]string{{"id", "=", alertId}})
25+
return
26+
}

controller/controller.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package controller
2+
3+
import (
4+
"context"
5+
"database/sql"
6+
)
7+
8+
type totalOutput struct {
9+
Total int `bun:"total" json:"total"`
10+
}
11+
12+
type costOutput struct {
13+
Total float64 `bun:"sum" json:"total"`
14+
}
15+
16+
type regionOutput struct {
17+
Region string `bun:"region" json:"region"`
18+
}
19+
20+
type providerOutput struct {
21+
Provider string `bun:"provider" json:"provider"`
22+
}
23+
24+
type serviceOutput struct {
25+
Service string `bun:"service" json:"service"`
26+
}
27+
28+
type accountOutput struct {
29+
Account string `bun:"account" json:"account"`
30+
}
31+
32+
type Repository interface {
33+
HandleQuery(context.Context, string, interface{}, [][3]string) (sql.Result, error)
34+
}
35+
36+
type Controller struct {
37+
repo Repository
38+
}
39+
40+
func New(repo Repository) *Controller {
41+
return &Controller{repo}
42+
}

controller/resources.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package controller
2+
3+
import (
4+
"context"
5+
"strings"
6+
7+
"github.com/tailwarden/komiser/models"
8+
"github.com/tailwarden/komiser/repository"
9+
)
10+
11+
func (ctrl *Controller) GetResource(c context.Context, resourceId string) (resource models.Resource, err error) {
12+
_, err = ctrl.repo.HandleQuery(c, repository.ListKey, &resource, [][3]string{{"resource_id", "=", resourceId}})
13+
return
14+
}
15+
16+
func (ctrl *Controller) GetResources(c context.Context, idList string) (resources []models.Resource, err error) {
17+
_, err = ctrl.repo.HandleQuery(c, repository.ListKey, &resources, [][3]string{{"id", "IN", "(" + strings.Trim(idList, "[]") + ")"}})
18+
return
19+
}
20+
21+
func (ctrl *Controller) ListResources(c context.Context) (resources []models.Resource, err error) {
22+
_, err = ctrl.repo.HandleQuery(c, repository.ListKey, &resources, [][3]string{})
23+
return
24+
}
25+
26+
func (ctrl *Controller) CountRegionsFromResources(c context.Context) (regions totalOutput, err error) {
27+
_, err = ctrl.repo.HandleQuery(c, repository.RegionResourceCountKey, &regions, [][3]string{})
28+
return
29+
}
30+
31+
func (ctrl *Controller) CountRegionsFromAccounts(c context.Context) (accounts totalOutput, err error) {
32+
_, err = ctrl.repo.HandleQuery(c, repository.AccountsResourceCountKey, &accounts, [][3]string{})
33+
return
34+
}
35+
36+
func (ctrl *Controller) SumResourceCost(c context.Context) (cost costOutput, err error) {
37+
_, err = ctrl.repo.HandleQuery(c, repository.ResourceCostSumKey, &cost, [][3]string{})
38+
return
39+
}

controller/stats.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package controller
2+
3+
import (
4+
"context"
5+
6+
"github.com/tailwarden/komiser/models"
7+
"github.com/tailwarden/komiser/repository"
8+
)
9+
10+
func (ctrl *Controller) LocationStatsBreakdown(c context.Context) (groups []models.OutputResources, err error) {
11+
_, err = ctrl.repo.HandleQuery(c, repository.LocationBreakdownStatKey, &groups, [][3]string{})
12+
return
13+
}
14+
15+
func (ctrl *Controller) ListRegions(c context.Context) (regions []regionOutput, err error) {
16+
_, err = ctrl.repo.HandleQuery(c, repository.ListRegionsKey, &regions, nil)
17+
return
18+
}
19+
20+
func (ctrl *Controller) ListProviders(c context.Context) (providers []providerOutput, err error) {
21+
_, err = ctrl.repo.HandleQuery(c, repository.ListProvidersKey, &providers, nil)
22+
return
23+
}
24+
25+
func (ctrl *Controller) ListServices(c context.Context) (services []serviceOutput, err error) {
26+
_, err = ctrl.repo.HandleQuery(c, repository.ListServicesKey, &services, nil)
27+
return
28+
}
29+
30+
func (ctrl *Controller) ListAccountNames(c context.Context) (accounts []accountOutput, err error) {
31+
_, err = ctrl.repo.HandleQuery(c, repository.ListAccountsKey, &accounts, nil)
32+
return
33+
}

controller/tags.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package controller
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/tailwarden/komiser/models"
8+
"github.com/tailwarden/komiser/repository"
9+
)
10+
11+
func (ctrl *Controller) UpdateTags(c context.Context, tags []models.Tag, resourceId string) (resource models.Resource, err error) {
12+
resource.Tags = tags
13+
_, err = ctrl.repo.HandleQuery(c, repository.UpdateTagsKey, &resource, [][3]string{{"id", "=", fmt.Sprint(resourceId)}})
14+
return
15+
}

controller/views.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package controller
2+
3+
import (
4+
"context"
5+
6+
"github.com/tailwarden/komiser/models"
7+
"github.com/tailwarden/komiser/repository"
8+
)
9+
10+
func (ctrl *Controller) GetView(c context.Context, viewId string) (view models.View, err error) {
11+
_, err = ctrl.repo.HandleQuery(c, repository.ListKey, &view, [][3]string{{"id", "=", viewId}})
12+
return
13+
}
14+
15+
func (ctrl *Controller) ListViews(c context.Context) (views []models.View, err error) {
16+
_, err = ctrl.repo.HandleQuery(c, repository.ListKey, &views, [][3]string{})
17+
return
18+
}
19+
20+
func (ctrl *Controller) InsertView(c context.Context, view models.View) (viewId int64, err error) {
21+
result, err := ctrl.repo.HandleQuery(c, repository.InsertKey, &view, nil)
22+
if err != nil {
23+
return
24+
}
25+
return result.LastInsertId()
26+
}
27+
28+
func (ctrl *Controller) UpdateView(c context.Context, view models.View, viewId string) (err error) {
29+
_, err = ctrl.repo.HandleQuery(c, repository.UpdateViewKey, &view, [][3]string{{"id", "=", viewId}})
30+
return
31+
}
32+
33+
func (ctrl *Controller) DeleteView(c context.Context, viewId string) (err error) {
34+
_, err = ctrl.repo.HandleQuery(c, repository.DeleteKey, new(models.View), [][3]string{{"id", "=", viewId}})
35+
return
36+
}
37+
38+
func (ctrl *Controller) UpdateViewExclude(c context.Context, view models.View, viewId string) (err error) {
39+
_, err = ctrl.repo.HandleQuery(c, repository.UpdateViewExcludeKey, &view, [][3]string{{"id", "=", viewId}})
40+
return
41+
}
42+
43+
func (ctrl *Controller) ListViewAlerts(c context.Context, viewId string) (alerts []models.Alert, err error) {
44+
_, err = ctrl.repo.HandleQuery(c, repository.ListKey, &alerts, [][3]string{{"view_id", "=", viewId}})
45+
return
46+
}

0 commit comments

Comments
 (0)