From e1ced6eaa682e914e625b82bbf372f4c2927e9cc Mon Sep 17 00:00:00 2001 From: bl4ko Date: Fri, 15 Mar 2024 10:50:33 +0100 Subject: [PATCH] fix: problem with cluster groups not being deleted by orphan manager --- internal/netbox/inventory/init_items.go | 24 ++++++++++++++++++++++-- internal/netbox/inventory/inventory.go | 6 ++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/internal/netbox/inventory/init_items.go b/internal/netbox/inventory/init_items.go index 0fb0728..679071d 100644 --- a/internal/netbox/inventory/init_items.go +++ b/internal/netbox/inventory/init_items.go @@ -178,6 +178,26 @@ func (nbi *NetboxInventory) InitSites(ctx context.Context) error { return nil } +// InitDefaultSite inits default site, which is used for hosts that have no corresponding site. +// This is because site is required for adding new hosts. +func (nbi *NetboxInventory) InitDefaultSite(ctx context.Context) error { + _, err := nbi.AddSite(ctx, &objects.Site{ + NetboxObject: objects.NetboxObject{ + Tags: []*objects.Tag{nbi.SsotTag}, + Description: "Default netbox-ssot site used for all hosts, that have no site matched.", + CustomFields: map[string]string{ + constants.CustomFieldSourceName: nbi.SsotTag.Name, + }, + }, + Name: constants.DefaultSite, + Slug: utils.Slugify(constants.DefaultSite), + }) + if err != nil { + return fmt.Errorf("init default site: %s", err) + } + return nil +} + // Collects all manufacturers from Netbox API and store them in NetBoxInventory. func (nbi *NetboxInventory) InitManufacturers(ctx context.Context) error { nbManufacturers, err := service.GetAll[objects.Manufacturer](ctx, nbi.NetboxAPI, "") @@ -374,13 +394,13 @@ func (nbi *NetboxInventory) InitClusterGroups(ctx context.Context) error { // Initialize internal index of cluster groups by name nbi.ClusterGroupsIndexByName = make(map[string]*objects.ClusterGroup) // OrphanManager takes care of all cluster groups created by netbox-ssot - nbi.OrphanManager[constants.DeviceRolesAPIPath] = make(map[int]bool, 0) + nbi.OrphanManager[constants.ClusterGroupsAPIPath] = make(map[int]bool, 0) for i := range nbClusterGroups { clusterGroup := &nbClusterGroups[i] nbi.ClusterGroupsIndexByName[clusterGroup.Name] = clusterGroup if slices.IndexFunc(clusterGroup.Tags, func(t *objects.Tag) bool { return t.Slug == nbi.SsotTag.Slug }) >= 0 { - nbi.OrphanManager[constants.DeviceRolesAPIPath][clusterGroup.ID] = true + nbi.OrphanManager[constants.ClusterGroupsAPIPath][clusterGroup.ID] = true } } nbi.Logger.Debug(ctx, "Successfully collected cluster groups from Netbox: ", nbi.ClusterGroupsIndexByName) diff --git a/internal/netbox/inventory/inventory.go b/internal/netbox/inventory/inventory.go index b5a6cb3..3ee6144 100644 --- a/internal/netbox/inventory/inventory.go +++ b/internal/netbox/inventory/inventory.go @@ -157,8 +157,9 @@ func NewNetboxInventory(ctx context.Context, logger *logger.Logger, nbConfig *pa 11: constants.DeviceRolesAPIPath, 12: constants.ClustersAPIPath, 13: constants.ClusterTypesAPIPath, - 14: constants.ContactAssignmentsAPIPath, - 15: constants.ContactsAPIPath, + 14: constants.ClusterGroupsAPIPath, + 15: constants.ContactAssignmentsAPIPath, + 16: constants.ContactsAPIPath, } nbi := &NetboxInventory{Ctx: ctx, Logger: logger, NetboxConfig: nbConfig, SourcePriority: sourcePriority, OrphanManager: make(map[string]map[int]bool), OrphanObjectPriority: orphanObjectPriority} return nbi @@ -183,6 +184,7 @@ func (nbi *NetboxInventory) Init() error { nbi.InitContactAssignments, nbi.InitTenants, nbi.InitSites, + nbi.InitDefaultSite, nbi.InitManufacturers, nbi.InitPlatforms, nbi.InitDevices,