Difference between revisions of "ZenDMD Tip - Recreate Device Rels"

From Zenoss Wiki
Jump to: navigation, search
(Created page with "Lets say d is your device object and you set its location relationship using the syntax d.location = '/NY' In the process you've destroyed the location relationship and repla...")
 
Line 12: Line 12:
  
 
if not getattr(d, "_relations", False): return
 
if not getattr(d, "_relations", False): return
relnames = d.getRelationshipNames()
+
    relnames = d.getRelationshipNames()
for name, schema in d._relations:
+
        for name, schema in d._relations:
try:
+
            try:
d._setObject(name, schema.createRelation(name))
+
                d._setObject(name, schema.createRelation(name))
except:
+
            except:
pass
+
                pass
  
 
d.location.__class__ will show it's a relationship and now d.setLocation('/') will work.
 
d.location.__class__ will show it's a relationship and now d.setLocation('/') will work.

Revision as of 18:23, 17 July 2012

Lets say d is your device object and you set its location relationship using the syntax d.location = '/NY'

In the process you've destroyed the location relationship and replaced it with a string! Functions like d.setLocation('/') won't work on this object type.

d.location.__class__ will show it's a string.

To delete the string, del d.location

d.location.__class__ will show result in an error since d.location doesnt exist.

Let's recreate the device relationships using it's schema by:

if not getattr(d, "_relations", False): return

   relnames = d.getRelationshipNames()
       for name, schema in d._relations:
           try:
               d._setObject(name, schema.createRelation(name))
            except:
               pass

d.location.__class__ will show it's a relationship and now d.setLocation('/') will work.

This will work for any relationship on a device object.

Best, --Shane