Snippetlib Snippet Types

Panos Snippet

class skilletlib.snippet.panos.PanosSnippet(metadata: dict, panoply: skilletlib.panoply.Panoply)
add_filters() → None

Each snippet sub-class can add additional filters. See the PanosSnippet for examples

Returns:None
cherry_pick_element(element: str, cherry_pick_path: str) → str

Cherry picking allows the skillet builder to pull out specific bits of a larger configuration and load only the smaller chunks. This is especially useful when combined with ‘when’ conditionals

Parameters:
  • element – string containing the jinja templated xml fragment
  • cherry_pick_path – string describing the relative xpath to use to cherry pick an xml node from the element given as a parameter
Returns:

rendered and cherry_picked element

cherry_pick_xpath(base_xpath: str, cherry_picked_xpath: str) → str

When cherry picking is active, we are only going to push a smaller portion of the xml fragment. As such, we need to combine the base xpath for the xml file and the xpath to the cherry_picked node.

Ensure we can combine the base xpath and the cherry_picked xpath cleanly ideally, we end up with something like base_xpath: /config/devices/entry[@name=’localhost.localdomain’]/deviceconfig/system cherry_picked: update-schedule/statistics-service/application-reports Because the cherry_pick xpath will return the named element, we need to strip the last node from the xpath in this case, in order to push the cherry_picked element back to the device, we need to set the xpath to /config/devices/entry[@name=’localhost.localdomain’]/deviceconfig/system/update-schedule/statistics-service

Parameters:
  • base_xpath – base xpath for the xml fragment
  • cherry_picked_xpath – relative xpath for cherry picking a portion of the xml fragment
Returns:

combined and rendered xpath

static compare_element_at_xpath(config: str, element: str, xpath: str, context: dict) → bool

Grab an xml fragment from the config given at xpath and compare it to this element

Parameters:
  • config – XML document string from which to pull the XML element to compare
  • element – element to check against
  • xpath – xpath to grab an xml fragment from the config for comparison
  • context – jinja context used to interpolate any variables that may be present in the template
Returns:

bool true if they match

execute(context: dict) → Tuple[str, str]

Execute this Snippet and return a tuple consisting on raw output and a string representing success, failure, or running.

Each snippet sub class must override this method!

Parameters:context – context to use for variable interpolation
Returns:Tuple containing raw snippet output and string indicated success or failure
get_default_output(results: str, status: str) → dict

Override the default snippet get_default_output to not include raw results

Parameters:
  • results – raw output from snippet execution
  • status – status of the snippet.execute method
Returns:

dict of default outputs

render_metadata(context: dict) → dict

Renders each item in the metadata using the provided context. Currently renders the xpath and element for PANOS type skillets

Parameters:context – dict containing key value pairs to
Returns:dict containing the snippet definition metadata with the attribute values rendered accordingly
sanitize_metadata(metadata: dict) → dict

Ensure all required keys are present in the snippet definition

Parameters:metadata – dict
Returns:dict

PAN Validation Snippet

class skilletlib.snippet.pan_validation.PanValidationSnippet(metadata: dict, panoply: skilletlib.panoply.Panoply)

Pan validation Snippet

execute(context: dict) → Tuple[str, str]

Execute method in pan_validation snippet overrides the execute method in panos to add ensure any exception caught always results in a failed test

Parameters:context – snippet context used for tests
Returns:tuple consisting of results, (success | failure)
handle_output_type_validation(results: str) → dict

Handle output type validation results

Parameters:results – results from the test execution
Returns:dict containing validation messages

REST Snippet

class skilletlib.snippet.rest.RestSnippet(payload_str: str, metadata: dict, session: requests.sessions.Session)

Rest Snippet

execute(raw_context: dict) → Tuple[str, str]

Execute this Snippet and return a tuple consisting on raw output and a string representing success, failure, or running.

Each snippet sub class must override this method!

Parameters:context – context to use for variable interpolation
Returns:Tuple containing raw snippet output and string indicated success or failure
sanitize_metadata(metadata: dict) → dict

Clean and sanitize metadata elements in this snippet definition

Parameters:metadata – dict
Returns:dict

Template Snippet

class skilletlib.snippet.template.TemplateSnippet(template_str, metadata)

TemplateSnippet implements a basic template object snippet

execute(context: dict) → Tuple[str, str]

Execute this Snippet and return a tuple consisting on raw output and a string representing success, failure, or running.

Each snippet sub class must override this method!

