RESTCONF 102: Generate RESTCONF API calls with Python

In this Learning Lab, you will learn the basics of how to leverage a RESTCONF API in Python.

Objective

Completion Time: 30 minutes

  • Understand how to leverage a RESTCONF API from Python
  • Explore ways to parse and use JSON returned from a RESTCONF API

Prerequisites

You will want to make sure you have gone through the Coding 101 lab if you are unfamiliar with retrieving results from a RESTful service. Also, this lab includes steps in the How to Setup Your Computer section to setup your development environment if needed.

Background

  • You will also want to make sure you have gone through the RESTCONF 101 lab if you are unfamiliar with the RESTCONF protocol.

  • If you are unfamiliar with Python, you may complete the Coding 10X and Coding 20X learning lab exercises.

  • This lab shall shall use a virtual router CSR1000V as a programmable device running in DevNet's Always On Sandbox.

Access to a Device that Supports a RESTCONF API

  • If you are working from your own machine, you can use the DevNet Sandbox Always-On CSR1000V Device: ios-xe-mgmt.cisco.com.
    • This is a single CSR1000v to which we can send RESTCONF API calls

Python

  • To run the code samples, you need to have Python 3 installed on your machine.
  • Note though, that the examples were tested on both Python 3.4 and 2.7, so you should be able to run the sample code on either version.

Python Libraries

  • These codes samples use the Requests library to simplify making RESTCONF API calls and NetAddr to manage IP addresses.
  • See How to Set Up Your Own Computer section above for how to install the Requests and NetAddr Libraries on your own machine.

Clone Git Repo

  • If you are working on a DevNet Learning Lab PC at a DevNet event
    • Open the Git Command window by clicking on the Git CMD icon on the Task Bar or click on the Start button, then in the Run bar type: git cmd and press the Enter key.
  • If you are working from your own workstation, on your desktop open a terminal.
  • Clone the RESTCONF example code
    • This GitHub repository provides sample code that you can run for this learning lab
      git clone https://github.com/CiscoDevNet/restconf-examples.git
      

Step 1. Setting the stage for device programmability

We are going to use the CSR1000v running IOS XE 3.17 to interface with example RESTCONF APIs in this lab. Before we begin, see the diagram below to set the stage for our device programmability exercises.

Per the diagram above, we shall use some example code to simulate an orchestrator that makes RESTCONF API calls against a network device. In the next step of the lab, we shall make our first RESTCONF API call using Python!