github.com/smartcontractkit/chainlink-testing-framework/libs@v0.0.0-20240227141906-ec710b4eb1a3/k8s/client/converter.go (about) 1 package client 2 3 import ( 4 "errors" 5 "fmt" 6 ) 7 8 type ConnectionMode int 9 10 const ( 11 LocalConnection ConnectionMode = iota 12 RemoteConnection 13 ) 14 15 // Protocol represents a URL scheme to use when fetching connection details 16 type Protocol int 17 18 const ( 19 // WS : Web Socket Protocol 20 WS Protocol = iota 21 // WSSUFFIX : Web Socket Protocol 22 WSSUFFIX 23 // WSS : Web Socket Secure Protocol 24 WSS 25 // HTTP : Hypertext Transfer Protocol 26 HTTP 27 // HTTPS : Hypertext Transfer Protocol Secure 28 HTTPS 29 POSTGRESQL 30 ) 31 32 // URLConverter converts ports to URLs 33 type URLConverter struct { 34 ci ConnectionInfo 35 err error 36 } 37 38 // NewURLConverter creates new URLConverter instance 39 func NewURLConverter(fp ConnectionInfo, err error) *URLConverter { 40 return &URLConverter{fp, err} 41 } 42 43 // As converts host/port to an URL 44 func (m *URLConverter) As(conn ConnectionMode, proto Protocol) (string, error) { 45 if m.err != nil { 46 return "", m.err 47 } 48 var host string 49 var port uint16 50 if conn == RemoteConnection { 51 host = m.ci.Host 52 port = m.ci.Ports.Remote 53 } else { 54 host = "127.0.0.1" 55 port = m.ci.Ports.Local 56 } 57 switch proto { 58 case HTTP: 59 return fmt.Sprintf("http://%s:%d", host, port), nil 60 case HTTPS: 61 return fmt.Sprintf("https://%s:%d", host, port), nil 62 case WS: 63 return fmt.Sprintf("ws://%s:%d", host, port), nil 64 case WSSUFFIX: 65 return fmt.Sprintf("ws://%s:%d/ws", host, port), nil 66 case WSS: 67 return fmt.Sprintf("wss://%s:%d", host, port), nil 68 case POSTGRESQL: 69 return fmt.Sprintf("postgresql://%s:%d", host, port), nil 70 default: 71 return "", errors.New("unknown protocol conversion type") 72 } 73 }