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 10, 2024
1 parent 86ab4eb commit 2f7b18c
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
56 changes: 56 additions & 0 deletions esileapclient/common/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import re
import operator

OPS = {
'>=': operator.ge,
'<=': operator.le,
'>': operator.gt,
'<': operator.lt,
'=': operator.eq,
}


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()
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, properties):
"""Filter a list of nodes based on property filters."""
if not properties:
return nodes
property_filters = []
for prop in properties:
key, op, value_str = parse_property_filter(prop)
value = convert_value(value_str)
property_filters.append((key, op, value))

filtered_nodes = []
for node in nodes:
match = True
for key, op, value in property_filters:
if key not in node.properties:
match = False
break
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
20 changes: 18 additions & 2 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 @@ -48,20 +49,35 @@ def get_parser(self, prog_name):
dest='lessee',
required=False,
help="Filter nodes by lessee.")
parser.add_argument(
'--property',
dest='properties',
required=False,
action='append',
help="Filter nodes by properties. Format: key>=value. \
Can be specified multiple times.",
metavar='"key>=value"')

return parser

def take_action(self, parsed_args):

client = self.app.client_manager.lease

# Initial filters dictionary
filters = {
'resource_class': parsed_args.resource_class,
'owner': parsed_args.owner,
'lessee': parsed_args.lessee
}

data = list(client.nodes(**filters))
# Retrieve all nodes with initial filters
all_nodes = list(client.nodes(**filters))

# Apply filtering based on properties
filtered_nodes = utils.filter_nodes_by_properties(
all_nodes, parsed_args.properties
)

if parsed_args.long:
columns = NODE_RESOURCE.detailed_fields.keys()
Expand All @@ -72,4 +88,4 @@ def take_action(self, parsed_args):

return (labels,
(oscutils.get_item_properties(s, columns)
for s in data))
for s in filtered_nodes))

0 comments on commit 2f7b18c

Please sign in to comment.