Skip to content
This repository has been archived by the owner on Dec 5, 2023. It is now read-only.

EOEPCA-515 New import export function in management_tools #91

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions src/handlers/mongo_handler.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#!/usr/bin/env python3
import pymongo
import logging
import json
from bson.objectid import ObjectId
from bson.json_util import dumps

class Mongo_Handler:

Expand Down Expand Up @@ -190,3 +193,81 @@ def insert_rpt_in_mongo(self, rpt: str, rpt_limit_uses: int, timestamp: str):
myres = { "rpt": rpt, "rpt_limit_uses": rpt_limit_uses, "timestamp": timestamp }
x = col.insert_one(myres)
return x


def export_database(self, res_path, tkn_path):
'''
Exports the main colection to a json file located in the / of the container or the specified path
'''
col = self.db['resources']
cursor = col.find({})
count = col.count_documents({})
path = ''
if res_path:
path = res_path
else:
path = "/resources.json"
with open(str(path), 'w') as file:
file.write('[')
aux = 0
for document in cursor:
aux += 1
n = document
file.write(dumps(document))
if aux != count:
file.write(',')
file.write(']')
#Same for the token collection
col = self.db['rpts']
cursor = col.find({})
count = col.count_documents({})
tknpath = ''
if tkn_path:
tknpath = tkn_path
else:
tknpath = "/rpts.json"
with open(str(tknpath), 'w') as file:
file.write('[')
aux = 0
for document in cursor:
aux += 1
n = document
file.write(dumps(document))
if aux != count:
file.write(',')
file.write(']')

return "Exported resources to "+ str(path) + " and rpts to " + str(tknpath)


def import_database(self, res_path, tkn_path):
'''
Imports the exported data of a previous database located in the / of the container or in the specified path
'''
col = self.db['resources']
path = ''
if res_path:
path = res_path
else:
path = "/resources.json"
with open(str(path)) as f:
n = f.readlines()
for i in n:
for u in json.loads(i):
oid = u["_id"]["$oid"]
u["_id"] = ObjectId(str(oid))
col.insert_one(u)
col = self.db['rpts']
tknpath = ''
if tkn_path:
tknpath = tkn_path
else:
tknpath = "/rpts.json"
with open(str(tknpath)) as f:
n = f.readlines()
for i in n:
for u in json.loads(i):
oid = u["_id"]["$oid"]
u["_id"] = ObjectId(str(oid))
col.insert_one(u)
return "Inserted "+ str(path) + ' and '+ str(tknpath) + ' files in database'
22 changes: 21 additions & 1 deletion src/management_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,33 @@ def remove_resources(user,resource,all):
return custom_mongo.remove_resources()
return "No action taken (missing --all flag?)"

def import_database(res_path, rpt_path):
print("importing database....")
return custom_mongo.import_database(res_path, rpt_path)

def export_database(res_path, rpt_path):
print("Exporting database....")
return custom_mongo.export_database(res_path, rpt_path)


parser = argparse.ArgumentParser(description='Operational management of resources.')
parser.add_argument('action', metavar='action', type=str,
help='Operation to perform: list/remove')
help='Operation to perform: list/remove import/export')
parser.add_argument('-u',
'--user',
help='Filter action by user ID')
parser.add_argument('-r',
'--resource',
help='Filter action by resource ID')

parser.add_argument('-p',
'--path',
help='Select directory from where to save/read the resources collection')

parser.add_argument('-t',
'--token',
help='Select directory from where to save/read the token collection')

parser.add_argument('-a',
'--all',
action='store_true',
Expand All @@ -48,6 +64,10 @@ def remove_resources(user,resource,all):
if args["resource"] is not None:
args["all"] = False
result = remove_resources(args['user'],args['resource'],args['all'])
elif args["action"] == "import":
result = import_database(args['path'], args['token'])
elif args["action"] == "export":
result = export_database(args['path'], args['token'])
else:
print("Allowed actions are 'remove' or 'list'")
sys.exit(-1)
Expand Down