I noticed on github that there was a tutorial on how to access the REST API on Juniper Contrail.
https://juniper.github.io/contrail-vnc/api-doc/html/tutorial_with_rest.html#
The easiest way to access this is by using cURL. Contrail uses tcp port 8082 for accessing it's REST API.
The url http:/contrail-ip/virtual-networks prints out a list of configured virtual networks on Contrail.
$ curl -X GET -H "Content-Type: application/json; charset=UTF-8" http://172.16.1.4:8082/virtual-networks | python -mjson.tool
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1229 100 1229 0 0 2129 0 --:--:-- --:--:-- --:--:-- 2129
{
"virtual-networks": [
{
"fq_name": [
"default-domain",
"default-project",
"__link_local__"
],
"href": "http://172.16.1.4:8082/virtual-network/4092df7b-997a-4ee7-a5cc-46d5db1187d4",
"uuid": "4092df7b-997a-4ee7-a5cc-46d5db1187d4"
},
{
"fq_name": [
"default-domain",
"default-project",
"default-virtual-network"
],
"href": "http://172.16.1.4:8082/virtual-network/34579f9a-064e-4048-96a7-a30355c54e44",
"uuid": "34579f9a-064e-4048-96a7-a30355c54e44"
},
{
"fq_name": [
"default-domain",
"default-project",
"ip-fabric"
],
"href": "http://172.16.100.104:8082/virtual-network/db7d1afe-bcaa-456b-b33a-9a36f6d176fe",
"uuid": "db7d1afe-bcaa-456b-b33a-9a36f6d176fe"
},
{
"fq_name": [
"default-domain",
"demo",
"Network1"
],
"href": "http://172.16.1.4:8082/virtual-network/380c0f4e-34b6-4901-98b2-61a8ed7afd7d",
"uuid": "380c0f4e-34b6-4901-98b2-61a8ed7afd7d"
},
{
"fq_name": [
"default-domain",
"demo",
"Network2"
],
"href": "http://172.16.100.104:8082/virtual-network/ffe03354-aaa2-4305-b615-654e14111134",
"uuid": "ffe03354-aaa2-4305-b615-654e14111134"
},
{
"fq_name": [
"default-domain",
"demo",
"Network3"
],
"href": "http://172.16.1.4:8082/virtual-network/93317422-eea4-4b77-88cb-5aaac3bb58b6",
"uuid": "93317422-eea4-4b77-88cb-5aaac3bb58b6"
}
]
}
However cURL, at least for me, isn’t capable of abstracting the data structures programatically. It's more like screen scraping. So I dug around the internet and noticed that python has a curl module.
http://pycurl.sourceforge.net/doc/quickstart.html#
Now I can use python to execute cURL to pull data from a REST API.
There are a few parts to this script.
The first part is a function to issue the cURL command.
The second part is to abstract the route-target from a virtual network created in the Contrail controller.
This could then be used later on with another script to create a template configuration and program the underlay gateway router with a VRF.
In this script I cheated a little by grabbing a specific virtual-network’s url address from the previous curl command. Then parsed the data to grab the information I was looking for.
—————————————
import pycurl
import StringIO
import json
#Function to issue cURL command
def get_url(WEB):
buf = StringIO.StringIO()
c = pycurl.Curl()
c.setopt(c.URL, WEB)
c.setopt(c.WRITEFUNCTION, buf.write)
c.perform()
body = buf.getvalue()
network = json.loads(body)
buf.close()
return network
#URL of virtual network on contrail
SITE = 'http://172.16.1.4:8082/virtual-network/380c0f4e-34b6-4901-98b2-61a8ed7afd7d'
objects = get_url(SITE)
#pretty print the json results
print json.dumps(objects, sort_keys=True, indent=4)
#This part is to grab the path of the Virtual Network name and RT from contrail
print "Name: ", objects['virtual-network']['name'], " RT: ", objects['virtual-network']['route_target_list']['route_target'][0]
Script in action:
——————
$ python contrail.py
{
"virtual-network": {
"fq_name": [
"default-domain",
"demo",
"Network1"
],
"href": "http://172.16.1.4:8082/virtual-network/380c0f4e-34b6-4901-98b2-61a8ed7afd7d",
"id_perms": {
"created": "2014-09-19T19:19:11.650288",
"description": null,
"enable": true,
"last_modified": "2014-09-27T05:22:30.453524",
"permissions": {
"group": "cloud-admin-group",
"group_access": 7,
"other_access": 7,
"owner": "cloud-admin",
"owner_access": 7
},
"uuid": {
"uuid_lslong": 11002964217786203517,
"uuid_mslong": 4038619794410719489
}
},
"instance_ip_back_refs": [
{
"attr": null,
"href": "http://172.16.1.4:8082/instance-ip/17b19dc0-b177-4df7-955b-57b8a87caf28",
"to": [
"17b19dc0-b177-4df7-955b-57b8a87caf28"
],
"uuid": "17b19dc0-b177-4df7-955b-57b8a87caf28"
},
{
"attr": null,
"href": "http://172.16.1.4:8082/instance-ip/d525ddce-3542-4986-b8d1-56f3831d8678",
"to": [
"d525ddce-3542-4986-b8d1-56f3831d8678"
],
"uuid": "d525ddce-3542-4986-b8d1-56f3831d8678"
}
],
"is_shared": false,
"name": "Network1",
"network_ipam_refs": [
{
"attr": {
"ipam_subnets": [
{
"default_gateway": "100.1.1.254",
"subnet": {
"gw": "100.1.1.254",
"ip_prefix": "100.1.1.0",
"ip_prefix_len": 24
}
}
]
},
"href": "http://172.16.1.4:8082/network-ipam/1f24fa35-b7bf-4d0f-8185-746f58e234c9",
"to": [
"default-domain",
"demo",
"Network1-ipam"
],
"uuid": "1f24fa35-b7bf-4d0f-8185-746f58e234c9"
}
],
"network_policy_refs": [
{
"attr": {
"sequence": {
"major": 0,
"minor": 0
},
"timer": null
},
"href": "http://172.16.1.4:8082/network-policy/0a1c1776-a323-4c00-959a-aada50b91be8",
"to": [
"default-domain",
"demo",
"net1<->net2"
],
"uuid": "0a1c1776-a323-4c00-959a-aada50b91be8"
}
],
"parent_href": "http://172.16.1.4:8082/project/3af66afe-3284-40cc-8b04-85c69af512c7",
"parent_type": "project",
"parent_uuid": "3af66afe-3284-40cc-8b04-85c69af512c7",
"route_target_list": {
"route_target": [
"target:64512:100"
]
},
"router_external": false,
"routing_instances": [
{
"href": "http://172.16.1.4:8082/routing-instance/06165761-3b55-417d-acf0-0ad27a9010d0",
"to": [
"default-domain",
"demo",
"Network1",
"Network1"
],
"uuid": "06165761-3b55-417d-acf0-0ad27a9010d0"
}
],
"uuid": "380c0f4e-34b6-4901-98b2-61a8ed7afd7d",
"virtual_machine_interface_back_refs": [
{
"attr": null,
"href": "http://172.16.1.4:8082/virtual-machine-interface/2aefccdc-bf34-4d3b-bd37-41f716274883",
"to": [
"e432ad9a-8f6e-4f7c-813b-18ada76bfd64",
"2aefccdc-bf34-4d3b-bd37-41f716274883"
],
"uuid": "2aefccdc-bf34-4d3b-bd37-41f716274883"
},
{
"attr": null,
"href": "http://172.16.1.4:8082/virtual-machine-interface/f20bb145-49cd-43bf-a6e5-9d9c50794244",
"to": [
"2e775d6f-8043-41df-9295-d1b8d8f705a2",
"f20bb145-49cd-43bf-a6e5-9d9c50794244"
],
"uuid": "f20bb145-49cd-43bf-a6e5-9d9c50794244"
}
],
"virtual_network_properties": {
"extend_to_external_routers": null,
"forwarding_mode": "l2_l3",
"network_id": 4,
"vxlan_network_identifier": null
}
}
}
Name: Network1 RT: target:64512:100
As you can see I was able to pull the "name" of the virtual network and the route-target of the virtual network. Later I can then create a script template to build the VRF on the Router Gateway like a MX. That's one step closer to network automation.
No comments:
Post a Comment