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

Implement mutators #56

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

lottspot
Copy link
Collaborator

@lottspot lottspot commented Aug 6, 2016

This patch introduces a new Mutators data type to entities. This data type is a stack of python callables which perform arbitrary modifications to a populated node or inventory dictionary before it is returned to the caller.

Writing mutators is simple; we simply need to provide a python callable within a module somewhere along PYTHONPATH (cli has been modified in this patch to append inventory_base_uri to sys.path for sane default behavior)

# mutator_mod.py
def some_callable(nodename, inventory):
  thisnode = inventory['nodes'][nodename]
  thisnode['parameters']['related_nodes'] = []
  for name, node in inventory['nodes'].items():
    if nodename in node['parameters'].get('related_to', []):
      thisnode['parameters']['related_nodes'].append(name)

Mutators are then specified in class or node definitions, just as other reclass datatypes.

# nodes/mynode.yml
parameters:
  hello: world
mutators:
  - mutator_pkg.mutator_mod.some_callable
# nodes/myrelatednode.yml
parameters:
  related_to:
    - mynode

This configuration would apply the mutator when all of the following conditions are satisfied:

  1. mynode is loaded
  2. Core calls _mutate with arguments named 'inventory' and 'nodename'

Core will only call _mutate with those arguments within Core.nodeinfo. So from the CLI, this mutator would be applied using reclass -n mynode, which would then produce the following output:

__reclass__:
  environment: base
  name: mynode
  node: ./mynode
  timestamp: Sat Aug  6 18:14:47 2016
  uri: yaml_fs:///mnt/files/shared/projects/reclass/.env/inventory/nodes/./mynode.yml
applications: []
classes: []
environment: base
parameters:
  hello: world
  related_nodes:
  - myrelatednode

The purpose of implementing this feature is to offer a way to create dynamic node parameters, primarily as a way to build relationships between nodes or classes based on other parameters.

@lottspot lottspot force-pushed the feature/mutators branch 2 times, most recently from 9e21892 to 9e8ac81 Compare August 7, 2016 12:19
@lottspot
Copy link
Collaborator Author

lottspot commented Aug 7, 2016

I guess it would actually be more accurate to point out that mutators need to be functions, and that arbitrary callables won't work. That's a limitation imposed by inspect.getargspec, which is a crucial piece of deciding whether a mutator should be run.

@lottspot
Copy link
Collaborator Author

Let's not merge this yet-- I need to resolve lottspot/reclass/issues/4 first

@bbinet
Copy link

bbinet commented Feb 8, 2017

Any news on this feature?

lottspot referenced this pull request in lottspot/reclass Feb 8, 2017
Mutators allow the definition of raw python functions to
manipulate entities before returning them to the caller.

The Mutators implementation obsoletes function interpolation
@lottspot
Copy link
Collaborator Author

lottspot commented Feb 8, 2017

This feature is still very much on my radar to complete. I hope to be able to carve out some time soon to hammer out the remaining issues and wrap this up.

@lottspot lottspot added this to the 1.5 milestone Feb 15, 2017
@ketzacoatl
Copy link

@lottspot, what is the situation that motivates this feature?

AndrewPickford pushed a commit to AndrewPickford/reclass that referenced this pull request Sep 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants