4
4
5
5
logger = logging .getLogger (__name__ )
6
6
7
+
7
8
class MibsRepository :
8
9
def __init__ (self , mongo_config ):
9
10
"""
10
11
Create a collection in mongodb to store mib files
11
12
"""
12
- self ._client = MongoClient (os .environ ['MONGO_SERVICE_SERVICE_HOST' ], int (os .environ ['MONGO_SERVICE_SERVICE_PORT' ]))
13
- self ._mibs = self ._client [mongo_config ['database' ]][mongo_config ['collection' ]]
14
-
15
-
13
+ self ._client = MongoClient (
14
+ os .environ ["MONGO_SERVICE_SERVICE_HOST" ],
15
+ int (os .environ ["MONGO_SERVICE_SERVICE_PORT" ]),
16
+ )
17
+ if os .environ .get ("MONGO_USER" ):
18
+ self ._client .admin .authenticate (
19
+ os .environ ["MONGO_USER" ],
20
+ os .environ ["MONGO_PASS" ],
21
+ mechanism = "SCRAM-SHA-1" ,
22
+ )
23
+ self ._mibs = self ._client [mongo_config ["database" ]][mongo_config ["collection" ]]
24
+
16
25
def upload_files (self , mib_files_dir ):
17
26
"""
18
27
Upload mib files from dir to mongo
19
28
@param mib_files_dir: the path of the mib files directory
20
29
"""
21
30
# TODO check duplicate before insert, using filename as PK
22
31
for filename in os .listdir (mib_files_dir ):
23
- file_path = mib_files_dir + "/" + filename
32
+ file_path = mib_files_dir + "/" + filename
24
33
# print(file_path)
25
- with open (file_path ,'r' ) as mib_file :
34
+ with open (file_path , "r" ) as mib_file :
26
35
# TODO add try catch, insert only if PK doesn't exit
27
36
try :
28
- self ._mibs .insert_one (dict (
29
- content = mib_file .read (),
30
- filename = filename ,
31
- _id = filename
32
- ))
37
+ self ._mibs .insert_one (
38
+ dict (content = mib_file .read (), filename = filename , _id = filename )
39
+ )
33
40
except Exception as e :
34
- logger .error (f"Error happened during insert mib files { filename } into mongo: { e } " )
35
-
36
-
41
+ logger .error (
42
+ f"Error happened during insert mib files { filename } into mongo: { e } "
43
+ )
37
44
38
45
def search_oid (self , oid ):
39
46
"""
@@ -52,27 +59,37 @@ def delete_mib(self, filename):
52
59
Delete mib files based on filename
53
60
@param filename: mib filename
54
61
"""
55
- self ._mibs .delete_many ({' filename' : {"$regex" : filename }})
62
+ self ._mibs .delete_many ({" filename" : {"$regex" : filename }})
56
63
57
64
def clear (self ):
58
65
"""
59
66
Clear the collection
60
67
"""
61
68
self ._mibs .remove ()
62
69
70
+
63
71
class OidsRepository :
64
72
def __init__ (self , mongo_config ):
65
- self ._client = MongoClient (os .environ ['MONGO_SERVICE_SERVICE_HOST' ], int (os .environ ['MONGO_SERVICE_SERVICE_PORT' ]))
66
- self ._oids = self ._client [mongo_config ['database' ]][mongo_config ['collection' ]]
73
+ self ._client = MongoClient (
74
+ os .environ ["MONGO_SERVICE_SERVICE_HOST" ],
75
+ int (os .environ ["MONGO_SERVICE_SERVICE_PORT" ]),
76
+ )
77
+ if os .environ .get ("MONGO_USER" ):
78
+ self ._client .admin .authenticate (
79
+ os .environ ["MONGO_USER" ],
80
+ os .environ ["MONGO_PASS" ],
81
+ mechanism = "SCRAM-SHA-1" ,
82
+ )
83
+ self ._oids = self ._client [mongo_config ["database" ]][mongo_config ["collection" ]]
67
84
68
85
def contains_oid (self , oid ):
69
- return self ._oids .find ({' oid' : oid }).count ()
86
+ return self ._oids .find ({" oid" : oid }).count ()
70
87
71
88
def add_oid (self , oid ):
72
- self ._oids .insert_one ({' oid' : oid })
89
+ self ._oids .insert_one ({" oid" : oid })
73
90
74
91
def delete_oid (self , oid ):
75
- self ._oids .delete_many ({' oid' : oid })
92
+ self ._oids .delete_many ({" oid" : oid })
76
93
77
94
def clear (self ):
78
- self ._oids .remove ()
95
+ self ._oids .remove ()
0 commit comments