• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

atlanticwave-sdx / sdx-controller / 10077941521

24 Jul 2024 01:49PM UTC coverage: 52.477% (-0.009%) from 52.486%
10077941521

push

github

web-flow
Merge pull request #300 from atlanticwave-sdx/fix/issue_mongodb_connstr

Use MONGODB_CONNSTRING when MONGO_HOST is not set

303 of 555 branches covered (54.59%)

Branch coverage included in aggregate %.

8 of 12 new or added lines in 1 file covered. (66.67%)

767 of 1484 relevant lines covered (51.68%)

2.07 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.39
/sdx_controller/utils/db_utils.py
1
import json
4✔
2
import logging
4✔
3
import os
4✔
4
from urllib.parse import urlparse
4✔
5

6
import pymongo
4✔
7

8
COLLECTION_NAMES = ["topologies", "connections", "breakdowns", "domains", "links"]
4✔
9

10
pymongo_logger = logging.getLogger("pymongo")
4✔
11
pymongo_logger.setLevel(logging.INFO)
4✔
12

13

14
def obfuscate_password_in_uri(uri: str) -> str:
4✔
15
    """
16
    Replace password field in URIs with a `*`, for logging.
17
    """
18
    parts = urlparse(uri)
4✔
19
    if parts.password:
4✔
20
        return uri.replace(parts.password, "*")
4✔
21
    else:
NEW
22
        return uri
×
23

24

25
class DbUtils(object):
4✔
26
    def __init__(self):
4✔
27
        self.db_name = os.environ.get("DB_NAME")
4✔
28
        if self.db_name is None:
4✔
29
            raise Exception("DB_NAME environment variable is not set")
4✔
30

31
        # self.config_table_name = os.environ.get("DB_CONFIG_TABLE_NAME")
32
        # if self.config_table_name is None:
33
        #     raise Exception("DB_CONFIG_TABLE_NAME environ variable is not set")
34

35
        mongo_user = os.getenv("MONGO_USER") or "guest"
4✔
36
        mongo_pass = os.getenv("MONGO_PASS") or "guest"
4✔
37
        mongo_host = os.getenv("MONGO_HOST")
4✔
38
        mongo_port = os.getenv("MONGO_PORT") or 27017
4✔
39

40
        if mongo_host is None:
4✔
NEW
41
            mongo_connstring = os.getenv("MONGODB_CONNSTRING")
×
NEW
42
            if mongo_connstring is None:
×
NEW
43
                raise Exception("Neither MONGO_HOST nor MONGODB_CONNSTRING is set")
×
44
        else:
45
            mongo_connstring = (
4✔
46
                f"mongodb://{mongo_user}:{mongo_pass}@{mongo_host}:{mongo_port}/"
47
            )
48

49
        self.logger = logging.getLogger(__name__)
4!
50
        self.logger.setLevel(logging.DEBUG)
4✔
51

52
        # Log DB URI, without a password.
53
        self.logger.info(f"[DB] Using {obfuscate_password_in_uri(mongo_connstring)}")
4✔
54

55
        self.mongo_client = pymongo.MongoClient(mongo_connstring)
4✔
56

57
    def initialize_db(self):
4✔
58
        self.logger.debug(f"Trying to load {self.db_name} from DB")
4✔
59

60
        if self.db_name not in self.mongo_client.list_database_names():
4✔
61
            self.logger.debug(f"No existing {self.db_name} from DB, creating table")
4✔
62
            self.sdxdb = self.mongo_client[self.db_name]
4✔
63
            self.logger.debug(f"DB {self.db_name} initialized")
4✔
64

65
        self.sdxdb = self.mongo_client[self.db_name]
4✔
66
        # config_col = self.sdxdb[self.config_table_name]
67
        for name in COLLECTION_NAMES:
4✔
68
            if name not in self.sdxdb.list_collection_names():
4✔
69
                self.sdxdb.create_collection(name)
4✔
70

71
        self.logger.debug(f"DB {self.db_name} initialized")
4✔
72

73
    def add_key_value_pair_to_db(self, collection, key, value):
4✔
74
        key = str(key)
4✔
75
        obj = self.read_from_db(collection, key)
4✔
76
        if obj is None:
4✔
77
            return self.sdxdb[collection].insert_one({key: value})
4✔
78

79
        query = {"_id": obj["_id"]}
4✔
80
        result = self.sdxdb[collection].replace_one(query, {key: value})
4✔
81
        return result
4✔
82

83
    def read_from_db(self, collection, key):
4✔
84
        key = str(key)
4✔
85
        return self.sdxdb[collection].find_one(
4✔
86
            {key: {"$exists": 1}, "deleted": {"$ne": True}}
87
        )
88

89
    def get_all_entries_in_collection(self, collection):
4✔
90
        db_collection = self.sdxdb[collection]
4✔
91
        # MongoDB has an ObjectId for each item, so need to exclude the ObjectIds
92
        all_entries = db_collection.find({"deleted": {"$ne": True}}, {"_id": 0})
4✔
93
        return all_entries
4✔
94

95
    def mark_deleted(self, collection, key):
4✔
96
        db_collection = self.sdxdb[collection]
4✔
97
        key = str(key)
4✔
98
        item_to_delete = self.read_from_db(collection, key)
4✔
99
        if item_to_delete is None:
4✔
100
            return False
×
101
        filter = {"_id": item_to_delete["_id"]}
4✔
102
        update = {"$set": {"deleted": True}}
4✔
103
        db_collection.update_one(filter, update)
4✔
104
        return True
4✔
105

106
    def delete_one_entry(self, collection, key):
4✔
107
        key = str(key)
×
108
        db_collection = self.sdxdb[collection]
×
109
        db_collection.delete_one({key: {"$exists": True}})
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc