aboutsummaryrefslogtreecommitdiffstats
path: root/opnfv_testapi/db/api.py
blob: a51e7fc665cddf1858e44853b1320ebeed249ec8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
##############################################################################
# Copyright (c) 2019 opnfv.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################

import motor

from opnfv_testapi.common.config import CONF

DB = motor.MotorClient(CONF.mongo_url)[CONF.mongo_dbname]


def db_update(collection, query, update_req):
    return _eval_db(collection, 'update', query, update_req, check_keys=False)


def db_delete(collection, query):
    return _eval_db(collection, 'remove', query)


def db_aggregate(collection, pipelines):
    return _eval_db(collection, 'aggregate', pipelines, allowDiskUse=True)


def db_list(collection, query):
    return _eval_db(collection, 'find', query)


def db_save(collection, data):
    return _eval_db(collection, 'insert', data, check_keys=False)


def db_find_one(collection, query):
    return _eval_db(collection, 'find_one', query)


def _eval_db(collection, method, *args, **kwargs):
    exec_collection = DB.__getattr__(collection)
    return exec_collection.__getattribute__(method)(*args, **kwargs)


def _eval_db_find_one(query, table=None):
    return _eval_db(table, 'find_one', query)