Open opens a MongoDB driver.
(_ context.Context, _ storepb.Engine, connCfg db.ConnectionConfig)
| 46 | |
| 47 | // Open opens a MongoDB driver. |
| 48 | func (d *Driver) Open(_ context.Context, _ storepb.Engine, connCfg db.ConnectionConfig) (db.Driver, error) { |
| 49 | connectionURI := getBasicMongoDBConnectionURI(connCfg) |
| 50 | opts := options.Client().ApplyURI(connectionURI) |
| 51 | tlscfg, err := util.GetTLSConfig(connCfg.DataSource) |
| 52 | if err != nil { |
| 53 | return nil, errors.Wrap(err, "failed to get SSL config") |
| 54 | } |
| 55 | if tlscfg != nil { |
| 56 | // Use the TLS config from util.GetTLSConfig which respects verify_tls_certificate setting |
| 57 | opts.SetTLSConfig(tlscfg) |
| 58 | } |
| 59 | client, err := mongo.Connect(opts) |
| 60 | if err != nil { |
| 61 | return nil, errors.Wrap(err, "failed to create MongoDB client") |
| 62 | } |
| 63 | d.client = client |
| 64 | d.connCfg = connCfg |
| 65 | d.databaseName = connCfg.ConnectionContext.DatabaseName |
| 66 | return d, nil |
| 67 | } |
| 68 | |
| 69 | // Close closes the MongoDB driver. |
| 70 | func (d *Driver) Close(ctx context.Context) error { |