Skip to content

Commit

Permalink
chore: add connection.merge
Browse files Browse the repository at this point in the history
  • Loading branch information
moshloop committed Nov 5, 2023
1 parent a212d15 commit 02e4ccb
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
3 changes: 3 additions & 0 deletions context/envvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import (
var envCache = cache.New(5*time.Minute, 10*time.Minute)

func GetEnvValueFromCache(ctx Context, input types.EnvVar, namespace string) (string, error) {
if namespace == "" {
namespace = ctx.GetNamespace()
}
if input.ValueFrom == nil {
return input.ValueStatic, nil
}
Expand Down
60 changes: 60 additions & 0 deletions models/connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,62 @@ func (c Connection) AsMap(removeFields ...string) map[string]any {
return asMap(c, removeFields...)
}

func (c Connection) Merge(ctx types.GetEnvVarFromCache, from any) (*Connection, error) {
if v, ok := from.(types.WithUsernamePassword); ok {
username := v.GetUsername()
if !username.IsEmpty() {
val, err := ctx.GetEnvValueFromCache(username, "")
if err != nil {
return nil, err
}
c.Username = val
}
password := v.GetPassword()
if !password.IsEmpty() {
val, err := ctx.GetEnvValueFromCache(password, "")
if err != nil {
return nil, err
}
c.Password = val
}
}

if v, ok := from.(types.WithCertificate); ok {
cert := v.GetCertificate()
if !cert.IsEmpty() {
val, err := ctx.GetEnvValueFromCache(cert, "")
if err != nil {
return nil, err
}
c.Certificate = val
}
}

if v, ok := from.(types.WithURL); ok {
url := v.GetURL()
if !url.IsEmpty() {
val, err := ctx.GetEnvValueFromCache(url, "")
if err != nil {
return nil, err
}
c.URL = val
}
}

if v, ok := from.(types.WithProperties); ok {

if c.Properties == nil {
c.Properties = make(types.JSONStringMap)
}
for k, v := range v.GetProperties() {
c.Properties[k] = v
}
}

return &c, nil

}

// AsGoGetterURL returns the connection as a url that's supported by https://github.com/hashicorp/go-getter
// Connection details are added to the url as query params
func (c Connection) AsGoGetterURL() (string, error) {
Expand All @@ -125,6 +181,10 @@ func (c Connection) AsGoGetterURL() (string, error) {
case ConnectionTypeGit:
q := parsedURL.Query()

if c.Username != "" || c.Password != "" {
parsedURL.User = url.UserPassword(c.Username, c.Password)
}

if c.Certificate != "" {
q.Set("sshkey", base64.URLEncoding.EncodeToString([]byte(c.Certificate)))
}
Expand Down
24 changes: 24 additions & 0 deletions types/envvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,30 @@ type EnvVar struct {
ValueFrom *EnvVarSource `json:"valueFrom,omitempty" yaml:"valueFrom,omitempty" protobuf:"bytes,3,opt,name=valueFrom"`
}

// With* interfaces provide a mechanism for connections to have their values overwritten by values specified lower in the
// heirachy

type WithUsernamePassword interface {
GetUsername() EnvVar
GetPassword() EnvVar
}

type WithCertificate interface {
GetCertificate() EnvVar
}

type WithURL interface {
GetURL() EnvVar
}

type WithProperties interface {
GetProperties() map[string]string
}

type GetEnvVarFromCache interface {
GetEnvValueFromCache(e EnvVar, namespace string) (string, error)
}

func (e EnvVar) String() string {
if e.ValueFrom == nil {
return e.ValueStatic
Expand Down

0 comments on commit 02e4ccb

Please sign in to comment.