This repository has been archived by the owner on May 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
rds.go
69 lines (54 loc) · 2.04 KB
/
rds.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/******************************************************************************
Cloud Resource Counter
File: rds.go
Summary: Provides a count of all RDS instances.
******************************************************************************/
package main
import (
"github.com/aws/aws-sdk-go/service/rds"
color "github.com/logrusorgru/aurora"
)
// RDSInstances retrieves the count of all RDS Instances either for all regions
// (allRegions is true) or the region associated with the session. This method
// gives status back to the user via the supplied ActivityMonitor instance.
func RDSInstances(sf ServiceFactory, am ActivityMonitor, allRegions bool) int {
// Indicate activity
am.StartAction("Retrieving RDS instance counts")
// Should we get the counts for all regions?
instanceCount := 0
if allRegions {
// Get the list of all enabled regions for this account
regionsSlice := GetEC2Regions(sf.GetEC2InstanceService(""), am)
// Loop through all of the regions
for _, regionName := range regionsSlice {
// Get the RDS instance counts for a specific region
instanceCount += rdsInstancesForSingleRegion(sf.GetRDSInstanceService(regionName), am)
}
} else {
// Get the RDS instance counts for the region selected by this session
instanceCount = rdsInstancesForSingleRegion(sf.GetRDSInstanceService(""), am)
}
// Indicate end of activity
am.EndAction("OK (%d)", color.Bold(instanceCount))
return instanceCount
}
func rdsInstancesForSingleRegion(rdsis *RDSInstanceService, am ActivityMonitor) int {
// Construct our input to find all RDS instances
input := &rds.DescribeDBInstancesInput{}
// Indicate activity
am.Message(".")
// Invoke our service
instanceCount := 0
err := rdsis.InspectInstances(input, func(page *rds.DescribeDBInstancesOutput, lastPage bool) bool {
// Loop through the DB Instances...
for _, dbi := range page.DBInstances {
if dbi.DBInstanceStatus != nil && *dbi.DBInstanceStatus == "available" {
instanceCount++
}
}
return true
})
// Check for error
am.CheckError(err)
return instanceCount
}