Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter optimize #12

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open

Conversation

velotiger
Copy link

Hi, I added comments to aci.py to explain the code and did a few optimizations:

  • Closure for listify_worker to encapsulate variables.
  • Put defaults for listify_worker in function definition.
  • Use join instead of + for string operations.
  • Put statements below if's where possible.
  • Got rid of some short-lived variables.
  • Replaced last loop by direct lookup of key.
  • Removed obsolete check for dict before recursive call.
    I hope you'll find this contribution useful.

Comments/explanations added to the code.
Optimizations:
* Use closure for listify function to encapsulate internal vars.
* Eliminated some intermediate vars.
* Replaced last loop by direct key lookup.
The original filter needs alternating dicts/lists in the inventory. This
alternative filter accepts lists and dicts in any order. The result for an
inventory with alternating dicts/lists is unchanged.

NEW: plugins/filter/aci2.py
MOD: plugins/filter/aci2.py
This fixes a bug in commit ad32ff5:
Attributes in intermediate items (neither top-level nor bottom) do not
appear in output. E.g. if you look for keywords «tenant», «vrf», «bd»,
«subnet» some attributes for «vrf» might be missing because they were
evaluated after the cache had been transferred to the result list.
To avoid this, all flat key/value pairs are evaluated before any dict
or list at the same level.

MOD: plugins/filter/aci2.py
* You can append a regex to each key (separated by an =–sign).
* Only keys whose name-attribute matches the regex will be included
  in the result.
* If the regex is omitted, all keys will be included (same
  behavior as before – backwards compatible).
* Example:
    "{{ aci_topology|aci_listify2('access_policy',
      'interface_policy_profile=.+998',
      'interface_selector') }}"
  All paths in the output match interface policy profiles that
  end in «998».
  E.g. interface selectors below a non-matching interface policy
  profile will be suppressed from the output.

MOD: plugins/filter/aci2.py
MOD: plugins/filter/aci2.py
Do not hard-code the attribute used for regex matching of
keys. The variable «NameAttr» specifies the name of this
attribute.

MOD: plugins/filter/aci2.py
Fixes this issue: Program ended with error if name-attribut's value
was not a string (used pattern matching on a non-string object).

MOD: plugins/filter/aci2.py
* Short description of alternative filter.
* One more example how to use the alternative filter (dynamically
  specify match pattern for keys at runtime).

MOD: README.md
MOD: plugins/filter/aci2.py
No functional changes or fixes.

Add more patterns to .gitignore

MOD: .gitignore
MOD: plugins/filter/aci2.py
* Get rid of extra loop that deletes flat key/value pairs
  after copying them to the cache.

MOD: plugins/filter/aci2.py
Instances of objects can be organized in lists (as before)
or dicts (new).
* This is useful if you need direct lookups by key.
* This is partly incompatible to previous versions!
  Nested dicts and lists in any order down the object
  tree are not supported any more. This feature was not
  not useful and therefore has been dropped.

MOD: README.md
MOD: plugins/filter/aci2.py
* Use a closure to set fixed values once for all
  (avoid setting them in every new call of the filter).
Previously scalar attributes have been deleted after evaluation.
This simplified further processing of lists and dicts.
However, if the object tree uses anchors and aliases, deleting
attributes invalidates the aliases.
* This is a backport of
  commit eed6b9a
  to the old listify filter (aci.py).
* Add an example for a subnet to the example inventory.

Ref: CiscoDevNet/ansible-aci#517 (comment)
Accetable attribute values: Replace negated check for list or
dict by explicit check for scalar types.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant