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

atlanticwave-sdx / sdx-controller / 9500593558

13 Jun 2024 01:29PM UTC coverage: 52.322% (+0.09%) from 52.235%
9500593558

push

github

web-flow
Merge pull request #288 from atlanticwave-sdx/remove-connection

Delete connection

300 of 547 branches covered (54.84%)

Branch coverage included in aggregate %.

19 of 23 new or added lines in 3 files covered. (82.61%)

1 existing line in 1 file now uncovered.

748 of 1456 relevant lines covered (51.37%)

2.05 hits per line

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

93.94
/sdx_controller/utils/db_utils.py
1
import json
4✔
2
import logging
4✔
3
import os
4✔
4

5
import pymongo
4✔
6

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

9

10
class DbUtils(object):
4✔
11
    def __init__(self):
4✔
12
        self.db_name = os.environ.get("DB_NAME")
4✔
13
        if self.db_name is None:
4✔
14
            raise Exception("DB_NAME environment variable is not set")
4✔
15

16
        # self.config_table_name = os.environ.get("DB_CONFIG_TABLE_NAME")
17
        # if self.config_table_name is None:
18
        #     raise Exception("DB_CONFIG_TABLE_NAME environ variable is not set")
19

20
        mongo_connstring = os.environ.get("MONGODB_CONNSTRING")
4✔
21
        if mongo_connstring is None:
4✔
22
            raise Exception("MONGODB_CONNSTRING environment variable is not set")
×
23
        self.mongo_client = pymongo.MongoClient(mongo_connstring)
4✔
24

25
        self.logger = logging.getLogger(__name__)
4✔
26
        self.logger.setLevel(logging.DEBUG)
4✔
27

28
    def initialize_db(self):
4✔
29
        self.logger.debug(f"Trying to load {self.db_name} from DB")
4✔
30

31
        if self.db_name not in self.mongo_client.list_database_names():
4✔
32
            self.logger.debug(f"No existing {self.db_name} from DB, creating table")
4✔
33
            self.sdxdb = self.mongo_client[self.db_name]
4✔
34
            self.logger.debug(f"DB {self.db_name} initialized")
4✔
35

36
        self.sdxdb = self.mongo_client[self.db_name]
4✔
37
        # config_col = self.sdxdb[self.config_table_name]
38
        for name in COLLECTION_NAMES:
4✔
39
            if name not in self.sdxdb.list_collection_names():
4✔
40
                self.sdxdb.create_collection(name)
4✔
41

42
        self.logger.debug(f"DB {self.db_name} initialized")
4✔
43

44
    def add_key_value_pair_to_db(self, collection, key, value):
4✔
45
        key = str(key)
4✔
46
        obj = self.read_from_db(collection, key)
4✔
47
        if obj is None:
4✔
48
            return self.sdxdb[collection].insert_one({key: value})
4✔
49

50
        query = {"_id": obj["_id"]}
4✔
51
        result = self.sdxdb[collection].replace_one(query, {key: value})
4✔
52
        return result
4✔
53

54
    def read_from_db(self, collection, key):
4✔
55
        key = str(key)
4✔
56
        return self.sdxdb[collection].find_one(
4✔
57
            {key: {"$exists": 1}, "deleted": {"$ne": True}}
58
        )
59

60
    def get_all_entries_in_collection(self, collection):
4✔
61
        db_collection = self.sdxdb[collection]
4✔
62
        # MongoDB has an ObjectId for each item, so need to exclude the ObjectIds
63
        all_entries = db_collection.find({"deleted": {"$ne": True}}, {"_id": 0})
4✔
64
        return all_entries
4✔
65

66
    def mark_deleted(self, collection, key):
4✔
67
        db_collection = self.sdxdb[collection]
4✔
68
        key = str(key)
4✔
69
        item_to_delete = self.read_from_db(collection, key)
4✔
70
        if item_to_delete is None:
4✔
UNCOV
71
            return False
×
72
        filter = {"_id": item_to_delete["_id"]}
4✔
73
        update = {"$set": {"deleted": True}}
4✔
74
        db_collection.update_one(filter, update)
4✔
75
        return True
4✔
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