Skip to content

Commit

Permalink
Merge pull request #774 from tailwarden/develop
Browse files Browse the repository at this point in the history
v3.0.15 release 🚀
  • Loading branch information
mlabouardy authored May 5, 2023
2 parents 97d0f1d + 95c77ca commit 591c345
Show file tree
Hide file tree
Showing 10 changed files with 196 additions and 17 deletions.
2 changes: 1 addition & 1 deletion dashboard/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "komiser-dashboard",
"version": "3.0.14",
"version": "3.0.15",
"private": true,
"scripts": {
"dev": "next dev -p 3002",
Expand Down
9 changes: 9 additions & 0 deletions handlers/dashboard_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ func (handler *ApiHandler) DashboardStatsHandler(c *gin.Context) {
Accounts: accounts.Count,
}

if handler.telemetry {
handler.analytics.TrackEvent("global_stats", map[string]interface{}{
"regions": regions.Count,
"resources": resources.Count,
"accounts": accounts.Count,
"cost": cost.Sum,
})
}

c.JSON(http.StatusOK, output)
}

Expand Down
8 changes: 8 additions & 0 deletions handlers/tags_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ func (handler *ApiHandler) BulkUpdateTagsHandler(c *gin.Context) {
}
}

if handler.telemetry {
handler.analytics.TrackEvent("tagging_resources", nil)
}

c.JSON(http.StatusCreated, gin.H{"message": "tags has been successfuly updated"})
}

Expand Down Expand Up @@ -56,5 +60,9 @@ func (handler *ApiHandler) UpdateTagsHandler(c *gin.Context) {
return
}

if handler.telemetry {
handler.analytics.TrackEvent("tagging_resources", nil)
}

