This repository has been archived by the owner on Dec 5, 2023. It is now read-only.
generated from EOEPCA/um-service-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
management_tools.py
71 lines (60 loc) · 2.36 KB
/
management_tools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/python3
import argparse
import sys
from policy_storage import Policy_Storage
import json
custom_mongo = Policy_Storage('mongodb')
def list_policies(user,resource,policy):
result = []
if policy is not None:
result = custom_mongo.get_policy_from_id(policy)
else:
result=custom_mongo.get_all_policies()
if user is not None:
result = list(filter(lambda x: x["ownership_id"] == user,result))
if resource is not None:
result = list(filter(lambda x: x["config"]["resource_id"] == resource,result))
if not user and not resource:
result = list(result)
return result
def remove_policies(user,resource,policy,all):
if policy is not None:
return custom_mongo.delete_policy(policy)
if all:
return custom_mongo.remove_policy_by_query({})
elif user or resource:
query = {}
if user is not None:
query.update({"ownership_id": user})
if resource is not None:
query.update({"config.resource_id": resource})
return custom_mongo.remove_policy_by_query(query)
else:
return "No action taken (missing --all flag?)"
parser = argparse.ArgumentParser(description='Operational management of policies.')
parser.add_argument('action', metavar='action', type=str,
help='Operation to perform: list/remove')
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',
'--policy',
help='Filter action by Policy ID')
parser.add_argument('-a',
'--all',
action='store_true',
help='Apply action to all policies.')
args = vars(parser.parse_args())
if args["action"] == "list":
result = list_policies(args['user'],args['resource'],args['policy'])
elif args["action"] == "remove":
if args["policy"] is not None:
args["all"] = False
result = remove_policies(args['user'],args['resource'],args['policy'],args['all'])
else:
print("Allowed actions are 'remove' or 'list'")
sys.exit(-1)
print(result)