Hello,
with the following yang, I defined a device with a list of vlans (id, ip_add and description).
augment /ncs:services {
list rfs_router_intf {
tailf:info "RFS interfaces vlan";
key rfs_name;
leaf rfs_name {
type string;
}
uses ncs:service-data;
ncs:servicepoint "rfs-router-intf";
leaf rfs_device_name {
type leafref {
tailf:info "________ Nom du switch";
path "/ncs:devices/ncs:device/ncs:name";
}
}
list rfs_vlan {
key rfs_vlan_id;
tailf:info "Liste des devices";
unique "rfs_ip_add";
leaf rfs_vlan_id {
tailf:info "________ Vlan number";
type uint32;
}
leaf rfs_ip_add {
tailf:info "________ adresse IP";
type string;
}
leaf rfs_description {
tailf:info "________ Description";
type string;
}
}
}
I want to customize the description using Python. I tried to loop wihtin Python, but only the last description was applied to all vlans.
for vlan in service.rfs_vlan:
desc = vlan.rfs_description
vars.add('DESCRIPTION', 'NSO - RFS - ' + str(desc))
template.apply('rfs-router-intf-template', vars)
Actually, I think that every vars.add("DESCRIPTION",..) was overwriting the previous one.
So I tried to loop in the template as follow:
<config-template xmlns="http://tail-f.com/ns/config/1.0"> <!-- Pas de servicepoint, sinon il override celui du Python, et le python n'est pas exécuté -->
<devices xmlns="http://tail-f.com/ns/ncs">
<device tags="merge">
<name>{rfs_device_name}</name>
<config>
<?foreach {/rfs_vlan}?>
<interface xmlns="http://tail-f.com/ned/cisco-nx">
<Vlan>
<name>{rfs_vlan_id}</name>
<!-- <description>{rfs_description}</description> -->
<description>{$DESCRIPTION}}</description>
<ip>
<address>
<ipaddr>{rfs_ip_add}</ipaddr>
</address>
</ip>
</Vlan>
</interface>
<?end?>
</config>
</device>
</devices>
</config-template>
The thing is I don't know how to access to the description in Python to modify it !!
As service is supposed to be a list, I tried several things, but none work.
@service.create
def cb_create(self, tctx, root, service, proplist):
self.log.info('Service create(service=', service._path, ')')
template = ncs.template.Template(service)
vars = ncs.template.Variables()
# tested, but failed
#desc = service.rfs_vlan[0]
#desc = service.rfs_vlan["rfs_description"]
#desc = service["rfs-router-intf"]["rfs_vlan"]
vars.add('DESCRIPTION', 'NSO - RFS - ' + str(desc))
template.apply('rfs-router-intf-template', vars)
What to I miss ?