inmanta / inmanta-core

Inmanta is an automation and orchestration tool
https://inmanta.com
Apache License 2.0
26 stars 7 forks source link

Issue/dict path improvs #7789

Closed edvgui closed 5 days ago

edvgui commented 1 week ago

Description

Add one more functionality to the dict path library: the possibility to emit all the dict path expression that would match one of the elements matched by a wild dict path for a given container. (see test and docstrings for example)

Context: When unwrapping an lsm service, in an update state, we often want to check which embedded entities have been created, updated, or removed. For each service we always need to implement this logic custom, which can be tedious if the embedded entity we care about is deep in the service attributes tree. One thing that would make it a lot easier was the possibility to say "for all the elements matching this [wild] dict path, tell me which ones have been added, updated, removed". But this is also very inconvenient to calculate because the notion of identity of each layer of embedded entity needs to be extracted from the dict path, which is not really what the library is meant to be used for. This new feature allows an easier approach: resolving all the paths that point to embedded entities in the previous set of attributes and in the current one, and just making the diff on the list of dict paths.

@inmanta.plugins.plugin()
def get_removed_entities(service: "lsm::ServiceEntity", wild_path: "string") -> "dict[]":
    previous_attributes = ...
    current_attributes = ...

    parsed_wild_path = dict_path.to_wild_path(wild_path)
    removed_entitites = (
        {str(p) for p in parsed_wild_path.resolve_wild_cards(previous_attributes)}
        - {str(p) for p in parsed_wild_path.resolve_wild_cards(current_attributes)}
    )

    return [
        dict_path.to_path(path).get_element(previous_attributes)
        for path in removed_entities
    ]

Self Check:

Strike through any lines that are not applicable (~~line~~) then check the box

wouterdb commented 6 days ago

Cool stuff

inmantaci commented 5 days ago

Processing this pull request

inmantaci commented 5 days ago

Merged into branches master in 0cf7def748d64ebd9dc2397b017a8266e5abc354

inmantaci commented 5 days ago

Processing #7808.