Skip to content

Commit

Permalink
exclude object classes from pruning #18
Browse files Browse the repository at this point in the history
refs: #18
  • Loading branch information
bb-Ricardo committed Nov 27, 2020
1 parent dcf47bc commit 98661fc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
10 changes: 6 additions & 4 deletions module/netbox/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,9 @@ def prune_data(self):
today = datetime.now()
for nb_object_sub_class in reversed(NetBoxObject.__subclasses__()):

if getattr(nb_object_sub_class, "prune", False) is False:
continue

for this_object in self.inventory.get_all_items(nb_object_sub_class):

if this_object.source is not None:
Expand Down Expand Up @@ -800,12 +803,11 @@ def just_delete_all_the_things(self):

for nb_object_sub_class in reversed(NetBoxObject.__subclasses__()):

# tags need to be deleted at the end
if nb_object_sub_class == NBTag:
if getattr(nb_object_sub_class, "prune", False) is False:
continue

# object has no tags so we can't be sure it was created with this program
if NBTagList not in nb_object_sub_class.data_model.values():
# tags need to be deleted at the end
if nb_object_sub_class == NBTag:
continue

for this_object in self.inventory.get_all_items(nb_object_sub_class):
Expand Down
12 changes: 10 additions & 2 deletions module/netbox/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,16 @@ def tag_all_the_things(self, netbox_handler):
# if object was tagged by this program in previous runs but is not present
# anymore then add the orphaned tag
else:
if netbox_handler.primary_tag in this_object.get_tags():
this_object.add_tags(netbox_handler.orphaned_tag)
if getattr(this_object, "prune", False) is True:
if netbox_handler.primary_tag in this_object.get_tags():
this_object.add_tags(netbox_handler.orphaned_tag)

# or just remove primary tag if pruning is disabled
else:
if netbox_handler.primary_tag in this_object.get_tags():
this_object.remove_tags(netbox_handler.primary_tag)
if netbox_handler.orphaned_tag in this_object.get_tags():
this_object.remove_tags(netbox_handler.orphaned_tag)

def query_ptr_records_for_all_ips(self):
"""
Expand Down
18 changes: 18 additions & 0 deletions module/netbox/object_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,7 @@ class NBTag(NetBoxObject):
name = "tag"
api_path = "extras/tags"
primary_key = "name"
prune = False
data_model = {
"name": 100,
"slug": 100,
Expand Down Expand Up @@ -789,6 +790,7 @@ class NBTenant(NetBoxObject):
name = "tenant"
api_path = "tenancy/tenants"
primary_key = "name"
prune = False
data_model = {
"name": 30,
"slug": 50,
Expand All @@ -802,6 +804,7 @@ class NBSite(NetBoxObject):
name = "site"
api_path = "dcim/sites"
primary_key = "name"
prune = False
data_model = {
"name": 50,
"slug": 50,
Expand All @@ -815,6 +818,7 @@ class NBVRF(NetBoxObject):
name = "VRF"
api_path = "ipam/vrfs"
primary_key = "name"
prune = False
data_model = {
"name": 50,
"description": 200,
Expand All @@ -829,6 +833,7 @@ class NBVLAN(NetBoxObject):
primary_key = "vid"
secondary_key = "name"
enforce_secondary_key = True
prune = False
data_model = {
"vid": int,
"name": 64,
Expand Down Expand Up @@ -905,6 +910,7 @@ class NBPrefix(NetBoxObject):
name = "IP prefix"
api_path = "ipam/prefixes"
primary_key = "prefix"
prune = False
data_model = {
"prefix": [IPv4Network, IPv6Network],
"site": NBSite,
Expand Down Expand Up @@ -936,6 +942,7 @@ class NBManufacturer(NetBoxObject):
name = "manufacturer"
api_path = "dcim/manufacturers"
primary_key = "name"
prune = False
data_model = {
"name": 50,
"slug": 50,
Expand All @@ -947,6 +954,7 @@ class NBDeviceType(NetBoxObject):
name = "device type"
api_path = "dcim/device-types"
primary_key = "model"
prune = False
data_model = {
"model": 50,
"slug": 50,
Expand All @@ -961,6 +969,7 @@ class NBPlatform(NetBoxObject):
name = "platform"
api_path = "dcim/platforms"
primary_key = "name"
prune = False
data_model = {
"name": 100,
"slug": 100,
Expand All @@ -973,6 +982,7 @@ class NBClusterType(NetBoxObject):
name = "cluster type"
api_path = "virtualization/cluster-types"
primary_key = "name"
prune = False
data_model = {
"name": 50,
"slug": 50,
Expand All @@ -984,6 +994,7 @@ class NBClusterGroup(NetBoxObject):
name = "cluster group"
api_path = "virtualization/cluster-groups"
primary_key = "name"
prune = False
data_model = {
"name": 50,
"slug": 50,
Expand All @@ -995,6 +1006,7 @@ class NBDeviceRole(NetBoxObject):
name = "device role"
api_path = "dcim/device-roles"
primary_key = "name"
prune = False
data_model = {
"name": 50,
"slug": 50,
Expand All @@ -1008,6 +1020,7 @@ class NBCluster(NetBoxObject):
name = "cluster"
api_path = "virtualization/clusters"
primary_key = "name"
prune = False
data_model = {
"name": 100,
"comments": str,
Expand All @@ -1030,6 +1043,7 @@ class NBDevice(NetBoxObject):
api_path = "dcim/devices"
primary_key = "name"
secondary_key = "site"
prune = True
data_model = {
"name": 64,
"device_type": NBDeviceType,
Expand Down Expand Up @@ -1059,6 +1073,7 @@ class NBVM(NetBoxObject):
api_path = "virtualization/virtual-machines"
primary_key = "name"
secondary_key = "cluster"
prune = True
data_model = {
"name": 64,
"status": ["offline", "active", "planned", "staged", "failed", "decommissioning"],
Expand All @@ -1082,6 +1097,7 @@ class NBVMInterface(NetBoxObject):
primary_key = "name"
secondary_key = "virtual_machine"
enforce_secondary_key = True
prune = True
data_model = {
"name": 64,
"virtual_machine": NBVM,
Expand All @@ -1102,6 +1118,7 @@ class NBInterface(NetBoxObject):
primary_key = "name"
secondary_key = "device"
enforce_secondary_key = True
prune = True
data_model = {
"name": 64,
"device": NBDevice,
Expand All @@ -1125,6 +1142,7 @@ class NBIPAddress(NetBoxObject):
api_path = "ipam/ip-addresses"
primary_key = "address"
is_primary = False
prune = True
data_model = {
"address": str,
"assigned_object_type": ["dcim.interface", "virtualization.vminterface"],
Expand Down

0 comments on commit 98661fc

Please sign in to comment.