def _update_port_acl_out_v1(interface_name, acl_name, list_type, **kwargs): """ Perform GET and PUT calls to apply ACL on an L3 interface. This function specifically applies an ACL to Egress traffic of the interface, which must be a routing interface. This function will set the interface to enable routing. :param interface_name: Alphanumeric String that is the name of the interface on which the ACL is applied to :param acl_name: Alphanumeric String that is the name of the ACL :param list_type: Alphanumeric String of ipv4 or ipv6 to specify the type of ACL :param kwargs: keyword s: requests.session object with loaded cookie jar keyword url: URL in main() function :return: True if successful, False otherwise """ port_name_percents = common_ops._replace_special_characters(interface_name) port_data = port.get_port(port_name_percents, depth=0, selector="configuration", **kwargs) # must remove these fields from the data since they can't be modified port_data.pop('name', None) port_data.pop('origin', None) acl_url = "/rest/v1/system/acls/%s/%s" % (acl_name, list_type) if list_type is "ipv6": port_data['aclv6_out_cfg'] = acl_url port_data['aclv6_out_cfg_version'] = random.randint( -9007199254740991, 9007199254740991) elif list_type is "ipv4": port_data['aclv4_out_cfg'] = acl_url port_data['aclv4_out_cfg_version'] = random.randint( -9007199254740991, 9007199254740991) port_data['routing'] = True target_url = kwargs["url"] + "system/ports/%s" % port_name_percents put_data = json.dumps(port_data, sort_keys=True, indent=4) response = kwargs["s"].put(target_url, data=put_data, verify=False) if not common_ops._response_ok(response, "PUT"): logging.warning( "FAIL: Applying ACL '%s' to Egress on Interface '%s' failed with status code %d: %s" % (acl_name, interface_name, response.status_code, response.text)) return False else: logging.info( "SUCCESS: Applying ACL '%s' to Egress on Interface '%s' succeeded" % (acl_name, interface_name)) return True
def _delete_vsx_interface_vlan_v1(vlan_id, **kwargs): """ Perform PUT calls on a VLAN interface to remove VSX IPv4 settings. :param vlan_id: Numeric ID of VLAN to that will be configured :param kwargs: keyword s: requests.session object with loaded cookie jar keyword url: URL in main() function :return: True if successful, False otherwise """ ports_list = port.get_all_ports(**kwargs) vlan_name = "vlan" + str(vlan_id) if "/rest/v1/system/ports/%s" % vlan_name not in ports_list: logging.warning( "FAIL: Deleting VSX information from VLAN Interface '%d' failed " "because VLAN Interface doesn't exist" % vlan_id) return False else: port_data = port.get_port(vlan_name, depth=0, selector="configuration", **kwargs) port_data["vsx_active_forwarding_enable"] = False port_data["vsx_sync"] = [] port_data["vsx_virtual_ip4"] = [] port_data.pop('vsx_virtual_gw_mac_v4', None) port_data.pop( 'name', None ) # must remove this item from the json since name can't be modified port_data.pop( 'origin', None ) # must remove this item from the json since origin can't be modified target_url = kwargs["url"] + "system/ports/%s" % vlan_name put_data = json.dumps(port_data, sort_keys=True, indent=4) response = kwargs["s"].put(target_url, data=put_data, verify=False) if not common_ops._response_ok(response, "PUT"): logging.warning( "FAIL: Deleting VSX information from VLAN Interface '%d' failed with status code %d: %s" % (vlan_id, response.status_code, response.text)) return False else: logging.info( "SUCCESS: Deleting VSX information from VLAN Interface '%d' succeeded" % vlan_id) return True
def _update_ospfv3_interface_authentication_v1(vrf, ospf_id, interface_name, auth_type, digest_key, auth_pass, **kwargs): """ Perform PUT calls to update an Interface with OSPFv3 to have authentication :param vrf: Alphanumeric name of the VRF the OSPFv3 ID belongs to :param ospf_id: OSPFv3 process ID between numbers 1-63 :param interface_name: Alphanumeric name of the interface that will be attached to the OSPFv3 area :param auth_type: Alphanumeric type of authentication, chosen between 'md5', 'null', and 'text' :param digest_key: Integer between 1-255 that functions as the digest key for the authentication method :param auth_pass: Alphanumeric text for the authentication password. Note that this will be translated to a base64 String in the configuration and json. :param kwargs: keyword s: requests.session object with loaded cookie jar keyword url: URL in main() function :return: True if successful, False otherwise """ ports_list = port.get_all_ports(**kwargs) port_name_percents = common_ops._replace_special_characters(interface_name) if "/rest/v1/system/ports/%s" % port_name_percents not in ports_list: port.add_l3_ipv4_port(interface_name, vrf=vrf, **kwargs) port_data = port.get_port(interface_name, depth=0, selector="configuration", **kwargs) # must remove these fields from the data since they can't be modified port_data.pop('name', None) port_data.pop('origin', None) port_data['ospf_auth_type'] = auth_type port_data['ospf_auth_md5_keys'] = {str(digest_key): auth_pass} port_data['ospf_if_type'] = "ospf_iftype_broadcast" port_data['routing'] = True port_data['vrf'] = "/rest/v1/system/vrfs/" + vrf target_url = kwargs["url"] + "system/ports/%s" % port_name_percents put_data = json.dumps(port_data, sort_keys=True, indent=4) response = kwargs["s"].put(target_url, data=put_data, verify=False) if not common_ops._response_ok(response, "PUT"): logging.warning("FAIL: Updating OSPFv3 %s Authentication for Port '%s' failed with status code %d: %s" % (ospf_id, interface_name, response.status_code, response.text)) return False else: logging.info("SUCCESS: Updating OSPFv3 %s Authentication for Port '%s' succeeded" % (ospf_id, interface_name)) return True
def _update_ospf_interface_type_v1(vrf, ospf_id, interface_name, interface_type, **kwargs): """ Perform PUT calls to update the type of OSPFv2 Interface given, as well as enable routing on the interface :param vrf: Alphanumeric name of the VRF the OSPF ID belongs to :param ospf_id: OSPF process ID between numbers 1-63 :param interface_name: Alphanumeric name of the interface that will be attached to the OSPF area :param interface_type: Alphanumeric type of OSPF interface. The options are 'broadcast', 'loopback', 'nbma', 'none', 'pointomultipoint', 'pointopoint', and 'virtuallink' :param kwargs: keyword s: requests.session object with loaded cookie jar keyword url: URL in main() function :return: True if successful, False otherwise """ ports_list = port.get_all_ports(**kwargs) port_name_percents = common_ops._replace_special_characters(interface_name) if "/rest/v1/system/ports/%s" % port_name_percents not in ports_list: port.add_l3_ipv4_port(interface_name, vrf=vrf, **kwargs) port_data = port.get_port(interface_name, depth=0, selector="configuration", **kwargs) # must remove these fields from the data since they can't be modified port_data.pop('name', None) port_data.pop('origin', None) port_data['ospf_if_type'] = "ospf_iftype_%s" % interface_type port_data['routing'] = True port_data['vrf'] = "/rest/v1/system/vrfs/" + vrf target_url = kwargs["url"] + "system/ports/%s" % port_name_percents put_data = json.dumps(port_data, sort_keys=True, indent=4) response = kwargs["s"].put(target_url, data=put_data, verify=False) if not common_ops._response_ok(response, "PUT"): logging.warning("FAIL: Updating OSPF %s interface type for Port '%s' failed with status code %d: %s" % (ospf_id, interface_name, response.status_code, response.text)) return False else: logging.info("SUCCESS: Updating OSPF %s interface type for Port '%s' succeeded" % (ospf_id, interface_name)) return True
def _clear_port_loop_protect_v1(port_name, **kwargs): """ Perform GET and PUT calls to clear a Port's Loop-protect settings :param port_name: Alphanumeric name of the Port :param kwargs: keyword s: requests.session object with loaded cookie jar keyword url: URL in main() function :return: True if successful, False otherwise """ port_name_percents = common_ops._replace_special_characters(port_name) port_data = port.get_port(port_name, depth=0, selector="configuration", **kwargs) port_data.pop('loop_protect_enable', None) port_data.pop('loop_protect_action', None) port_data['loop_protect_vlan'] = [] # must remove these fields from the data since they can't be modified port_data.pop('name', None) port_data.pop('origin', None) target_url = kwargs["url"] + "system/ports/%s" % port_name_percents put_data = json.dumps(port_data, sort_keys=True, indent=4) response = kwargs["s"].put(target_url, data=put_data, verify=False) if not common_ops._response_ok(response, "PUT"): logging.warning( "FAIL: Clearing Loop-protect options on Port '%s' failed with status code %d: %s" % (port_name, response.status_code, response.text)) return False else: logging.info( "SUCCESS: Clearing the Loop-protect options on Port '%s' succeeded" % (port_name)) return True
def _update_port_loop_protect_v1(interface_name, action="", vlan_list=[], **kwargs): """ Perform GET and PUT calls to apply Loop-protect options on an interface. Note that Loop-protect requires that the interface is L2, so this function will also update the interface to reflect that. :param interface_name: Alphanumeric String that is the name of the interface that will apply loop-protect options :param action: Alphanumeric String that will specify the actions for the Loop-protect interface. The options are "do-not-disable", "tx-disable", "tx-rx-disable", or None. :param vlan_list: List of VLANs that will be configured for Loop-protect on the interface :param kwargs: keyword s: requests.session object with loaded cookie jar keyword url: URL in main() function :return: True if successful, False otherwise """ port_name_percents = common_ops._replace_special_characters(interface_name) port_data = port.get_port(port_name_percents, depth=0, selector="configuration", **kwargs) # must remove these fields from the data since they can't be modified port_data.pop('name', None) port_data.pop('origin', None) port_data['loop_protect_enable'] = True # make interface L2 port_data['routing'] = False # strings appended to output prints for status action_output = "" vlan_output = "" if action not in ['do-not-disable', 'tx-disable', 'tx-rx-disable', None]: raise Exception( "ERROR: Action should be 'do-not-disable', 'tx-disable', 'tx-rx-disable' or None" ) elif action: port_data['loop_protect_action'] = action action_output = " with Action %s " % action if vlan_list: vlan_output = " with VLAN(s) [" for vlan in vlan_list: vlan_url = "/rest/v1/system/vlans/%s" % vlan if vlan_url not in port_data['loop_protect_vlan']: port_data['loop_protect_vlan'].append(vlan_url) vlan_output += (" " + str(vlan)) vlan_output += "] " target_url = kwargs["url"] + "system/ports/%s" % port_name_percents put_data = json.dumps(port_data, sort_keys=True, indent=4) response = kwargs["s"].put(target_url, data=put_data, verify=False) if not common_ops._response_ok(response, "PUT"): logging.warning( "FAIL: Applying Loop-protect to Interface '%s'%s%s failed with status code %d: %s" % (interface_name, action_output, vlan_output, response.status_code, response.text)) return False else: logging.info( "SUCCESS: Applying Loop-protect to Interface '%s'%s%s succeeded" % (interface_name, action_output, vlan_output)) return True
def _update_vsx_interface_vlan_v1(vlan_id, active_forwarding, vsx_sync, act_gw_mac, act_gw_ip, **kwargs): """ Perform PUT calls on a VLAN interface to configure VSX IPv4 settings. :param vlan_id: Numeric ID of VLAN to that will be configured :param active_forwarding: True or False Boolean to set VSX active forwarding :param vsx_sync: Set of alphanumeric values to enable VSX configuration synchronization. The options are any combination of 'active-gateways', 'irdp', and 'policies' :param act_gw_mac: Alphanumeric value of the Virtual MAC address for the interface active gateway :param act_gw_ip: Alphanumeric value of the Virtual IP address for the interface active gateway :param kwargs: keyword s: requests.session object with loaded cookie jar keyword url: URL in main() function :return: True if successful, False otherwise """ ports_list = port.get_all_ports(**kwargs) vlan_name = "vlan" + str(vlan_id) if "/rest/v1/system/ports/%s" % vlan_name not in ports_list: logging.warning( "FAIL: Adding VSX information to VLAN Interface '%d' failed because VLAN " "Interface doesn't exist" % vlan_id) return False else: port_data = port.get_port(vlan_name, depth=0, selector="configuration", **kwargs) vsx_sync_set = [] if vsx_sync == None: vsx_sync = {} if "active-gateways" in vsx_sync: vsx_sync_set.append("^vsx_virtual.*") if "irdp" in vsx_sync: vsx_sync_set.append(".irdp.*") if "policies" in vsx_sync: vsx_sync_set.append("^policy.*") port_data["vsx_active_forwarding_enable"] = active_forwarding port_data["vsx_sync"] = vsx_sync_set port_data["vsx_virtual_gw_mac_v4"] = act_gw_mac port_data["vsx_virtual_ip4"] = [act_gw_ip] port_data.pop( 'name', None ) # must remove this item from the json since name can't be modified port_data.pop( 'origin', None ) # must remove this item from the json since origin can't be modified target_url = kwargs["url"] + "system/ports/%s" % vlan_name put_data = json.dumps(port_data, sort_keys=True, indent=4) response = kwargs["s"].put(target_url, data=put_data, verify=False) if not common_ops._response_ok(response, "PUT"): logging.warning( "FAIL: Adding VSX information to VLAN Interface '%d' failed with status code %d: %s" % (vlan_id, response.status_code, response.text)) return False else: logging.info( "SUCCESS: Adding VSX information to VLAN Interface '%d' succeeded" % vlan_id) return True