azure icon indicating copy to clipboard operation
azure copied to clipboard

default_compare can not handle list of dicts without id/name keys

Open Klaas- opened this issue 10 months ago • 1 comments

SUMMARY

I am trying to implement a new module. I am trying to use the pre-existing functions.

A minimal example:

test_object = {
    'all_of': 
        [
            {
                'any_of': [
                    {
                        'equals': 'Unknown',
                        'field': 'properties.previousHealthStatus'
                    }
                ]
            },
            {
                'any_of': [
                    {
                        'equals': 'PlatformInitiated',
                        'field': 'properties.cause'
                    }
                ]
            }
        ]
}
test = self.default_compare({}, test_object, test_object, '', result_compare)

The error was: TypeError: '<' not supported between instances of 'dict' and 'dict'

I am not really sure how this should be handled. Can this function be extended to also compare dicts without breaking existing usage?

https://github.com/ansible-collections/azure/blob/40e5c3f36090f4a465d5eae5a62fe2ceffa4422a/plugins/module_utils/azure_rm_common_ext.py#L264-L267

I think I can fix it for this usecase if we just compare them in order, but that does not really reflect what it should do, order in "any_of" is not important. Not 100% sure how to proceed here :)

ISSUE TYPE
  • Bug Report
COMPONENT NAME

azure_rm_common_ext.py

COLLECTION VERSION
3.4.0
EXPECTED RESULTS

default_compare works for lists of dicts without keys

ACTUAL RESULTS
The error was: TypeError: '<' not supported between instances of 'dict' and 'dict'

Klaas- avatar Jun 18 '25 13:06 Klaas-

Can we do something like this:

                    else:
                        try:
                            key = next(iter(old[0]))
                            new = sorted(new, key=lambda x: x.get(key, None))
                            old = sorted(old, key=lambda x: x.get(key, None))
                        except TypeError:
                            for i in range(len(new)):
                                if not self.default_compare(modifiers, new[i], old[i], path + '/*', result):
                                    comparison_result = False

Klaas- avatar Jun 18 '25 13:06 Klaas-

#1971 merged, we can closed it!

Fred-sun avatar Aug 01 '25 08:08 Fred-sun