Newer
Older
from config import Config
from db.db import MongoDatabse
from flask import jsonify, current_app
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
class HelperOperations:
def __init__(self):
self.db = MongoDatabse()
self.mimetype = 'application/json'
self.config = Config().get_config()
def get_invokers(self, uuid, invoker_id, page, page_size, sort_order):
current_app.logger.debug(f"Getting the invokers")
invoker_col = self.db.get_col_by_name(self.db.invoker_col)
total_invokers = invoker_col.count_documents({})
filter = {}
if uuid:
filter["uuid"]=uuid
if invoker_id:
filter["api_invoker_id"]=invoker_id
sort_direction = pymongo.DESCENDING if sort_order == "desc" else pymongo.ASCENDING
if page_size and page:
index = (page - 1) * page_size
documents = invoker_col.find(filter,{"_id":0}).sort("onboarding_date", sort_direction).skip(index).limit(page_size)
pages = (total_invokers + page_size - 1) // page_size
else:
documents = invoker_col.find(filter,{"_id":0}).sort("onboarding_date", sort_direction)
pages = 1
list_invokers= list(documents)
long = len(list_invokers)
return jsonify(message="Invokers returned successfully",
invokers=list_invokers,
total = total_invokers,
long = long,
totalPages = pages,
sortOrder = sort_order), 200
def get_providers(self, uuid, provider_id, page, page_size, sort_order):
current_app.logger.debug(f"Getting the providers")
provider_col = self.db.get_col_by_name(self.db.provider_col)
total_providers = provider_col.count_documents({})
filter = {}
if uuid:
filter["uuid"]=uuid
if provider_id:
filter["api_prov_dom_id"]=provider_id
sort_direction = pymongo.DESCENDING if sort_order == "desc" else pymongo.ASCENDING
if page_size and page:
index = (page - 1) * page_size
documents = provider_col.find(filter,{"_id":0}).sort("onboarding_date", sort_direction).skip(index).limit(page_size)
pages = (total_providers + page_size - 1) // page_size
else:
documents = provider_col.find(filter,{"_id":0}).sort("onboarding_date", sort_direction)
pages = 1
list_providers = list(documents)
long = len(list_providers)
return jsonify(message="Providers returned successfully",
providers=list_providers,
total = total_providers,
long = long,
totalPages = pages,
sortOrder = sort_order), 200
def get_services(self, service_id, apf_id, api_name, page, page_size, sort_order):
current_app.logger.debug(f"Getting the services")
service_col = self.db.get_col_by_name(self.db.services_col)
total_services = service_col.count_documents({})
filter = {}
if service_id:
filter["api_id"]=service_id
if apf_id:
filter["apf_id"]=apf_id
if api_name:
filter["api_name"]=api_name
sort_direction = pymongo.DESCENDING if sort_order == "desc" else pymongo.ASCENDING
if page_size and page:
index = (page - 1) * page_size
documents = service_col.find(filter,{"_id":0}).sort("onboarding_date", sort_direction).skip(index).limit(page_size)
pages = (total_services + page_size - 1) // page_size
else:
documents = service_col.find(filter,{"_id":0}).sort("onboarding_date", sort_direction)
pages = 1
list_services= list(documents)
long = len(list_services)
return jsonify(message="Services returned successfully",
services=list_services,
total = total_services,
long = long,
totalPages = pages,
sortOrder = sort_order), 200
def get_security(self, invoker_id, page, page_size):
current_app.logger.debug(f"Getting the security context")
security_col = self.db.get_col_by_name(self.db.security_context_col)
total_security = security_col.count_documents({})
filter = {}
if invoker_id:
filter["api_invoker_id"]=invoker_id
if page_size and page:
index = (page - 1) * page_size
documents = security_col.find(filter,{"_id":0}).skip(index).limit(page_size)
pages = (total_security + page_size - 1) // page_size
else:
documents = security_col.find(filter,{"_id":0})
pages = 1
list_security= list(documents)
long = len(list_security)
return jsonify(message="Security context returned successfully",
security=list_security,
total = total_security,
long = long,
totalPages = pages), 200
def get_events(self, subscriber_id, subscription_id, page, page_size):
current_app.logger.debug(f"Getting the events")
events_col = self.db.get_col_by_name(self.db.events)
total_events = events_col.count_documents({})
filter = {}
if subscriber_id:
filter["subscriber_id"]=subscriber_id
if subscription_id:
filter["subscription_id"]=subscription_id
if page_size and page:
index = (page - 1) * page_size
documents = events_col.find(filter,{"_id":0}).skip(index).limit(page_size)
pages = (total_events + page_size - 1) // page_size
else:
documents = events_col.find(filter,{"_id":0})
pages = 1
list_events= list(documents)
long = len(list_events)
return jsonify(message="Events returned successfully",
events=list_events,
total = total_events,
long = long,
totalPages = pages), 200
def remove_entities(self, uuid):
current_app.logger.debug(f"Removing entities for uuid: {uuid}")
invoker_col = self.db.get_col_by_name(self.db.invoker_col)
provider_col = self.db.get_col_by_name(self.db.provider_col)
try:
if invoker_col.count_documents({'uuid':uuid}) == 0 and provider_col.count_documents({'uuid':uuid}) == 0:
current_app.logger.debug(f"No entities found for uuid: {uuid}")
return jsonify(message=f"No entities found for uuid: {uuid}"), 204
for invoker in invoker_col.find({'uuid':uuid}, {"_id":0}):
current_app.logger.debug(f"Removing Invoker: {invoker["api_invoker_id"]}")
url = 'https://{}/api-invoker-management/v1/onboardedInvokers/{}'.format(os.getenv('CAPIF_HOSTNAME'), invoker["api_invoker_id"])
requests.request("DELETE", url, cert=(
'/usr/src/app/helper_service/certs/superadmin.crt', '/usr/src/app/helper_service/certs/superadmin.key'), verify='/usr/src/app/helper_service/certs/ca_root.crt')
for provider in provider_col.find({'uuid':uuid}, {"_id":0}):
current_app.logger.debug(f"Removing Provider: {provider["api_prov_dom_id"]}")
url = 'https://{}/api-provider-management/v1/registrations/{}'.format(os.getenv('CAPIF_HOSTNAME'), provider["api_prov_dom_id"])
requests.request("DELETE", url, cert=(
'/usr/src/app/helper_service/certs/superadmin.crt', '/usr/src/app/helper_service/certs/superadmin.key'), verify='/usr/src/app/helper_service/certs/ca_root.crt')
except Exception as e:
current_app.logger.debug(f"Error deleting user entities: {e}")
jsonify(message=f"Error deleting user entities: {e}"), 500
current_app.logger.debug(f"User entities removed successfully")
return jsonify(message="User entities removed successfully"), 200