From 62561f0deba77742f2dd8e1f85886e3faf631217 Mon Sep 17 00:00:00 2001 From: Armingol Date: Thu, 21 Sep 2023 17:00:14 +0200 Subject: [PATCH] Fix formatting of "component". Code cleanup --- src/context/service/database/Component.py | 15 +++++++-------- src/context/service/database/ConfigRule.py | 2 -- .../service/database/models/ComponentModel.py | 17 +++++++++-------- 3 files changed, 16 insertions(+), 18 deletions(-) diff --git a/src/context/service/database/Component.py b/src/context/service/database/Component.py index 17d3cb9ff..3f2776787 100644 --- a/src/context/service/database/Component.py +++ b/src/context/service/database/Component.py @@ -40,16 +40,16 @@ def compose_components_data( dict_components : List[Dict] = list() for position,component in enumerate(components): str_kind = component.WhichOneof('config_rule') - message = (grpc_message_to_json_string(getattr(component, str_kind, {}))) - data = json.loads(message) - resource_key = data["resource_key"] + message = (grpc_message_to_json_string(getattr(component, str_kind, {}))) + data = json.loads(message) + resource_key = data["resource_key"] resource_value = data["resource_value"] if '/inventory' in resource_key: resource_value_data = json.loads(resource_value) - name = resource_value_data.pop('name', None) - type_ = resource_value_data.pop('class', None) - parent = resource_value_data.pop('parent-component-references', None) - attributes = resource_value_data.pop('attributes', {}) + name = resource_value_data.pop('name', None) + type_ = resource_value_data.pop('class', None) + parent = resource_value_data.pop('parent-component-references', None) + attributes = resource_value_data.pop('attributes', {}) if len(resource_value_data) > 0: LOGGER.warning('Discarding Component Leftovers: {:s}'.format(str(resource_value_data))) @@ -68,6 +68,5 @@ def compose_components_data( 'created_at' : now, 'updated_at' : now, } - LOGGER.debug('dict_component={:s}'.format(str(dict_component))) dict_components.append(dict_component) return dict_components diff --git a/src/context/service/database/ConfigRule.py b/src/context/service/database/ConfigRule.py index 8275a3ffe..1c5a2c7d5 100644 --- a/src/context/service/database/ConfigRule.py +++ b/src/context/service/database/ConfigRule.py @@ -35,8 +35,6 @@ def compose_config_rules_data( dict_config_rules : List[Dict] = list() for position,config_rule in enumerate(config_rules): - #LOGGER.info("REQUEST") - #LOGGER.info(position,config_rule) str_kind = config_rule.WhichOneof('config_rule') kind = ConfigRuleKindEnum._member_map_.get(str_kind.upper()) # pylint: disable=no-member dict_config_rule = { diff --git a/src/context/service/database/models/ComponentModel.py b/src/context/service/database/models/ComponentModel.py index 964cb9bf3..60927e944 100644 --- a/src/context/service/database/models/ComponentModel.py +++ b/src/context/service/database/models/ComponentModel.py @@ -25,9 +25,9 @@ class ComponentModel(_Base): #Inherit component_uuid = Column(UUID(as_uuid=False), primary_key=True) #Unique identifier that serves as a primary key for this table device_uuid = Column(ForeignKey('device.device_uuid',ondelete='CASCADE' ), nullable=False, index=True) #Foreign Key relationship with the field device_uuid from the Device table (CASCADE' behavior for deletion, meaning when a device is deleted, its components will also be dele) name = Column(String, nullable=False) #String field that stores the name of the component - type = Column(String, nullable=False) #String field that stores the name of the component - attributes = Column(String, nullable=False) #String field that stores data about the component - parent = Column(String, nullable=False) #String field that stores data about the component + type = Column(String, nullable=False) #String field that stores the type of the component + attributes = Column(String, nullable=False) #String field that stores the attributes of the component + parent = Column(String, nullable=False) #String field that stores the parent of the component created_at = Column(DateTime, nullable=False) #Stores the creaton timestamp for the component updated_at = Column(DateTime, nullable=False) #Stores the last upadted timestamp for the component @@ -40,11 +40,12 @@ class ComponentModel(_Base): #Inherit } def dump(self) -> Dict: - data = json.loads(self.attributes) - data['component_uuid'] = {'uuid': self.component_uuid}, - data['name'] = self.name - data['type'] = self.type - data['parent'] = self.parent + data = dict() + data['attributes'] = json.loads(self.attributes) + data['component_uuid'] = {'uuid': self.component_uuid} + data['name'] = self.name + data['type'] = self.type + data['parent'] = self.parent return data def dump_name(self) -> Dict: -- GitLab