Skip to content

Commit

Permalink
Add validation for capability value - updating the value
Browse files Browse the repository at this point in the history
Earlier only the validation for capability value - if the value for the capability
already exists is checked when the capability is first assigned to the node.

Does not consider the case, when the value is updated for a node.
Adding another method in models to check if any node has the same value for a capability
when is_unique is True

Change-Id: I115a6de12cabc2e32b5c10669b0f9c7b0fdbb5f5
  • Loading branch information
AnishReddyRavula committed Mar 25, 2024
1 parent a20ba99 commit 5c516bc
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion blazar/db/sqlalchemy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,8 @@ def to_dict(self):
return super(ComputeHostExtraCapability, self).to_dict()

@validates('capability_id')
def validate_capability_value(self, key, 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()
Expand All @@ -332,6 +333,31 @@ def validate_capability_value(self, key, capability_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
from blazar.db.sqlalchemy import facade_wrapper
session = facade_wrapper.get_session()
extra_capability = session.query(ExtraCapability).filter_by(id=self.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
existing_capability = (
session.query(ComputeHostExtraCapability).filter(
ComputeHostExtraCapability.computehost_id!=self.computehost_id,
ComputeHostExtraCapability.capability_id==extra_capability.id,
ComputeHostExtraCapability.capability_value==capability_value,
ComputeHostExtraCapability.deleted==None
)
).first()
if existing_capability:
raise ValueError(
f"{extra_capability.capability_name} must be unique. "
f"Please select unique {extra_capability.capability_name} for "
f"{self.computehost_id}"
)
return capability_value


# Floating IP
Expand Down

0 comments on commit 5c516bc

Please sign in to comment.