def test_skillet_includes(): """ Tests to verify the Skillet object is successfully compiled from all included skillets. The 'include_other_skillets' skillet includes snippets and variables from two other skillets in the same directory :return: None """ skillet_path = '../example_skillets/skillet_includes/' skillet_loader = SkilletLoader(path=skillet_path) skillet: Skillet = skillet_loader.get_skillet_with_name( 'include_other_skillets') # verify we can find and load the correct skillet assert skillet.name == 'include_other_skillets' # verify the correct number of snippets. assert len(skillet.snippets) == 9 included_snippet: Snippet = skillet.get_snippet_by_name( 'network_profiles.check_network_profiles') # verify we can get an included snippet from the skillet object assert included_snippet is not None # verify the 'label' metadata attribute has been overridden correctly assert included_snippet.metadata.get( 'label', '') == 'Check Network Profiles Override' included_variable: dict = skillet.get_variable_by_name('another_variable') # verify the included variable is present in the compiled skillet assert included_variable is not None # verify the default value is correctly overridden from the included variable assert included_variable.get('default', '') == 'test123456' second_included_variable: dict = skillet.get_variable_by_name( 'some_update_variable') assert second_included_variable is not None another_included_variable: dict = skillet.get_variable_by_name( 'zone_to_test') assert another_included_variable["default"] == "untrust" # Ensure using includes / overrides leaves our original skillet definition intact # added for issue #163 child_skillet: Skillet = skillet_loader.get_skillet_with_name( 'network_profiles') child_snippet: Snippet = child_skillet.get_snippet_by_name( 'check_network_profiles') assert child_snippet.metadata.get('label', '') == 'Ensure Named profile exists'
def test_workflow_transform_skillet(): """ Load and execute the workflow skillet and ensure all child skillets are executed properly :return: None """ skillet_path = '../example_skillets/workflow_transform/' skillet_loader = SkilletLoader(path=skillet_path) skillet: Skillet = skillet_loader.get_skillet_with_name( 'workflow_transform') # verify we can find and load the correct skillet assert skillet.name == 'workflow_transform' out = skillet.execute(context) assert 'pan_validation' in out assert 'outputs' in out assert 'snippets' in out for k, v in out.get('pan_validation', {}).items(): r = str(v.get('results', 'False')) print(f'{k:60}{r}') assert r == 'True' assert 'zones' in skillet.context
def test_workflow_skillet(): """ Load and execute the workflow skillet and ensure all child skillets are executed properly :return: None """ skillet_path = '../example_skillets/workflow_example/' skillet_loader = SkilletLoader(path=skillet_path) skillet: Skillet = skillet_loader.get_skillet_with_name( 'example_workflow_with_filtering') # verify we can find and load the correct skillet assert skillet.name == 'example_workflow_with_filtering' out = skillet.execute(context) assert 'pan_validation' in out assert 'outputs' in out assert 'snippets' in out # ensure the correct number of tests are included and executed and reported in the output assert len(out['pan_validation']) == 6 # ensure all three snippets were found and executed assert len(skillet.snippet_outputs) == 3
def test_get_skillet_by_name(): """ Test to verify skilletLoader can successfully load all skillets found in the 'skillet_incluedes' directory and return the one with the 'include_other_skillets' name :return: None """ skillet_path = '../example_skillets/skillet_includes/' skillet_loader = SkilletLoader(path=skillet_path) skillet = skillet_loader.get_skillet_with_name('include_other_skillets') assert skillet is not None
def test_load_skillet(): """ Test to verify skilletLoader can successfully load all skillets found in the 'workflow_example' directory and return the one with the 'example_workflow_with_filtering' name :return: None """ skillet_path = '../example_skillets/workflow_example/' skillet_loader = SkilletLoader(path=skillet_path) skillet = skillet_loader.get_skillet_with_name( 'example_workflow_with_filtering') assert skillet is not None
def main(): # define available arguments/parameters a user can pass to the module module_args = dict(skillet=dict(type='str', required=True), skillet_path=dict(type='str', required=True), vars=dict(type='dict'), provider=dict(type='dict', required=True)) module = AnsibleModule( argument_spec=module_args, supports_check_mode=False, ) # create our context dict. Every skillet requires a context that contains connection information # as well as any vars required for that particular skillet skillet_context = dict() skillet_context.update(module.params['vars']) skillet_context.update(module.params['provider']) skillet_loader = SkilletLoader(module.params['skillet_path']) skillet = skillet_loader.get_skillet_with_name(module.params['skillet']) if skillet is None: module.fail_json(msg='Could not find Skillet with name {0}'.format( module.params['name'])) # refuse to run any non panos / panorama skillet types if not skillet.type == 'pan_validation': module.fail_json(msg='Invalid Skillet Type') try: output = skillet.execute(skillet_context) output_str = json.dumps(output) for snippet in output['pan_validation']: if not output['pan_validation'][snippet]['results']: module.fail_json(msg=output_str) module.exit_json(changed=False, stdout=output_str) except PanoplyException as p: module.fail_json(msg='{0}'.format(p))
#!/usr/bin/env python3 from skilletlib import SkilletLoader sl = SkilletLoader('.') skillet = sl.get_skillet_with_name('panos_cli_example') context = dict() context['cli_command'] = 'show system info' context['username'] = '******' context['password'] = '******' context['ip_address'] = 'NOPE' output = skillet.execute(context) print(output.get('output_template', 'n/a'))
def test_skillet_includes(): """ Tests to verify the Skillet object is successfully compiled from all included skillets. The 'include_other_skillets' skillet includes snippets and variables from two other skillets in the same directory :return: None """ skillet_path = '../example_skillets/skillet_includes/' skillet_loader = SkilletLoader(path=skillet_path) skillet: Skillet = skillet_loader.get_skillet_with_name( 'include_other_skillets') # verify we can find and load the correct skillet assert skillet.name == 'include_other_skillets' # verify the correct number of snippets. assert len(skillet.snippets) == 14 # Check that parent snippets and variables are brought in correctly not_included_snippet: Snippet = skillet.get_snippet_by_name('parse_config') # verify that non-included snippets are appended to the skillet, from include_other_skillets assert not_included_snippet is not None parent_preserve_variable: dict = skillet.get_variable_by_name( 'shared_base_variable') # verify that the override variables brought up to the parent are preserved assert "toggle_hint" not in parent_preserve_variable # Check that a snippet of this format is brought in correctly # - name: something # include: child_skillet simple_include_snippet: Snippet = skillet.get_snippet_by_name( 'update_schedule.get_update_schedule') # verify that snippet with just include: keyword is brought in properly, from update_schedule assert simple_include_snippet is not None simple_include_variable: dict = skillet.get_variable_by_name( 'some_update_variable') # verify that variable with just include: keyword is brought in properly, from update_schedule assert simple_include_variable is not None # Check that a snippet of this format is brought in correctly # - name: something # include: child_skillet # include_snippets: # - name: snippet_1 # attribute: included_snip_only_snippet: Snippet = skillet.get_snippet_by_name( 'network_profiles.check_network_profiles') # verify we can get an included snippet from the skillet object assert included_snip_only_snippet is not None # verify the 'label' metadata attribute has been overridden correctly assert included_snip_only_snippet.metadata.get( 'label', '') == 'Check Network Profiles Override' included_snip_only_variable: dict = skillet.get_variable_by_name( 'another_variable') # verify the child variable is present in compiled skillet assert included_snip_only_variable is not None # Check that a snippet of this format is brought in correctly # - name: something # include: child_skillet # include_variables: # - name: variable_1 # attribute: included_var_only_snippet: Snippet = skillet.get_snippet_by_name( 'check_zones.ensure_desired_zone') # verify we can get an included snippet from the skillet object assert included_var_only_snippet is not None included_var_only_variable: dict = skillet.get_variable_by_name( 'zone_to_test') # verify the child's default gets overridden because of the parent's attribute definition assert included_var_only_variable["default"] == "untrust" # Check that a snippet of this format is brought in correctly # - name: something # include: child_skillet # include_variables: # - name: all # attribute: merge_snip_snippet: Snippet = skillet.get_snippet_by_name( "qos_class.qos_parse") # verify that the snippets get added to compiled snippet assert merge_snip_snippet is not None merged_override_from_all_variable: dict = skillet.get_variable_by_name( 'qos_class') # verify that shared children variables merge attributes assert merged_override_from_all_variable["toggle_hint"] is not None assert "internet" in merged_override_from_all_variable["toggle_hint"][ "value"] assert "untrust" in merged_override_from_all_variable["toggle_hint"][ "value"] child_override_1_variable: dict = skillet.get_variable_by_name( 'child_1_unique_variable') # verify that child override works assert child_override_1_variable["toggle_hint"] is not None child_override_2_variable: dict = skillet.get_variable_by_name( 'child_2_unique_variable') # verify that child override works assert child_override_2_variable["toggle_hint"] is not None # Added for issue #163 child_skillet: Skillet = skillet_loader.get_skillet_with_name( 'network_profiles') child_snippet: Snippet = child_skillet.get_snippet_by_name( 'check_network_profiles') # ensure using includes / overrides leaves our original skillet definition intact assert child_snippet.metadata.get('label', '') == 'Ensure Named profile exists'