Skip to content

Commit

Permalink
fix: adding inventory fields without base and mandatory profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtekzyla committed Oct 12, 2022
1 parent 7df8cfb commit 2b28113
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 15 deletions.
4 changes: 2 additions & 2 deletions backend/SC4SNMP_UI_backend/common/conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ def _backend2ui_map(self, document: dict, **kwargs):
}
var_binds.append(new_vb)

if "conditions" in document[profile_name]:
backend_condition = document[profile_name]["conditions"]
if "condition" in document[profile_name]:
backend_condition = document[profile_name]["condition"]
condition_type = backend_condition["type"]
field = backend_condition["field"] if condition_type == "field" else ""
patterns = [{"pattern": p} for p in backend_condition["patterns"]] if condition_type == "field" else None
Expand Down
60 changes: 50 additions & 10 deletions backend/SC4SNMP_UI_backend/common/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,20 @@ def __init__(self, conversion: InventoryConversion, db):
self._db = db

def read_from_backend(self):
groups_query = list(self._db.groups.find({}))
self._load_groups_from_mongo()

inventory_backend = list(self._db.inventory.find({}))
self._inventory_ui = []
for record in inventory_backend:
self._process_inventory_line(record)
for group_in_record in self._groups_inventory.values():
converted = self._conversion.backend2ui(group_in_record)
self._inventory_ui.append(converted)
return self._inventory_ui

def _load_groups_from_mongo(self):
self._groups_backend = {}
groups_query = list(self._db.groups.find({}))
for group_from_query in groups_query:
gr_name = get_group_name_from_backend(group_from_query)

Expand All @@ -24,15 +36,6 @@ def read_from_backend(self):
}
self._groups_backend[gr_name].update(new_device)

inventory_backend = list(self._db.inventory.find({}))
self._inventory_ui = []
for record in inventory_backend:
self._process_inventory_line(record)
for group_in_record in self._groups_inventory.values():
converted = self._conversion.backend2ui(group_in_record)
self._inventory_ui.append(converted)
return self._inventory_ui

def _process_inventory_line(self, record: dict):
if record["group"] is None:
converted = self._conversion.backend2ui(record)
Expand Down Expand Up @@ -88,4 +91,41 @@ def _gather_group(self, record: dict):
if security_engine is not False:
self._groups_inventory[group_name]['security_engine'] = security_engine

def add_record(self, record: dict):
# TODO: add mandatory profiles and base profiles if smart = true
address = record['address']
if address[0].isdigit():
converted = self._conversion.ui2backend(record, delete=False, group=None)
self._db.inventory.insert_one(converted)
else:
self._load_groups_from_mongo()
if address not in self._groups_backend.keys():
# TODO: display error if user wants to add nonexistent group
return None
else:
all_records = []
for key, value in self._groups_backend[address].items():
ip_address = key.split(":")[0]
port = key.split(":")[1]
port = int(port) if len(port) > 0 else record['port']
community = value['community'] if 'community' in value.keys() else record['community']
secret = value['secret'] if 'secret' in value.keys() else record['secret']
version = value['version'] if 'version' in value.keys() else record['version']
security_engine = value['security_engine'] if 'security_engine' in value.keys() else record['securityEngine']

inventory_data = {
'address': ip_address,
'port': port,
'version': version,
'community': community,
'secret': secret,
'security_engine': security_engine,
'walk_interval': record['walkInterval'],
'profiles': record['profiles'],
'smart_profiles': record['smartProfiles'],
'group': address,
'delete': False
}
all_records.append(inventory_data)
self._db.inventory.insert_many(all_records)

8 changes: 5 additions & 3 deletions backend/SC4SNMP_UI_backend/ui_handling/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ def get_profile_names():
profiles = db.profiles.find()
profiles_list = []
for pr in list(profiles):
profiles_list.append(profile_conversion.backend2ui(pr))
converted = profile_conversion.backend2ui(pr)
if converted['conditions']['condition'] not in ['mandatory', 'base']:
profiles_list.append(converted)
return json_util.dumps([el["profileName"] for el in profiles_list])


Expand Down Expand Up @@ -213,8 +215,8 @@ def get_inventory_count():
@cross_origin()
def add_inventory_record():
inventory_obj = request.json
print(inventory_obj)
db.inventory_ui.insert_one(inventory_obj)
inventory_processing = InventoryProcessing(inventory_conversion, db)
inventory_processing.add_record(inventory_obj)
return "success"


Expand Down

0 comments on commit 2b28113

Please sign in to comment.