Skip to content

Commit

Permalink
Apply property filters
Browse files Browse the repository at this point in the history
  • Loading branch information
Surbhi Kanthed committed Jun 5, 2024
1 parent 8afcc4a commit e20cd39
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 15 deletions.
44 changes: 44 additions & 0 deletions esileapclient/common/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import re
import operator


def parse_property_filter(filter_str):
"""Parse a property filter string into a key, operator, and value."""
match = re.match(r'([^><=]+)([><=]+)(.+)', filter_str)
if not match:
raise ValueError(f"Invalid property filter format: {filter_str}")
key, op_str, value_str = match.groups()
ops = {
'>=': operator.ge,
'<=': operator.le,
'>': operator.gt,
'<': operator.lt,
'=': operator.eq,
}
return key, ops[op_str], value_str


def convert_value(value_str):
"""Convert a value string to an appropriate type for comparison."""
try:
return int(value_str)
except ValueError:
try:
return float(value_str)
except ValueError:
return value_str


def filter_nodes_by_properties(nodes, property_filters):
"""Filter a list of nodes based on property filters."""
filtered_nodes = []
for node in nodes:
match = True
for key, op, value in property_filters:
node_value = convert_value(node.properties.get(key, ''))
if not op(node_value, value):
match = False
break
if match:
filtered_nodes.append(node)
return filtered_nodes
29 changes: 14 additions & 15 deletions esileapclient/osc/v1/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from osc_lib import utils as oscutils

from esileapclient.v1.node import Node as NODE_RESOURCE
from esileapclient.common import utils

LOG = logging.getLogger(__name__)

Expand Down Expand Up @@ -53,8 +54,9 @@ def get_parser(self, prog_name):
dest='properties',
required=False,
action='append',
help="Filter nodes by properties. Format: key=value. \
Can be specified multiple times.")
help="Filter nodes by properties. Format: key>=value. \
Can be specified multiple times.",
metavar='"key>=value"')

return parser

Expand All @@ -70,29 +72,26 @@ def take_action(self, parsed_args):
}

# Add property filters if provided
property_filters = {}
property_filters = []
if parsed_args.properties:
for prop in parsed_args.properties:
key, value = prop.split('=')
property_filters[key] = value
key, op, value_str = utils.parse_property_filter(prop)
value = utils.convert_value(value_str)
property_filters.append((key, op, value))

# print("Filters: ", filters) # Debug filters content
# print("Filters: ", filters)
# print("Property Filters: ", property_filters)

# Retrieve all nodes with initial filters
all_nodes = list(client.nodes(**filters))
# print("All Nodes: ", all_nodes)

# Apply manual filtering based on properties
filtered_nodes = []
for node in all_nodes:
match = True
for key, value in property_filters.items():
if node.properties.get(key) != value:
match = False
break
if match:
filtered_nodes.append(node)
filtered_nodes = utils.filter_nodes_by_properties(
all_nodes, property_filters
)

# print("Filtered Nodes: ", filtered_nodes)

if parsed_args.long:
columns = NODE_RESOURCE.detailed_fields.keys()
Expand Down

0 comments on commit e20cd39

Please sign in to comment.