summaryrefslogtreecommitdiffstats
path: root/src/k8splugin/internal/db/mongo.go
diff options
context:
space:
mode:
authorBin Yang <bin.yang@windriver.com>2019-03-29 05:17:07 +0000
committerGerrit Code Review <gerrit@onap.org>2019-03-29 05:17:07 +0000
commitb930ac1c8b810662545fb7fdd776a9698e9825b1 (patch)
tree948993fffeaaa1f21cfb030b1370ce5f8e81af6b /src/k8splugin/internal/db/mongo.go
parentb2b175eae0f4e8b5b0cb9ccbeeca1e98065feeb5 (diff)
parentb6288d59577dd40d6d19841bf030c45f9ec8aa50 (diff)
Merge "Add update method to db interface"
Diffstat (limited to 'src/k8splugin/internal/db/mongo.go')
-rw-r--r--src/k8splugin/internal/db/mongo.go43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/k8splugin/internal/db/mongo.go b/src/k8splugin/internal/db/mongo.go
index 8c422380..a9e9d98a 100644
--- a/src/k8splugin/internal/db/mongo.go
+++ b/src/k8splugin/internal/db/mongo.go
@@ -164,6 +164,49 @@ func (m *MongoStore) Create(coll string, key Key, tag string, data interface{})
return nil
}
+// Update is used to update a DB entry
+func (m *MongoStore) Update(coll string, key Key, tag string, data interface{}) error {
+ if data == nil || !m.validateParams(coll, key, tag) {
+ return pkgerrors.New("No Data to update")
+ }
+
+ c := getCollection(coll, m)
+ ctx := context.Background()
+
+ //Get the masterkey document based on given key
+ filter := bson.D{{"key", key}}
+ keydata, err := decodeBytes(c.FindOne(context.Background(), filter))
+ if err != nil {
+ return pkgerrors.Errorf("Error finding master table: %s", err.Error())
+ }
+
+ //Read the tag objectID from document
+ tagoid, ok := keydata.Lookup(tag).ObjectIDOK()
+ if !ok {
+ return pkgerrors.Errorf("Error finding objectID for tag %s", tag)
+ }
+
+ //Update the document with new data
+ filter = bson.D{{"_id", tagoid}}
+
+ _, err = decodeBytes(
+ c.FindOneAndUpdate(
+ ctx,
+ filter,
+ bson.D{
+ {"$set", bson.D{
+ {tag, data},
+ }},
+ },
+ options.FindOneAndUpdate().SetReturnDocument(options.After)))
+
+ if err != nil {
+ return pkgerrors.Errorf("Error updating record: %s", err.Error())
+ }
+
+ return nil
+}
+
// Unmarshal implements an unmarshaler for bson data that
// is produced from the mongo database
func (m *MongoStore) Unmarshal(inp []byte, out interface{}) error {