Post-modification call back in python
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-05-2019 12:40 AM - edited 03-01-2019 04:16 AM
Hi all,
we are trying to create a services that configures an SVI via python/template and then in python change the state of the interface to shut or no shut depending on an admin_state field we have added to the yang model of the services.
issue we are having is that we cannot update the state of the interface IE shut or no shut via python as its says the vlan interface is not yet in the CDB
>>> device.config.nx__interface.Vlan["3001"].shutdown.exists()
True
>>>
>>> t = m.start_write_trans()
>>> root = maagic.get_root(t)
>>> device = root.devices.device['N7K-VDC-1']
>>>
>>> device.config.nx__interface.Vlan["3001"].shutdown.exists()
True
>>>
>>> device.config.nx__interface.Vlan["3001"].shutdown.delete()
>>> device.config.nx__interface.Vlan["3001"].shutdown.exists()
False
>>>
>>> t.apply()
>>>
how can we work around this issue without making an additional call
Thanks
Regards
Yale

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-05-2019 01:26 AM
Can you elaborate on the issue - I am not quite sure if I follow what it is you'd like to work that doesn't?
Also, why are you trying to use a post-modification call-back in the first place instead of doing this from your service code?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-05-2019 11:47 PM
the services creates the vlan interface via a template, we did then try to set the interfaces state (shut /no shut) via python but we get this error
File "/opt/nso_custom_python/nso_utils/config_modify_functions.py", line 29, in set_admin_state
device.config.nx__interface.Vlan[vlan_name].shutdown.delete()
File "/opt/ncs/ncs-4.7.2.1/src/ncs/pyapi/ncs/maagic.py", line 1076, in __getitem__
raise KeyError('%s not in %s' % (keystr, self._path))
KeyError: '{3001} not in /ncs:devices/device{N7K-VDC-1}/config/nx:interface/Vlan'
thus we thought we should look at post-mod

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2019 01:09 AM
Okay. So, how do you apply the template and where do you have the python code? Perhaps you have a code snippet or something like that? It seems like you ought to be able to do this as long as you are in the same transaction and just after the template application.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2019 01:27 AM
@service.create
def cb_create(self, tctx, root, service, proplist):
# set all require variables here e.g. ...
vars.add('device', 'foo')
vars.add('vrf_name', 'bar')
# apply the template
template = ncs.template.Template(service)
template.apply(template_name, vars)
# break into python
m = maapi.Maapi()
m.start_user_session('admin','foobar')
t = m.start_write_trans()
root = maagic.get_root(t)
device = root.devices.device['N7K-VDC-1']
device.config.nx__interface.Vlan["3001"].shutdown.create()
t.apply()

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2019 01:43 AM
So the problem here is that you create a new transaction, which does not run inside of the service transaction. In general you almost never want to start a transaction from inside your create code.
Instead you want to re-use the handle you already have available to you. You have an argument named 'root' which is a maagic object that you can use to make your changes, use that directly instead of creating your own root object and things ought to work.
