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 e79abde
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
61 changes: 61 additions & 0 deletions models/connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"time"

"github.com/flanksource/commons/hash"
dutyContext "github.com/flanksource/duty/context"
"github.com/flanksource/duty/types"
"github.com/google/uuid"
)
Expand Down Expand Up @@ -105,6 +106,62 @@ func (c Connection) AsMap(removeFields ...string) map[string]any {
return asMap(c, removeFields...)
}

func (c Connection) Merge(ctx dutyContext.Context, from any) (*Connection, error) {
if v, ok := from.(types.WithUsernamePassword); ok {
username := v.GetUsername()
if !username.IsEmpty() {
val, err := ctx.GetEnvValueFromCache(username, ctx.Namespace)
if err != nil {
return nil, err
}
c.Username = val
}
password := v.GetPassword()
if !password.IsEmpty() {
val, err := ctx.GetEnvValueFromCache(password, ctx.Namespace)
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, ctx.Namespace)
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, ctx.Namespace)
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 +182,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
20 changes: 20 additions & 0 deletions types/envvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,26 @@ 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
}

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

0 comments on commit e79abde

Please sign in to comment.