From c87c43d4ff1f4da106d6ea765369a2874e05edca Mon Sep 17 00:00:00 2001 From: Azanul Date: Thu, 1 Aug 2024 12:11:10 +0530 Subject: [PATCH 1/3] feat: support aws redshift cluster Signed-off-by: Azanul --- providers/aws/redshift/cluster.go | 90 +++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 providers/aws/redshift/cluster.go diff --git a/providers/aws/redshift/cluster.go b/providers/aws/redshift/cluster.go new file mode 100644 index 000000000..d77082561 --- /dev/null +++ b/providers/aws/redshift/cluster.go @@ -0,0 +1,90 @@ +package redshift + +import ( + "context" + "fmt" + "time" + + log "github.com/sirupsen/logrus" + + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/redshift" + "github.com/aws/aws-sdk-go-v2/service/sts" + "github.com/tailwarden/komiser/models" + "github.com/tailwarden/komiser/providers" + awsUtils "github.com/tailwarden/komiser/providers/aws/utils" +) + +func Resources(ctx context.Context, client providers.ProviderClient) ([]models.Resource, error) { + resources := make([]models.Resource, 0) + var config redshift.DescribeClustersInput + redshiftClient := redshift.NewFromConfig(*client.AWSClient) + + stsClient := sts.NewFromConfig(*client.AWSClient) + stsOutput, err := stsClient.GetCallerIdentity(ctx, &sts.GetCallerIdentityInput{}) + if err != nil { + return resources, err + } + + accountId := stsOutput.Account + + serviceCost, err := awsUtils.GetCostAndUsage(ctx, client.AWSClient.Region, "Redshift") + if err != nil { + log.Warnln("Couldn't fetch Redshift cost and usage:", err) + } + + for { + output, err := redshiftClient.DescribeClusters(ctx, &config) + if err != nil { + return resources, err + } + + for _, cluster := range output.Clusters { + resourceArn := fmt.Sprintf("arn:aws:redshift:%s:%s:cluster/%s", client.AWSClient.Region, *accountId, *cluster.ClusterIdentifier) + outputTags := cluster.Tags + + tags := make([]models.Tag, 0) + + for _, tag := range outputTags { + tags = append(tags, models.Tag{ + Key: *tag.Key, + Value: *tag.Value, + }) + } + + monthlyCost := float64(0) + + resources = append(resources, models.Resource{ + Provider: "AWS", + Account: client.Name, + Service: "Redshift EventSubscription", + ResourceId: resourceArn, + Region: client.AWSClient.Region, + Name: *cluster.ClusterIdentifier, + Cost: monthlyCost, + Metadata: map[string]string{ + "serviceCost": fmt.Sprint(serviceCost), + }, + Tags: tags, + FetchedAt: time.Now(), + Link: fmt.Sprintf("https://%s.console.aws.amaxon.com/redshift/home?region=%s/clusters/%s", client.AWSClient.Region, client.AWSClient.Region, *cluster.ClusterIdentifier), + }) + + } + + if aws.ToString(output.Marker) == "" { + break + } + config.Marker = output.Marker + } + + log.WithFields(log.Fields{ + "provider": "AWS", + "account": client.Name, + "region": client.AWSClient.Region, + "service": "Redshift EventSubscription", + "resources": len(resources), + }).Info("Fetched resources") + return resources, nil + +} From 19d8c32481ce5a592d9798f701aba4dabd343537 Mon Sep 17 00:00:00 2001 From: Azanul Date: Thu, 1 Aug 2024 12:11:24 +0530 Subject: [PATCH 2/3] refac: redundent if and dot imports removed Signed-off-by: Azanul --- providers/aws/redshift/eventsubscription.go | 30 ++++++++++----------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/providers/aws/redshift/eventsubscription.go b/providers/aws/redshift/eventsubscription.go index b4b52317b..eea5996ef 100644 --- a/providers/aws/redshift/eventsubscription.go +++ b/providers/aws/redshift/eventsubscription.go @@ -10,13 +10,13 @@ import ( "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/redshift" "github.com/aws/aws-sdk-go-v2/service/sts" - . "github.com/tailwarden/komiser/models" - . "github.com/tailwarden/komiser/providers" + "github.com/tailwarden/komiser/models" + "github.com/tailwarden/komiser/providers" awsUtils "github.com/tailwarden/komiser/providers/aws/utils" ) -func EventSubscriptions(ctx context.Context, client ProviderClient) ([]Resource, error) { - resources := make([]Resource, 0) +func EventSubscriptions(ctx context.Context, client providers.ProviderClient) ([]models.Resource, error) { + resources := make([]models.Resource, 0) var config redshift.DescribeEventSubscriptionsInput redshiftClient := redshift.NewFromConfig(*client.AWSClient) @@ -45,20 +45,18 @@ func EventSubscriptions(ctx context.Context, client ProviderClient) ([]Resource, resourceArn := fmt.Sprintf("arn:aws:redshift:%s:%s:eventsubscripion/%s", client.AWSClient.Region, *accountId, *eventSubscription.CustSubscriptionId) outputTags := eventSubscription.Tags - tags := make([]Tag, 0) + tags := make([]models.Tag, 0) - if err == nil { - for _, tag := range outputTags { - tags = append(tags, Tag{ - Key: *tag.Key, - Value: *tag.Value, - }) - } + for _, tag := range outputTags { + tags = append(tags, models.Tag{ + Key: *tag.Key, + Value: *tag.Value, + }) } monthlyCost := float64(0) - resources = append(resources, Resource{ + resources = append(resources, models.Resource{ Provider: "AWS", Account: client.Name, Service: "Redshift EventSubscription", @@ -69,9 +67,9 @@ func EventSubscriptions(ctx context.Context, client ProviderClient) ([]Resource, Metadata: map[string]string{ "serviceCost": fmt.Sprint(serviceCost), }, - Tags: tags, - FetchedAt: time.Now(), - Link: fmt.Sprintf("https://%s.console.aws.amaxon.com/redshift/home?region=%s/event-subscriptions/%s", client.AWSClient.Region, client.AWSClient.Region, *eventSubscription.CustSubscriptionId), + Tags: tags, + FetchedAt: time.Now(), + Link: fmt.Sprintf("https://%s.console.aws.amaxon.com/redshift/home?region=%s/event-subscriptions/%s", client.AWSClient.Region, client.AWSClient.Region, *eventSubscription.CustSubscriptionId), }) } } From cf2cac2b62a045da544f11a4cb3c14769c1fbe19 Mon Sep 17 00:00:00 2001 From: Azanul Date: Thu, 1 Aug 2024 12:17:10 +0530 Subject: [PATCH 3/3] refac: service name typo Signed-off-by: Azanul --- providers/aws/redshift/cluster.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/providers/aws/redshift/cluster.go b/providers/aws/redshift/cluster.go index d77082561..ddedbe8ad 100644 --- a/providers/aws/redshift/cluster.go +++ b/providers/aws/redshift/cluster.go @@ -57,7 +57,7 @@ func Resources(ctx context.Context, client providers.ProviderClient) ([]models.R resources = append(resources, models.Resource{ Provider: "AWS", Account: client.Name, - Service: "Redshift EventSubscription", + Service: "Redshift Cluster", ResourceId: resourceArn, Region: client.AWSClient.Region, Name: *cluster.ClusterIdentifier, @@ -82,7 +82,7 @@ func Resources(ctx context.Context, client providers.ProviderClient) ([]models.R "provider": "AWS", "account": client.Name, "region": client.AWSClient.Region, - "service": "Redshift EventSubscription", + "service": "Redshift Cluster", "resources": len(resources), }).Info("Fetched resources") return resources, nil