Skip to content

6 some resources are not created as models #7

Open
wants to merge 2 commits into
base: master
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
11 changes: 9 additions & 2 deletions sebastes/processor/processor.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import collections
import dataclasses
import os
import typing
Expand Down Expand Up @@ -25,6 +26,7 @@ def __init__(self, redfish_datas: typing.List[RedfishData], base_dir: str):
self._redfish_datas = redfish_datas
self._base_dir = base_dir
self._problems: typing.List[Problem] = []
self._collections = collections.defaultdict(int)

@property
def problems(self) -> typing.List[Problem]:
Expand Down Expand Up @@ -94,7 +96,7 @@ def generate_lib(self) -> None:

for data in self._redfish_datas:
if data.category == RedfishCategory.ELEMENT:
if data not in processed_data and data.parent not in processed_data:
if data not in processed_data:
processed_data.append(data)
processed_data.append(data.parent)
element = self._process_data(data)
Expand All @@ -106,7 +108,12 @@ def generate_lib(self) -> None:
for from_, targets in collection.result.imports.items():
for target in targets:
imports.append(Import(from_=from_, import_=target, alias=None))
file_name = collection.redfish_data.file_name
collection_file_name = collection.redfish_data.file_name
self._collections[collection_file_name] += 1
if self._collections[collection_file_name] != 1:
file_name = f'{collection_file_name}{self._collections[collection_file_name]}'
else:
file_name = collection.redfish_data.file_name
self._save_module([element.result.body, collection.result.body], imports, file_name)
init_data.append((file_name, [collection.redfish_data.full_name, element.redfish_data.full_name]))

Expand Down
70 changes: 54 additions & 16 deletions sebastes/scanner/scanner.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import collections
import copy
import dataclasses
import enum
import functools
import hashlib
import json
import random
import re
Expand Down Expand Up @@ -82,7 +85,7 @@ def category(self) -> RedfishCategory:
return RedfishCategory.COLLECTION
elif self.parent is not None and \
self.parent.category == RedfishCategory.COLLECTION and \
self.name in self.parent.name:
self.name[:1] in self.parent.name: # if name without additional number.
return RedfishCategory.ELEMENT
else:
return RedfishCategory.RESOURCE
Expand All @@ -101,6 +104,13 @@ def name(self) -> str:
"""
return self._name

@name.setter
def name(self, name: str) -> None:
"""
Redfish model name
"""
self._name = name

@property
def full_name(self) -> str:
"""
Expand Down Expand Up @@ -156,10 +166,15 @@ def __str__(self) -> str:

def __eq__(self, other: typing.Any) -> bool:
if isinstance(other, self.__class__):
return other.full_name == self.full_name and other.parent == self.parent
return other.full_name == self.full_name and other.parent == self.parent and other.__hash__() == self.__hash__()
else:
return False

def __hash__(self):
hash_str = ''.join(sorted([k for k in self._data.keys()]))
hash_str = hash_str + self._name
return hashlib.md5(hash_str.encode()).hexdigest()


# noinspection PyBroadException
class Scanner:
Expand Down Expand Up @@ -203,19 +218,22 @@ def _get_model_name(data: dict) -> typing.Optional[str]:

def _get_uris(self, data: dict) -> typing.List[str]:
result: typing.List[str] = []
for key, value in data.items():
if key == '@odata.id' and value not in result:
result.append(value)
if key == 'Members':
if len(data['Members']) > self._max_collection:
for _entry in random.sample(data['Members'], self._max_collection):
result += self._get_uris(_entry)
else:
for _entry in data['Members']:
result += self._get_uris(_entry)
if isinstance(value, dict):
result += self._get_uris(value)
return result
try:
for key, value in data.items():
if key == '@odata.id' and value not in result:
result.append(value)
if key == 'Members':
if len(data['Members']) > self._max_collection:
for _entry in random.sample(data['Members'], self._max_collection):
result += self._get_uris(_entry)
else:
for _entry in data['Members']:
result += self._get_uris(_entry)
if isinstance(value, dict):
result += self._get_uris(value)
return result
except Exception as e:
print(e)

def _get_json(self, url: str) -> dict:
headers = {'content-type': 'application/json'}
Expand All @@ -236,6 +254,10 @@ def _get_json(self, url: str) -> dict:
raise Exception(response.content.decode())

def scan_models(self, entry_point: str = "/redfish/v1/", parent: typing.Optional[RedfishData] = None) -> None:
self._scan_models(entry_point, parent)
self._process_duplicated_names()

def _scan_models(self, entry_point: str, parent: typing.Optional[RedfishData] = None) -> None:
"""
Scan target endpoint and all it's children.
:param entry_point: scan start point
Expand All @@ -261,8 +283,24 @@ def scan_models(self, entry_point: str = "/redfish/v1/", parent: typing.Optional

for uri in self._get_uris(data):
if uri not in self._scanned_uris:
self.scan_models(entry_point=uri, parent=model)
self._scan_models(entry_point=uri, parent=model)
except Exception as error:
self._problems.append(Problem(url=entry_point, description=str(error)))
else:
log.info(f'Models limit was reached - {self._max_models}')

def _process_duplicated_names(self):
"""
Process collection elements with the same parents but with different schemas.
"""
scanned_models = collections.defaultdict(int)
scanned_parents = []
for model in self._redfish:
scanned_models[model.name] += 1
if scanned_models[model.name] > 1: # If there are models with the same parent and different schemas.
if model.parent in scanned_parents and model.category == RedfishCategory.ELEMENT:
new_parent = copy.deepcopy(model.parent) # Create new parent with new name.
new_parent.name = f'{new_parent.name}{scanned_models[model.name]}'
model._parent = new_parent
model.name = f'{model.name}{scanned_models[model.name]}'
scanned_parents.append(model.parent)