Example: Changing an interface from layer 3 to layer 2

The following example is an excerpt of Python code that changes an existing interface from a layer 3 interface to a layer 2 interface.

To convert the interface, the Python code in this excerpt does the following:

  1. Uses a GET request to get the configuration attributes of all the ports in the default VRF.

    In the configuration database, the set of ports are represented as a table.

  2. Creates a JSON dictionary of the ports and their configuration attributes.

  3. Removes the target port from the dictionary.

    The port variable was defined using the standard port notation, which uses slashes. However, in REST URIs, the slash must be replaced by its percent-encoded equivalent, %2F. Therefore the variable used to represent the port in this code is convert_port instead of port.

  4. Uses a PUT request to replace the port table in the configuration database with the modified information in the JSON dictionary.

    When this step completes, the layer 3 port 1/1/1 is removed from the configuration of the switch.

  5. Defines the layer 2 port JSON data that will be used when creating the layer 2 port.

    When specifying the port in JSON data, you use the standard port notation using slashes, so the port variable is used instead of the convert_port variable.

  6. Creates a layer 2 interface by using a POST request to add the target port to the /system/ports resource.

The code also handles logging into the switch, handling the session cookie, disabling switch certificate verification for every request (using the verify=False parameter), and logging out of the switch. The code in this excerpt uses the requests library.

import requests
import urllib3

def main():
    ip_address = ""
    credentials = {"username": "admin", "password": "admin123"}
    base_url = "https://{0}/rest/v1/".format(ip_address)
    # the target port
    port = "1/1/1"
    # port string must be converted
    convert_port = port.replace("/", "%2F")
    # requests session so cookie is persisted between calls
    s = requests.Session()
    vlan_mode = "access"
    vlan = 99
        # login
        s.post(base_url + "login", params=credentials, verify=False)
        # GET the existing layer 3 port table
        l3_ports = s.get(base_url + "system/vrfs/default?selector=configuration", verify=False)
        # create a dictionary from the l3 port table
        l3_dict = l3_ports.json()
        # remove the target port from the layer 3 port table dictionary
        # PUT call to return the amended layer 3 port table to the switch
        s.put(base_url + "system/vrfs/default", json=l3_dict, verify=False)
        # layer 2 data for the target port
        l2port_data = {
            "referenced_by": "/rest/v1/system/bridge",
            "name": port,
            "vlan_mode": vlan_mode,
            "vlan_tag": "/rest/v1/system/bridge/vlans/" + str(vlan),
            "interfaces": ["/rest/v1/system/interfaces/{}".format(convert_port)],
            "admin": "up"
        # POST new layer 2 target port
        new_int = s.post(base_url + "system/ports", json=l2port_data, verify=False)
        # logout
        s.post(base_url + "logout", verify=False)

if __name__ == "__main__":