Skip to content

Commit

Permalink
refactor: all functions in add items now have the same logic
Browse files Browse the repository at this point in the history
  • Loading branch information
bl4ko committed Mar 4, 2024
1 parent 8b5de46 commit 4486012
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 21 deletions.
30 changes: 16 additions & 14 deletions internal/netbox/inventory/add_items.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ func (nbi *NetboxInventory) AddTag(ctx context.Context, newTag *objects.Tag) (*o
// AddContact adds a contact to the local netbox inventory.
func (nbi *NetboxInventory) AddTenant(ctx context.Context, newTenant *objects.Tenant) (*objects.Tenant, error) {
newTenant.Tags = append(newTenant.Tags, nbi.SsotTag)
nbi.TenantsLock.Lock()
defer nbi.TenantsLock.Unlock()
if _, ok := nbi.TenantsIndexByName[newTenant.Name]; ok {
oldTenant := nbi.TenantsIndexByName[newTenant.Name]
diffMap, err := utils.JSONDiffMapExceptID(newTenant, oldTenant, false, nbi.SourcePriority)
Expand Down Expand Up @@ -247,34 +249,34 @@ func (nbi *NetboxInventory) AddContactAssignment(ctx context.Context, newCA *obj
return nbi.ContactAssignmentsIndexByContentTypeAndObjectIDAndContactIDAndRoleID[newCA.ContentType][newCA.ObjectID][newCA.Contact.ID][newCA.Role.ID], nil
}

func (nbi *NetboxInventory) AddCustomField(ctx context.Context, newCf *objects.CustomField) error {
func (nbi *NetboxInventory) AddCustomField(ctx context.Context, newCf *objects.CustomField) (*objects.CustomField, error) {
nbi.CustomFieldsLock.Lock()
defer nbi.CustomFieldsLock.Unlock()
if _, ok := nbi.CustomFieldsIndexByName[newCf.Name]; ok {
oldCustomField := nbi.CustomFieldsIndexByName[newCf.Name]
diffMap, err := utils.JSONDiffMapExceptID(newCf, oldCustomField, false, nbi.SourcePriority)
if err != nil {
return err
return nil, err
}
if len(diffMap) > 0 {
nbi.Logger.Debug(ctx, "Custom field ", newCf.Name, " already exists in Netbox but is out of date. Patching it... ")
patchedCf, err := service.Patch[objects.CustomField](ctx, nbi.NetboxAPI, oldCustomField.ID, diffMap)
if err != nil {
return err
return nil, err
}
nbi.CustomFieldsIndexByName[newCf.Name] = patchedCf
} else {
nbi.Logger.Debug(ctx, "Custom field ", newCf.Name, " already exists in Netbox and is up to date...")
}
} else {
nbi.Logger.Debug(ctx, "Custom field ", newCf.Name, " does not exist in Netbox. Creating it...")
newCf, err := service.Create[objects.CustomField](ctx, nbi.NetboxAPI, newCf)
createdCf, err := service.Create[objects.CustomField](ctx, nbi.NetboxAPI, newCf)
if err != nil {
return err
return nil, err
}
nbi.CustomFieldsIndexByName[newCf.Name] = newCf
nbi.CustomFieldsIndexByName[createdCf.Name] = createdCf
}
return nil
return nbi.CustomFieldsIndexByName[newCf.Name], nil
}

func (nbi *NetboxInventory) AddClusterGroup(ctx context.Context, newCg *objects.ClusterGroup) (*objects.ClusterGroup, error) {
Expand Down Expand Up @@ -345,7 +347,7 @@ func (nbi *NetboxInventory) AddClusterType(ctx context.Context, newClusterType *
return newClusterType, nil
}

func (nbi *NetboxInventory) AddCluster(ctx context.Context, newCluster *objects.Cluster) error {
func (nbi *NetboxInventory) AddCluster(ctx context.Context, newCluster *objects.Cluster) (*objects.Cluster, error) {
newCluster.Tags = append(newCluster.Tags, nbi.SsotTag)

nbi.ClustersLock.Lock()
Expand All @@ -356,27 +358,27 @@ func (nbi *NetboxInventory) AddCluster(ctx context.Context, newCluster *objects.
delete(nbi.OrphanManager[service.ClustersAPIPath], oldCluster.ID)
diffMap, err := utils.JSONDiffMapExceptID(newCluster, oldCluster, false, nbi.SourcePriority)
if err != nil {
return err
return nil, err
}
if len(diffMap) > 0 {
nbi.Logger.Debug(ctx, "Cluster ", newCluster.Name, " already exists in Netbox but is out of date. Patching it...")
patchedCluster, err := service.Patch[objects.Cluster](ctx, nbi.NetboxAPI, oldCluster.ID, diffMap)
if err != nil {
return err
return nil, err
}
nbi.ClustersIndexByName[newCluster.Name] = patchedCluster
} else {
nbi.Logger.Debug(ctx, "Cluster ", newCluster.Name, " already exists in Netbox and is up to date...")
}
} else {
nbi.Logger.Debug(ctx, "Cluster ", newCluster.Name, " does not exist in Netbox. Creating it...")
newCluster, err := service.Create[objects.Cluster](ctx, nbi.NetboxAPI, newCluster)
createdCluster, err := service.Create[objects.Cluster](ctx, nbi.NetboxAPI, newCluster)
if err != nil {
return err
return nil, err
}
nbi.ClustersIndexByName[newCluster.Name] = newCluster
nbi.ClustersIndexByName[createdCluster.Name] = createdCluster
}
return nil
return nbi.ClustersIndexByName[newCluster.Name], nil
}

func (nbi *NetboxInventory) AddDeviceRole(ctx context.Context, newDeviceRole *objects.DeviceRole) (*objects.DeviceRole, error) {
Expand Down
8 changes: 4 additions & 4 deletions internal/netbox/inventory/init_items.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ func (nbi *NetboxInventory) InitCustomFields(ctx context.Context) error {
// - host_memory
// - sourceId - this is used to store the ID of the source object in Netbox (interfaces).
func (nbi *NetboxInventory) InitSsotCustomFields(ctx context.Context) error {
err := nbi.AddCustomField(ctx, &objects.CustomField{
_, err := nbi.AddCustomField(ctx, &objects.CustomField{
Name: constants.CustomFieldSourceName,
Label: constants.CustomFieldSourceLabel,
Type: objects.CustomFieldTypeText,
Expand All @@ -316,7 +316,7 @@ func (nbi *NetboxInventory) InitSsotCustomFields(ctx context.Context) error {
if err != nil {
return err
}
err = nbi.AddCustomField(ctx, &objects.CustomField{
_, err = nbi.AddCustomField(ctx, &objects.CustomField{
Name: constants.CustomFieldSourceIDName,
Label: constants.CustomFieldSourceIDLabel,
Type: objects.CustomFieldTypeText,
Expand All @@ -331,7 +331,7 @@ func (nbi *NetboxInventory) InitSsotCustomFields(ctx context.Context) error {
if err != nil {
return err
}
err = nbi.AddCustomField(ctx, &objects.CustomField{
_, err = nbi.AddCustomField(ctx, &objects.CustomField{
Name: constants.CustomFieldHostCPUCoresName,
Label: constants.CustomFieldHostCPUCoresLabel,
Type: objects.CustomFieldTypeText,
Expand All @@ -346,7 +346,7 @@ func (nbi *NetboxInventory) InitSsotCustomFields(ctx context.Context) error {
if err != nil {
return err
}
err = nbi.AddCustomField(ctx, &objects.CustomField{
_, err = nbi.AddCustomField(ctx, &objects.CustomField{
Name: constants.CustomFieldHostMemoryName,
Label: constants.CustomFieldHostMemoryLabel,
Type: objects.CustomFieldTypeText,
Expand Down
1 change: 1 addition & 0 deletions internal/netbox/inventory/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ type NetboxInventory struct {
IPAdressesIndexByAddress map[string]*objects.IPAddress

// We also store locks for all objects, so inventory can be updated by multiple parallel goroutines
TenantsLock sync.Mutex
TagsLock sync.Mutex
SitesLock sync.Mutex
ContactRolesLock sync.Mutex
Expand Down
2 changes: 1 addition & 1 deletion internal/source/ovirt/ovirt_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func (o *OVirtSource) syncClusters(nbi *inventory.NetboxInventory) error {
Site: clusterSite,
Tenant: clusterTenant,
}
err := nbi.AddCluster(o.Ctx, nbCluster)
_, err := nbi.AddCluster(o.Ctx, nbCluster)
if err != nil {
return fmt.Errorf("failed to add oVirt cluster %s as Netbox cluster: %v", clusterName, err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/source/vmware/vmware_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (vc *VmwareSource) syncClusters(nbi *inventory.NetboxInventory) error {
Site: clusterSite,
Tenant: clusterTenant,
}
err := nbi.AddCluster(vc.Ctx, nbCluster)
_, err := nbi.AddCluster(vc.Ctx, nbCluster)
if err != nil {
return fmt.Errorf("failed to add vmware cluster %s as Netbox cluster: %v", clusterName, err)
}
Expand Down Expand Up @@ -708,7 +708,7 @@ func (vc *VmwareSource) syncVms(nbi *inventory.NetboxInventory) error {
} else {
fieldName = utils.Alphanumeric(fieldName)
if _, ok := nbi.CustomFieldsIndexByName[fieldName]; !ok {
err := nbi.AddCustomField(vc.Ctx, &objects.CustomField{
_, err := nbi.AddCustomField(vc.Ctx, &objects.CustomField{
Name: fieldName,
Type: objects.CustomFieldTypeText,
CustomFieldUIVisible: &objects.CustomFieldUIVisibleIfSet,
Expand Down

0 comments on commit 4486012

Please sign in to comment.