<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Referencing nested lists in python in NSO Developer Hub Discussions</title>
    <link>https://community.cisco.com/t5/nso-developer-hub-discussions/referencing-nested-lists-in-python/m-p/3959223#M4518</link>
    <description>&lt;P&gt;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.&lt;/P&gt;</description>
    <pubDate>Fri, 15 Nov 2019 10:57:15 GMT</pubDate>
    <dc:creator>tcragg1</dc:creator>
    <dc:date>2019-11-15T10:57:15Z</dc:date>
    <item>
      <title>Referencing nested lists in python</title>
      <link>https://community.cisco.com/t5/nso-developer-hub-discussions/referencing-nested-lists-in-python/m-p/3958900#M4517</link>
      <description>&lt;P&gt;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:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;list Configurator {&lt;BR /&gt;&amp;nbsp; list IPVPN {&lt;BR /&gt;&amp;nbsp; &amp;nbsp; leaf vrf_name {&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;def cb_create(self, tctx, root, service, proplist):&lt;BR /&gt;self.log.info('Service create(service=', service._path, ')')&lt;BR /&gt;vrf_name = service.IPVPN[vrf_name]&lt;BR /&gt;vrf_number=vrf_name[7:12]&lt;BR /&gt;vars = ncs.template.Variables()&lt;BR /&gt;vars.add('vrf_serial', vrf_number)&lt;BR /&gt;template = ncs.template.Template(service)&lt;BR /&gt;template.apply('Configurator-template', vars)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When I try to configure the service, I get the following error when viewing the native config:&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;errors: reason: Python cb_create error. local variable 'vrf_name' referenced before assignment&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;What am I missing in the python code?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Nov 2019 20:38:11 GMT</pubDate>
      <guid>https://community.cisco.com/t5/nso-developer-hub-discussions/referencing-nested-lists-in-python/m-p/3958900#M4517</guid>
      <dc:creator>jordanmeador</dc:creator>
      <dc:date>2019-11-14T20:38:11Z</dc:date>
    </item>
    <item>
      <title>Re: Referencing nested lists in python</title>
      <link>https://community.cisco.com/t5/nso-developer-hub-discussions/referencing-nested-lists-in-python/m-p/3959223#M4518</link>
      <description>&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Nov 2019 10:57:15 GMT</pubDate>
      <guid>https://community.cisco.com/t5/nso-developer-hub-discussions/referencing-nested-lists-in-python/m-p/3959223#M4518</guid>
      <dc:creator>tcragg1</dc:creator>
      <dc:date>2019-11-15T10:57:15Z</dc:date>
    </item>
    <item>
      <title>Re: Referencing nested lists in python</title>
      <link>https://community.cisco.com/t5/nso-developer-hub-discussions/referencing-nested-lists-in-python/m-p/3959398#M4520</link>
      <description>&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;&lt;P&gt;list Configurator {&lt;BR /&gt;...&lt;BR /&gt;leaf UA_EVC_ID {&lt;BR /&gt;mandatory true;&lt;BR /&gt;type string;&lt;BR /&gt;...&lt;BR /&gt;list IPVPN {&lt;BR /&gt;description "this will create an instance of an ipvpn service";&lt;BR /&gt;key evc_id;&lt;BR /&gt;leaf evc_id {&lt;BR /&gt;mandatory true;&lt;BR /&gt;type string;&lt;BR /&gt;...&lt;BR /&gt;leaf vrf_name {&lt;BR /&gt;mandatory true;&lt;BR /&gt;type string;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Nov 2019 16:32:59 GMT</pubDate>
      <guid>https://community.cisco.com/t5/nso-developer-hub-discussions/referencing-nested-lists-in-python/m-p/3959398#M4520</guid>
      <dc:creator>jordanmeador</dc:creator>
      <dc:date>2019-11-15T16:32:59Z</dc:date>
    </item>
    <item>
      <title>Re: Referencing nested lists in python</title>
      <link>https://community.cisco.com/t5/nso-developer-hub-discussions/referencing-nested-lists-in-python/m-p/3959815#M4521</link>
      <description>&lt;P&gt;All your service inputs are provided to you through the 'service' cb_create() argument.&lt;/P&gt;
&lt;P&gt;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'.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;for vpn in service.IPVPN:
  vrf_name = vpn.vrf_name
  # or
  vrf_name = vpn['vrf_name']&lt;/PRE&gt;
&lt;P&gt;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?).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;vpn_key = 'foo'
some_leaf = service.IPVPN[vpn_key].some_leaf&lt;/PRE&gt;</description>
      <pubDate>Sun, 17 Nov 2019 07:01:14 GMT</pubDate>
      <guid>https://community.cisco.com/t5/nso-developer-hub-discussions/referencing-nested-lists-in-python/m-p/3959815#M4521</guid>
      <dc:creator>yfherzog</dc:creator>
      <dc:date>2019-11-17T07:01:14Z</dc:date>
    </item>
    <item>
      <title>Re: Referencing nested lists in python</title>
      <link>https://community.cisco.com/t5/nso-developer-hub-discussions/referencing-nested-lists-in-python/m-p/3985345#M4522</link>
      <description>&lt;P&gt;That got it!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;for vpn in service.IPVPN:&lt;BR /&gt;&amp;nbsp; vrf_name = vpn.vrf_name&lt;BR /&gt;&amp;nbsp; vrf_number=vrf_name[6:12]&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks so much for the help!&lt;/P&gt;</description>
      <pubDate>Mon, 18 Nov 2019 16:23:30 GMT</pubDate>
      <guid>https://community.cisco.com/t5/nso-developer-hub-discussions/referencing-nested-lists-in-python/m-p/3985345#M4522</guid>
      <dc:creator>jordanmeador</dc:creator>
      <dc:date>2019-11-18T16:23:30Z</dc:date>
    </item>
  </channel>
</rss>

