Mongo api changes 75/105975/1
authorEric Multanen <eric.w.multanen@intel.com>
Wed, 15 Apr 2020 00:48:55 +0000 (17:48 -0700)
committerEric Multanen <eric.w.multanen@intel.com>
Wed, 15 Apr 2020 00:48:55 +0000 (17:48 -0700)
Add RemoveTag call to allow removal of attribute from
a mongo document.
Allow find to return and empty list.

Issue-ID: MULTICLOUD-1029
Signed-off-by: Eric Multanen <eric.w.multanen@intel.com>
Change-Id: I315efb8a4da485b697727254ccc9a5828f11a0d0

src/orchestrator/pkg/infra/db/mongo.go
src/orchestrator/pkg/infra/db/store.go

index b33d6c6..a3fdc57 100644 (file)
@@ -539,9 +539,6 @@ func (m *MongoStore) Find(coll string, key Key, tag string) ([][]byte, error) {
                }
                result = append(result, data)
        }
-       if len(result) == 0 {
-               return result, pkgerrors.Errorf("Did not find any objects with tag: %s", tag)
-       }
        return result, nil
 }
 
@@ -587,3 +584,31 @@ func (m *MongoStore) Remove(coll string, key Key) error {
        }
        return nil
 }
+
+// RemoveTag is used to remove an element from a document
+func (m *MongoStore) RemoveTag(coll string, key Key, tag string) error {
+       c := getCollection(coll, m)
+       ctx := context.Background()
+
+       filter, err := m.findFilter(key)
+       if err != nil {
+               return err
+       }
+
+       _, err = decodeBytes(
+               c.FindOneAndUpdate(
+                       ctx,
+                       filter,
+                       bson.D{
+                               {"$unset", bson.D{
+                                       {tag, ""},
+                               }},
+                       },
+                       options.FindOneAndUpdate().SetUpsert(true).SetReturnDocument(options.After)))
+
+       if err != nil {
+               return pkgerrors.Errorf("Error removing tag: %s", err.Error())
+       }
+
+       return nil
+}
index 9c6532f..a332fcd 100644 (file)
@@ -64,6 +64,9 @@ type Store interface {
 
        // Remove all the document(s) matching the key
        RemoveAll(coll string, key Key) error
+
+       // Remove the specifiec tag from the document matching the key
+       RemoveTag(coll string, key Key, tag string) error
 }
 
 // CreateDBClient creates the DB client