cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1361
Views
5
Helpful
4
Replies

Referencing nested lists in python

jordanmeador
Level 1
Level 1

I am creating a service module and have a number of nested lists. I am running into a number of errors with my python code while trying to reference a leaf that is 2 lists deep:

 

list Configurator {
  list IPVPN {
    leaf vrf_name {

 

I have the following python configured to take characters in the middle of the vrf_name leaf and assign them to a variable vrf_serial:

 

def cb_create(self, tctx, root, service, proplist):
self.log.info('Service create(service=', service._path, ')')
vrf_name = service.IPVPN[vrf_name]
vrf_number=vrf_name[7:12]
vars = ncs.template.Variables()
vars.add('vrf_serial', vrf_number)
template = ncs.template.Template(service)
template.apply('Configurator-template', vars)

 

When I try to configure the service, I get the following error when viewing the native config:

errors: reason: Python cb_create error. local variable 'vrf_name' referenced before assignment

 

What am I missing in the python code?

 

1 Accepted Solution

Accepted Solutions

All your service inputs are provided to you through the 'service' cb_create() argument.

You don't get a new variable name for each node in your service model, but instead you get them nested under one argument which is 'service'.

 

As mentioned before, if you want access to vrf_name, you'd need to access list IPVPN first, and you can do that by iterating the list.

 

for vpn in service.IPVPN:
  vrf_name = vpn.vrf_name
  # or
  vrf_name = vpn['vrf_name']

If you know the ID (key value) for a specific list entry, you can also access it directly, but I suppose this is not the case here (vrf-name is the key?).

 

vpn_key = 'foo'
some_leaf = service.IPVPN[vpn_key].some_leaf

View solution in original post

4 Replies 4

tcragg1
Cisco Employee
Cisco Employee

You are trying to use the variable vrf_name as the key to select a list element from the IPVPN list, but you haven't defined what vrf_name is before that point as far as python is concerned. I think you will need to iterate over the elements in the IPVPN list with a 'for' loop in python for it to access the vrf_name leaf values.

The vrf_name variable is gettting filled out when building the service in NSO. I can call a variable in the top-level list, but not in the one underneath it. 

list Configurator {
...
leaf UA_EVC_ID {
mandatory true;
type string;
...
list IPVPN {
description "this will create an instance of an ipvpn service";
key evc_id;
leaf evc_id {
mandatory true;
type string;
...
leaf vrf_name {
mandatory true;
type string;
}

 

If I call the UA_EVC_ID, I can get that to pass through and have the characters extracted from it. But if i call vrf_name, it says its not loaded. Would I have to load the IPVPN list first and then grab the vrf_name out of there?

 

 

All your service inputs are provided to you through the 'service' cb_create() argument.

You don't get a new variable name for each node in your service model, but instead you get them nested under one argument which is 'service'.

 

As mentioned before, if you want access to vrf_name, you'd need to access list IPVPN first, and you can do that by iterating the list.

 

for vpn in service.IPVPN:
  vrf_name = vpn.vrf_name
  # or
  vrf_name = vpn['vrf_name']

If you know the ID (key value) for a specific list entry, you can also access it directly, but I suppose this is not the case here (vrf-name is the key?).

 

vpn_key = 'foo'
some_leaf = service.IPVPN[vpn_key].some_leaf

That got it! 

 

for vpn in service.IPVPN:
  vrf_name = vpn.vrf_name
  vrf_number=vrf_name[6:12]

 

Thanks so much for the help!

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the NSO Developer community: