Skip to content

Commit

Permalink
fix: problem with cluster groups not being deleted by orphan manager
Browse files Browse the repository at this point in the history
  • Loading branch information
bl4ko committed Mar 15, 2024
1 parent 2efb255 commit e1ced6e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
24 changes: 22 additions & 2 deletions internal/netbox/inventory/init_items.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "")
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions internal/netbox/inventory/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -183,6 +184,7 @@ func (nbi *NetboxInventory) Init() error {
nbi.InitContactAssignments,
nbi.InitTenants,
nbi.InitSites,
nbi.InitDefaultSite,
nbi.InitManufacturers,
nbi.InitPlatforms,
nbi.InitDevices,
Expand Down

0 comments on commit e1ced6e

Please sign in to comment.