Skip to content

Commit

Permalink
Fixing logic with custom fields, and defining logic for has priority …
Browse files Browse the repository at this point in the history
…for slices
  • Loading branch information
bl4ko committed Feb 14, 2024
1 parent 9e8484c commit 3cbd74b
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 105 deletions.
3 changes: 3 additions & 0 deletions internal/constants/symbols.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ const (
CustomFieldSourceName = "source"
CustomFieldSourceLabel = "Source"

CustomFieldSourceIdName = "source_id"
CustomFieldSourceIdLabel = "Source ID"

CustomFieldHostCpuCoresName = "host_cpu_cores"
CustomFieldHostCpuCoresLabel = "Host CPU cores"

Expand Down
19 changes: 17 additions & 2 deletions internal/netbox/inventory/init_items.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (nbi *NetboxInventory) InitAdminContactRole() error {
NetboxObject: objects.NetboxObject{
Description: "Auto generated contact role by netbox-ssot for admins of vms.",
CustomFields: map[string]string{
constants.CustomFieldHostCpuCoresLabel: nbi.SsotTag.Name,
constants.CustomFieldSourceName: nbi.SsotTag.Name,
},
},
Name: objects.AdminContactRoleName,
Expand Down Expand Up @@ -310,7 +310,22 @@ func (netboxInventory *NetboxInventory) InitSsotCustomFields() error {
DisplayWeight: 100,
Description: "Source origin of the object",
SearchWeight: 1000,
ContentTypes: []string{"dcim.device", "dcim.devicerole", "dcim.devicetype", "dcim.interface", "dcim.location", "dcim.manufacturer", "dcim.platform", "dcim.region", "dcim.site", "ipam.ipaddress", "ipam.vlangroup", "ipam.vlan", "ipam.prefix", "tenancy.tenantgroup", "tenancy.tenant", "tenancy.contact", "tenancy.contactassignment", "tenancy.contactgroup", "virtualization.cluster", "virtualization.clustergroup", "virtualization.clustertype", "virtualization.virtualmachine", "virtualization.vminterface"},
ContentTypes: []string{"dcim.device", "dcim.devicerole", "dcim.devicetype", "dcim.interface", "dcim.location", "dcim.manufacturer", "dcim.platform", "dcim.region", "dcim.site", "ipam.ipaddress", "ipam.vlangroup", "ipam.vlan", "ipam.prefix", "tenancy.tenantgroup", "tenancy.tenant", "tenancy.contact", "tenancy.contactassignment", "tenancy.contactgroup", "tenancy.contactrole", "virtualization.cluster", "virtualization.clustergroup", "virtualization.clustertype", "virtualization.virtualmachine", "virtualization.vminterface"},
})
if err != nil {
return err
}
err = netboxInventory.AddCustomField(&objects.CustomField{
Name: constants.CustomFieldSourceIdName,
Label: constants.CustomFieldSourceLabel,
Type: objects.CustomFieldTypeText,
FilterLogic: objects.FilterLogicLoose,
CustomFieldUIVisible: &objects.CustomFieldUIVisibleAlways,
CustomFieldUIEditable: &objects.CustomFieldUIEditableYes,
DisplayWeight: 100,
Description: "Source ID of the object",
SearchWeight: 1000,
ContentTypes: []string{"dcim.device", "dcim.devicerole", "dcim.devicetype", "dcim.interface", "dcim.location", "dcim.manufacturer", "dcim.platform", "dcim.region", "dcim.site", "ipam.ipaddress", "ipam.vlangroup", "ipam.vlan", "ipam.prefix", "tenancy.tenantgroup", "tenancy.tenant", "tenancy.contact", "tenancy.contactassignment", "tenancy.contactgroup", "tenancy.contactrole", "virtualization.cluster", "virtualization.clustergroup", "virtualization.clustertype", "virtualization.virtualmachine", "virtualization.vminterface"},
})
if err != nil {
return err
Expand Down
5 changes: 0 additions & 5 deletions internal/netbox/objects/dcim.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,6 @@ type Device struct {
// Priority
// Additional comments.
Comments string `json:"comments,omitempty"`

// CustomFields is a dictionary of custom fields defined for the device type. map[customFieldName]: valueStr
CustomFields map[string]string `json:"custom_fields,omitempty"`
}

func (d Device) String() string {
Expand Down Expand Up @@ -406,8 +403,6 @@ type Interface struct {
TaggedVlans []*Vlan `json:"tagged_vlans,omitempty"`
// UntaggedVlan
UntaggedVlan *Vlan `json:"untagged_vlan,omitempty"`
// CustomFields that can be added to a device. We use source_id custom field to store the id of the interface in the source system.
CustomFields map[string]string `json:"custom_fields,omitempty"`
}

func (i Interface) String() string {
Expand Down
3 changes: 0 additions & 3 deletions internal/netbox/objects/virtualization.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,6 @@ type VM struct {

// Additional Comments
Comments string `json:"comments,omitempty"`

// Custom fields
CustomFields map[string]string `json:"custom_fields,omitempty"`
}

func (vm VM) String() string {
Expand Down
2 changes: 1 addition & 1 deletion internal/source/dnac/dnac_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ func (ds *DnacSource) SyncDeviceInterfaces(nbi *inventory.NetboxInventory) error
NetboxObject: objects.NetboxObject{
Tags: ds.CommonConfig.SourceTags,
CustomFields: map[string]string{
constants.CustomFieldHostCpuCoresName: ds.SourceConfig.Name,
constants.CustomFieldSourceName: ds.SourceConfig.Name,
},
},
Address: fmt.Sprintf("%s/%d", iface.IPv4Address, defaultMask),
Expand Down
26 changes: 11 additions & 15 deletions internal/source/ovirt/ovirt_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,9 @@ func (o *OVirtSource) syncHosts(nbi *inventory.NetboxInventory) error {
Description: hostDescription,
Tags: o.CommonConfig.SourceTags,
CustomFields: map[string]string{
constants.CustomFieldSourceName: o.SourceConfig.Name,
constants.CustomFieldSourceName: o.SourceConfig.Name,
constants.CustomFieldHostCpuCoresName: hostCpuCores,
constants.CustomFieldHostMemoryName: fmt.Sprintf("%d GB", mem),
},
},
Name: hostName,
Expand All @@ -310,10 +312,6 @@ func (o *OVirtSource) syncHosts(nbi *inventory.NetboxInventory) error {
SerialNumber: hostSerialNumber,
AssetTag: hostAssetTag,
DeviceType: hostDeviceType,
CustomFields: map[string]string{
"host_cpu_cores": hostCpuCores,
"host_memory": fmt.Sprintf("%d GB", mem),
},
}
nbHost, err = nbi.AddDevice(nbHost)
if err != nil {
Expand Down Expand Up @@ -443,18 +441,16 @@ func (o *OVirtSource) syncHostNics(nbi *inventory.NetboxInventory, ovirtHost *ov
Tags: o.CommonConfig.SourceTags,
Description: nicComment,
CustomFields: map[string]string{
constants.CustomFieldSourceName: o.SourceConfig.Name,
constants.CustomFieldSourceName: o.SourceConfig.Name,
constants.CustomFieldSourceIdName: nicId,
},
},
Device: nbHost,
Name: nicName,
Speed: objects.InterfaceSpeed(nicSpeedKbps),
Status: nicEnabled,
MTU: int(nicMtu),
Type: nicType,
CustomFields: map[string]string{
"source_id": nicId,
},
Device: nbHost,
Name: nicName,
Speed: objects.InterfaceSpeed(nicSpeedKbps),
Status: nicEnabled,
MTU: int(nicMtu),
Type: nicType,
TaggedVlans: nicTaggedVlans,
}

Expand Down
43 changes: 20 additions & 23 deletions internal/source/vmware/vmware_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,9 @@ func (vc *VmwareSource) syncHosts(nbi *inventory.NetboxInventory) error {

nbHost := &objects.Device{
NetboxObject: objects.NetboxObject{Tags: vc.CommonConfig.SourceTags, CustomFields: map[string]string{
constants.CustomFieldSourceName: vc.SourceConfig.Name,
constants.CustomFieldSourceName: vc.SourceConfig.Name,
constants.CustomFieldHostCpuCoresName: fmt.Sprintf("%d", hostCpuCores),
constants.CustomFieldHostMemoryName: fmt.Sprintf("%d GB", hostMemGB),
}},
Name: hostName,
Status: hostStatus,
Expand All @@ -242,10 +244,6 @@ func (vc *VmwareSource) syncHosts(nbi *inventory.NetboxInventory) error {
SerialNumber: hostSerialNumber,
AssetTag: hostAssetTag,
DeviceType: hostDeviceType,
CustomFields: map[string]string{
"host_cpu_cores": fmt.Sprintf("%d", hostCpuCores),
"host_memory": fmt.Sprintf("%d GB", hostMemGB),
},
}
nbHost, err = nbi.AddDevice(nbHost)
if err != nil {
Expand Down Expand Up @@ -692,11 +690,13 @@ func (vc *VmwareSource) syncVms(nbi *inventory.NetboxInventory) error {
if err != nil {
return fmt.Errorf("vm's custom field %s: %s", fieldName, err)
}
vmCustomFields[fieldName] = field.Value
}
vmCustomFields[fieldName] = field.Value
}
}
}
vmCustomFields[constants.CustomFieldSourceName] = vc.SourceConfig.Name
vmCustomFields[constants.CustomFieldSourceIdName] = vm.Self.Value

// netbox description has constraint <= len(200 characters)
// In this case we make a comment
Expand All @@ -708,24 +708,21 @@ func (vc *VmwareSource) syncVms(nbi *inventory.NetboxInventory) error {

newVM, err := nbi.AddVM(&objects.VM{
NetboxObject: objects.NetboxObject{
Tags: vc.CommonConfig.SourceTags,
Description: vmDescription,
CustomFields: map[string]string{
constants.CustomFieldSourceName: vc.SourceConfig.Name,
},
Tags: vc.CommonConfig.SourceTags,
Description: vmDescription,
CustomFields: vmCustomFields,
},
Name: vmName,
Cluster: vmCluster,
Site: vmSite,
Tenant: vmTenant,
Status: vmStatus,
Host: vmHost,
Platform: vmPlatform,
VCPUs: float32(vmVCPUs),
Memory: int(vmMemory), // MBs
Disk: int(vmDiskSizeB / 1024 / 1024 / 1024), // GBs
Comments: vmComments,
CustomFields: vmCustomFields,
Name: vmName,
Cluster: vmCluster,
Site: vmSite,
Tenant: vmTenant,
Status: vmStatus,
Host: vmHost,
Platform: vmPlatform,
VCPUs: float32(vmVCPUs),
Memory: int(vmMemory), // MBs
Disk: int(vmDiskSizeB / 1024 / 1024 / 1024), // GBs
Comments: vmComments,
})

if err != nil {
Expand Down
56 changes: 32 additions & 24 deletions internal/utils/diff_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,9 @@ func addSliceDiff(newSlice reflect.Value, existingSlice reflect.Value, jsonTag s
// If first slice is nil, that means that we reset the value
if !newSlice.IsValid() || newSlice.Len() == 0 {
if existingSlice.IsValid() && existingSlice.Len() > 0 {
diffMap[jsonTag] = []interface{}{}
if hasPriority {
diffMap[jsonTag] = []interface{}{}
}
}
return nil
}
Expand All @@ -206,12 +208,16 @@ func addSliceDiff(newSlice reflect.Value, existingSlice reflect.Value, jsonTag s
strSet[newSlice.Index(j).Interface().(string)] = true
}
if len(strSet) != existingSlice.Len() {
diffMap[jsonTag] = newSlice.Interface()
if hasPriority {
diffMap[jsonTag] = newSlice.Interface()
}
} else {
for j := 0; j < existingSlice.Len(); j++ {
if !strSet[existingSlice.Index(j).Interface().(string)] {
diffMap[jsonTag] = newSlice.Interface()
return nil
if hasPriority {
for j := 0; j < existingSlice.Len(); j++ {
if !strSet[existingSlice.Index(j).Interface().(string)] {
diffMap[jsonTag] = newSlice.Interface()
return nil
}
}
}
}
Expand All @@ -231,24 +237,26 @@ func addSliceDiff(newSlice reflect.Value, existingSlice reflect.Value, jsonTag s
newIdSet[id] = true
}

newIdSlice := make([]int, 0, len(newIdSet))
for id := range newIdSet {
newIdSlice = append(newIdSlice, id)
}
slices.Sort(newIdSlice)

if len(newIdSet) != existingSlice.Len() {
diffMap[jsonTag] = newIdSlice
} else {
for j := 0; j < existingSlice.Len(); j++ {
existingSliceEl := existingSlice.Index(j)
if existingSlice.Index(j).Kind() == reflect.Ptr {
existingSliceEl = existingSliceEl.Elem()
}
id = existingSliceEl.FieldByName("Id").Interface().(int)
if _, ok := newIdSet[id]; !ok {
diffMap[jsonTag] = newIdSlice
return nil
if hasPriority {
newIdSlice := make([]int, 0, len(newIdSet))
for id := range newIdSet {
newIdSlice = append(newIdSlice, id)
}
slices.Sort(newIdSlice)

if len(newIdSet) != existingSlice.Len() && hasPriority {
diffMap[jsonTag] = newIdSlice
} else {
for j := 0; j < existingSlice.Len(); j++ {
existingSliceEl := existingSlice.Index(j)
if existingSlice.Index(j).Kind() == reflect.Ptr {
existingSliceEl = existingSliceEl.Elem()
}
id = existingSliceEl.FieldByName("Id").Interface().(int)
if _, ok := newIdSet[id]; !ok {
diffMap[jsonTag] = newIdSlice
return nil
}
}
}
}
Expand Down
Loading

0 comments on commit 3cbd74b

Please sign in to comment.