From d1ba65ac5744a575c2e180a4ee8579799ec51664 Mon Sep 17 00:00:00 2001 From: Azanul Date: Tue, 14 Nov 2023 00:10:45 +0530 Subject: [PATCH] fix: Civo Database not detected Signed-off-by: Azanul --- providers/civo/civo.go | 5 ++--- providers/civo/storage/databases.go | 26 ++++++++++++++++++-------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/providers/civo/civo.go b/providers/civo/civo.go index 715611f1a..3a64e690f 100644 --- a/providers/civo/civo.go +++ b/providers/civo/civo.go @@ -2,10 +2,9 @@ package civo import ( "context" - "log" "github.com/civo/civogo" - "github.com/sirupsen/logrus" + log "github.com/sirupsen/logrus" "github.com/tailwarden/komiser/providers" "github.com/tailwarden/komiser/providers/civo/compute" "github.com/tailwarden/komiser/providers/civo/kubernetes" @@ -51,7 +50,7 @@ func FetchResources(ctx context.Context, client providers.ProviderClient, db *bu for _, resource := range resources { _, err := db.NewInsert().Model(&resource).On("CONFLICT (resource_id) DO UPDATE").Set("cost = EXCLUDED.cost, relations=EXCLUDED.relations").Exec(context.Background()) if err != nil { - logrus.WithError(err).Errorf("db trigger failed") + log.WithError(err).Errorf("db trigger failed") } } if telemetry { diff --git a/providers/civo/storage/databases.go b/providers/civo/storage/databases.go index 3f103d1f3..356253c63 100644 --- a/providers/civo/storage/databases.go +++ b/providers/civo/storage/databases.go @@ -3,7 +3,6 @@ package storage import ( "context" "fmt" - "strconv" "time" "github.com/civo/civogo" @@ -13,22 +12,25 @@ import ( "github.com/tailwarden/komiser/providers" ) -func Databases(ctx context.Context, client providers.ProviderClient) ([]models.Resource, error) { +func Databases(_ context.Context, client providers.ProviderClient) ([]models.Resource, error) { resources := make([]models.Resource, 0) + // pagination is not yet implemented in client.CivoClient.ListDatabases() in Civo client library. Once its implemented, it would look like something below + // func (c *Client) ListDatabases(page int, perPage int) (*PaginatedDatabaseList, error) {} paginatedDatabases, err := client.CivoClient.ListDatabases() if err != nil { return resources, err } - // pagination is not yet implemented in client.CivoClient.ListDatabases() in Civo client library. Once its implemented, it would look like something below - // func (c *Client) ListDatabases(page int, perPage int) (*PaginatedDatabaseList, error) {} + + sizes, err := client.CivoClient.ListInstanceSizes() + if err != nil { + return resources, err + } + sizeMap := getSizeMap(sizes) for _, resource := range paginatedDatabases.Items { - resourceInGB, err := strconv.Atoi(resource.Size) - if err != nil { - return resources, nil - } + resourceInGB := sizeMap[resource.Size] monthlyCost := float64((resourceInGB / 20) * (20 + (resource.Nodes-1)*15)) @@ -81,3 +83,11 @@ func getDatabaseRelation(db civogo.Database) []models.Link { return rel } + +func getSizeMap(sizes []civogo.InstanceSize) map[string]int { + sm := make(map[string]int) + for _, size := range sizes { + sm[size.Name] = size.DiskGigabytes + } + return sm +}