Best Python code snippet using lisa_python
pn_vrouter_ospf6.py
Source:pn_vrouter_ospf6.py
1#!/usr/bin/python2# Copyright: (c) 2018, Pluribus Networks3# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)4from __future__ import absolute_import, division, print_function5__metaclass__ = type6ANSIBLE_METADATA = {'metadata_version': '1.1',7 'status': ['preview'],8 'supported_by': 'community'}9DOCUMENTATION = """10---11module: pn_vrouter_ospf612author: "Pluribus Networks (@rajaspachipulusu17)"13version_added: "2.8"14short_description: CLI command to add/remove vrouter-ospf615description:16 - This module can be used to add interface ip to OSPF6 protocol17 or remove interface ip from OSPF6 protocol on vRouter.18options:19 pn_cliswitch:20 description:21 - Target switch to run the CLI on.22 required: false23 type: str24 state:25 description:26 - State the action to perform. Use C(present) to add vrouter-ospf6 and27 C(absent) to remove interface from vrouter-ospf6.28 required: true29 type: str30 choices: ['present', 'absent']31 pn_ospf6_area:32 description:33 - area id for this interface in IPv4 address format.34 required: false35 type: str36 pn_nic:37 description:38 - OSPF6 control for this interface.39 required: false40 type: str41 pn_vrouter_name:42 description:43 - name of service config.44 required: false45 type: str46"""47EXAMPLES = """48- name: Add vrouter interface nic to ospf649 pn_vrouter_ospf6:50 pn_cliswitch: "sw01"51 state: "present"52 pn_vrouter_name: "foo-vrouter"53 pn_nic: "eth0.4092"54 pn_ospf6_area: "0.0.0.0"55- name: Remove vrouter interface nic to ospf656 pn_vrouter_ospf6:57 pn_cliswitch: "sw01"58 state: "absent"59 pn_vrouter_name: "foo-vrouter"60 pn_nic: "eth0.4092"61"""62RETURN = """63command:64 description: the CLI command run on the target node.65 returned: always66 type: str67stdout:68 description: set of responses from the vrouter-ospf6 command.69 returned: always70 type: list71stderr:72 description: set of error responses from the vrouter-ospf6 command.73 returned: on error74 type: list75changed:76 description: indicates whether the CLI caused changes on the target.77 returned: always78 type: bool79"""80from ansible.module_utils.basic import AnsibleModule81from ansible.module_utils.network.netvisor.pn_nvos import pn_cli, run_cli82from ansible.module_utils.network.netvisor.netvisor import run_commands83def check_cli(module, cli):84 """85 This method checks if vRouter exists on the target node.86 This method also checks for idempotency using the vrouter-interface-show87 command.88 If the given vRouter exists, return VROUTER_EXISTS as True else False.89 If nic_str exists on the given vRouter, return NIC_EXISTS as True else90 False. This is required for vrouter-ospf6-remove.91 :param module: The Ansible module to fetch input parameters92 :param cli: The CLI string93 :return Booleans: VROUTER_EXISTS, NIC_EXISTS94 """95 vrouter_name = module.params['pn_vrouter_name']96 nic_str = module.params['pn_nic']97 # Check for vRouter98 check_vrouter = cli + ' vrouter-show format name no-show-headers '99 out = run_commands(module, check_vrouter)[1]100 out = out.split()101 VROUTER_EXISTS = True if vrouter_name in out else False102 if nic_str:103 # Check for nic104 show = cli + ' vrouter-ospf6-show vrouter-name %s format nic no-show-headers' % vrouter_name105 out = run_commands(module, show)[1]106 NIC_EXISTS = True if nic_str in out else False107 return VROUTER_EXISTS, NIC_EXISTS108def main():109 """ This section is for arguments parsing """110 state_map = dict(111 present='vrouter-ospf6-add',112 absent='vrouter-ospf6-remove'113 )114 module = AnsibleModule(115 argument_spec=dict(116 pn_cliswitch=dict(required=False, type='str'),117 state=dict(required=True, type='str',118 choices=state_map.keys()),119 pn_ospf6_area=dict(required=False, type='str'),120 pn_nic=dict(required=False, type='str'),121 pn_vrouter_name=dict(required=False, type='str'),122 ),123 required_if=(124 ["state", "present", ["pn_vrouter_name", "pn_nic",125 "pn_ospf6_area"]],126 ["state", "absent", ["pn_vrouter_name", "pn_nic"]]127 ),128 )129 # Accessing the arguments130 cliswitch = module.params['pn_cliswitch']131 state = module.params['state']132 ospf6_area = module.params['pn_ospf6_area']133 nic = module.params['pn_nic']134 vrouter_name = module.params['pn_vrouter_name']135 command = state_map[state]136 # Building the CLI command string137 cli = pn_cli(module, cliswitch)138 VROUTER_EXISTS, NIC_EXISTS = check_cli(module, cli)139 if VROUTER_EXISTS is False:140 module.fail_json(141 failed=True,142 msg='vRouter %s does not exist' % vrouter_name143 )144 cli += ' %s vrouter-name %s ' % (command, vrouter_name)145 if command == 'vrouter-ospf6-add':146 if NIC_EXISTS is True:147 module.exit_json(148 skipped=True,149 msg='OSPF6 with nic %s already exist' % nic150 )151 if nic:152 cli += ' nic %s' % nic153 if ospf6_area:154 cli += ' ospf6-area %s ' % ospf6_area155 if command == 'vrouter-ospf6-remove':156 if NIC_EXISTS is False:157 module.exit_json(158 skipped=True,159 msg='OSPF6 with nic %s does not exist' % nic160 )161 if nic:162 cli += ' nic %s' % nic163 run_cli(module, cli, state_map)164if __name__ == '__main__':...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!