github.com/aavshr/aws-sdk-go@v1.41.3/service/rds/rdsutils/builder.go (about) 1 package rdsutils 2 3 import ( 4 "fmt" 5 "net/url" 6 7 "github.com/aavshr/aws-sdk-go/aws/awserr" 8 "github.com/aavshr/aws-sdk-go/aws/credentials" 9 ) 10 11 // ConnectionFormat is the type of connection that will be 12 // used to connect to the database 13 type ConnectionFormat string 14 15 // ConnectionFormat enums 16 const ( 17 NoConnectionFormat ConnectionFormat = "" 18 TCPFormat ConnectionFormat = "tcp" 19 ) 20 21 // ErrNoConnectionFormat will be returned during build if no format had been 22 // specified 23 var ErrNoConnectionFormat = awserr.New("NoConnectionFormat", "No connection format was specified", nil) 24 25 // ConnectionStringBuilder is a builder that will construct a connection 26 // string with the provided parameters. params field is required to have 27 // a tls specification and allowCleartextPasswords must be set to true. 28 type ConnectionStringBuilder struct { 29 dbName string 30 endpoint string 31 region string 32 user string 33 creds *credentials.Credentials 34 35 connectFormat ConnectionFormat 36 params url.Values 37 } 38 39 // NewConnectionStringBuilder will return an ConnectionStringBuilder 40 func NewConnectionStringBuilder(endpoint, region, dbUser, dbName string, creds *credentials.Credentials) ConnectionStringBuilder { 41 return ConnectionStringBuilder{ 42 dbName: dbName, 43 endpoint: endpoint, 44 region: region, 45 user: dbUser, 46 creds: creds, 47 } 48 } 49 50 // WithEndpoint will return a builder with the given endpoint 51 func (b ConnectionStringBuilder) WithEndpoint(endpoint string) ConnectionStringBuilder { 52 b.endpoint = endpoint 53 return b 54 } 55 56 // WithRegion will return a builder with the given region 57 func (b ConnectionStringBuilder) WithRegion(region string) ConnectionStringBuilder { 58 b.region = region 59 return b 60 } 61 62 // WithUser will return a builder with the given user 63 func (b ConnectionStringBuilder) WithUser(user string) ConnectionStringBuilder { 64 b.user = user 65 return b 66 } 67 68 // WithDBName will return a builder with the given database name 69 func (b ConnectionStringBuilder) WithDBName(dbName string) ConnectionStringBuilder { 70 b.dbName = dbName 71 return b 72 } 73 74 // WithParams will return a builder with the given params. The parameters 75 // will be included in the connection query string 76 // 77 // Example: 78 // v := url.Values{} 79 // v.Add("tls", "rds") 80 // b := rdsutils.NewConnectionBuilder(endpoint, region, user, dbname, creds) 81 // connectStr, err := b.WithParams(v).WithTCPFormat().Build() 82 func (b ConnectionStringBuilder) WithParams(params url.Values) ConnectionStringBuilder { 83 b.params = params 84 return b 85 } 86 87 // WithFormat will return a builder with the given connection format 88 func (b ConnectionStringBuilder) WithFormat(f ConnectionFormat) ConnectionStringBuilder { 89 b.connectFormat = f 90 return b 91 } 92 93 // WithTCPFormat will set the format to TCP and return the modified builder 94 func (b ConnectionStringBuilder) WithTCPFormat() ConnectionStringBuilder { 95 return b.WithFormat(TCPFormat) 96 } 97 98 // Build will return a new connection string that can be used to open a connection 99 // to the desired database. 100 // 101 // Example: 102 // b := rdsutils.NewConnectionStringBuilder(endpoint, region, user, dbname, creds) 103 // connectStr, err := b.WithTCPFormat().Build() 104 // if err != nil { 105 // panic(err) 106 // } 107 // const dbType = "mysql" 108 // db, err := sql.Open(dbType, connectStr) 109 func (b ConnectionStringBuilder) Build() (string, error) { 110 if b.connectFormat == NoConnectionFormat { 111 return "", ErrNoConnectionFormat 112 } 113 114 authToken, err := BuildAuthToken(b.endpoint, b.region, b.user, b.creds) 115 if err != nil { 116 return "", err 117 } 118 119 connectionStr := fmt.Sprintf("%s:%s@%s(%s)/%s", 120 b.user, authToken, string(b.connectFormat), b.endpoint, b.dbName, 121 ) 122 123 if len(b.params) > 0 { 124 connectionStr = fmt.Sprintf("%s?%s", connectionStr, b.params.Encode()) 125 } 126 return connectionStr, nil 127 }