F5 XC reviewing API requests which the GUI sends and a backup of the config with Python/Ansible
Short Description
The F5 XC Distributed Cloud GUI in the background sends API requests with JSON body to the system and those requests can be easily reviewed.
Problem solved by this Code Snippet
If someone wonders how to do some tasks that the XC GUI does the same way but with automation through the API and JSON then this article will help them. Also at the end I have shown how to retrive XC json data with API.
How to use this Code Snippet
Reviewing the API requests that are generated by the XC GUI.
Full Code Snippet
There are 3 ways to review the API requests that the GUI generates.
- On each XC element like for example the load balancer you can click on the JSON and see the JSON code. The JSON code can even be directly edited from the GUI Dashboard!
- The API documentation can be reviewed directly from the XC GUI.
- The final option is just to use the browser developer tools and to see what API requests are send by the F5 XC. This feature is now present on most F5 new products like F5OS(Velos/rSeries) and F5 NEXT 😉
The XC JSON created objects from the API are a form of a backup configuration. Even if the objects were created from the GUI then API GET requests can be used to retrive their JSON data and this can be saved to a backup file in the form of snapshot.
I have used Python with requests library and the url and API key are added as user input arguments. The script can be used to get information like the XC LB or service policies. As example "/api/config/namespaces/default/service_policys". The script will first call an API endpoint to get for example all the load balancer or service policy names and then it will use the names get the config for each individual service policy or load balancer, using a for loop. There is time.sleep(1) to add 1 second slowness between each api request.
The code can have the full url like https://{tenant_name}.console.ves.volterra.io/api/config/namespaces/{namespace}service_policys and the api token be added during script execution or the arguments can be input at the start of script execution by commenting out url = sys.argv[1] and api_token = sys.argv[2] and executing the script like python3 service_policy.py {argument1} {argument2}.
The api token by default is hidden using the getpass library for extra security.
See Github for the code:
Nikoolayy1/xc_api_script: XC API script to retrieve basic json config (github.com)
In some cases using Terraform for XC will be best as XC has strong Terraform support as seen at the link below. Ansible can also be used but XC does not have many developed Ansible modules, so in many cases the Ansible URI module will need to be used and the Ansible URI module (ansible.builtin.uri module – Interacts with webservices — Ansible Documentation) in the backgroung is just the python requests or http.client module, as Ansible is python in the background, so better use python directly in that case.
XC Terraform:
Terraform | F5 Distributed Cloud Tech Docs
F5 Distributed Cloud WAAP deployment with Terrafor... - DevCentral
Using Terraform and F5® Distributed Cloud Mesh to ... - DevCentral
Example Ansible code (even if I said python is better in this case 😀)
xc_api_script/xc_ansible at main · Nikoolayy1/xc_api_script (github.com)
There is a new XC API portal to test API directly and there is no need postman etc. Just select your tenant and add your token F5 Distributed Cloud Dev Portal (volterra.io)
F5 have also released a backup script for XC Distributed Cloud Configuration at https://github.com/f5devcentral/f5-professional-services/tree/main/tools/f5-xc-backup