c.JSON(http.StatusCreated, gin.H{"message": "tags has been successfuly updated"})
}
12 changes: 8 additions & 4 deletions models/price.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ type PricingResult struct {
Product struct {
Sku string `json:"sku"`
} `json:"product"`
Terms map[string]map[string]map[string]map[string]struct {
PricePerUnit struct {
USD string `json:"USD"`
} `json:"pricePerUnit"`
Terms struct {
OnDemand map[string]struct {
PriceDimensions map[string]struct {
PricePerUnit struct {
USD string `json:"USD"`
} `json:"pricePerUnit"`
} `json:"priceDimensions"`
} `json:"OnDemand"`
} `json:"terms"`
}
2 changes: 2 additions & 0 deletions providers/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func listOfSupportedServices() []providers.FetchDataFunction {
kms.Keys,
rds.Clusters,
rds.Instances,
rds.Snapshots,
elb.LoadBalancers,
efs.ElasticFileStorage,
apigateway.Apis,
Expand All @@ -80,6 +81,7 @@ func listOfSupportedServices() []providers.FetchDataFunction {
ec2.PlacementGroups,
systemsmanager.MaintenanceWindows,
ec2.VpcEndpoints,
ec2.VpcPeeringConnections,
}
}

Expand Down
21 changes: 15 additions & 6 deletions providers/aws/ec2/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,26 @@ func Instances(ctx context.Context, client providers.ProviderClient) ([]models.R
hourlyCost := 0.0

if pricingOutput != nil && len(pricingOutput.PriceList) > 0 {
b, _ := json.Marshal(pricingOutput.PriceList[0])
s, _ := strconv.Unquote(string(b))

pricingResult := models.PricingResult{}
err = json.Unmarshal([]byte(s), &pricingResult)
err := json.Unmarshal([]byte(pricingOutput.PriceList[0]), &pricingResult)
if err != nil {
log.WithError(err).Error("could not unmarshal")
log.Fatalf("Failed to unmarshal JSON: %v", err)
}

hourlyCostRaw := pricingResult.Terms["OnDemand"][fmt.Sprintf("%s.JRTCKXETXF", pricingResult.Product.Sku)]["priceDimensions"][fmt.Sprintf("%s.JRTCKXETXF.6YS6EN2CT7", pricingResult.Product.Sku)].PricePerUnit.USD
hourlyCost, _ = strconv.ParseFloat(hourlyCostRaw, 64)
for _, onDemand := range pricingResult.Terms.OnDemand {
for _, priceDimension := range onDemand.PriceDimensions {
hourlyCost, err = strconv.ParseFloat(priceDimension.PricePerUnit.USD, 64)
if err != nil {
log.Fatalf("Failed to parse hourly cost: %v", err)
}
break
}
break
}

//log.Printf("Hourly cost EC2: %f", hourlyCost)

}

monthlyCost := float64(hourlyUsage) * hourlyCost
Expand Down
File renamed without changes.
73 changes: 73 additions & 0 deletions providers/aws/ec2/vpc_peering_connection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package ec2

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/ec2"
. "github.com/tailwarden/komiser/models"
. "github.com/tailwarden/komiser/providers"
)

func VpcPeeringConnections(ctx context.Context, client ProviderClient) ([]Resource, error) {
var config ec2.DescribeVpcPeeringConnectionsInput
resources := make([]Resource, 0)
ec2Client := ec2.NewFromConfig(*client.AWSClient)

for {
output, err := ec2Client.DescribeVpcPeeringConnections(ctx, &config)
if err != nil {
return resources, err
}

for _, vpcPeeringConnection := range output.VpcPeeringConnections {
name := ""
tags := make([]Tag, 0)
for _, tag := range vpcPeeringConnection.Tags {
if *tag.Key == "Name" {
name = *tag.Value
}
tags = append(tags, Tag{
Key: *tag.Key,
Value: *tag.Value,
})
}

resources = append(resources, Resource{
Provider: "AWS",
Account: client.Name,
Service: "VPC Peering Connection",
Region: client.AWSClient.Region,
Name: name,
ResourceId: *vpcPeeringConnection.VpcPeeringConnectionId,
FetchedAt: time.Now(),
Cost: 0,
Tags: tags,
Link: fmt.Sprintf(
"https:/%s.console.aws.amazon.com/vpc/home?region=%s#PeeringConnectionDetails:VpcPeeringConnectionId=%s",
client.AWSClient.Region,
client.AWSClient.Region,
*vpcPeeringConnection.VpcPeeringConnectionId,
),
})
}

if aws.ToString(output.NextToken) == "" {
break
}

config.NextToken = output.NextToken
}
log.WithFields(log.Fields{
"provider": "AWS",
"account": client.Name,
"region": client.AWSClient.Region,
"service": "VPC Peering Connection",
"resources": len(resources),
}).Info("Fetched resources")
return resources, nil
}
21 changes: 15 additions & 6 deletions providers/aws/rds/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,26 @@ func Instances(ctx context.Context, client providers.ProviderClient) ([]models.R

hourlyCost := 0.0
if pricingOutput != nil && len(pricingOutput.PriceList) > 0 {
b, _ := json.Marshal(pricingOutput.PriceList[0])
s, _ := strconv.Unquote(string(b))
log.Infof(`Raw pricingOutput.PriceList[0] : %s`, pricingOutput.PriceList[0])

pricingResult := models.PricingResult{}
err = json.Unmarshal([]byte(s), &pricingResult)
err := json.Unmarshal([]byte(pricingOutput.PriceList[0]), &pricingResult)
if err != nil {
log.WithError(err).Error("could not unmarshal")
log.Fatalf("Failed to unmarshal JSON: %v", err)
}

hourlyCostRaw := pricingResult.Terms["OnDemand"][fmt.Sprintf("%s.JRTCKXETXF", pricingResult.Product.Sku)]["priceDimensions"][fmt.Sprintf("%s.JRTCKXETXF.6YS6EN2CT7", pricingResult.Product.Sku)].PricePerUnit.USD
hourlyCost, _ = strconv.ParseFloat(hourlyCostRaw, 64)
for _, onDemand := range pricingResult.Terms.OnDemand {
for _, priceDimension := range onDemand.PriceDimensions {
hourlyCost, err = strconv.ParseFloat(priceDimension.PricePerUnit.USD, 64)
if err != nil {
log.Fatalf("Failed to parse hourly cost: %v", err)
}
break
}
break
}

//log.Printf("Hourly cost RDS: %f", hourlyCost)
}

monthlyCost := float64(hourlyUsage) * hourlyCost
Expand Down
65 changes: 65 additions & 0 deletions providers/aws/rds/snapshots.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package rds

import (
"context"
"fmt"
"time"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/rds"
log "github.com/sirupsen/logrus"
"github.com/tailwarden/komiser/models"
"github.com/tailwarden/komiser/providers"
)

func Snapshots(ctx context.Context, client providers.ProviderClient) ([]models.Resource, error) {
var config rds.DescribeDBSnapshotsInput
resources := make([]models.Resource, 0)
rdsClient := rds.NewFromConfig(*client.AWSClient)

for {
output, err := rdsClient.DescribeDBSnapshots(ctx, &config)
if err != nil {
return resources, err
}

for _, snapshot := range output.DBSnapshots {
tags := make([]models.Tag, 0)
for _, tag := range snapshot.TagList {
tags = append(tags, models.Tag{
Key: *tag.Key,
Value: *tag.Value,
})
}

_snapshotName := *snapshot.DBSnapshotIdentifier

resources = append(resources, models.Resource{
Provider: "AWS",
Account: client.Name,
Service: "RDS Snapshot",
Region: client.AWSClient.Region,
ResourceId: *snapshot.DBSnapshotArn,
Name: _snapshotName,
FetchedAt: time.Now(),
Tags: tags,
Link: fmt.Sprintf("https://%s.console.aws.amazon.com/rds/home?region=%s#snapshot:id=%s", client.AWSClient.Region, client.AWSClient.Region, *snapshot.DBSnapshotIdentifier),
})
}

if aws.ToString(output.Marker) == "" {
break
}

config.Marker = output.Marker
}

log.WithFields(log.Fields{
"provider": "AWS",
"account": client.Name,
"region": client.AWSClient.Region,
"service": "RDS Snapshot",
"resources": len(resources),
}).Info("Fetched resources")
return resources, nil
}

0 comments on commit 591c345

Please sign in to comment.