diff --git a/api/api_gomux.go b/api/api_gomux.go index b06f82e..3e3dabc 100644 --- a/api/api_gomux.go +++ b/api/api_gomux.go @@ -18,7 +18,7 @@ import ( "github.com/gorilla/websocket" "github.com/swaggo/http-swagger" - "go.mongodb.org/mongo-driver/bson/primitive" + "go.mongodb.org/mongo-driver/v2/bson" "github.com/square/etre" "github.com/square/etre/app" @@ -494,13 +494,13 @@ func (api *API) id(next http.Handler) http.Handler { rc := ctx.Value(reqKey).(*req) var err error - var entityId primitive.ObjectID + var entityId bson.ObjectID id := r.PathValue("id") // 1. from URL if id == "" { err = ErrMissingParam.New("missing id param") } else { - entityId, err = primitive.ObjectIDFromHex(id) // 2. convert to/validate as ObjectID + entityId, err = bson.ObjectIDFromHex(id) // 2. convert to/validate as ObjectID if err != nil { err = ErrInvalidParam.New("id '%s' is not a valid ObjectID: %v", id, err) } @@ -1297,8 +1297,8 @@ func (api *API) WriteResult(rc *req, w http.ResponseWriter, ids interface{}, err diffs := ids.([]etre.Entity) writes = make([]etre.Write, len(diffs)) for i, diff := range diffs { - // _id from db is primitive.ObjectID, convert to string - id := diff["_id"].(primitive.ObjectID).Hex() + // _id from db is bson.ObjectID, convert to string + id := diff["_id"].(bson.ObjectID).Hex() writes[i] = etre.Write{ EntityId: id, URI: api.addr + etre.API_ROOT + "/entity/" + id, @@ -1322,8 +1322,8 @@ func (api *API) WriteResult(rc *req, w http.ResponseWriter, ids interface{}, err case etre.Entity: // Entity from DeleteLabel diff := ids.(etre.Entity) - // _id from db is primitive.ObjectID, convert to string - id := diff["_id"].(primitive.ObjectID).Hex() + // _id from db is bson.ObjectID, convert to string + id := diff["_id"].(bson.ObjectID).Hex() writes = []etre.Write{ { EntityId: id, diff --git a/api/api_test.go b/api/api_test.go index 58105e5..1183d7d 100644 --- a/api/api_test.go +++ b/api/api_test.go @@ -16,7 +16,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "go.mongodb.org/mongo-driver/bson/primitive" + "go.mongodb.org/mongo-driver/v2/bson" "github.com/square/etre" "github.com/square/etre/api" @@ -60,9 +60,9 @@ var testEntities = []etre.Entity{ var testEntityIds = []string{"59f10d2a5669fc79103a0000", "59f10d2a5669fc79103a1111", "59f10d2a5669fc79103a2222"} var ( - testEntityId0, _ = primitive.ObjectIDFromHex(testEntityIds[0]) - testEntityId1, _ = primitive.ObjectIDFromHex(testEntityIds[1]) - testEntityId2, _ = primitive.ObjectIDFromHex(testEntityIds[2]) + testEntityId0, _ = bson.ObjectIDFromHex(testEntityIds[0]) + testEntityId1, _ = bson.ObjectIDFromHex(testEntityIds[1]) + testEntityId2, _ = bson.ObjectIDFromHex(testEntityIds[2]) ) var testEntitiesWithObjectIDs = []etre.Entity{ diff --git a/cdc/changestream/server.go b/cdc/changestream/server.go index 562eac4..9260e1b 100644 --- a/cdc/changestream/server.go +++ b/cdc/changestream/server.go @@ -11,8 +11,8 @@ import ( "github.com/square/etre" - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/v2/bson" + "go.mongodb.org/mongo-driver/v2/mongo" ) var ( diff --git a/cdc/changestream/server_test.go b/cdc/changestream/server_test.go index 80ec494..2cacdf3 100644 --- a/cdc/changestream/server_test.go +++ b/cdc/changestream/server_test.go @@ -9,8 +9,8 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/v2/bson" + "go.mongodb.org/mongo-driver/v2/mongo" "github.com/square/etre" "github.com/square/etre/cdc" diff --git a/cdc/store.go b/cdc/store.go index ffdcf8c..d091d7f 100644 --- a/cdc/store.go +++ b/cdc/store.go @@ -11,9 +11,9 @@ import ( "sort" "time" - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/mongo" - "go.mongodb.org/mongo-driver/mongo/options" + "go.mongodb.org/mongo-driver/v2/bson" + "go.mongodb.org/mongo-driver/v2/mongo" + "go.mongodb.org/mongo-driver/v2/mongo/options" "github.com/square/etre" ) @@ -107,8 +107,9 @@ func (s *store) Read(f Filter) ([]etre.CDCEvent, error) { // etre.CDC to match so, below, cursor.All() doesn't have to realloc the // slice. For small fetches, this is overkill, but it makes large fetchs // (>100k events) very quick and efficient. - opts := options.Count().SetMaxTime(5 * time.Second) - count, err := s.coll.CountDocuments(context.TODO(), q, opts) + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + count, err := s.coll.CountDocuments(ctx, q, options.Count()) if err != nil { return nil, err } diff --git a/cdc/store_test.go b/cdc/store_test.go index 8dd5490..bbde0a3 100644 --- a/cdc/store_test.go +++ b/cdc/store_test.go @@ -12,9 +12,9 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/mongo" - "go.mongodb.org/mongo-driver/mongo/options" + "go.mongodb.org/mongo-driver/v2/bson" + "go.mongodb.org/mongo-driver/v2/mongo" + "go.mongodb.org/mongo-driver/v2/mongo/options" "github.com/square/etre" "github.com/square/etre/cdc" @@ -47,7 +47,7 @@ func setup(t *testing.T, fallbackFile string, wrp cdc.RetryPolicy) cdc.Store { // First time, create unique index on "x" if coll == nil { iv := cdcColl.Indexes() - if _, err := iv.DropAll(context.TODO()); err != nil { + if err := iv.DropAll(context.TODO()); err != nil { t.Fatal(err) } idx := mongo.IndexModel{ diff --git a/db/db.go b/db/db.go index 50d8c88..e41f9c4 100644 --- a/db/db.go +++ b/db/db.go @@ -3,15 +3,14 @@ package db import ( - "context" "crypto/tls" "crypto/x509" - "io/ioutil" "log" + "os" "time" - "go.mongodb.org/mongo-driver/mongo" - "go.mongodb.org/mongo-driver/mongo/options" + "go.mongodb.org/mongo-driver/v2/mongo" + "go.mongodb.org/mongo-driver/v2/mongo/options" "github.com/square/etre/config" ) @@ -68,24 +67,7 @@ func (d Default) Connect(cfg config.DatasourceConfig) (*mongo.Client, error) { log.Printf("WARNING: No database username for %s specified in config. Authentication will fail unless MongoDB access control is disabled.", cfg.URL) } - client, err := mongo.NewClient(opts) - if err != nil { - return nil, err - } - - // mongo.Connect() does not actually connect: - // The Client.Connect method starts background goroutines to monitor the - // state of the deployment and does not do any I/O in the main goroutine to - // prevent the main goroutine from blocking. Therefore, it will not error if - // the deployment is down. - // https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo?tab=doc#Connect - // The caller must call client.Ping() to actually connect. Consequently, - // we don't need a context here. As long as there's not a bug in the mongo - // driver, this won't block. - if err := client.Connect(context.Background()); err != nil { - return nil, err - } - return client, nil + return mongo.Connect(opts) } func loadTLS(cfg config.DatasourceConfig) (*tls.Config, error) { @@ -95,7 +77,7 @@ func loadTLS(cfg config.DatasourceConfig) (*tls.Config, error) { // Root CA if cfg.TLSCA != "" { - caCert, err := ioutil.ReadFile(cfg.TLSCA) + caCert, err := os.ReadFile(cfg.TLSCA) if err != nil { return nil, err } diff --git a/entity/entity.go b/entity/entity.go index c7960b1..b551eff 100644 --- a/entity/entity.go +++ b/entity/entity.go @@ -8,9 +8,8 @@ import ( "github.com/square/etre" "github.com/square/etre/query" - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/bson/primitive" - "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/v2/bson" + "go.mongodb.org/mongo-driver/v2/mongo" ) type DbError struct { @@ -66,16 +65,16 @@ func Filter(q query.Query) bson.M { if p.Label == etre.META_LABEL_ID { switch p.Value.(type) { case string: - id, _ := primitive.ObjectIDFromHex(p.Value.(string)) + id, _ := bson.ObjectIDFromHex(p.Value.(string)) filter[p.Label] = bson.M{operatorMap[p.Operator]: id} case []string: vals := p.Value.([]string) - oids := make([]primitive.ObjectID, len(vals)) + oids := make([]bson.ObjectID, len(vals)) for i, v := range vals { - oids[i], _ = primitive.ObjectIDFromHex(v) + oids[i], _ = bson.ObjectIDFromHex(v) } filter[p.Label] = bson.M{operatorMap[p.Operator]: oids} - case primitive.ObjectID: + case bson.ObjectID: filter[p.Label] = bson.M{operatorMap[p.Operator]: p.Value} default: panic(fmt.Sprintf("invalid _id value type: %T", p.Value)) diff --git a/entity/store.go b/entity/store.go index 4bead76..cd11d22 100644 --- a/entity/store.go +++ b/entity/store.go @@ -6,10 +6,9 @@ import ( "context" "time" - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/bson/primitive" - "go.mongodb.org/mongo-driver/mongo" - "go.mongodb.org/mongo-driver/mongo/options" + "go.mongodb.org/mongo-driver/v2/bson" + "go.mongodb.org/mongo-driver/v2/mongo" + "go.mongodb.org/mongo-driver/v2/mongo/options" "github.com/square/etre" "github.com/square/etre/cdc" @@ -38,7 +37,7 @@ type store struct { } // NewStore creates a Store. -func NewStore(entities map[string]*mongo.Collection, cdcStore cdc.Store) store { +func NewStore(entities map[string]*mongo.Collection, cdcStore cdc.Store) Store { return store{ coll: entities, cdcs: cdcStore, @@ -64,8 +63,12 @@ func (s store) ReadEntities(entityType string, q query.Query, f etre.QueryFilter // "es -u node.metacluster zone=pd" returns a list of unique metacluster names. // This is 10x faster than "es node.metacluster zone=pd | sort -u". if len(f.ReturnLabels) == 1 && f.Distinct { - values, err := c.Distinct(s.ctx, f.ReturnLabels[0], Filter(q)) - if err != nil { + res := c.Distinct(s.ctx, f.ReturnLabels[0], Filter(q)) + if err := res.Err(); err != nil { + return nil, s.dbError(err, "db-read-distinct") + } + var values []string + if err := res.Decode(&values); err != nil { return nil, s.dbError(err, "db-read-distinct") } entities := make([]etre.Entity, len(values)) @@ -126,7 +129,7 @@ func (s store) CreateEntities(wo WriteOp, entities []etre.Entity) ([]string, err newIds := make([]string, 0, len(entities)) for i := range entities { - entities[i]["_id"] = primitive.NewObjectID() + entities[i]["_id"] = bson.NewObjectID() entities[i]["_type"] = wo.EntityType entities[i]["_rev"] = int64(0) @@ -134,7 +137,7 @@ func (s store) CreateEntities(wo WriteOp, entities []etre.Entity) ([]string, err if err != nil { return newIds, s.dbError(err, "db-insert") } - id := res.InsertedID.(primitive.ObjectID) + id := res.InsertedID.(bson.ObjectID) newIds = append(newIds, id.Hex()) // Create a CDC event. @@ -196,7 +199,7 @@ func (s store) UpdateEntities(wo WriteOp, q query.Query, patch etre.Entity) ([]e } opts := options.FindOneAndUpdate().SetProjection(p) - nextId := map[string]primitive.ObjectID{} + nextId := map[string]bson.ObjectID{} for cursor.Next(s.ctx) { if err := cursor.Decode(&nextId); err != nil { return diffs, s.dbError(err, "db-cursor-decode") @@ -223,7 +226,7 @@ func (s store) UpdateEntities(wo WriteOp, q query.Query, patch etre.Entity) ([]e cp := cdcPartial{ op: "u", - id: orig["_id"].(primitive.ObjectID), + id: orig["_id"].(bson.ObjectID), rev: orig.Rev() + 1, old: &old, new: &patch, @@ -267,7 +270,7 @@ func (s store) DeleteEntities(wo WriteOp, q query.Query) ([]etre.Entity, error) deleted = append(deleted, old) ce := cdcPartial{ op: "d", - id: old["_id"].(primitive.ObjectID), + id: old["_id"].(bson.ObjectID), old: &old, new: nil, rev: old.Rev() + 1, @@ -287,7 +290,7 @@ func (s store) DeleteLabel(wo WriteOp, label string) (etre.Entity, error) { panic("invalid entity type passed to DeleteLabel: " + wo.EntityType) } - id, _ := primitive.ObjectIDFromHex(wo.EntityId) + id, _ := bson.ObjectIDFromHex(wo.EntityId) filter := bson.M{"_id": id} update := bson.M{ "$unset": bson.M{label: ""}, // removes label, Mongo expects "" (see $unset docs) @@ -323,7 +326,7 @@ func (s store) DeleteLabel(wo WriteOp, label string) (etre.Entity, error) { cp := cdcPartial{ op: "u", - id: old["_id"].(primitive.ObjectID), + id: old["_id"].(bson.ObjectID), new: &new, old: &old, rev: old.Rev() + 1, @@ -356,7 +359,7 @@ func (s store) dbError(err error, errType string) error { // which makes a complete CDCEvent from the partial and a WriteOp. type cdcPartial struct { op string - id primitive.ObjectID + id bson.ObjectID old *etre.Entity new *etre.Entity rev int64 diff --git a/entity/store_test.go b/entity/store_test.go index b2709c6..84cfbed 100644 --- a/entity/store_test.go +++ b/entity/store_test.go @@ -8,10 +8,9 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/bson/primitive" - "go.mongodb.org/mongo-driver/mongo" - "go.mongodb.org/mongo-driver/mongo/options" + "go.mongodb.org/mongo-driver/v2/bson" + "go.mongodb.org/mongo-driver/v2/mongo" + "go.mongodb.org/mongo-driver/v2/mongo/options" "github.com/square/etre" "github.com/square/etre/entity" @@ -41,15 +40,21 @@ func setup(t *testing.T, cdcm *mock.CDCStore) entity.Store { client, coll, err = test.DbCollections(entityTypes) require.NoError(t, err) } - // Reset the collection: delete all entities and insert the standard test entities nodesColl := coll[entityType] _, err := nodesColl.DeleteMany(context.TODO(), bson.D{{}}) require.NoError(t, err) + c, err := nodesColl.Find(context.TODO(), bson.D{{}}) // ensure the collection exists + require.NoError(t, err) + var all []etre.Entity + err = c.All(context.TODO(), &all) // load any existing test data + require.NoError(t, err) + require.Len(t, all, 0) + // Create unique index on "x" iv := nodesColl.Indexes() - if _, err := iv.DropAll(context.TODO()); err != nil { + if err := iv.DropAll(context.TODO()); err != nil { // Don't error; Mongo returns an error if the namespace doesn't exist, // which is what we want, so it's not an error in this context t.Log(err) @@ -70,7 +75,7 @@ func setup(t *testing.T, cdcm *mock.CDCStore) entity.Store { require.NoError(t, err) require.Len(t, res.InsertedIDs, len(testNodes), "mongo-driver returned %d doc ids, expected %d", len(res.InsertedIDs), len(testNodes)) for i, id := range res.InsertedIDs { - testNodes[i]["_id"] = id.(primitive.ObjectID) + testNodes[i]["_id"] = id.(bson.ObjectID) } return entity.NewStore(coll, cdcm) @@ -240,9 +245,9 @@ func TestCreateEntitiesMultiple(t *testing.T) { assert.Len(t, ids, len(testData)) // Verify that the last CDC event we create is as expected. - id1, _ := primitive.ObjectIDFromHex(ids[0]) - id2, _ := primitive.ObjectIDFromHex(ids[1]) - id3, _ := primitive.ObjectIDFromHex(ids[2]) + id1, _ := bson.ObjectIDFromHex(ids[0]) + id2, _ := bson.ObjectIDFromHex(ids[1]) + id3, _ := bson.ObjectIDFromHex(ids[2]) expectEvents := []etre.CDCEvent{ { Id: gotEvents[0].Id, // non-deterministic @@ -311,7 +316,7 @@ func TestCreateEntitiesMultiplePartialSuccess(t *testing.T) { assert.Len(t, ids, 1) // Only x=5 written/inserted, so only a CDC event for it - id1, _ := primitive.ObjectIDFromHex(ids[0]) + id1, _ := bson.ObjectIDFromHex(ids[0]) expectEvents := []etre.CDCEvent{ { Id: gotEvents[0].Id, // non-deterministic @@ -409,9 +414,9 @@ func TestUpdateEntities(t *testing.T) { gotEvents[i].Id = "" gotEvents[i].Ts = 0 } - id1, _ := testNodes[0]["_id"].(primitive.ObjectID) - id2, _ := testNodes[1]["_id"].(primitive.ObjectID) - id3, _ := testNodes[2]["_id"].(primitive.ObjectID) + id1, _ := testNodes[0]["_id"].(bson.ObjectID) + id2, _ := testNodes[1]["_id"].(bson.ObjectID) + id3, _ := testNodes[2]["_id"].(bson.ObjectID) expectEvent := []etre.CDCEvent{ { EntityId: id1.Hex(), @@ -470,7 +475,7 @@ func TestUpdateEntitiesById(t *testing.T) { // ---------------------------------------------------------------------- // This matches first test node - q, _ := query.Translate("_id=" + testNodes[0]["_id"].(primitive.ObjectID).Hex()) + q, _ := query.Translate("_id=" + testNodes[0]["_id"].(bson.ObjectID).Hex()) patch := etre.Entity{"y": "y"} // y=a -> y=y wo1 := entity.WriteOp{ EntityType: entityType, @@ -528,9 +533,9 @@ func TestUpdateEntitiesById(t *testing.T) { gotEvents[i].Id = "" gotEvents[i].Ts = 0 } - id1, _ := testNodes[0]["_id"].(primitive.ObjectID) - id2, _ := testNodes[1]["_id"].(primitive.ObjectID) - id3, _ := testNodes[2]["_id"].(primitive.ObjectID) + id1, _ := testNodes[0]["_id"].(bson.ObjectID) + id2, _ := testNodes[1]["_id"].(bson.ObjectID) + id3, _ := testNodes[2]["_id"].(bson.ObjectID) expectEvent := []etre.CDCEvent{ { EntityId: id1.Hex(), @@ -635,9 +640,9 @@ func TestDeleteEntities(t *testing.T) { gotEvents[i].Id = "" gotEvents[i].Ts = 0 } - id1, _ := testNodes[0]["_id"].(primitive.ObjectID) - id2, _ := testNodes[1]["_id"].(primitive.ObjectID) - id3, _ := testNodes[2]["_id"].(primitive.ObjectID) + id1, _ := testNodes[0]["_id"].(bson.ObjectID) + id2, _ := testNodes[1]["_id"].(bson.ObjectID) + id3, _ := testNodes[2]["_id"].(bson.ObjectID) expectEvent := []etre.CDCEvent{ { EntityId: id1.Hex(), @@ -683,7 +688,7 @@ func TestDeleteLabel(t *testing.T) { wo := entity.WriteOp{ EntityType: entityType, - EntityId: testNodes[0]["_id"].(primitive.ObjectID).Hex(), + EntityId: testNodes[0]["_id"].(bson.ObjectID).Hex(), Caller: username, } gotOld, err := store.DeleteLabel(wo, "foo") @@ -715,7 +720,7 @@ func TestDeleteLabel(t *testing.T) { gotEvents[i].Id = "" gotEvents[i].Ts = 0 } - id1, _ := testNodes[0]["_id"].(primitive.ObjectID) + id1, _ := testNodes[0]["_id"].(bson.ObjectID) expectedEventNew := etre.Entity{ "_id": testNodes[0]["_id"], "_type": testNodes[0]["_type"], diff --git a/entity/v09_test.go b/entity/v09_test.go index ce79876..0ca3231 100644 --- a/entity/v09_test.go +++ b/entity/v09_test.go @@ -8,10 +8,9 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/bson/primitive" - "go.mongodb.org/mongo-driver/mongo" - "go.mongodb.org/mongo-driver/mongo/options" + "go.mongodb.org/mongo-driver/v2/bson" + "go.mongodb.org/mongo-driver/v2/mongo" + "go.mongodb.org/mongo-driver/v2/mongo/options" "github.com/square/etre" "github.com/square/etre/entity" @@ -39,7 +38,7 @@ func setupV09(t *testing.T, cdcm *mock.CDCStore) entity.Store { // First time, create unique index on "x" if coll == nil { iv := nodesColl.Indexes() - _, err = iv.DropAll(context.TODO()) + err = iv.DropAll(context.TODO()) require.NoError(t, err) idx := mongo.IndexModel{ @@ -64,8 +63,8 @@ func setupV09(t *testing.T, cdcm *mock.CDCStore) entity.Store { require.NoError(t, err) assert.Len(t, res.InsertedIDs, len(v09testNodes)) for i, id := range res.InsertedIDs { - v09testNodes[i]["_id"] = id.(primitive.ObjectID) - v09testNodes_int32[i]["_id"] = id.(primitive.ObjectID) + v09testNodes[i]["_id"] = id.(bson.ObjectID) + v09testNodes_int32[i]["_id"] = id.(bson.ObjectID) } return entity.NewStore(coll, cdcm) @@ -93,9 +92,9 @@ func TestV09CreateEntitiesMultiple(t *testing.T) { assert.Len(t, ids, len(testData)) // Verify that the last CDC event we create is as expected. - id1, _ := primitive.ObjectIDFromHex(ids[0]) - id2, _ := primitive.ObjectIDFromHex(ids[1]) - id3, _ := primitive.ObjectIDFromHex(ids[2]) + id1, _ := bson.ObjectIDFromHex(ids[0]) + id2, _ := bson.ObjectIDFromHex(ids[1]) + id3, _ := bson.ObjectIDFromHex(ids[2]) expectEvents := []etre.CDCEvent{ { Id: gotEvents[0].Id, // non-deterministic @@ -175,7 +174,7 @@ func TestV09UpdateEntities(t *testing.T) { gotEvents[i].Id = "" gotEvents[i].Ts = 0 } - id1, _ := v09testNodes[0]["_id"].(primitive.ObjectID) + id1, _ := v09testNodes[0]["_id"].(bson.ObjectID) expectEvent := []etre.CDCEvent{ { EntityId: id1.Hex(), @@ -223,9 +222,9 @@ func TestV09DeleteEntities(t *testing.T) { gotEvents[i].Id = "" gotEvents[i].Ts = 0 } - id1, _ := v09testNodes[0]["_id"].(primitive.ObjectID) - id2, _ := v09testNodes[1]["_id"].(primitive.ObjectID) - id3, _ := v09testNodes[2]["_id"].(primitive.ObjectID) + id1, _ := v09testNodes[0]["_id"].(bson.ObjectID) + id2, _ := v09testNodes[1]["_id"].(bson.ObjectID) + id3, _ := v09testNodes[2]["_id"].(bson.ObjectID) expectEvent := []etre.CDCEvent{ { EntityId: id1.Hex(), @@ -267,7 +266,7 @@ func TestV09DeleteLabel(t *testing.T) { wo := entity.WriteOp{ EntityType: entityType, - EntityId: v09testNodes[0]["_id"].(primitive.ObjectID).Hex(), + EntityId: v09testNodes[0]["_id"].(bson.ObjectID).Hex(), Caller: username, } gotOld, err := store.DeleteLabel(wo, "y") @@ -299,7 +298,7 @@ func TestV09DeleteLabel(t *testing.T) { gotEvents[i].Id = "" gotEvents[i].Ts = 0 } - id1, _ := v09testNodes[0]["_id"].(primitive.ObjectID) + id1, _ := v09testNodes[0]["_id"].(bson.ObjectID) expectedEventNew := etre.Entity{ "_id": v09testNodes[0]["_id"], "_type": v09testNodes[0]["_type"], diff --git a/go.mod b/go.mod index 0feed6b..0b7af25 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,7 @@ require ( github.com/stretchr/testify v1.9.0 github.com/swaggo/http-swagger v1.3.4 github.com/swaggo/swag v1.16.4 - go.mongodb.org/mongo-driver v1.17.1 + go.mongodb.org/mongo-driver/v2 v2.1.0 gopkg.in/yaml.v2 v2.4.0 ) @@ -27,9 +27,8 @@ require ( github.com/go-openapi/swag v0.23.0 // indirect github.com/golang/snappy v0.0.4 // indirect github.com/josharian/intern v1.0.0 // indirect - github.com/klauspost/compress v1.13.6 // indirect + github.com/klauspost/compress v1.16.7 // indirect github.com/mailru/easyjson v0.7.7 // indirect - github.com/montanaflynn/stats v0.7.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/swaggo/files v1.0.1 // indirect github.com/xdg-go/pbkdf2 v1.0.0 // indirect diff --git a/go.sum b/go.sum index 04efaae..b57473f 100644 --- a/go.sum +++ b/go.sum @@ -28,16 +28,14 @@ github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aN github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= -github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc= -github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= +github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I= +github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= -github.com/montanaflynn/stats v0.7.1 h1:etflOAAHORrCC44V+aR6Ftzort912ZU+YLiSTuV8eaE= -github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= @@ -60,8 +58,8 @@ github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gi github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 h1:ilQV1hzziu+LLM3zUTJ0trRztfwgjqKnBWNtSRkbmwM= github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78/go.mod h1:aL8wCCfTfSfmXjznFBSZNN13rSJjlIOI1fUNAtF7rmI= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= -go.mongodb.org/mongo-driver v1.17.1 h1:Wic5cJIwJgSpBhe3lx3+/RybR5PiYRMpVFgO7cOHyIM= -go.mongodb.org/mongo-driver v1.17.1/go.mod h1:wwWm/+BuOddhcq3n68LKRmgk2wXzmF6s0SFOa0GINL4= +go.mongodb.org/mongo-driver/v2 v2.1.0 h1:/ELnVNjmfUKDsoBisXxuJL0noR9CfeUIrP7Yt3R+egg= +go.mongodb.org/mongo-driver/v2 v2.1.0/go.mod h1:AWiLRShSrk5RHQS3AEn3RL19rqOzVq49MCpWQ3x/huI= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.35.0 h1:b15kiHdrGCHrP6LvwaQ3c03kgNhhiMgvlhxHQhmg2Xs= diff --git a/server/server.go b/server/server.go index 5052d99..caa3515 100644 --- a/server/server.go +++ b/server/server.go @@ -8,7 +8,7 @@ import ( "log" "time" - "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/v2/mongo" "github.com/square/etre" "github.com/square/etre/api" diff --git a/server/server_test.go b/server/server_test.go index 8d8a413..468bfdf 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -6,7 +6,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/v2/mongo" "github.com/square/etre/app" "github.com/square/etre/auth" diff --git a/test/db.go b/test/db.go index c42f00f..f5cc523 100644 --- a/test/db.go +++ b/test/db.go @@ -3,12 +3,10 @@ package test import ( - "context" "log" - "time" - "go.mongodb.org/mongo-driver/mongo" - "go.mongodb.org/mongo-driver/mongo/options" + "go.mongodb.org/mongo-driver/v2/mongo" + "go.mongodb.org/mongo-driver/v2/mongo/options" ) var ( @@ -20,15 +18,10 @@ var ( func DbCollections(entityTypes []string) (*mongo.Client, map[string]*mongo.Collection, error) { url := "mongodb://" + url log.Printf("Connecting to %s", url) - client, err := mongo.NewClient(options.Client().ApplyURI(url)) + client, err := mongo.Connect(options.Client().ApplyURI(url)) if err != nil { return nil, nil, err } - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() - if err := client.Connect(ctx); err != nil { - return nil, nil, err - } coll := map[string]*mongo.Collection{} for _, t := range entityTypes { coll[t] = client.Database(database).Collection(t) diff --git a/test/mock/changestream.go b/test/mock/changestream.go index a9aea4c..66193e7 100644 --- a/test/mock/changestream.go +++ b/test/mock/changestream.go @@ -3,8 +3,7 @@ package mock import ( - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/bson/primitive" + "go.mongodb.org/mongo-driver/v2/bson" "github.com/square/etre" "github.com/square/etre/cdc/changestream" @@ -106,10 +105,10 @@ func (s Stream) Error() error { // -------------------------------------------------------------------------- var RawInsertEvents = []bson.M{ - primitive.M{ - "_id": primitive.M{"_data": primitive.Binary{Data: []uint8{0x82, 0x5e, 0x8f, 0x5c, 0xf7, 0x0, 0x0, 0x0, 0x26, 0x46, 0x3c, 0x5f, 0x69, 0x64, 0x0, 0x3c, 0x61, 0x62, 0x63, 0x0, 0x0, 0x5a, 0x10, 0x4, 0x4d, 0x5e, 0xa3, 0x2c, 0x5c, 0x27, 0x4c, 0xf8, 0xb1, 0xad, 0x80, 0x30, 0x3a, 0x44, 0xbc, 0x82, 0x4}}}, - "documentKey": primitive.M{"_id": "abc"}, - "fullDocument": primitive.M{ // etre.CDCEvent + bson.M{ + "_id": bson.M{"_data": bson.Binary{Data: []uint8{0x82, 0x5e, 0x8f, 0x5c, 0xf7, 0x0, 0x0, 0x0, 0x26, 0x46, 0x3c, 0x5f, 0x69, 0x64, 0x0, 0x3c, 0x61, 0x62, 0x63, 0x0, 0x0, 0x5a, 0x10, 0x4, 0x4d, 0x5e, 0xa3, 0x2c, 0x5c, 0x27, 0x4c, 0xf8, 0xb1, 0xad, 0x80, 0x30, 0x3a, 0x44, 0xbc, 0x82, 0x4}}}, + "documentKey": bson.M{"_id": "abc"}, + "fullDocument": bson.M{ // etre.CDCEvent "_id": "abc", "entityId": "e13", "entityType": "node", @@ -117,9 +116,9 @@ var RawInsertEvents = []bson.M{ "rev": int64(7), "ts": 54, "user": "mike", - "new": primitive.M{"_id": "e13", "_rev": int64(7), "_type": "node", "foo": "bar"}, + "new": bson.M{"_id": "e13", "_rev": int64(7), "_type": "node", "foo": "bar"}, }, - "ns": primitive.M{"coll": "cdc", "db": "etre_test"}, + "ns": bson.M{"coll": "cdc", "db": "etre_test"}, "operationType": "insert", }, } diff --git a/test/mock/db.go b/test/mock/db.go index ea22de9..4fb9525 100644 --- a/test/mock/db.go +++ b/test/mock/db.go @@ -1,7 +1,7 @@ package mock import ( - "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/v2/mongo" "github.com/square/etre/config" "github.com/square/etre/db"