Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan-artie committed Oct 4, 2024
1 parent 6c23184 commit db98a68
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 12 deletions.
1 change: 1 addition & 0 deletions config/mongodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type StreamingSettings struct {
}

type MongoDB struct {
URI string `yaml:"uri"`
Host string `yaml:"host"`
Username string `yaml:"username,omitempty"`
Password string `yaml:"password,omitempty"`
Expand Down
30 changes: 20 additions & 10 deletions lib/mongo/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,32 @@ package mongo

import (
"crypto/tls"
"fmt"

"github.com/artie-labs/reader/config"
"go.mongodb.org/mongo-driver/mongo/options"
)

func OptsFromConfig(cfg config.MongoDB) *options.ClientOptions {
opts := options.Client().ApplyURI(cfg.Host)
if !cfg.DisableTLS {
opts = opts.SetTLSConfig(&tls.Config{})
func OptsFromConfig(cfg config.MongoDB) (*options.ClientOptions, error) {
var opts *options.ClientOptions

if cfg.URI != "" {
opts = options.Client().ApplyURI(cfg.URI)
} else if cfg.Host != "" {
opts = options.Client().ApplyURI(cfg.Host)
if cfg.Username != "" && cfg.Password != "" {
opts = opts.SetAuth(options.Credential{
Username: cfg.Username,
Password: cfg.Password,
})
}
} else {
return nil, fmt.Errorf("mongoDB requires a URI or host")
}

if cfg.Username != "" && cfg.Password != "" {
opts = opts.SetAuth(options.Credential{
Username: cfg.Username,
Password: cfg.Password,
})
if !cfg.DisableTLS {
opts = opts.SetTLSConfig(&tls.Config{})
}

return opts
return opts, nil
}
14 changes: 13 additions & 1 deletion lib/mongo/config_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package mongo

import (
"testing"

"github.com/artie-labs/reader/config"
"github.com/stretchr/testify/assert"
"testing"
)

func TestOptsFromConfig(t *testing.T) {
Expand Down Expand Up @@ -38,4 +39,15 @@ func TestOptsFromConfig(t *testing.T) {
opts := OptsFromConfig(cfg)
assert.Nil(t, opts.TLSConfig)
}
{
// Using URI:
cfg := config.MongoDB{
URI: "mongodb://user:pass@localhost",
}

opts := OptsFromConfig(cfg)
assert.NotNil(t, opts.TLSConfig)
assert.Equal(t, "user", opts.Auth.Username)
assert.Equal(t, "pass", opts.Auth.Password)
}
}
10 changes: 9 additions & 1 deletion sources/mongo/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@ type Source struct {
}

func Load(ctx context.Context, cfg config.MongoDB) (*Source, bool, error) {
client, err := mongo.Connect(ctx, mongoLib.OptsFromConfig(cfg))
opts, err := mongoLib.OptsFromConfig(cfg)
if err != nil {
return nil, false, fmt.Errorf("options for MongoDB are invalid: %w", err)
}
if err := opts.Validate(); err != nil {
return nil, false, fmt.Errorf("validation failed for MongoDB options: %w", err)
}

client, err := mongo.Connect(ctx, opts)
if err != nil {
return nil, false, fmt.Errorf("failed to connect to MongoDB: %w", err)
}
Expand Down

0 comments on commit db98a68

Please sign in to comment.