Creating a mock API endpoint for testing
NetBrain is a tool we use at work for CMDB, documentation, and network troubleshooting. NetBrain exposes an API which gives tools like Nornir access to dynamically generated inventory and group objects. However the modules, don’t exist yet; I am writing one.
The issue is the NetBrain system sits on an access restricted network in our remote data center, and I want to write code at home without being on VPN. By using Python and Flask, I was able to write a mock NetBrain API, that simulates connecting to the remote server. This approach has the added benefit of not using any resources on the production server while I work on the code and perform debugging.
By changing the Flask route decorator with the path to the API resource and returning the appropriate information, this concept can be extended to any project where mock API access is needed. For example, the login API: /ServicesAPI/API/V1/Session
does not have to perform any “real” login functions. The data passed into the API is accepted, but ignored. What is important is the API returns the same data structure as the real system, in this case a token
object with a value. None of the data has to be accurate, it only has to contain something to pass into the next function.
Code to simulate NetBrain API:
|
|
Run the Flask app:
export FLASK_APP=./mock-api.py
flash run