Parameters:context – context to use for variable interpolation
Returns:Tuple containing raw snippet output and string indicated success or failure

Base Snippet

This is base class from which all Snippets derive.

class skilletlib.snippet.base.Snippet(metadata: dict)

BaseSnippet implements a basic Noop snippet

add_filters() → None

Each snippet sub-class can add additional filters. See the PanosSnippet for examples

Returns:None
capture_outputs(results: str, status: str) → dict

All snippet output or portions of snippet output can be captured and saved on the context as a new variable

Parameters:
  • results – the raw output from the snippet execution
  • status – status of the snippet.execute method
Returns:

a dictionary containing all captured variables

execute(context: dict) → Tuple[str, str]

Execute this Snippet and return a tuple consisting on raw output and a string representing success, failure, or running.

Each snippet sub class must override this method!

Parameters:context – context to use for variable interpolation
Returns:Tuple containing raw snippet output and string indicated success or failure
execute_conditional(test: str, context: dict) → bool

Evaluate ‘test’ conditionals and return a bool

Parameters:
  • test – string of the conditional to execute
  • context – jinja context containing previous outputs and user supplied variables
Returns:

boolean

get_default_output(results: str, status: str) → dict

each snippet type can override this method to provide it’s own default output. This is used when there are no variables defined to be captured

Parameters:
  • results – raw output from snippet execution
  • status – status of the snippet.execute method
Returns:

dict of default outputs

get_loop_parameter() → list

Returns the loop parameter for this snippet. If a loop parameter is not defined in the snippet def, this returns a list with a single blank str. Otherwise, return the value of the loop parameter as a list.

Returns:value of loop_parameter from the context or a list with a single blank str
get_output() → Tuple[str, str]

get_output can be used when a snippet executes async and cannot or will not return output right away snippets that operate async must override this method

Returns:Tuple containing the skillet output as a str and a str indicating success of failure
get_output_variables() → list

Returns a list of all output variables. This is used to determine if a snippet variable should be considered undeclared.

Returns:list of str representing output variable names
get_snippet_variables() → list

Returns a list of variables defined in this snippet that are NOT defined as outputs

Returns:list of str representing variables found in the jinja templates
get_variables_from_template(template_str: str) → list

Returns a list of jinja2 variable found in the template

Parameters:template_str – jinja2 template
Returns:list of variables declared in the template
is_filtered(context) → bool

Determines if a snippet should be available for execution based on the presence of the __filter_snippets object in the context. Snippets can be filtered by the following:

include_by_name: list of names to check. Only snippet names included in this list will be executed include_by_tag: list of tags to check. Only snippets with those tags will be executed include_by_regex: regular expression match. Only snippets whose name matches the regex will be executed

any snippet that does not match any of the above rules will be filtered out. The rules are inclusive OR

Parameters:context – Snippet Context
Returns:bool
render(template_str: str, context: (<class 'dict'>, None)) → str

Convenience method to quickly render a template_str using the provided context

Parameters:
  • template_str – jinja2 template to render
  • context – context to pass to the jinja2 environment
Returns:

rendered string

render_metadata(context: dict) → dict

Each snippet sub class can override this method to perform jinja variable interpolation on various items in it’s snippet definition. For example, the PanosSnippet will check the ‘xpath’ attribute and perform the required interpolation.

This handles regular strings, lists, and dictionaries such as:

in snippet class template_metadata = {‘render_me’, ‘render_all’, ‘render_list’}

in metadata snippets:

  • name: render_me_snippet render_me: render_{{ this }}

  • name: render_all_snippet render_all:

    a_key: some_{{ value }} another_key: some_other_{{ value }}

  • name: render_list_snippet render_list:

    • here_is_a_{{ value }}
    • another_{{ value }}
Parameters:context – context from environment
Returns:metadata with jinja rendered variables
reset_metadata()

Reset the metadata to the original metadata. This is used during looping so we can render items in the metadata on each iteration.

Returns:None
sanitize_metadata(metadata: dict) → dict

method to sanitize metadata. Each snippet type can override this provide extra logic over and above just checking the required and optional fields

Parameters:metadata – snippet metadata
Returns:sanitized snippet metadata
should_execute(context: dict) → bool

Evaluate ‘when’ conditionals and return a bool if this snippet should be executed

Parameters:context – jinja context containing previous outputs and user supplied variables
Returns:boolean
update_context(context: dict) → dict

This will update the snippet context with the passed in dict. This gets called inside of ‘should_execute’

Parameters:context – dict of the outer context
Returns:newly updated context