From e9bafadee405925771c5c784429c37836c76d4f6 Mon Sep 17 00:00:00 2001 From: Abhay Kumar Gupta Date: Sat, 17 Jun 2023 12:34:37 +0530 Subject: [PATCH 01/13] Create /sql/sql.go fil-issue(#823) --- providers/linode/sql/sql.go | 184 ++++++++++++++++++++++++++++++++++++ 1 file changed, 184 insertions(+) create mode 100644 providers/linode/sql/sql.go diff --git a/providers/linode/sql/sql.go b/providers/linode/sql/sql.go new file mode 100644 index 000000000..4b25d9a5a --- /dev/null +++ b/providers/linode/sql/sql.go @@ -0,0 +1,184 @@ +package sql + +import ( + "context" + "fmt" + "strings" + "time" + + "github.com/linode/linodego" + log "github.com/sirupsen/logrus" + + "github.com/tailwarden/komiser/models" + . "github.com/tailwarden/komiser/models" + "github.com/tailwarden/komiser/providers" +) + +// Cost data for Dedicated CPU instances +var dedicatedCPUCosts = map[string]map[string]float64{ + "Dedicated 4GB": { + "1 Node": 65.00, + "3 Node": 195.00, + }, + "Dedicated 8GB": { + "1 Node": 130.00, + "3 Node": 390.00, + }, + "Dedicated 16GB": { + "1 Node": 260.00, + "3 Node": 780.00, + }, + "Dedicated 32GB": { + "1 Node": 520.00, + "3 Node": 1560.00, + }, + "Dedicated 64GB": { + "1 Node": 1040.00, + "3 Node": 3120.00, + }, + "Dedicated 96GB": { + "1 Node": 1560.00, + "3 Node": 4680.00, + }, + "Dedicated 128GB": { + "1 Node": 2080.00, + "3 Node": 6240.00, + }, + "Dedicated 256GB": { + "1 Node": 4160.00, + "3 Node": 12480.00, + }, + "Dedicated 512GB": { + "1 Node": 8320.00, + "3 Node": 24960.00, + }, +} + +// Cost data for Shared CPU instances +var sharedCPUCosts = map[string]map[string]float64{ + "Shared 1GB": { + "1 Node": 15.00, + "3 Node": 35.00, + }, + "Shared 2GB": { + "1 Node": 30.00, + "3 Node": 70.00, + }, + "Shared 4GB": { + "1 Node": 60.00, + "3 Node": 140.00, + }, + "Shared 8GB": { + "1 Node": 120.00, + "3 Node": 280.00, + }, + "Shared 16GB": { + "1 Node": 240.00, + "3 Node": 560.00, + }, + "Shared 32GB": { + "1 Node": 480.00, + "3 Node": 1120.00, + }, + "Shared 64GB": { + "1 Node": 960.00, + "3 Node": 2240.00, + }, + "Shared 96GB": { + "1 Node": 1440.00, + "3 Node": 3360.00, + }, + "Shared 128GB": { + "1 Node": 1920.00, + "3 Node": 4480.00, + }, + "Shared 192GB": { + "1 Node": 2880.00, + "3 Node": 6720.00, + }, + "Shared 256GB": { + "1 Node": 3840.00, + "3 Node": 8960.00, + }, +} + +// GetSQLInstances fetches SQL instances from the provider and returns them as resources. +func GetSQLInstances(ctx context.Context, client providers.ProviderClient) ([]models.Resource, error) { + resources := make([]models.Resource, 0) + + instances, err := client.SQLClient.GetInstances(ctx) + if err != nil { + return resources, err + } + + for _, instance := range instances { + tags := make([]models.Tag, 0) + for _, tag := range instance.Tags { + if strings.Contains(tag, ":") { + parts := strings.Split(tag, ":") + tags = append(tags, models.Tag{ + Key: parts[0], + Value: parts[1], + }) + } else { + tags = append(tags, models.Tag{ + Key: tag, + Value: tag, + }) + } + } + + // Calculate the cost based on the instance type and node count + cost, ok := getSQLInstanceCost(instance.Type, instance.NodeCount) + if !ok { + log.Warnf("Failed to calculate cost for SQL instance: %s, Type: %s, NodeCount: %d", instance.ID, instance.Type, instance.NodeCount) + } + + resources = append(resources, models.Resource{ + Provider: "Linode", + Account: client.Name, + Service: "SQL", + Region: instance.Region, + ResourceId: instance.ID, + Cost: cost, + Name: instance.Label, + FetchedAt: time.Now(), + CreatedAt: instance.Created, + Tags: tags, + Link: fmt.Sprintf("https://cloud.linode.com/databases/%s", instance.ID), + }) + } + + log.WithFields(log.Fields{ + "provider": "Linode", + "account": client.Name, + "service": "SQL", + "resources": len(resources), + }).Info("Fetched resources") + return resources, nil +} + +// getSQLInstanceCost calculates the cost for a SQL instance based on the instance type and node count. +func getSQLInstanceCost(instanceType string, nodeCount int) (float64, bool) { + var costs map[string]map[string]float64 + + if strings.HasPrefix(instanceType, "Dedicated") { + costs = dedicatedCPUCosts + } else if strings.HasPrefix(instanceType, "Shared") { + costs = sharedCPUCosts + } else { + return 0, false + } + + costMap, ok := costs[instanceType] + if !ok { + return 0, false + } + + cost, ok := costMap[fmt.Sprintf("%d Node", nodeCount)] + if !ok { + return 0, false + } + + return cost, true +} From 334d4bac2ce04380462ef8c1b565868d88cb415c Mon Sep 17 00:00:00 2001 From: Abhay Kumar Gupta Date: Sat, 17 Jun 2023 12:36:07 +0530 Subject: [PATCH 02/13] Update instances.go To display cost as per usage. --- providers/linode/compute/instances.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/providers/linode/compute/instances.go b/providers/linode/compute/instances.go index b15a9a529..98402c038 100644 --- a/providers/linode/compute/instances.go +++ b/providers/linode/compute/instances.go @@ -39,13 +39,24 @@ func Linodes(ctx context.Context, client providers.ProviderClient) ([]Resource, } } + cost, ok := GetCost(instance.Type) + if !ok { + log.WithFields(log.Fields{ + "provider": "Linode", + "account": client.Name, + "service": "Linode", + "instance": instance.Label, + "instanceID": instance.ID, + }).Warn("Failed to get cost for instance") + } + resources = append(resources, models.Resource{ Provider: "Linode", Account: client.Name, Service: "Linode", Region: instance.Region, ResourceId: fmt.Sprintf("%d", instance.ID), - Cost: 0, + Cost: cost, Name: instance.Label, FetchedAt: time.Now(), CreatedAt: *instance.Created, From 0bf7e5716312bcc10f522dd64e2eed48310f5e04 Mon Sep 17 00:00:00 2001 From: Abhay Kumar Gupta Date: Sat, 17 Jun 2023 21:21:49 +0530 Subject: [PATCH 03/13] Update instances.go --- providers/linode/compute/instances.go | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/providers/linode/compute/instances.go b/providers/linode/compute/instances.go index 98402c038..b15a9a529 100644 --- a/providers/linode/compute/instances.go +++ b/providers/linode/compute/instances.go @@ -39,24 +39,13 @@ func Linodes(ctx context.Context, client providers.ProviderClient) ([]Resource, } } - cost, ok := GetCost(instance.Type) - if !ok { - log.WithFields(log.Fields{ - "provider": "Linode", - "account": client.Name, - "service": "Linode", - "instance": instance.Label, - "instanceID": instance.ID, - }).Warn("Failed to get cost for instance") - } - resources = append(resources, models.Resource{ Provider: "Linode", Account: client.Name, Service: "Linode", Region: instance.Region, ResourceId: fmt.Sprintf("%d", instance.ID), - Cost: cost, + Cost: 0, Name: instance.Label, FetchedAt: time.Now(), CreatedAt: *instance.Created, From 3db9939704ae6689fa3dd783ba28bb7a2528c7ef Mon Sep 17 00:00:00 2001 From: Abhay Kumar Gupta Date: Sun, 18 Jun 2023 18:32:47 +0530 Subject: [PATCH 04/13] Update linode.go imported sql.go --- providers/linode/linode.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/providers/linode/linode.go b/providers/linode/linode.go index fc5153ee7..3717cbd35 100644 --- a/providers/linode/linode.go +++ b/providers/linode/linode.go @@ -10,6 +10,7 @@ import ( "github.com/tailwarden/komiser/providers" "github.com/tailwarden/komiser/providers/linode/compute" + "github.com/tailwarden/komiser/providers/linode/sql" "github.com/tailwarden/komiser/providers/linode/storage" "github.com/uptrace/bun" ) @@ -23,6 +24,7 @@ func listOfSupportedServices() []providers.FetchDataFunction { storage.Buckets, networking.NodeBalancers, networking.Firewalls, + sql.Sql, } } From 7342fc2f5c3cb4764935c2bb8bb661838a663bf6 Mon Sep 17 00:00:00 2001 From: Abhay Kumar Gupta Date: Sun, 18 Jun 2023 18:41:18 +0530 Subject: [PATCH 05/13] Update linode.go //make call to the `GetSQLInstances` function of /sql/sql.go --- providers/linode/linode.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/providers/linode/linode.go b/providers/linode/linode.go index 3717cbd35..bce79d8b5 100644 --- a/providers/linode/linode.go +++ b/providers/linode/linode.go @@ -24,7 +24,7 @@ func listOfSupportedServices() []providers.FetchDataFunction { storage.Buckets, networking.NodeBalancers, networking.Firewalls, - sql.Sql, + sql.GetSQLInstances, } } From 64894dcda44d078d6dd5723c419719a04fb0b9b2 Mon Sep 17 00:00:00 2001 From: Abhay Kumar Gupta Date: Wed, 21 Jun 2023 05:46:29 +0530 Subject: [PATCH 06/13] Update sql.go Changes Function name --- providers/linode/sql/sql.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/providers/linode/sql/sql.go b/providers/linode/sql/sql.go index 4b25d9a5a..333cbc0b8 100644 --- a/providers/linode/sql/sql.go +++ b/providers/linode/sql/sql.go @@ -102,8 +102,8 @@ var sharedCPUCosts = map[string]map[string]float64{ }, } -// GetSQLInstances fetches SQL instances from the provider and returns them as resources. -func GetSQLInstances(ctx context.Context, client providers.ProviderClient) ([]models.Resource, error) { +// Instances fetches SQL instances from the provider and returns them as resources. +func Instances(ctx context.Context, client providers.ProviderClient) ([]models.Resource, error) { resources := make([]models.Resource, 0) instances, err := client.SQLClient.GetInstances(ctx) @@ -129,7 +129,7 @@ func GetSQLInstances(ctx context.Context, client providers.ProviderClient) ([]mo } // Calculate the cost based on the instance type and node count - cost, ok := getSQLInstanceCost(instance.Type, instance.NodeCount) + cost, ok := InstancesCost(instance.Type, instance.NodeCount) if !ok { log.Warnf("Failed to calculate cost for SQL instance: %s, Type: %s, NodeCount: %d", instance.ID, instance.Type, instance.NodeCount) } @@ -158,8 +158,8 @@ func GetSQLInstances(ctx context.Context, client providers.ProviderClient) ([]mo return resources, nil } -// getSQLInstanceCost calculates the cost for a SQL instance based on the instance type and node count. -func getSQLInstanceCost(instanceType string, nodeCount int) (float64, bool) { +// InstancesCost calculates the cost for a SQL instance based on the instance type and node count. +func InstancesCost(instanceType string, nodeCount int) (float64, bool) { var costs map[string]map[string]float64 if strings.HasPrefix(instanceType, "Dedicated") { From 756a56dfce7f772a948b2e8f4d7b701659971366 Mon Sep 17 00:00:00 2001 From: Abhay Kumar Gupta Date: Wed, 21 Jun 2023 05:48:02 +0530 Subject: [PATCH 07/13] Update linode.go --- providers/linode/linode.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/providers/linode/linode.go b/providers/linode/linode.go index bce79d8b5..70b6de4d7 100644 --- a/providers/linode/linode.go +++ b/providers/linode/linode.go @@ -24,7 +24,7 @@ func listOfSupportedServices() []providers.FetchDataFunction { storage.Buckets, networking.NodeBalancers, networking.Firewalls, - sql.GetSQLInstances, + sql.Instances, } } From 6191ec6bae93edacf4c751e1658e77714f3e1505 Mon Sep 17 00:00:00 2001 From: Abhay Kumar Gupta Date: Fri, 23 Jun 2023 09:03:30 +0530 Subject: [PATCH 08/13] Update sql.go --- providers/linode/sql/sql.go | 136 +++++++++++------------------------- 1 file changed, 41 insertions(+), 95 deletions(-) diff --git a/providers/linode/sql/sql.go b/providers/linode/sql/sql.go index 333cbc0b8..c4d6d5a15 100644 --- a/providers/linode/sql/sql.go +++ b/providers/linode/sql/sql.go @@ -15,91 +15,31 @@ import ( ) // Cost data for Dedicated CPU instances -var dedicatedCPUCosts = map[string]map[string]float64{ - "Dedicated 4GB": { - "1 Node": 65.00, - "3 Node": 195.00, - }, - "Dedicated 8GB": { - "1 Node": 130.00, - "3 Node": 390.00, - }, - "Dedicated 16GB": { - "1 Node": 260.00, - "3 Node": 780.00, - }, - "Dedicated 32GB": { - "1 Node": 520.00, - "3 Node": 1560.00, - }, - "Dedicated 64GB": { - "1 Node": 1040.00, - "3 Node": 3120.00, - }, - "Dedicated 96GB": { - "1 Node": 1560.00, - "3 Node": 4680.00, - }, - "Dedicated 128GB": { - "1 Node": 2080.00, - "3 Node": 6240.00, - }, - "Dedicated 256GB": { - "1 Node": 4160.00, - "3 Node": 12480.00, - }, - "Dedicated 512GB": { - "1 Node": 8320.00, - "3 Node": 24960.00, - }, +var dedicatedCPUCosts = map[string]float64{ + "Dedicated 4GB": 65.00, + "Dedicated 8GB": 130.00, + "Dedicated 16GB": 260.00, + "Dedicated 32GB": 520.00, + "Dedicated 64GB": 1040.00, + "Dedicated 96GB": 1560.00, + "Dedicated 128GB": 2080.00, + "Dedicated 256GB": 4160.00, + "Dedicated 512GB": 8320.00, } // Cost data for Shared CPU instances -var sharedCPUCosts = map[string]map[string]float64{ - "Shared 1GB": { - "1 Node": 15.00, - "3 Node": 35.00, - }, - "Shared 2GB": { - "1 Node": 30.00, - "3 Node": 70.00, - }, - "Shared 4GB": { - "1 Node": 60.00, - "3 Node": 140.00, - }, - "Shared 8GB": { - "1 Node": 120.00, - "3 Node": 280.00, - }, - "Shared 16GB": { - "1 Node": 240.00, - "3 Node": 560.00, - }, - "Shared 32GB": { - "1 Node": 480.00, - "3 Node": 1120.00, - }, - "Shared 64GB": { - "1 Node": 960.00, - "3 Node": 2240.00, - }, - "Shared 96GB": { - "1 Node": 1440.00, - "3 Node": 3360.00, - }, - "Shared 128GB": { - "1 Node": 1920.00, - "3 Node": 4480.00, - }, - "Shared 192GB": { - "1 Node": 2880.00, - "3 Node": 6720.00, - }, - "Shared 256GB": { - "1 Node": 3840.00, - "3 Node": 8960.00, - }, +var sharedCPUCosts = map[string]float64{ + "Shared 1GB": 15.00, + "Shared 2GB": 30.00, + "Shared 4GB": 60.00, + "Shared 8GB": 120.00, + "Shared 16GB": 240.00, + "Shared 32GB": 480.00, + "Shared 64GB": 960.00, + "Shared 96GB": 1440.00, + "Shared 128GB": 1920.00, + "Shared 192GB": 2880.00, + "Shared 256GB": 3840.00, } // Instances fetches SQL instances from the provider and returns them as resources. @@ -160,23 +100,29 @@ func Instances(ctx context.Context, client providers.ProviderClient) ([]models.R // InstancesCost calculates the cost for a SQL instance based on the instance type and node count. func InstancesCost(instanceType string, nodeCount int) (float64, bool) { - var costs map[string]map[string]float64 + var cost float64 if strings.HasPrefix(instanceType, "Dedicated") { - costs = dedicatedCPUCosts - } else if strings.HasPrefix(instanceType, "Shared") { - costs = sharedCPUCosts - } else { - return 0, false - } + cost, ok := dedicatedCPUCosts[instanceType] + if !ok { + return 0, false + } - costMap, ok := costs[instanceType] - if !ok { - return 0, false - } + // Adjust cost for 3 Node instances + if nodeCount == 3 { + cost *= 3 + } + } else if strings.HasPrefix(instanceType, "Shared") { + cost, ok := sharedCPUCosts[instanceType] + if !ok { + return 0, false + } - cost, ok := costMap[fmt.Sprintf("%d Node", nodeCount)] - if !ok { + // Adjust cost for 3 Node instances + if nodeCount == 3 { + cost *= 2.333 + } + } else { return 0, false } From 1284150fa6e9840d213a8b87e346727ad12fd0c3 Mon Sep 17 00:00:00 2001 From: Abhay Kumar Gupta Date: Thu, 29 Jun 2023 14:35:39 +0530 Subject: [PATCH 09/13] Update resource.go --- models/resource.go | 1 + 1 file changed, 1 insertion(+) diff --git a/models/resource.go b/models/resource.go index 785781939..31255d2a3 100644 --- a/models/resource.go +++ b/models/resource.go @@ -18,6 +18,7 @@ type Resource struct { Tags []Tag `json:"tags" bun:"tags,default:'[]'"` Link string `json:"link" bson:"link"` Value string `bun:",scanonly"` //to be deprecated + NodeCount int `json:"nodeCount"` // Add the NodeCount field } type Tag struct { From 42d2d49a1d0befb07409c05b5f5de54d73b03cd2 Mon Sep 17 00:00:00 2001 From: Abhay Kumar Gupta Date: Thu, 29 Jun 2023 14:46:32 +0530 Subject: [PATCH 10/13] Update instances.go --- providers/linode/compute/instances.go | 89 ++++++++++++++------------- 1 file changed, 46 insertions(+), 43 deletions(-) diff --git a/providers/linode/compute/instances.go b/providers/linode/compute/instances.go index b15a9a529..876a167ba 100644 --- a/providers/linode/compute/instances.go +++ b/providers/linode/compute/instances.go @@ -14,51 +14,54 @@ import ( "github.com/tailwarden/komiser/providers" ) -func Linodes(ctx context.Context, client providers.ProviderClient) ([]Resource, error) { - resources := make([]Resource, 0) +type LinodeInstance struct { + Instance *linodego.Instance + NodeCount int +} + +func Linodes(ctx context.Context, client providers.ProviderClient, linodeInstances []LinodeInstance) ([]Resource, error) { + resources := make([]Resource, 0) - instances, err := client.LinodeClient.ListInstances(ctx, &linodego.ListOptions{}) - if err != nil { - return resources, err - } + for _, linodeInstance := range linodeInstances { + instance := linodeInstance.Instance - for _, instance := range instances { - tags := make([]Tag, 0) - for _, tag := range instance.Tags { - if strings.Contains(tag, ":") { - parts := strings.Split(tag, ":") - tags = append(tags, models.Tag{ - Key: parts[0], - Value: parts[1], - }) - } else { - tags = append(tags, models.Tag{ - Key: tag, - Value: tag, - }) - } - } + tags := make([]Tag, 0) + for _, tag := range instance.Tags { + if strings.Contains(tag, ":") { + parts := strings.Split(tag, ":") + tags = append(tags, models.Tag{ + Key: parts[0], + Value: parts[1], + }) + } else { + tags = append(tags, models.Tag{ + Key: tag, + Value: tag, + }) + } + } - resources = append(resources, models.Resource{ - Provider: "Linode", - Account: client.Name, - Service: "Linode", - Region: instance.Region, - ResourceId: fmt.Sprintf("%d", instance.ID), - Cost: 0, - Name: instance.Label, - FetchedAt: time.Now(), - CreatedAt: *instance.Created, - Tags: tags, - Link: fmt.Sprintf("https://cloud.linode.com/linodes/%d", instance.ID), - }) - } + resources = append(resources, models.Resource{ + Provider: "Linode", + Account: client.Name, + Service: "Linode", + Region: instance.Region, + ResourceId: fmt.Sprintf("%d", instance.ID), + Cost: 0, + Name: instance.Label, + FetchedAt: time.Now(), + CreatedAt: *instance.Created, + Tags: tags, + Link: fmt.Sprintf("https://cloud.linode.com/linodes/%d", instance.ID), + NodeCount: linodeInstance.NodeCount, // Include the NodeCount value + }) + } - log.WithFields(log.Fields{ - "provider": "Linode", - "account": client.Name, - "service": "Linode", - "resources": len(resources), - }).Info("Fetched resources") - return resources, nil + log.WithFields(log.Fields{ + "provider": "Linode", + "account": client.Name, + "service": "Linode", + "resources": len(resources), + }).Info("Fetched resources") + return resources, nil } From ead0390317913606e114f297716426d538d32acf Mon Sep 17 00:00:00 2001 From: Abhay Kumar Gupta Date: Thu, 29 Jun 2023 15:13:13 +0530 Subject: [PATCH 11/13] Update linode.go --- providers/linode/linode.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/providers/linode/linode.go b/providers/linode/linode.go index 70b6de4d7..0ccda90f2 100644 --- a/providers/linode/linode.go +++ b/providers/linode/linode.go @@ -17,7 +17,7 @@ import ( func listOfSupportedServices() []providers.FetchDataFunction { return []providers.FetchDataFunction{ - compute.Linodes, + // compute.Linodes, compute.LKEClusters, storage.Volumes, storage.Databases, From 53eea82c2991865d1aa331f3fec9f15dfbcb18e1 Mon Sep 17 00:00:00 2001 From: Abhay Kumar Gupta Date: Thu, 29 Jun 2023 15:29:45 +0530 Subject: [PATCH 12/13] Update sql.go --- providers/linode/sql/sql.go | 54 +++++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 17 deletions(-) diff --git a/providers/linode/sql/sql.go b/providers/linode/sql/sql.go index c4d6d5a15..b5be5cf43 100644 --- a/providers/linode/sql/sql.go +++ b/providers/linode/sql/sql.go @@ -10,7 +10,7 @@ import ( log "github.com/sirupsen/logrus" "github.com/tailwarden/komiser/models" - . "github.com/tailwarden/komiser/models" + // . "github.com/tailwarden/komiser/models" "github.com/tailwarden/komiser/providers" ) @@ -46,7 +46,8 @@ var sharedCPUCosts = map[string]float64{ func Instances(ctx context.Context, client providers.ProviderClient) ([]models.Resource, error) { resources := make([]models.Resource, 0) - instances, err := client.SQLClient.GetInstances(ctx) + // Fetch instances from the Linode provider + instances, err := GetInstances(ctx, client) if err != nil { return resources, err } @@ -68,10 +69,10 @@ func Instances(ctx context.Context, client providers.ProviderClient) ([]models.R } } - // Calculate the cost based on the instance type and node count - cost, ok := InstancesCost(instance.Type, instance.NodeCount) + // Calculate the cost based on the instance type + cost, ok := InstancesCost(instance.Type) if !ok { - log.Warnf("Failed to calculate cost for SQL instance: %s, Type: %s, NodeCount: %d", instance.ID, instance.Type, instance.NodeCount) + log.Warnf("Failed to calculate cost for SQL instance: %s, Type: %s", instance.ID, instance.Type) } resources = append(resources, models.Resource{ @@ -79,13 +80,14 @@ func Instances(ctx context.Context, client providers.ProviderClient) ([]models.R Account: client.Name, Service: "SQL", Region: instance.Region, - ResourceId: instance.ID, + ResourceId: fmt.Sprintf("%d", instance.ID), Cost: cost, Name: instance.Label, FetchedAt: time.Now(), - CreatedAt: instance.Created, + CreatedAt: *instance.Created, Tags: tags, - Link: fmt.Sprintf("https://cloud.linode.com/databases/%s", instance.ID), + Link: fmt.Sprintf("https://cloud.linode.com/databases/%d", instance.ID), + NodeCount: instance.NodeCount, }) } @@ -98,21 +100,22 @@ func Instances(ctx context.Context, client providers.ProviderClient) ([]models.R return resources, nil } -// InstancesCost calculates the cost for a SQL instance based on the instance type and node count. +// InstancesCost calculates the cost for the given SQL instance type and node count. func InstancesCost(instanceType string, nodeCount int) (float64, bool) { - var cost float64 - - if strings.HasPrefix(instanceType, "Dedicated") { + // Calculate cost based on instance type + if strings.Contains(instanceType, "Dedicated") { cost, ok := dedicatedCPUCosts[instanceType] if !ok { return 0, false } - // Adjust cost for 3 Node instances + // Adjust cost based on the NodeCount if nodeCount == 3 { cost *= 3 } - } else if strings.HasPrefix(instanceType, "Shared") { + + return cost, true + } else if strings.Contains(instanceType, "Shared") { cost, ok := sharedCPUCosts[instanceType] if !ok { return 0, false @@ -121,10 +124,27 @@ func InstancesCost(instanceType string, nodeCount int) (float64, bool) { // Adjust cost for 3 Node instances if nodeCount == 3 { cost *= 2.333 + + return cost, true + } + + return 0, false +} + + // GetInstances retrieves SQL instances from the Linode provider. +func GetInstances(ctx context.Context, client providers.ProviderClient) ([]linodego.Instance, error) { + instances, err := client.LinodeClient.ListInstances(ctx, nil) + if err != nil { + return nil, err + } + + sqlInstances := make([]linodego.Instance, 0) + + for _, instance := range instances { + if instance.Type == "db" { + sqlInstances = append(sqlInstances, instance) } - } else { - return 0, false } - return cost, true + return sqlInstances, nil } From 1446b306e3e39065fb4a9854396425c66b0301cb Mon Sep 17 00:00:00 2001 From: Abhay Kumar Gupta Date: Mon, 3 Jul 2023 09:37:08 +0530 Subject: [PATCH 13/13] Update and rename sql.go to mysql.go --- providers/linode/sql/{sql.go => mysql.go} | 90 ++++++++++------------- 1 file changed, 38 insertions(+), 52 deletions(-) rename providers/linode/sql/{sql.go => mysql.go} (51%) diff --git a/providers/linode/sql/sql.go b/providers/linode/sql/mysql.go similarity index 51% rename from providers/linode/sql/sql.go rename to providers/linode/sql/mysql.go index b5be5cf43..1c15d69e2 100644 --- a/providers/linode/sql/sql.go +++ b/providers/linode/sql/mysql.go @@ -6,11 +6,10 @@ import ( "strings" "time" - "github.com/linode/linodego" + // "github.com/linode/linodego" log "github.com/sirupsen/logrus" "github.com/tailwarden/komiser/models" - // . "github.com/tailwarden/komiser/models" "github.com/tailwarden/komiser/providers" ) @@ -42,66 +41,58 @@ var sharedCPUCosts = map[string]float64{ "Shared 256GB": 3840.00, } -// Instances fetches SQL instances from the provider and returns them as resources. +// Instances fetches MySQL instances from the provider and returns them as resources. func Instances(ctx context.Context, client providers.ProviderClient) ([]models.Resource, error) { resources := make([]models.Resource, 0) - // Fetch instances from the Linode provider - instances, err := GetInstances(ctx, client) + // Fetch MySQL databases from the Linode provider + databases, err := client.LinodeClient.ListMySQLDatabases(ctx, nil) if err != nil { return resources, err } - for _, instance := range instances { - tags := make([]models.Tag, 0) - for _, tag := range instance.Tags { - if strings.Contains(tag, ":") { - parts := strings.Split(tag, ":") - tags = append(tags, models.Tag{ - Key: parts[0], - Value: parts[1], - }) - } else { - tags = append(tags, models.Tag{ - Key: tag, - Value: tag, - }) - } + for _, database := range databases { + // Get the cluster size for the database + clusterSize, err := GetClusterSize(ctx, client, database.ID) + if err != nil { + log.Warnf("Failed to get cluster size for MySQL database: %d, Error: %s", database.ID, err.Error()) + // Skip this database and continue with the next one + continue } - // Calculate the cost based on the instance type - cost, ok := InstancesCost(instance.Type) + // Calculate the cost based on the database type and cluster size + cost, ok := InstancesCost(database.Type, clusterSize) if !ok { - log.Warnf("Failed to calculate cost for SQL instance: %s, Type: %s", instance.ID, instance.Type) + log.Warnf("Failed to calculate cost for MySQL database: %d, Type: %s", database.ID, database.Type) + // Skip this database and continue with the next one + continue } resources = append(resources, models.Resource{ Provider: "Linode", Account: client.Name, - Service: "SQL", - Region: instance.Region, - ResourceId: fmt.Sprintf("%d", instance.ID), + Service: "MySQL", + Region: database.Region, + ResourceId: fmt.Sprintf("%d", database.ID), Cost: cost, - Name: instance.Label, + Name: database.Label, FetchedAt: time.Now(), - CreatedAt: *instance.Created, - Tags: tags, - Link: fmt.Sprintf("https://cloud.linode.com/databases/%d", instance.ID), - NodeCount: instance.NodeCount, + CreatedAt: *database.Created, + Link: fmt.Sprintf("https://cloud.linode.com/databases/%d", database.ID), }) } log.WithFields(log.Fields{ "provider": "Linode", "account": client.Name, - "service": "SQL", + "service": "MySQL", "resources": len(resources), }).Info("Fetched resources") return resources, nil } -// InstancesCost calculates the cost for the given SQL instance type and node count. -func InstancesCost(instanceType string, nodeCount int) (float64, bool) { +// InstancesCost calculates the cost for the given MySQL instance type and cluster size. +func InstancesCost(instanceType string, clusterSize int) (float64, bool) { // Calculate cost based on instance type if strings.Contains(instanceType, "Dedicated") { cost, ok := dedicatedCPUCosts[instanceType] @@ -109,42 +100,37 @@ func InstancesCost(instanceType string, nodeCount int) (float64, bool) { return 0, false } - // Adjust cost based on the NodeCount - if nodeCount == 3 { + // Adjust cost based on the cluster size + if clusterSize == 3 { cost *= 3 - } return cost, true + } + } else if strings.Contains(instanceType, "Shared") { cost, ok := sharedCPUCosts[instanceType] if !ok { return 0, false } - // Adjust cost for 3 Node instances - if nodeCount == 3 { + // Adjust cost for the cluster size + if clusterSize == 3 { cost *= 2.333 return cost, true + } + } return 0, false } - // GetInstances retrieves SQL instances from the Linode provider. -func GetInstances(ctx context.Context, client providers.ProviderClient) ([]linodego.Instance, error) { - instances, err := client.LinodeClient.ListInstances(ctx, nil) +// GetClusterSize retrieves the cluster size for a specific MySQL instance. +func GetClusterSize(ctx context.Context, client providers.ProviderClient, instanceID int) (int, error) { + instance, err := client.LinodeClient.GetMySQLDatabase(ctx, instanceID) if err != nil { - return nil, err - } - - sqlInstances := make([]linodego.Instance, 0) - - for _, instance := range instances { - if instance.Type == "db" { - sqlInstances = append(sqlInstances, instance) - } + return 0, err } - return sqlInstances, nil + return instance.ClusterSize, nil }