-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[AWS/RDS] Feat: adding RDS Support (#12)
Former-commit-id: fc24b83
- Loading branch information
Showing
6 changed files
with
219 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,142 @@ | ||
package aws | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/service/rds" | ||
"github.com/mensaah/reka/provider/aws/utils" | ||
|
||
"github.com/mensaah/reka/resource" | ||
) | ||
|
||
// returns only instance IDs of unprotected rds instances | ||
func getrdsInstanceDetails(svc *rds.Client, output *rds.DescribeDBClustersOutput, region string) ([]*resource.Resource, error) { | ||
var rdsInstances []*resource.Resource | ||
rdsLogger.Debug("Fetching RDS Details") | ||
for _, instance := range output.DBClusters { | ||
tags := make(resource.Tags) | ||
for _, t := range instance.TagList { | ||
tags[*t.Key] = *t.Value | ||
} | ||
tags["creation-date"] = (*instance.ClusterCreateTime).String() | ||
rds := NewResource(*instance.DBClusterIdentifier, rdsName) | ||
rds.Region = region | ||
// Get CreationDate by getting LaunchTime of attached Volume | ||
rds.CreationDate = *instance.ClusterCreateTime | ||
rds.Tags = tags | ||
rds.Status = utils.GetRDSStatus(*instance.Status) | ||
rdsInstances = append(rdsInstances, rds) | ||
} | ||
|
||
return rdsInstances, nil | ||
} | ||
|
||
// GetAllRDSInstances Get all instances | ||
func GetAllRDSInstances(cfg aws.Config) ([]*resource.Resource, error) { | ||
rdsLogger.Debug("Fetching RDS Clusters") | ||
|
||
svc := rds.NewFromConfig(cfg) | ||
params := &rds.DescribeDBClustersInput{} | ||
|
||
// Build the request with its input parameters | ||
resp, err := svc.DescribeDBClusters(context.TODO(), params) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
instances, err := getrdsInstanceDetails(svc, resp, cfg.Region) | ||
if err != nil { | ||
return nil, err | ||
} | ||
rdsLogger.Debugf("Found %d RDS clusters", len(instances)) | ||
return instances, nil | ||
} | ||
|
||
// StopRDSInstances Stop Running Instances | ||
func StopRDSInstances(cfg aws.Config, instances []*resource.Resource) error { | ||
svc := rds.NewFromConfig(cfg) | ||
var instanceIds []rds.StopDBClusterInput | ||
|
||
for _, instance := range instances { | ||
if instance.IsActive() { | ||
instanceIds = append(instanceIds, rds.StopDBClusterInput{ | ||
DBClusterIdentifier: &instance.UUID, | ||
}) | ||
} | ||
} | ||
|
||
if len(instanceIds) <= 0 { | ||
return nil | ||
} | ||
|
||
for _, instance := range instanceIds { | ||
rdsLogger.Debug("Stopping RDS Clusters ", instance.DBClusterIdentifier, " ...") | ||
resp, err := svc.StopDBCluster(context.TODO(), &instance) | ||
// TODO Attach error to specific instance where the error occurred if possible | ||
if err != nil { | ||
fmt.Println(resp, err) | ||
} | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// ResumeRDSInstances Resume Stopped instances | ||
func ResumeRDSInstances(cfg aws.Config, instances []*resource.Resource) error { | ||
svc := rds.NewFromConfig(cfg) | ||
var instanceIds []*rds.StartDBClusterInput | ||
|
||
for _, instance := range instances { | ||
if instance.IsStopped() { | ||
instanceIds = append(instanceIds, &rds.StartDBClusterInput{ | ||
DBClusterIdentifier: &instance.UUID, | ||
}) | ||
} | ||
} | ||
|
||
if len(instanceIds) <= 0 { | ||
return nil | ||
} | ||
|
||
for _, instance := range instanceIds { | ||
rdsLogger.Debug("Starting RDS Cluster ", instance.DBClusterIdentifier, " ...") | ||
resp, err := svc.StartDBCluster(context.TODO(), instance) | ||
// TODO Attach error to specific instance where the error occurred if possible | ||
if err != nil { | ||
fmt.Println(resp, err) | ||
} | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// TerminateRDSInstances Shutdown instances | ||
func TerminateRDSInstances(cfg aws.Config, instances []*resource.Resource) error { | ||
svc := rds.NewFromConfig(cfg) | ||
var instanceIds []*rds.DeleteDBClusterInput | ||
|
||
for _, instance := range instances { | ||
if instance.IsStopped() || instance.IsActive() { | ||
instanceIds = append(instanceIds, &rds.DeleteDBClusterInput{ | ||
DBClusterIdentifier: &instance.UUID, | ||
}) | ||
} | ||
} | ||
|
||
if len(instanceIds) <= 0 { | ||
return nil | ||
} | ||
|
||
for _, instance := range instanceIds { | ||
rdsLogger.Debug("Terminating RDS Cluster ", instance.DBClusterIdentifier, " ...") | ||
resp, err := svc.DeleteDBCluster(context.TODO(), instance) | ||
// TODO Attach error to specific instance where the error occurred if possible | ||
if err != nil { | ||
fmt.Println(resp, err) | ||
} | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,46 @@ | ||
package aws | ||
|
||
import ( | ||
log "github.com/sirupsen/logrus" | ||
|
||
"github.com/mensaah/reka/config" | ||
"github.com/mensaah/reka/resource" | ||
) | ||
|
||
// Manages RDS instances on the AWS. | ||
// RDS resources support stopping/resuming and terminating instances. | ||
|
||
var rdsManager resource.Manager | ||
|
||
const ( | ||
// Name of resource | ||
rdsName = "rds" | ||
// LongName descriptive name for resource | ||
rdsLongName = "Relational Database Service" | ||
) | ||
|
||
var rdsLogger *log.Entry | ||
|
||
func newRDSManager(cfg *config.Config, logPath string) resource.Manager { | ||
rdsLogger = config.GetLogger(rdsName, logPath) | ||
|
||
rdsManager = resource.Manager{ | ||
Name: rdsName, | ||
LongName: rdsLongName, | ||
Config: cfg, | ||
Logger: logger, | ||
GetAll: func() ([]*resource.Resource, error) { | ||
return GetAllRDSInstances(*cfg.Aws) | ||
}, | ||
Destroy: func(resources []*resource.Resource) error { | ||
return TerminateRDSInstances(*cfg.Aws, resources) | ||
}, | ||
Stop: func(resources []*resource.Resource) error { | ||
return StopRDSInstances(*cfg.Aws, resources) | ||
}, | ||
Resume: func(resources []*resource.Resource) error { | ||
return ResumeRDSInstances(*cfg.Aws, resources) | ||
}, | ||
} | ||
return rdsManager | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters