Skip to content

Commit

Permalink
improved validation in computehost extra capability model
Browse files Browse the repository at this point in the history
Previously, validation was only performed for the `capability_id` and `capability_value` attributes separately.
This update consolidates the validation logic into a single method called `validate_capability`, which is invoked for both attributes.
Additionally, the docstring for the method has been updated to provide clarity on when each validation scenario occurs.

Change-Id: I149784b5e4be71b34dfebad470cc645fed5656b4
  • Loading branch information
AnishReddyRavula committed Mar 25, 2024
1 parent 5c516bc commit b904185
Showing 1 changed file with 28 additions and 24 deletions.
52 changes: 28 additions & 24 deletions blazar/db/sqlalchemy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,32 +314,36 @@ class ComputeHostExtraCapability(mb.BlazarBase, mb.SoftDeleteMixinWithUuid):
def to_dict(self):
return super(ComputeHostExtraCapability, self).to_dict()

@validates('capability_id')
def validate_capability_id(self, key, capability_id):
# this validation occurs only upon creating the capability
from blazar.db.sqlalchemy import facade_wrapper
session = facade_wrapper.get_session()
extra_capability = session.query(ExtraCapability).filter_by(id=capability_id).first()
if extra_capability and extra_capability.is_unique:
existing_capability = (
session.query(ComputeHostExtraCapability).filter_by(
capability_id=extra_capability.id, capability_value=self.capability_value, deleted=None)
).first()
if existing_capability:
raise ValueError(
f"Values for {extra_capability.capability_name} must be unique. "
f"Please select unique {extra_capability.capability_name} for "
f"{self.computehost_id}"
)
return capability_id

@validates('capability_value')
def validate_capability_value(self, key, capability_value):
# this validation occurs when updating the capability value
# this is important when for Eg: node_name capability's value is updated
@validates('capability_id', 'capability_value')
def validate_capability(self, key, value):
"""Validates the capability ID and value before assigning
them to a ComputeHostExtraCapability instance.
This validation method is called for two scenarios:
1. When a capability ID is first assigned to a compute host.
both capability_id and capability_value are validated in this case
2. When a capability value is updated for an existing capability ID.
Args:
key (str): The attribute key being validated
('capability_id' or 'capability_value').
value (str)
Raises:
ValueError
Returns:
str
"""
from blazar.db.sqlalchemy import facade_wrapper
session = facade_wrapper.get_session()
extra_capability = session.query(ExtraCapability).filter_by(id=self.capability_id).first()

# Determine the capability ID and value to validate based on the key
capability_id = value if key == "capability_id" else self.capability_id
capability_value = value if key == "capability_value" else self.capability_value

extra_capability = session.query(ExtraCapability).filter_by(id=capability_id).first()
if extra_capability and extra_capability.is_unique:
# exclude the current compute_host
# we should allow updating the node_name with current name
Expand All @@ -357,7 +361,7 @@ def validate_capability_value(self, key, capability_value):
f"Please select unique {extra_capability.capability_name} for "
f"{self.computehost_id}"
)
return capability_value
return value


# Floating IP
Expand Down

0 comments on commit b904185

Please sign in to comment.