Skip to content

Commit

Permalink
Add URI to MongoDB config (#513)
Browse files Browse the repository at this point in the history
Signed-off-by: Nathan <[email protected]>
  • Loading branch information
nathan-artie authored Oct 9, 2024
1 parent 5349dd7 commit ffae007
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 21 deletions.
18 changes: 12 additions & 6 deletions config/mongodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"

"github.com/artie-labs/reader/constants"
"github.com/artie-labs/transfer/lib/stringutil"
)

func (s StreamingSettings) Validate() error {
Expand All @@ -27,9 +26,7 @@ type StreamingSettings struct {
}

type MongoDB struct {
Host string `yaml:"host"`
Username string `yaml:"username,omitempty"`
Password string `yaml:"password,omitempty"`
URI string `yaml:"uri"`
Database string `yaml:"database"`
Collections []Collection `yaml:"collections"`
StreamingSettings StreamingSettings `yaml:"streamingSettings,omitempty"`
Expand All @@ -38,6 +35,11 @@ type MongoDB struct {
// DisableFullDocumentBeforeChange - This is relevant if you're connecting to Document DB.
// BSON field '$changeStream.fullDocumentBeforeChange' is an unknown field.
DisableFullDocumentBeforeChange bool `yaml:"disableFullDocumentBeforeChange,omitempty"`

// Deprecated - use [MongoDB.URI] instead.
Host string `yaml:"host"`
Username string `yaml:"username,omitempty"`
Password string `yaml:"password,omitempty"`
}

type Collection struct {
Expand All @@ -60,8 +62,12 @@ func (m MongoDB) GetStreamingBatchSize() int32 {
}

func (m MongoDB) Validate() error {
if stringutil.Empty(m.Host, m.Database) {
return fmt.Errorf("one of the MongoDB settings is empty: host or database")
if m.Host == "" && m.URI == "" {
return fmt.Errorf("either host or URI must be passed in")
}

if m.Database == "" {
return fmt.Errorf("database is empty")
}

if len(m.Collections) == 0 {
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) {
opts := options.Client()

if cfg.URI != "" {
opts = opts.ApplyURI(cfg.URI)
} else if cfg.Host != "" {
opts = opts.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
}
24 changes: 20 additions & 4 deletions 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 All @@ -14,7 +15,8 @@ func TestOptsFromConfig(t *testing.T) {
Password: "password",
}

opts := OptsFromConfig(cfg)
opts, err := OptsFromConfig(cfg)
assert.NoError(t, err)
assert.NotNil(t, opts.TLSConfig)
assert.Equal(t, "user", opts.Auth.Username)
assert.Equal(t, "password", opts.Auth.Password)
Expand All @@ -25,7 +27,8 @@ func TestOptsFromConfig(t *testing.T) {
Host: "localhost",
}

opts := OptsFromConfig(cfg)
opts, err := OptsFromConfig(cfg)
assert.NoError(t, err)
assert.Nil(t, opts.Auth)
}
{
Expand All @@ -35,7 +38,20 @@ func TestOptsFromConfig(t *testing.T) {
DisableTLS: true,
}

opts := OptsFromConfig(cfg)
opts, err := OptsFromConfig(cfg)
assert.NoError(t, err)
assert.Nil(t, opts.TLSConfig)
}
{
// Using URI:
cfg := config.MongoDB{
URI: "mongodb://user:pass@localhost",
}

opts, err := OptsFromConfig(cfg)
assert.NoError(t, err)
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("failed to build options for MongoDB: %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 ffae007

Please sign in to comment.