Skip to content

Commit

Permalink
feat: add to model helpers for s3 and aws connections
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Aug 26, 2024
1 parent 48deef0 commit d6b4fc6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
15 changes: 15 additions & 0 deletions connection/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/aws/aws-sdk-go-v2/credentials/stscreds"
"github.com/aws/aws-sdk-go-v2/service/sts"
"github.com/flanksource/duty/context"
"github.com/flanksource/duty/models"
"github.com/flanksource/duty/types"
"github.com/henvic/httpretty"
)
Expand Down Expand Up @@ -47,6 +48,20 @@ func (t *AWSConnection) GetURL() types.EnvVar {
return types.EnvVar{ValueStatic: t.Endpoint}
}

func (t AWSConnection) ToModel() models.Connection {
return models.Connection{
Type: models.ConnectionTypeAWS,
Username: t.AccessKey.ValueStatic,
Password: t.SecretKey.ValueStatic,
URL: t.Endpoint,
InsecureTLS: t.SkipTLSVerify,
Properties: types.JSONStringMap{
"region": t.Region,
"assumeRole": t.AssumeRole,
},
}
}

// Populate populates an AWSConnection with credentials and other information.
// If a connection name is specified, it'll be used to populate the endpoint, accessKey and secretKey.
func (t *AWSConnection) Populate(ctx ConnectionContext) error {
Expand Down
16 changes: 16 additions & 0 deletions connection/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strconv"

"github.com/flanksource/commons/collections"
"github.com/flanksource/duty/models"
)

// +kubebuilder:object:generate=true
Expand Down Expand Up @@ -58,3 +59,18 @@ func (t *S3Connection) Populate(ctx ConnectionContext) error {

return nil
}

func (c S3Connection) ToModel() models.Connection {
conn := c.AWSConnection.ToModel()
conn.Type = models.ConnectionTypeS3
if c.Bucket != "" {
conn.Properties["bucket"] = c.Bucket
}
if c.ObjectPath != "" {
conn.Properties["objectPath"] = c.ObjectPath
}
if c.UsePathStyle {
conn.Properties["usePathStyle"] = strconv.FormatBool(c.UsePathStyle)
}
return conn
}
8 changes: 8 additions & 0 deletions models/connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ type Connection struct {
CreatedBy *uuid.UUID `gorm:"column:created_by" json:"created_by,omitempty" faker:"-" `
}

func (c *Connection) SetProperty(key, value string) {
if c.Properties == nil {
c.Properties = make(types.JSONStringMap)
}

c.Properties[key] = value
}

func (c Connection) TableName() string {
return "connections"
}
Expand Down

0 comments on commit d6b4fc6

Please sign in to comment.