-
Notifications
You must be signed in to change notification settings - Fork 0
/
rds.go
136 lines (113 loc) · 4.14 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
Copyright 2018 The Crossplane Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package rds
import (
"fmt"
"strings"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/rds"
"github.com/aws/aws-sdk-go-v2/service/rds/rdsiface"
"github.com/crossplaneio/crossplane/pkg/apis/aws/database/v1alpha1"
)
// Instance crossplane representation of the to AWS DBInstance
type Instance struct {
Name string
ARN string
Status string
Endpoint string
}
// NewInstance returns new Instance structure
func NewInstance(instance *rds.DBInstance) *Instance {
endpoint := ""
if instance.Endpoint != nil {
endpoint = aws.StringValue(instance.Endpoint.Address)
}
return &Instance{
Name: aws.StringValue(instance.DBInstanceIdentifier),
ARN: aws.StringValue(instance.DBInstanceArn),
Status: aws.StringValue(instance.DBInstanceStatus),
Endpoint: endpoint,
}
}
// Client defines RDS RDSClient operations
type Client interface {
CreateInstance(string, string, *v1alpha1.RDSInstanceSpec) (*Instance, error)
GetInstance(name string) (*Instance, error)
DeleteInstance(name string) (*Instance, error)
}
type rdsClient struct {
rds rdsiface.RDSAPI
}
// NewClient creates new RDS RDSClient with provided AWS Configurations/Credentials
func NewClient(config *aws.Config) Client {
return &rdsClient{rds.New(*config)}
}
// CreateInstance creates RDS Instance with provided Specification
func (r *rdsClient) CreateInstance(name, password string, spec *v1alpha1.RDSInstanceSpec) (*Instance, error) {
input := CreateDBInstanceInput(name, password, spec)
output, err := r.rds.CreateDBInstanceRequest(input).Send()
if err != nil {
return nil, err
}
return NewInstance(output.DBInstance), nil
}
// GetInstance finds RDS Instance by name
func (r *rdsClient) GetInstance(name string) (*Instance, error) {
input := rds.DescribeDBInstancesInput{DBInstanceIdentifier: &name}
output, err := r.rds.DescribeDBInstancesRequest(&input).Send()
if err != nil {
return nil, err
}
outputCount := len(output.DBInstances)
if outputCount == 0 || outputCount > 1 {
return nil, fmt.Errorf("rds instance [%s] is not found", name)
}
return NewInstance(&output.DBInstances[0]), nil
}
// DeleteInstance deletes RDS Instance
func (r *rdsClient) DeleteInstance(name string) (*Instance, error) {
input := rds.DeleteDBInstanceInput{
DBInstanceIdentifier: &name,
SkipFinalSnapshot: aws.Bool(true),
}
output, err := r.rds.DeleteDBInstanceRequest(&input).Send()
if err != nil {
return nil, err
}
return NewInstance(output.DBInstance), nil
}
// IsErrorAlreadyExists returns true if the supplied error indicates a cluster
// does already exists.
func IsErrorAlreadyExists(err error) bool {
return strings.Contains(err.Error(), rds.ErrCodeDBClusterAlreadyExistsFault)
}
// IsErrorNotFound helper function to test for ErrCodeDBInstanceNotFoundFault error
func IsErrorNotFound(err error) bool {
return strings.Contains(err.Error(), rds.ErrCodeDBInstanceNotFoundFault)
}
// CreateDBInstanceInput from RDSInstanceSpec
func CreateDBInstanceInput(name, password string, spec *v1alpha1.RDSInstanceSpec) *rds.CreateDBInstanceInput {
return &rds.CreateDBInstanceInput{
DBInstanceIdentifier: aws.String(name),
AllocatedStorage: aws.Int64(spec.Size),
DBInstanceClass: aws.String(spec.Class),
Engine: aws.String(spec.Engine),
EngineVersion: aws.String(spec.EngineVersion),
MasterUsername: aws.String(spec.MasterUsername),
MasterUserPassword: aws.String(password),
BackupRetentionPeriod: aws.Int64(0),
VpcSecurityGroupIds: spec.SecurityGroups,
PubliclyAccessible: aws.Bool(true),
DBSubnetGroupName: aws.String(spec.SubnetGroupName),
}
}