Setting URL correctly when username/password is not provided (#369)
* Adding Username/password optional * fixing lint errors
This commit is contained in:
parent
a6a0e3e3bc
commit
7b0fbe65cc
|
@ -44,7 +44,10 @@ const (
|
||||||
defaultCollectionName = "daprCollection"
|
defaultCollectionName = "daprCollection"
|
||||||
|
|
||||||
// mongodb://<username>:<password@<host>/<database><params>
|
// mongodb://<username>:<password@<host>/<database><params>
|
||||||
connectionURIFormat = "mongodb://%s:%s@%s/%s%s"
|
connectionURIFormatWithAuthentication = "mongodb://%s:%s@%s/%s%s"
|
||||||
|
|
||||||
|
// mongodb://<host>/<database><params>
|
||||||
|
connectionURIFormat = "mongodb://%s/%s%s"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MongoDB is a state store implementation for MongoDB
|
// MongoDB is a state store implementation for MongoDB
|
||||||
|
@ -262,15 +265,14 @@ func (m *MongoDB) doTransaction(sessCtx mongo.SessionContext, operations []state
|
||||||
}
|
}
|
||||||
|
|
||||||
func getMongoURI(metadata *mongoDBMetadata) string {
|
func getMongoURI(metadata *mongoDBMetadata) string {
|
||||||
return fmt.Sprintf(connectionURIFormat, metadata.username, metadata.password, metadata.host, metadata.databaseName, metadata.params)
|
if metadata.username != "" && metadata.password != "" {
|
||||||
|
return fmt.Sprintf(connectionURIFormatWithAuthentication, metadata.username, metadata.password, metadata.host, metadata.databaseName, metadata.params)
|
||||||
|
}
|
||||||
|
return fmt.Sprintf(connectionURIFormat, metadata.host, metadata.databaseName, metadata.params)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getMongoDBClient(metadata *mongoDBMetadata) (*mongo.Client, error) {
|
func getMongoDBClient(metadata *mongoDBMetadata) (*mongo.Client, error) {
|
||||||
var uri string
|
uri := getMongoURI(metadata)
|
||||||
|
|
||||||
if metadata.username != "" && metadata.password != "" {
|
|
||||||
uri = getMongoURI(metadata)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set client options
|
// Set client options
|
||||||
clientOptions := options.Client().ApplyURI(uri)
|
clientOptions := options.Client().ApplyURI(uri)
|
||||||
|
|
|
@ -83,6 +83,25 @@ func TestGetMongoDBMetadata(t *testing.T) {
|
||||||
assert.Equal(t, expected, uri)
|
assert.Equal(t, expected, uri)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("Valid connectionstring without username", func(t *testing.T) {
|
||||||
|
properties := map[string]string{
|
||||||
|
host: "localhost:27017",
|
||||||
|
databaseName: "TestDB",
|
||||||
|
collectionName: "TestCollection",
|
||||||
|
}
|
||||||
|
m := state.Metadata{
|
||||||
|
Properties: properties,
|
||||||
|
}
|
||||||
|
|
||||||
|
metadata, err := getMongoDBMetaData(m)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
uri := getMongoURI(metadata)
|
||||||
|
expected := "mongodb://localhost:27017/TestDB"
|
||||||
|
|
||||||
|
assert.Equal(t, expected, uri)
|
||||||
|
})
|
||||||
|
|
||||||
t.Run("Valid connectionstring with params", func(t *testing.T) {
|
t.Run("Valid connectionstring with params", func(t *testing.T) {
|
||||||
properties := map[string]string{
|
properties := map[string]string{
|
||||||
host: "127.0.0.2",
|
host: "127.0.0.2",
|
||||||
|
|
Loading…
Reference in New Issue