Creating ZenDMD Tip - Delete List Of Locations

From Zenoss Wiki
This is the approved revision of this page, as well as being the most recent.
Jump to: navigation, search

Initiated from http://community.zenoss.org/message/73758#73758

Lets say you have a large (or small I suppose) list of locations that you want to delete. The Web UI is less than desirable for this so you can use zendmd to pull this off.

loc_names_to_delete = ("Location1", "Location2")
for loc in dmd.Locations:
    if loc in loc_names_to_delete:
        dmd.Locations.manage_deleteOrganizer(loc)

For the skeptics in the audience, here you can see it in action.

Welcome to the Zenoss dmd command shell!
'dmd' is bound to the DataRoot. 'zhelp()' to get a list of commands.
Use TAB-TAB to see a list of zendmd related commands.
Tab completion also works for objects -- hit tab after an object name and '.'
 (eg dmd. + tab-key).
>>> # Show current Locations
>>> print dmd.Locations.getOrganizerNames()
['/', '/Location1', '/Location2', '/Location3']
>>>
>>> # Now define the locations you want to delete
>>> loc_names_to_delete = ("Location1", "Location2")
>>>
>>> # Now loop through the locations and delete the ones
>>> # you no longer want
>>> for loc in dmd.Locations:
...     if loc in loc_names_to_delete:
...         dmd.Locations.manage_deleteOrganizer(loc)
...
>>> # Now display your locations
>>> print dmd.Locations.getOrganizerNames()
['/', '/Location3']
>>>
>>> # And finally commit your deletes
>>> commit()
>>>
>>>