Skip to content

Commit

Permalink
print the database connection URI
Browse files Browse the repository at this point in the history
  • Loading branch information
afeld committed Jan 1, 2018
1 parent af8e833 commit 62b62eb
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 21 deletions.
34 changes: 20 additions & 14 deletions connector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,35 @@ Name: {{.Name}}
{{if .HasRepl }}To connect:
{{.LaunchCmd}}
{{.LaunchCmd}}
{{end}}Leave this terminal open while you want to use the SSH tunnel. Press Control-C to stop.
{{end}}Connection URI (note this may vary slightly by client):
{{.ConnectionUri}}
Leave this terminal open while you want to use the SSH tunnel. Press Control-C to stop.
`

type localConnectionData struct {
Port int
User string
Pass string
Name string
HasRepl bool
LaunchCmd service.LaunchCmd
Port int
User string
Pass string
Name string
HasRepl bool
LaunchCmd service.LaunchCmd
ConnectionUri string
}

func manualConnect(srv service.Service, tunnel launcher.SSHTunnel, creds models.Credentials) (err error) {
launchCmd := srv.GetLaunchCmd(tunnel.LocalPort, creds)
connectionData := localConnectionData{
Port: tunnel.LocalPort,
User: creds.GetUsername(),
Pass: creds.GetPassword(),
Name: creds.GetDBName(),
HasRepl: srv.HasRepl(),
LaunchCmd: launchCmd,
Port: tunnel.LocalPort,
User: creds.GetUsername(),
Pass: creds.GetPassword(),
Name: creds.GetDBName(),
HasRepl: srv.HasRepl(),
LaunchCmd: launchCmd,
ConnectionUri: srv.GetConnectionUri(tunnel.LocalPort, creds),
}

tmpl, err := template.New("").Parse(manualConnectInstructions)
Expand Down
6 changes: 6 additions & 0 deletions service/mongo_db.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package service

import (
"fmt"
"strconv"

"github.com/18F/cf-service-connect/models"
Expand All @@ -12,6 +13,11 @@ func (p mongoDB) Match(si models.ServiceInstance) bool {
return si.ContainsTerms("mongo")
}

// https://docs.mongodb.com/manual/reference/connection-string/
func (p mongoDB) GetConnectionUri(localPort int, creds models.Credentials) string {
return fmt.Sprintf("mongodb://%s:%s@localhost:%d/%s", creds.GetUsername(), creds.GetPassword(), localPort, creds.GetDBName())
}

func (p mongoDB) HasRepl() bool {
return true
}
Expand Down
6 changes: 6 additions & 0 deletions service/mysql.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package service

import (
"fmt"
"strconv"

"github.com/18F/cf-service-connect/models"
Expand All @@ -12,6 +13,11 @@ func (p mySQL) Match(si models.ServiceInstance) bool {
return si.ContainsTerms("mysql")
}

// https://dev.mysql.com/doc/mysql-shell-excerpt/5.7/en/mysql-shell-connection-using-uri.html
func (p mySQL) GetConnectionUri(localPort int, creds models.Credentials) string {
return fmt.Sprintf("mysql://%s:%s@localhost:%d/%s", creds.GetUsername(), creds.GetPassword(), localPort, creds.GetDBName())
}

func (p mySQL) HasRepl() bool {
return true
}
Expand Down
14 changes: 7 additions & 7 deletions service/psql.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ func (p pSQL) Match(si models.ServiceInstance) bool {
return si.ContainsTerms("psql", "postgres")
}

// https://www.postgresql.org/docs/current/static/libpq-connect.html#LIBPQ-CONNSTRING
func (p pSQL) GetConnectionUri(localPort int, creds models.Credentials) string {
return fmt.Sprintf("postgresql://%s:%s@localhost:%d/%s", creds.GetUsername(), creds.GetPassword(), localPort, creds.GetDBName())
}

func (p pSQL) HasRepl() bool {
return true
}

func (p pSQL) GetLaunchCmd(localPort int, creds models.Credentials) LaunchCmd {
return LaunchCmd{
Envs: map[string]string{
"PGPASSWORD": creds.GetPassword(),
},
Cmd: "psql",
Args: []string{
"-h", "localhost",
"-p", fmt.Sprintf("%d", localPort),
creds.GetDBName(),
creds.GetUsername(),
// http://www.starkandwayne.com/blog/using-a-postgres-uri-with-psql/
p.GetConnectionUri(localPort, creds),
},
}
}
Expand Down
6 changes: 6 additions & 0 deletions service/redis.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package service

import (
"fmt"
"strconv"

"github.com/18F/cf-service-connect/models"
Expand All @@ -12,6 +13,11 @@ func (p redis) Match(si models.ServiceInstance) bool {
return si.ContainsTerms("redis")
}

// https://www.iana.org/assignments/uri-schemes/prov/redis
func (p redis) GetConnectionUri(localPort int, creds models.Credentials) string {
return fmt.Sprintf("redis://%s:%s@localhost:%d/%s", creds.GetUsername(), creds.GetPassword(), localPort, creds.GetDBName())
}

func (p redis) HasRepl() bool {
return true
}
Expand Down
1 change: 1 addition & 0 deletions service/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import "github.com/18F/cf-service-connect/models"

type Service interface {
Match(si models.ServiceInstance) bool
GetConnectionUri(localPort int, creds models.Credentials) string
HasRepl() bool
GetLaunchCmd(localPort int, creds models.Credentials) LaunchCmd
}
Expand Down
4 changes: 4 additions & 0 deletions service/unknown_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ func (p unknownService) Match(si models.ServiceInstance) bool {
return true
}

func (p unknownService) GetConnectionUri(localPort int, creds models.Credentials) string {
return "unknown"
}

func (p unknownService) HasRepl() bool {
return false
}
Expand Down

0 comments on commit 62b62eb

Please sign in to comment.