Skip to content

Commit da5c3bf

Browse files
authored
Merge pull request #1668 from grycap/devel
Save data in json format in mongo
2 parents ca3a348 + a83cc1e commit da5c3bf

File tree

6 files changed

+17
-16
lines changed

6 files changed

+17
-16
lines changed

IM/CloudInfo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,11 @@ def get_cloud_list(auth_data):
140140
return res
141141

142142
def serialize(self):
143-
return json.dumps(self.__dict__)
143+
return self.__dict__
144144

145145
@staticmethod
146146
def deserialize(str_data):
147-
dic = json.loads(str_data)
147+
dic = str_data if isinstance(str_data, dict) else json.loads(str_data)
148148
nwecloud = CloudInfo()
149149
nwecloud.__dict__.update(dic)
150150
return nwecloud

IM/InfrastructureInfo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,13 @@ def serialize(self):
143143
odict['radl'] = str(odict['radl'])
144144
if odict['extra_info'] and "TOSCA" in odict['extra_info']:
145145
odict['extra_info'] = {'TOSCA': odict['extra_info']['TOSCA'].serialize()}
146-
return json.dumps(odict)
146+
return odict
147147

148148
@staticmethod
149149
def deserialize(str_data):
150150
newinf = InfrastructureInfo()
151151
newinf.creation_date = None
152-
dic = json.loads(str_data)
152+
dic = str_data if isinstance(str_data, dict) else json.loads(str_data)
153153
vm_list = dic['vm_list']
154154
vm_master_id = dic['vm_master']
155155
dic['vm_master'] = None

IM/InfrastructureList.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import time
2020
import logging
2121
import threading
22+
import json
2223

2324
from IM.db import DataBase
2425
from IM.config import Config
@@ -242,7 +243,8 @@ def _save_data_to_db(db_url, inf_list, inf_id=None):
242243
else:
243244
res = db.execute("replace into inf_list (id, deleted, data, date, auth)"
244245
" values (%s, %s, %s, now(), %s)",
245-
(inf.id, int(inf.deleted), data, inf.auth.serialize()))
246+
(inf.id, int(inf.deleted), json.dumps(data),
247+
json.dumps(inf.auth.serialize())))
246248

247249
db.close()
248250
return res
@@ -266,18 +268,16 @@ def _gen_where_from_auth(auth):
266268

267269
@staticmethod
268270
def _gen_filter_from_auth(auth):
269-
like = ""
271+
usernames = []
270272
if auth:
271273
for elem in auth.getAuthInfo('InfrastructureManager'):
272274
if elem.get("admin"):
273275
return {}
274276
if elem.get("username"):
275-
if like:
276-
like += "|"
277-
like += '"%s"' % elem.get("username")
277+
usernames.append(elem.get("username"))
278278

279-
if like:
280-
return {"auth": {"$regex": like}}
279+
if usernames:
280+
return {"auth": {"$elemMatch": {"username": {"$in": usernames}}}}
281281
else:
282282
return {}
283283

IM/Stats.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Stats():
3434

3535
@staticmethod
3636
def _get_data(str_data, init_date=None, end_date=None):
37-
dic = json.loads(str_data)
37+
dic = str_data if isinstance(str_data, dict) else json.loads(str_data)
3838
resp = {'creation_date': ''}
3939
if 'creation_date' in dic and dic['creation_date']:
4040
creation_date = datetime.datetime.fromtimestamp(float(dic['creation_date']))

IM/VirtualMachine.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,11 @@ def serialize(self):
122122
odict['requested_radl'] = str(odict['requested_radl'])
123123
if odict['cloud']:
124124
odict['cloud'] = odict['cloud'].serialize()
125-
return json.dumps(odict)
125+
return odict
126126

127127
@staticmethod
128128
def deserialize(str_data):
129-
dic = json.loads(str_data)
129+
dic = str_data if isinstance(str_data, dict) else json.loads(str_data)
130130
if dic['cloud']:
131131
dic['cloud'] = IM.CloudInfo.CloudInfo.deserialize(dic['cloud'])
132132
if dic['info']:

IM/auth.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,12 @@ def read_auth_data(filename):
223223
return res
224224

225225
def serialize(self):
226-
return json.dumps(self.auth_list, sort_keys=True)
226+
return self.auth_list
227227

228228
@staticmethod
229229
def deserialize(str_data):
230-
return Authentication(json.loads(str_data))
230+
data = str_data if isinstance(str_data, list) else json.loads(str_data)
231+
return Authentication(data)
231232

232233
def delAuthInfo(self, auth_type, host=None):
233234
"""

0 commit comments

Comments
 (0)