Exemplo n.º 1
0
def _update_ospf_interface_type(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
    """
    interface_name_percents = common_ops._replace_special_characters(interface_name)

    interface_data = interface.get_interface(interface_name, depth=1, selector="writable", **kwargs)

    interface_data['ospf_if_type'] = "ospf_iftype_%s" % interface_type
    interface_data['routing'] = True
    interface_data['vrf'] = "/rest/v10.04/system/vrfs/" + vrf

    target_url = kwargs["url"] + "system/interfaces/%s" % interface_name_percents
    put_data = json.dumps(interface_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 Interface '%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 Interface '%s' succeeded" % (ospf_id, interface_name))
        return True
Exemplo n.º 2
0
def _update_port_acl_out(interface_name, acl_name, list_type, **kwargs):
    """
    Perform GET and PUT calls to apply ACL on an 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 name of the interface on which the ACL is applied to
    :param acl_name: Alphanumeric 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)
    int_data = interface.get_interface(port_name_percents,
                                       depth=1,
                                       selector="writable",
                                       **kwargs)

    acl_key = "{},{}".format(acl_name, list_type)
    acl_value = "/rest/v10.04/system/acls/" + acl_key

    if interface_name.startswith('lag'):
        if int_data['interfaces']:
            int_data['interfaces'] = common_ops._dictionary_to_list_values(
                int_data['interfaces'])

    if list_type is "ipv6":
        int_data['aclv6_out_cfg'] = acl_value
        int_data['aclv6_out_cfg_version'] = random.randint(
            -9007199254740991, 9007199254740991)
    elif list_type is "ipv4":
        int_data['aclv4_out_cfg'] = acl_value
        int_data['aclv4_out_cfg_version'] = random.randint(
            -9007199254740991, 9007199254740991)

    int_data['routing'] = True

    target_url = kwargs["url"] + "system/interfaces/%s" % port_name_percents
    put_data = json.dumps(int_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
Exemplo n.º 3
0
def _clear_port_loop_protect(interface_name, **kwargs):
    """
    Perform GET and PUT calls to clear an Interface's Loop-protect settings

    :param interface_name: Alphanumeric name of 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
    """
    int_name_percents = common_ops._replace_special_characters(interface_name)

    int_data = interface.get_interface(interface_name,
                                       depth=1,
                                       selector="writable",
                                       **kwargs)

    if interface_name.startswith('lag'):
        if int_data['interfaces']:
            int_data['interfaces'] = common_ops._dictionary_to_list_values(
                int_data['interfaces'])

    if int_data['vlan_trunks']:
        int_data['vlan_trunks'] = common_ops._dictionary_to_list_values(
            int_data['vlan_trunks'])

    int_data['loop_protect_enable'] = None
    int_data['loop_protect_action'] = "tx-disable"
    int_data['loop_protect_vlan'] = []

    target_url = kwargs["url"] + "system/interfaces/%s" % int_name_percents
    put_data = json.dumps(int_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 Interface '%s' failed with status code %d: %s"
            % (interface_name, response.status_code, response.text))
        return False
    else:
        logging.info(
            "SUCCESS: Clearing the Loop-protect options on Interface '%s' succeeded"
            % (interface_name))
        return True
Exemplo n.º 4
0
def _update_l2_lag_interface(name, mc_lag=False, fallback_enabled=False, **kwargs):
    """
    Perform GET and PUT calls to update the Interface table entry for L2 LAG interface.

    :param name: Alphanumeric name of LAG Port
    :param mc_lag: Boolean to determine if the LAG is multi-chassis. Defaults to False if not specified.
    :param fallback_enabled: Boolean to determine if the LAG uses LACP fallback. Defaults to False if not specified.
    :param kwargs:
        keyword s: requests.session object with loaded cookie jar
        keyword url: URL in main() function
    :return: True if successful, False otherwise
    """

    int_name_percents = common_ops._replace_special_characters(name)

    int_data = interface.get_interface(name, 1, "writable", **kwargs)

    if int_data['interfaces']:
        int_data['interfaces'] = common_ops._dictionary_to_list_values(int_data['interfaces'])

    if int_data['vlan_trunks']:
        int_data['vlan_trunks'] = common_ops._dictionary_to_list_values(int_data['vlan_trunks'])

    if int_data['vlan_tag']:
        # Convert the dictionary to a URI string
        int_data['vlan_tag'] = common_ops._dictionary_to_string(int_data['vlan_tag'])

    int_data['other_config']['mclag_enabled'] = mc_lag
    int_data['other_config']['lacp-fallback'] = fallback_enabled

    target_url = kwargs["url"] + "system/interfaces/%s" % int_name_percents
    put_data = json.dumps(int_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 LAG Interface entry '%s' "
                        "failed with status code %d: %s" % (name, response.status_code, response.text))
        return False
    else:
        logging.info("SUCCESS: Updating LAG Interface entry '%s' "
                     "succeeded" % name)
        return True
Exemplo n.º 5
0
def _delete_vsx_interface_vlan(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
    """
    ints_list = interface.get_all_interfaces(**kwargs)
    vlan_name = "vlan" + str(vlan_id)

    if vlan_name not in ints_list:
        logging.warning(
            "FAIL: Deleting VSX information to VLAN Interface '%d' failed because "
            "VLAN Interface doesn't exist" % vlan_id)
        return False
    else:
        interface_vsx_data = interface.get_interface(vlan_name,
                                                     depth=2,
                                                     selector="writable",
                                                     **kwargs)

        interface_vsx_data["vsx_active_forwarding_enable"] = None
        interface_vsx_data["vsx_sync"] = None
        interface_vsx_data["vsx_virtual_gw_mac_v4"] = None
        interface_vsx_data["vsx_virtual_ip4"] = []

        target_url = kwargs["url"] + "system/interfaces/" + vlan_name
        put_data = json.dumps(interface_vsx_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
Exemplo n.º 6
0
def _update_ospf_interface_authentication(vrf, ospf_id, interface_name, auth_type, digest_key, auth_pass, **kwargs):
    """
    Perform PUT calls to update an Interface with OSPF to have authentication

    :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 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
    """
    interface_name_percents = common_ops._replace_special_characters(interface_name)

    interface_data = interface.get_interface(interface_name, depth=1, selector="writable", **kwargs)

    interface_data['ospf_auth_type'] = auth_type
    interface_data['ospf_auth_md5_keys'] = {str(digest_key): auth_pass}
    interface_data['ospf_if_type'] = "ospf_iftype_broadcast"
    interface_data['routing'] = True
    interface_data['vrf'] = "/rest/v10.04/system/vrfs/%s" % vrf

    target_url = kwargs["url"] + "system/interfaces/%s" % interface_name_percents
    put_data = json.dumps(interface_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 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 OSPF %s Authentication for Port '%s' succeeded" % (ospf_id, interface_name))
        return True
Exemplo n.º 7
0
def _update_port_loop_protect(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
    """
    int_name_percents = common_ops._replace_special_characters(interface_name)
    int_data = interface.get_interface(int_name_percents,
                                       depth=1,
                                       selector="writable",
                                       **kwargs)

    if interface_name.startswith('lag'):
        if int_data['interfaces']:
            int_data['interfaces'] = common_ops._dictionary_to_list_values(
                int_data['interfaces'])

    if int_data['vlan_trunks']:
        int_data['vlan_trunks'] = common_ops._dictionary_to_list_values(
            int_data['vlan_trunks'])
    if int_data['loop_protect_vlan']:
        int_data['loop_protect_vlan'] = common_ops._dictionary_to_list_values(
            int_data['loop_protect_vlan'])

    int_data['loop_protect_enable'] = True
    # make interface L2
    int_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:
        int_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/v10.04/system/vlans/%s" % vlan
            if vlan_url not in int_data['loop_protect_vlan']:
                int_data['loop_protect_vlan'].append(vlan_url)
                vlan_output += (str(vlan) + " ")
        vlan_output += "] "

    target_url = kwargs["url"] + "system/interfaces/%s" % int_name_percents
    put_data = json.dumps(int_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
Exemplo n.º 8
0
def _update_vsx_interface_vlan(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
    """
    ints_list = interface.get_all_interfaces(**kwargs)
    vlan_name = "vlan" + str(vlan_id)

    if vlan_name not in ints_list:
        logging.warning(
            "FAIL: Adding VSX information to VLAN Interface '%d' failed because "
            "VLAN Interface doesn't exist" % vlan_id)
        return False
    else:
        interface_vsx_data = interface.get_interface(vlan_name,
                                                     depth=1,
                                                     selector="writable",
                                                     **kwargs)

        vsx_sync_set = []
        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.*")

        if interface_vsx_data['vrf']:
            # Convert the dictionary to a URI string
            interface_vsx_data['vrf'] = list(
                interface_vsx_data['vrf'].values())[0]

        interface_vsx_data["vsx_active_forwarding_enable"] = active_forwarding
        interface_vsx_data["vsx_sync"] = vsx_sync_set
        interface_vsx_data["vsx_virtual_gw_mac_v4"] = act_gw_mac
        interface_vsx_data["vsx_virtual_ip4"] = [act_gw_ip]

        target_url = kwargs["url"] + "system/interfaces/" + vlan_name
        put_data = json.dumps(interface_vsx_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