You can download the Contrail python API Bindings from here
https://github.com/Juniper/contrail-python-api
Install instructions
git clone https://github.com/Juniper/contrail-python-api
cd contrail-python-api
python setup.py install
So the basic example explains how to connect using the Client Method and list Virtual_Networks
import pycontrail.client as client
conn = client.Client(url='http://10.84.34.141:8082',
auth_params={'type':'keystone',
'auth_url':'http://10.84.34.141:5000/v2.0',
'username':'admin',
'password':'secret',
'tenant_name':'admin'})
conn.virtual_networks_list()
I am going to explain how to collect Data that will make some sense, although a lot of python enthusiasts will figure it out but if this is any help id be happy
reason why i wanted to share this script is that i had taken time to write the loops to get all the dict data within lists etc.
I printed all the output in JSON format as final output
from pycontrail import client
import jsonpickle
import jsonpickle
conn = client.Client(
url='http://1.1.1.1:8082',auth_params={'type':'keystone','auth_url':'http://2.2.2.2:5000/v2.0','username':'admin','password':'password','tenant_name':'admin'})
route_instance_uuid_list=[]
bb = conn.routing_instances_list()
for each_route_instance in bb['routing-instances']:
route_instance_uuid_list.append(each_route_instance['uuid'])
route_instance_detail_list=[]
for each_route_instance in route_instance_uuid_list:
vn_detail_dict={'name':None,'route_instance':[],'route_target':[]}
aa= conn.routing_instance_read(id=each_route_instance)
vn_detail_dict['name'] =aa.name
# print aa.name
#route_instance
try:
route_instance=[]
for each_routing in aa.routing_instance_refs:
route_instance_dict={}
route_instance_dict['route_instance']= dict(zip(range(0,len(each_routing['to'])), each_routing['to']))
route_instance.append(route_instance_dict)
except:
pass
vn_detail_dict['route_instance']=route_instance
#route_instance
#route_targets
try:
route_target=[]
for each_routetarget in aa.route_target_refs:
route_target_dict={}
route_target_dict['route_target']= dict(zip(range(0,len(each_routetarget['to'])), each_routetarget['to']))
route_target.append(route_target_dict)
except:
pass
vn_detail_dict['route_target']=route_target
#route_targets
route_instance_detail_list.append(vn_detail_dict)
vn_instance_uuid_list=[]
vn=conn.virtual_networks_list()
for each_vn in vn['virtual-networks']:
vn_instance_uuid_list.append(each_vn['uuid'])
vn_instance_detail_list=[]
for each_vn_instance in vn_instance_uuid_list:
vn_instance_detail_dict={}
gg=conn.virtual_network_read(id=each_vn_instance)
vn_instance_detail_dict['virtual_network_network_id']= gg.virtual_network_network_id
vn_instance_detail_dict['uuid']= gg.uuid
vn_instance_detail_dict['name']= gg.name
vn_instance_detail_dict['parent_name']= gg.parent_name
vn_instance_detail_dict['parent_uuid']= gg.parent_uuid
try:
vn_instance_detail_dict['forwarding_mode']= gg.virtual_network_properties.forwarding_mode
for each_ipam in gg.network_ipam_refs:
ipam_subnet_list=[]
for each_ipam_subnet in each_ipam['attr'].ipam_subnets:
ipam_subnet_dict={}
ipam_subnet_dict['default_gateway']= each_ipam_subnet.default_gateway
ipam_subnet_dict['dns_server_address']= each_ipam_subnet.dns_server_address
ipam_subnet_dict['subnet_uuid']= each_ipam_subnet.subnet_uuid
ipam_subnet_dict['subnet_name'] =each_ipam_subnet.subnet_name
ipam_subnet_dict['ip_prefix_len']= each_ipam_subnet.subnet.ip_prefix_len
ipam_subnet_dict['ip_prefix']= each_ipam_subnet.subnet.ip_prefix
ipam_subnet_list.append(ipam_subnet_dict)
allocation_pool_list=[]
for each_allocation_pool in each_ipam_subnet.allocation_pools:
allocation_pool_dict={}
allocation_pool_dict['start'] =each_allocation_pool.start
allocation_pool_dict['end']= each_allocation_pool.end
allocation_pool_list.append(allocation_pool_dict)
ipam_subnet_dict['allocation_pool']=allocation_pool_list
vn_instance_detail_dict['ipam']=ipam_subnet_list
network_policy_list=[]
for each_network_policy in gg.network_policy_refs:
network_policy_dict={}
network_policy_dict['uuid']= each_network_policy['uuid']
network_policy_dict['network_policy'] =dict(zip(range(0,len(each_network_policy['to'])), each_network_policy['to']))
network_policy_list.append(network_policy_dict)
vn_instance_detail_dict['network_policy']=network_policy_list
except:
pass
vn_instance_detail_list.append(vn_instance_detail_dict)
np_uuid_list=[]
np_list = conn.network_policys_list()
for each_np in np_list['network-policys']:
np_uuid_list.append(each_np['uuid'])
each_np_detail_list=[]
for each_np_uuid in np_uuid_list:
each_np_detail_dict={'policy':[]}
nppp = conn.network_policy_read(id=each_np_uuid)
each_np_detail_dict['uuid']= nppp.uuid
each_np_detail_dict['name']= nppp.name
each_np_detail_dict['parent_name']= nppp.parent_name
try:
each_policy_list=[]
for each_policy_entry in nppp.network_policy_entries.policy_rule:
each_policy_dict={'dst_addresses':[],'src_addresses':[]}
each_policy_dict['protocol']= each_policy_entry.protocol
each_policy_dict['ethertype']= each_policy_entry.ethertype
each_policy_dict['direction']= each_policy_entry.direction
each_policy_dict['rule_uuid']= each_policy_entry.rule_uuid
each_policy_dict['simple_action']= each_policy_entry.action_list.simple_action
each_policy_dict['gateway_name']= each_policy_entry.action_list.gateway_name
dst_address_list=[]
for each_dst_address in each_policy_entry.dst_addresses:
dst_address_dict={}
dst_address_dict['network_policy']= each_dst_address.network_policy
dst_address_dict['security_group']= each_dst_address.security_group
dst_address_dict['subnet']= each_dst_address.subnet
dst_address_dict['virtual_network']= each_dst_address.virtual_network
dst_address_list.append(dst_address_dict)
each_policy_dict['dst_addresses']=dst_address_list
src_address_list=[]
for each_src_address in each_policy_entry.src_addresses:
src_address_dict={}
src_address_dict['network_policy']= each_src_address.network_policy
src_address_dict['security_group']= each_src_address.security_group
src_address_dict['subnet']= each_src_address.subnet
src_address_dict['virtual_network']= each_src_address.virtual_network
src_address_list.append(src_address_dict)
each_policy_dict['src_addresses']=src_address_list
each_policy_list.append(each_policy_dict)
each_np_detail_dict['policy']=each_policy_list
except:
pass
each_np_detail_list.append(each_np_detail_dict)
final_output = {
'virtual_network_instance':vn_instance_detail_list,
'route_instance':route_instance_detail_list,
'network_policy':each_np_detail_list
}
print jsonpickle.pickler.encode(final_output)
Bhatiaji, thank u very much sir. This was very helpful.
ReplyDelete