|
| 1 | +package rds |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + log "github.com/sirupsen/logrus" |
| 9 | + |
| 10 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 11 | + "github.com/aws/aws-sdk-go-v2/service/rds" |
| 12 | + "github.com/tailwarden/komiser/models" |
| 13 | + "github.com/tailwarden/komiser/providers" |
| 14 | + "github.com/tailwarden/komiser/utils" |
| 15 | +) |
| 16 | + |
| 17 | +func Proxies(ctx context.Context, client providers.ProviderClient) ([]models.Resource, error) { |
| 18 | + var config rds.DescribeDBProxiesInput |
| 19 | + resources := make([]models.Resource, 0) |
| 20 | + rdsClient := rds.NewFromConfig(*client.AWSClient) |
| 21 | + |
| 22 | + oldRegion := client.AWSClient.Region |
| 23 | + client.AWSClient.Region = "us-east-1" |
| 24 | + client.AWSClient.Region = oldRegion |
| 25 | + |
| 26 | + for { |
| 27 | + output, err := rdsClient.DescribeDBProxies(ctx, &config) |
| 28 | + if err != nil { |
| 29 | + return resources, err |
| 30 | + } |
| 31 | + |
| 32 | + for _, proxy := range output.DBProxies { |
| 33 | + var _ProxyName string = *proxy.DBProxyName |
| 34 | + startOfMonth := utils.BeginningOfMonth(time.Now()) |
| 35 | + hourlyUsage := 0 |
| 36 | + if (*proxy.CreatedDate).Before(startOfMonth) { |
| 37 | + hourlyUsage = int(time.Since(startOfMonth).Hours()) |
| 38 | + } else { |
| 39 | + hourlyUsage = int(time.Since(*proxy.CreatedDate).Hours()) |
| 40 | + } |
| 41 | + |
| 42 | + hourlyCost := 0.0 |
| 43 | + monthlyCost := float64(hourlyUsage) * hourlyCost |
| 44 | + |
| 45 | + resources = append(resources, models.Resource{ |
| 46 | + Provider: "AWS", |
| 47 | + Account: client.Name, |
| 48 | + Service: "RDS Proxy", |
| 49 | + Region: client.AWSClient.Region, |
| 50 | + ResourceId: *proxy.DBProxyArn, |
| 51 | + Cost: monthlyCost, |
| 52 | + Name: _ProxyName, |
| 53 | + FetchedAt: time.Now(), |
| 54 | + Link: fmt.Sprintf("https:/%s.console.aws.amazon.com/rds/home?region=%s#proxies:id=%s", client.AWSClient.Region, client.AWSClient.Region, *proxy.DBProxyName), |
| 55 | + }) |
| 56 | + } |
| 57 | + |
| 58 | + if aws.ToString(output.Marker) == "" { |
| 59 | + break |
| 60 | + } |
| 61 | + |
| 62 | + config.Marker = output.Marker |
| 63 | + } |
| 64 | + log.WithFields(log.Fields{ |
| 65 | + "provider": "AWS", |
| 66 | + "account": client.Name, |
| 67 | + "region": client.AWSClient.Region, |
| 68 | + "service": "RDS Proxy", |
| 69 | + "resources": len(resources), |
| 70 | + }).Info("Fetched resources") |
| 71 | + return resources, nil |
| 72 | +} |
0 commit comments