Skip to content

Commit cef61ac

Browse files
authored
Merge pull request #67 from janboll/refactor-rds
Add tests to rds.go
2 parents f70c42c + 3e884e9 commit cef61ac

File tree

10 files changed

+596
-392
lines changed

10 files changed

+596
-392
lines changed

main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"time"
1010

1111
"github.com/app-sre/aws-resource-exporter/pkg"
12+
"github.com/app-sre/aws-resource-exporter/pkg/awsclient"
1213
"github.com/aws/aws-sdk-go/aws"
1314
"github.com/aws/aws-sdk-go/aws/session"
1415
"github.com/aws/aws-sdk-go/service/sts"
@@ -124,7 +125,7 @@ func run() int {
124125
level.Info(logger).Log("msg", "Starting"+namespace, "version", version.Info())
125126
level.Info(logger).Log("msg", "Build context", version.BuildContext())
126127

127-
pkg.AwsExporterMetrics = pkg.NewExporterMetrics()
128+
awsclient.AwsExporterMetrics = awsclient.NewExporterMetrics(namespace)
128129

129130
var configFile string
130131
if path := os.Getenv("AWS_RESOURCE_EXPORTER_CONFIG_FILE"); path != "" {
@@ -137,7 +138,7 @@ func run() int {
137138
level.Error(logger).Log("msg", "Could not load configuration file", "err", err)
138139
return 1
139140
}
140-
collectors := append(cs, pkg.AwsExporterMetrics)
141+
collectors := append(cs, awsclient.AwsExporterMetrics)
141142
prometheus.MustRegister(
142143
collectors...,
143144
)

pkg/awsclient/awsclient.go

Lines changed: 69 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
package awsclient
44

55
import (
6+
"context"
7+
68
"github.com/aws/aws-sdk-go/aws/request"
9+
"github.com/aws/aws-sdk-go/service/rds"
710
"github.com/aws/aws-sdk-go/service/servicequotas"
811
"github.com/aws/aws-sdk-go/service/servicequotas/servicequotasiface"
912

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

26+
//RDS
27+
DescribeDBInstancesPagesWithContext(ctx aws.Context, input *rds.DescribeDBInstancesInput, fn func(*rds.DescribeDBInstancesOutput, bool) bool, opts ...request.Option) error
28+
DescribeDBLogFilesPagesWithContext(ctx aws.Context, input *rds.DescribeDBLogFilesInput, fn func(*rds.DescribeDBLogFilesOutput, bool) bool, opts ...request.Option) error
29+
DescribePendingMaintenanceActionsPagesWithContext(ctx aws.Context, input *rds.DescribePendingMaintenanceActionsInput, fn func(*rds.DescribePendingMaintenanceActionsOutput, bool) bool, opts ...request.Option) error
30+
DescribeDBLogFilesAll(ctx context.Context, instanceId string) ([]*rds.DescribeDBLogFilesOutput, error)
31+
DescribePendingMaintenanceActionsAll(ctx context.Context) ([]*rds.ResourcePendingMaintenanceActions, error)
32+
DescribeDBInstancesAll(ctx context.Context) ([]*rds.DBInstance, error)
33+
2334
// Service Quota
24-
GetServiceQuota(*servicequotas.GetServiceQuotaInput) (*servicequotas.GetServiceQuotaOutput, error)
2535
GetServiceQuotaWithContext(ctx aws.Context, input *servicequotas.GetServiceQuotaInput, opts ...request.Option) (*servicequotas.GetServiceQuotaOutput, error)
26-
RequestServiceQuotaIncrease(*servicequotas.RequestServiceQuotaIncreaseInput) (*servicequotas.RequestServiceQuotaIncreaseOutput, error)
27-
ListRequestedServiceQuotaChangeHistory(*servicequotas.ListRequestedServiceQuotaChangeHistoryInput) (*servicequotas.ListRequestedServiceQuotaChangeHistoryOutput, error)
28-
ListRequestedServiceQuotaChangeHistoryByQuota(*servicequotas.ListRequestedServiceQuotaChangeHistoryByQuotaInput) (*servicequotas.ListRequestedServiceQuotaChangeHistoryByQuotaOutput, error)
2936
}
3037

3138
type awsClient struct {
3239
ec2Client ec2iface.EC2API
40+
rdsClient rds.RDS
3341
serviceQuotasClient servicequotasiface.ServiceQuotasAPI
3442
}
3543

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

40-
func (c *awsClient) DeleteSubnet(input *ec2.DeleteSubnetInput) (*ec2.DeleteSubnetOutput, error) {
41-
return c.ec2Client.DeleteSubnet(input)
48+
func (c *awsClient) DescribeDBLogFilesPagesWithContext(ctx aws.Context, input *rds.DescribeDBLogFilesInput, fn func(*rds.DescribeDBLogFilesOutput, bool) bool, opts ...request.Option) error {
49+
return c.rdsClient.DescribeDBLogFilesPagesWithContext(ctx, input, fn, opts...)
50+
}
51+
52+
func (c *awsClient) DescribeDBInstancesPagesWithContext(ctx aws.Context, input *rds.DescribeDBInstancesInput, fn func(*rds.DescribeDBInstancesOutput, bool) bool, opts ...request.Option) error {
53+
return c.rdsClient.DescribeDBInstancesPagesWithContext(ctx, input, fn, opts...)
4254
}
4355

44-
func (c *awsClient) GetServiceQuota(input *servicequotas.GetServiceQuotaInput) (*servicequotas.GetServiceQuotaOutput, error) {
45-
return c.serviceQuotasClient.GetServiceQuota(input)
56+
func (c *awsClient) DescribePendingMaintenanceActionsPagesWithContext(ctx aws.Context, input *rds.DescribePendingMaintenanceActionsInput, fn func(*rds.DescribePendingMaintenanceActionsOutput, bool) bool, opts ...request.Option) error {
57+
return c.rdsClient.DescribePendingMaintenanceActionsPagesWithContext(ctx, input, fn, opts...)
4658
}
4759

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

52-
func (c *awsClient) RequestServiceQuotaIncrease(input *servicequotas.RequestServiceQuotaIncreaseInput) (*servicequotas.RequestServiceQuotaIncreaseOutput, error) {
53-
return c.serviceQuotasClient.RequestServiceQuotaIncrease(input)
64+
func (c *awsClient) DescribeDBLogFilesAll(ctx context.Context, instanceId string) ([]*rds.DescribeDBLogFilesOutput, error) {
65+
input := &rds.DescribeDBLogFilesInput{
66+
DBInstanceIdentifier: &instanceId,
67+
}
68+
69+
var logOutPuts []*rds.DescribeDBLogFilesOutput
70+
err := c.DescribeDBLogFilesPagesWithContext(ctx, input, func(ddlo *rds.DescribeDBLogFilesOutput, b bool) bool {
71+
AwsExporterMetrics.IncrementRequests()
72+
logOutPuts = append(logOutPuts, ddlo)
73+
return true
74+
})
75+
76+
if err != nil {
77+
AwsExporterMetrics.IncrementErrors()
78+
return nil, err
79+
}
80+
81+
return logOutPuts, nil
5482
}
5583

56-
func (c *awsClient) ListRequestedServiceQuotaChangeHistory(input *servicequotas.ListRequestedServiceQuotaChangeHistoryInput) (*servicequotas.ListRequestedServiceQuotaChangeHistoryOutput, error) {
57-
return c.serviceQuotasClient.ListRequestedServiceQuotaChangeHistory(input)
84+
func (c *awsClient) DescribePendingMaintenanceActionsAll(ctx context.Context) ([]*rds.ResourcePendingMaintenanceActions, error) {
85+
describePendingMaintInput := &rds.DescribePendingMaintenanceActionsInput{}
86+
87+
var instancesPendMaintActionsData []*rds.ResourcePendingMaintenanceActions
88+
err := c.DescribePendingMaintenanceActionsPagesWithContext(ctx, describePendingMaintInput, func(dpm *rds.DescribePendingMaintenanceActionsOutput, b bool) bool {
89+
AwsExporterMetrics.IncrementRequests()
90+
instancesPendMaintActionsData = append(instancesPendMaintActionsData, dpm.PendingMaintenanceActions...)
91+
return true
92+
})
93+
94+
if err != nil {
95+
AwsExporterMetrics.IncrementErrors()
96+
return nil, err
97+
}
98+
99+
return instancesPendMaintActionsData, nil
58100
}
59101

60-
func (c *awsClient) ListRequestedServiceQuotaChangeHistoryByQuota(input *servicequotas.ListRequestedServiceQuotaChangeHistoryByQuotaInput) (*servicequotas.ListRequestedServiceQuotaChangeHistoryByQuotaOutput, error) {
61-
return c.serviceQuotasClient.ListRequestedServiceQuotaChangeHistoryByQuota(input)
102+
func (c *awsClient) DescribeDBInstancesAll(ctx context.Context) ([]*rds.DBInstance, error) {
103+
input := &rds.DescribeDBInstancesInput{}
104+
105+
var instances []*rds.DBInstance
106+
err := c.DescribeDBInstancesPagesWithContext(ctx, input, func(ddo *rds.DescribeDBInstancesOutput, b bool) bool {
107+
AwsExporterMetrics.IncrementRequests()
108+
instances = append(instances, ddo.DBInstances...)
109+
return true
110+
})
111+
if err != nil {
112+
AwsExporterMetrics.IncrementErrors()
113+
return nil, err
114+
}
115+
return instances, nil
62116
}
63117

64118
func NewClientFromSession(sess *session.Session) Client {
65119
return &awsClient{
66120
ec2Client: ec2.New(sess),
67121
serviceQuotasClient: servicequotas.New(sess),
122+
rdsClient: *rds.New(sess),
68123
}
69124
}

pkg/exporter.go renamed to pkg/awsclient/exporter.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package pkg
1+
package awsclient
22

33
import (
44
"sync"
@@ -23,7 +23,7 @@ type ExporterMetrics struct {
2323
}
2424

2525
// NewExporterMetrics creates a new exporter metrics instance
26-
func NewExporterMetrics() *ExporterMetrics {
26+
func NewExporterMetrics(namespace string) *ExporterMetrics {
2727
return &ExporterMetrics{
2828
APIRequests: prometheus.NewDesc(
2929
prometheus.BuildFQName(namespace, "", "apirequests"),

0 commit comments

Comments
 (0)