Skip to content

Commit

Permalink
Merge pull request #67 from janboll/refactor-rds
Browse files Browse the repository at this point in the history
Add tests to rds.go
  • Loading branch information
janboll authored Oct 5, 2022
2 parents f70c42c + 3e884e9 commit cef61ac
Show file tree
Hide file tree
Showing 10 changed files with 596 additions and 392 deletions.
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/app-sre/aws-resource-exporter/pkg"
"github.com/app-sre/aws-resource-exporter/pkg/awsclient"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sts"
Expand Down Expand Up @@ -124,7 +125,7 @@ func run() int {
level.Info(logger).Log("msg", "Starting"+namespace, "version", version.Info())
level.Info(logger).Log("msg", "Build context", version.BuildContext())

pkg.AwsExporterMetrics = pkg.NewExporterMetrics()
awsclient.AwsExporterMetrics = awsclient.NewExporterMetrics(namespace)

var configFile string
if path := os.Getenv("AWS_RESOURCE_EXPORTER_CONFIG_FILE"); path != "" {
Expand All @@ -137,7 +138,7 @@ func run() int {
level.Error(logger).Log("msg", "Could not load configuration file", "err", err)
return 1
}
collectors := append(cs, pkg.AwsExporterMetrics)
collectors := append(cs, awsclient.AwsExporterMetrics)
prometheus.MustRegister(
collectors...,
)
Expand Down
83 changes: 69 additions & 14 deletions pkg/awsclient/awsclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
package awsclient

import (
"context"

"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/rds"
"github.com/aws/aws-sdk-go/service/servicequotas"
"github.com/aws/aws-sdk-go/service/servicequotas/servicequotasiface"

Expand All @@ -20,50 +23,102 @@ type Client interface {
//EC2
DescribeTransitGatewaysWithContext(ctx aws.Context, input *ec2.DescribeTransitGatewaysInput, opts ...request.Option) (*ec2.DescribeTransitGatewaysOutput, error)

//RDS
DescribeDBInstancesPagesWithContext(ctx aws.Context, input *rds.DescribeDBInstancesInput, fn func(*rds.DescribeDBInstancesOutput, bool) bool, opts ...request.Option) error
DescribeDBLogFilesPagesWithContext(ctx aws.Context, input *rds.DescribeDBLogFilesInput, fn func(*rds.DescribeDBLogFilesOutput, bool) bool, opts ...request.Option) error
DescribePendingMaintenanceActionsPagesWithContext(ctx aws.Context, input *rds.DescribePendingMaintenanceActionsInput, fn func(*rds.DescribePendingMaintenanceActionsOutput, bool) bool, opts ...request.Option) error
DescribeDBLogFilesAll(ctx context.Context, instanceId string) ([]*rds.DescribeDBLogFilesOutput, error)
DescribePendingMaintenanceActionsAll(ctx context.Context) ([]*rds.ResourcePendingMaintenanceActions, error)
DescribeDBInstancesAll(ctx context.Context) ([]*rds.DBInstance, error)

// Service Quota
GetServiceQuota(*servicequotas.GetServiceQuotaInput) (*servicequotas.GetServiceQuotaOutput, error)
GetServiceQuotaWithContext(ctx aws.Context, input *servicequotas.GetServiceQuotaInput, opts ...request.Option) (*servicequotas.GetServiceQuotaOutput, error)
RequestServiceQuotaIncrease(*servicequotas.RequestServiceQuotaIncreaseInput) (*servicequotas.RequestServiceQuotaIncreaseOutput, error)
ListRequestedServiceQuotaChangeHistory(*servicequotas.ListRequestedServiceQuotaChangeHistoryInput) (*servicequotas.ListRequestedServiceQuotaChangeHistoryOutput, error)
ListRequestedServiceQuotaChangeHistoryByQuota(*servicequotas.ListRequestedServiceQuotaChangeHistoryByQuotaInput) (*servicequotas.ListRequestedServiceQuotaChangeHistoryByQuotaOutput, error)
}

type awsClient struct {
ec2Client ec2iface.EC2API
rdsClient rds.RDS
serviceQuotasClient servicequotasiface.ServiceQuotasAPI
}

func (c *awsClient) DescribeTransitGatewaysWithContext(ctx aws.Context, input *ec2.DescribeTransitGatewaysInput, opts ...request.Option) (*ec2.DescribeTransitGatewaysOutput, error) {
return c.ec2Client.DescribeTransitGatewaysWithContext(ctx, input, opts...)
}

func (c *awsClient) DeleteSubnet(input *ec2.DeleteSubnetInput) (*ec2.DeleteSubnetOutput, error) {
return c.ec2Client.DeleteSubnet(input)
func (c *awsClient) DescribeDBLogFilesPagesWithContext(ctx aws.Context, input *rds.DescribeDBLogFilesInput, fn func(*rds.DescribeDBLogFilesOutput, bool) bool, opts ...request.Option) error {
return c.rdsClient.DescribeDBLogFilesPagesWithContext(ctx, input, fn, opts...)
}

func (c *awsClient) DescribeDBInstancesPagesWithContext(ctx aws.Context, input *rds.DescribeDBInstancesInput, fn func(*rds.DescribeDBInstancesOutput, bool) bool, opts ...request.Option) error {
return c.rdsClient.DescribeDBInstancesPagesWithContext(ctx, input, fn, opts...)
}

func (c *awsClient) GetServiceQuota(input *servicequotas.GetServiceQuotaInput) (*servicequotas.GetServiceQuotaOutput, error) {
return c.serviceQuotasClient.GetServiceQuota(input)
func (c *awsClient) DescribePendingMaintenanceActionsPagesWithContext(ctx aws.Context, input *rds.DescribePendingMaintenanceActionsInput, fn func(*rds.DescribePendingMaintenanceActionsOutput, bool) bool, opts ...request.Option) error {
return c.rdsClient.DescribePendingMaintenanceActionsPagesWithContext(ctx, input, fn, opts...)
}

func (c *awsClient) GetServiceQuotaWithContext(ctx aws.Context, input *servicequotas.GetServiceQuotaInput, opts ...request.Option) (*servicequotas.GetServiceQuotaOutput, error) {
return c.serviceQuotasClient.GetServiceQuotaWithContext(ctx, input, opts...)
}

func (c *awsClient) RequestServiceQuotaIncrease(input *servicequotas.RequestServiceQuotaIncreaseInput) (*servicequotas.RequestServiceQuotaIncreaseOutput, error) {
return c.serviceQuotasClient.RequestServiceQuotaIncrease(input)
func (c *awsClient) DescribeDBLogFilesAll(ctx context.Context, instanceId string) ([]*rds.DescribeDBLogFilesOutput, error) {
input := &rds.DescribeDBLogFilesInput{
DBInstanceIdentifier: &instanceId,
}

var logOutPuts []*rds.DescribeDBLogFilesOutput
err := c.DescribeDBLogFilesPagesWithContext(ctx, input, func(ddlo *rds.DescribeDBLogFilesOutput, b bool) bool {
AwsExporterMetrics.IncrementRequests()
logOutPuts = append(logOutPuts, ddlo)
return true
})

if err != nil {
AwsExporterMetrics.IncrementErrors()
return nil, err
}

return logOutPuts, nil
}

func (c *awsClient) ListRequestedServiceQuotaChangeHistory(input *servicequotas.ListRequestedServiceQuotaChangeHistoryInput) (*servicequotas.ListRequestedServiceQuotaChangeHistoryOutput, error) {
return c.serviceQuotasClient.ListRequestedServiceQuotaChangeHistory(input)
func (c *awsClient) DescribePendingMaintenanceActionsAll(ctx context.Context) ([]*rds.ResourcePendingMaintenanceActions, error) {
describePendingMaintInput := &rds.DescribePendingMaintenanceActionsInput{}

var instancesPendMaintActionsData []*rds.ResourcePendingMaintenanceActions
err := c.DescribePendingMaintenanceActionsPagesWithContext(ctx, describePendingMaintInput, func(dpm *rds.DescribePendingMaintenanceActionsOutput, b bool) bool {
AwsExporterMetrics.IncrementRequests()
instancesPendMaintActionsData = append(instancesPendMaintActionsData, dpm.PendingMaintenanceActions...)
return true
})

if err != nil {
AwsExporterMetrics.IncrementErrors()
return nil, err
}

return instancesPendMaintActionsData, nil
}

func (c *awsClient) ListRequestedServiceQuotaChangeHistoryByQuota(input *servicequotas.ListRequestedServiceQuotaChangeHistoryByQuotaInput) (*servicequotas.ListRequestedServiceQuotaChangeHistoryByQuotaOutput, error) {
return c.serviceQuotasClient.ListRequestedServiceQuotaChangeHistoryByQuota(input)
func (c *awsClient) DescribeDBInstancesAll(ctx context.Context) ([]*rds.DBInstance, error) {
input := &rds.DescribeDBInstancesInput{}

var instances []*rds.DBInstance
err := c.DescribeDBInstancesPagesWithContext(ctx, input, func(ddo *rds.DescribeDBInstancesOutput, b bool) bool {
AwsExporterMetrics.IncrementRequests()
instances = append(instances, ddo.DBInstances...)
return true
})
if err != nil {
AwsExporterMetrics.IncrementErrors()
return nil, err
}
return instances, nil
}

func NewClientFromSession(sess *session.Session) Client {
return &awsClient{
ec2Client: ec2.New(sess),
serviceQuotasClient: servicequotas.New(sess),
rdsClient: *rds.New(sess),
}
}
4 changes: 2 additions & 2 deletions pkg/exporter.go → pkg/awsclient/exporter.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pkg
package awsclient

import (
"sync"
Expand All @@ -23,7 +23,7 @@ type ExporterMetrics struct {
}

// NewExporterMetrics creates a new exporter metrics instance
func NewExporterMetrics() *ExporterMetrics {
func NewExporterMetrics(namespace string) *ExporterMetrics {
return &ExporterMetrics{
APIRequests: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "apirequests"),
Expand Down
Loading

0 comments on commit cef61ac

Please sign in to comment.