Skip to content

Commit

Permalink
refractor(golangci-lint): fixing linter issues
Browse files Browse the repository at this point in the history
  • Loading branch information
bl4ko committed Jan 16, 2025
1 parent eb6d461 commit 046efc0
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 152 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ repos:
- id: go-mod-tidy

- repo: https://github.com/gitleaks/gitleaks
rev: v8.23.0
rev: v8.23.1
hooks:
- id: gitleaks

- repo: https://github.com/streetsidesoftware/cspell-cli
rev: v8.17.0
rev: v8.17.1
hooks:
- id: cspell

Expand Down
2 changes: 2 additions & 0 deletions internal/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const (
IOSXE SourceType = "ios-xe"
)

const WildcardIP = "0.0.0.0"

const SsotTagColor = "00add8"
const SsotTagName = "netbox-ssot"
const SsotTagDescription = "Tag used by netbox-ssot to mark devices that are managed by it"
Expand Down
2 changes: 1 addition & 1 deletion internal/source/fortigate/fortigate_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type InterfaceResponse struct {
MAC string `json:"macaddr"`
VlanID int `json:"vlanid"`
SecondaryIP []SecondaryIP `json:"secondaryip"`
VRRPIP []VRRPIP `json:"vrrp"`
VRRPIP []VRRPIP `json:"vrrp"`
}

type SecondaryIP struct {
Expand Down
6 changes: 3 additions & 3 deletions internal/source/fortigate/fortigate_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func (fs *FortigateSource) syncInterfaces(nbi *inventory.NetboxInventory) error
func syncInterfaceIPs(fs *FortigateSource, nbi *inventory.NetboxInventory, iface InterfaceResponse, nbIface *objects.Interface) (*objects.IPAddress, error) {
var NBIPAddress *objects.IPAddress
ipAndMask := strings.Split(iface.IP, " ")
if len(ipAndMask) == 2 && ipAndMask[0] != "0.0.0.0" {
if len(ipAndMask) == 2 && ipAndMask[0] != constants.WildcardIP {
if utils.IsPermittedIPAddress(ipAndMask[0], fs.SourceConfig.PermittedSubnets, fs.SourceConfig.IgnoredSubnets) {
maskBits, err := utils.MaskToBits(ipAndMask[1])
if err != nil {
Expand All @@ -245,7 +245,7 @@ func syncInterfaceIPs(fs *FortigateSource, nbi *inventory.NetboxInventory, iface
if len(iface.SecondaryIP) > 0 {
for _, secondaryIP := range iface.SecondaryIP {
ipAndMask := strings.Split(secondaryIP.IP, " ")
if len(ipAndMask) == 2 && ipAndMask[0] != "0.0.0.0" {
if len(ipAndMask) == 2 && ipAndMask[0] != constants.WildcardIP {
if utils.IsPermittedIPAddress(ipAndMask[0], fs.SourceConfig.PermittedSubnets, fs.SourceConfig.IgnoredSubnets) {
maskBits, err := utils.MaskToBits(ipAndMask[1])
if err != nil {
Expand Down Expand Up @@ -273,7 +273,7 @@ func syncInterfaceIPs(fs *FortigateSource, nbi *inventory.NetboxInventory, iface
if len(iface.VRRPIP) > 0 {
for _, vrrp := range iface.VRRPIP {
ipAndMask := []string{vrrp.VRIP, "255.255.255.255"}
if len(ipAndMask) == 2 && ipAndMask[0] != "0.0.0.0" {
if len(ipAndMask) == 2 && ipAndMask[0] != constants.WildcardIP {
if utils.IsPermittedIPAddress(ipAndMask[0], fs.SourceConfig.PermittedSubnets, fs.SourceConfig.IgnoredSubnets) {
maskBits, err := utils.MaskToBits(ipAndMask[1])
if err != nil {
Expand Down
302 changes: 156 additions & 146 deletions internal/source/ovirt/ovirt_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,181 +155,191 @@ func (o *OVirtSource) syncClusters(nbi *inventory.NetboxInventory) error {
// as devices.
func (o *OVirtSource) syncHosts(nbi *inventory.NetboxInventory) error {
for hostID, host := range o.Hosts {
hostName, exists := host.Name()
if !exists {
o.Logger.Warningf(o.Ctx, "name of host with id=%s is empty", hostID)
hostStruct, err := extractHostData(o, nbi, host, hostID)
if err != nil {
return fmt.Errorf("extract host data: %s", err)
}
hostCluster, _ := nbi.GetCluster(o.Clusters[host.MustCluster().MustId()].MustName())

hostSite, err := common.MatchHostToSite(o.Ctx, nbi, hostName, o.SourceConfig.HostSiteRelations)
nbHost, err := nbi.AddDevice(o.Ctx, hostStruct)
if err != nil {
return fmt.Errorf("hostSite: %s", err)
return fmt.Errorf("failed to add oVirt host %+v with error: %v", hostStruct, err)
}
hostTenant, err := common.MatchHostToTenant(o.Ctx, nbi, hostName, o.SourceConfig.HostTenantRelations)

// We also need to sync nics separately, because nic is a separate object in netbox
err = o.syncHostNics(nbi, host, nbHost)
if err != nil {
return fmt.Errorf("hostTenant: %s", err)
return fmt.Errorf("failed to sync oVirt host %s nics with error: %v", nbHost.Name, err)
}
}
return nil
}

// Extract host hardware information if possible, if not use generic values
var hostSerialNumber, hostUUID string
hostManufacturerName := constants.DefaultManufacturer
hostModel := constants.DefaultModel
if hwInfo, exists := host.HardwareInformation(); exists {
hostUUID, _ = hwInfo.Uuid()
if !o.SourceConfig.IgnoreSerialNumbers {
hostSerialNumber, _ = hwInfo.SerialNumber()
}
if manufacturerName, exists := hwInfo.Manufacturer(); exists {
hostManufacturerName = manufacturerName
hostManufacturerName = utils.SerializeManufacturerName(hostManufacturerName)
}
if modelName, exists := hwInfo.ProductName(); exists {
hostModel = modelName
}
}
// extractHostData is a helper function for syncHosts that extracts host
// data and returns it as an *objects.Device.
func extractHostData(o *OVirtSource, nbi *inventory.NetboxInventory, host *ovirtsdk4.Host, hostID string) (*objects.Device, error) {
hostName, exists := host.Name()
if !exists {
o.Logger.Warningf(o.Ctx, "name of host with id=%s is empty", hostID)
}
hostCluster, _ := nbi.GetCluster(o.Clusters[host.MustCluster().MustId()].MustName())

var deviceSlug string
deviceData, hasDeviceData := devices.DeviceTypesMap[hostManufacturerName][hostModel]
if hasDeviceData {
deviceSlug = deviceData.Slug
} else {
deviceSlug = utils.GenerateDeviceTypeSlug(hostManufacturerName, hostModel)
}
hostSite, err := common.MatchHostToSite(o.Ctx, nbi, hostName, o.SourceConfig.HostSiteRelations)
if err != nil {
return nil, fmt.Errorf("hostSite: %s", err)
}
hostTenant, err := common.MatchHostToTenant(o.Ctx, nbi, hostName, o.SourceConfig.HostTenantRelations)
if err != nil {
return nil, fmt.Errorf("hostTenant: %s", err)
}

hostManufacturerStruct := &objects.Manufacturer{
Name: hostManufacturerName,
Slug: utils.Slugify(hostManufacturerName),
}
hostManufacturer, err := nbi.AddManufacturer(o.Ctx, hostManufacturerStruct)
if err != nil {
return fmt.Errorf("failed adding oVirt Manufacturer %v with error: %s", hostManufacturerStruct, err)
// Extract host hardware information if possible, if not use generic values
var hostSerialNumber, hostUUID string
hostManufacturerName := constants.DefaultManufacturer
hostModel := constants.DefaultModel
if hwInfo, exists := host.HardwareInformation(); exists {
hostUUID, _ = hwInfo.Uuid()
if !o.SourceConfig.IgnoreSerialNumbers {
hostSerialNumber, _ = hwInfo.SerialNumber()
}

var hostDeviceType *objects.DeviceType
hostDeviceTypeStruct := &objects.DeviceType{
Manufacturer: hostManufacturer,
Model: hostModel,
Slug: deviceSlug,
if manufacturerName, exists := hwInfo.Manufacturer(); exists {
hostManufacturerName = manufacturerName
hostManufacturerName = utils.SerializeManufacturerName(hostManufacturerName)
}
hostDeviceType, err = nbi.AddDeviceType(o.Ctx, hostDeviceTypeStruct)
if err != nil {
return fmt.Errorf("failed adding oVirt DeviceType %v with error: %s", hostDeviceTypeStruct, err)
if modelName, exists := hwInfo.ProductName(); exists {
hostModel = modelName
}
}

var hostStatus *objects.DeviceStatus
ovirtStatus, exists := host.Status()
if exists {
switch ovirtStatus {
case ovirtsdk4.HOSTSTATUS_UP:
hostStatus = &objects.DeviceStatusActive
default:
hostStatus = &objects.DeviceStatusOffline
}
}
var deviceSlug string
deviceData, hasDeviceData := devices.DeviceTypesMap[hostManufacturerName][hostModel]
if hasDeviceData {
deviceSlug = deviceData.Slug
} else {
deviceSlug = utils.GenerateDeviceTypeSlug(hostManufacturerName, hostModel)
}

var hostPlatform *objects.Platform
osDistribution := constants.DefaultOSName
osVersion := constants.DefaultOSVersion
cpuArch := constants.DefaultCPUArch
if os, exists := host.Os(); exists {
if ovirtOsType, exists := os.Type(); exists {
osDistribution = ovirtOsType
}
if ovirtOsVersion, exists := os.Version(); exists {
if osMajorVersion, exists := ovirtOsVersion.Major(); exists {
osVersion = fmt.Sprintf("%d", osMajorVersion)
}
}
// We extract architecture from reported_kernel_cmdline
if reportedKernelCmdline, exists := os.ReportedKernelCmdline(); exists {
cpuArch = utils.ExtractCPUArch(reportedKernelCmdline)
if bitArch, ok := constants.Arch2Bit[cpuArch]; ok {
cpuArch = bitArch
}
}
}
platformName := utils.GeneratePlatformName(osDistribution, osVersion, cpuArch)
hostPlatformStruct := &objects.Platform{
Name: platformName,
Slug: utils.Slugify(platformName),
Manufacturer: hostManufacturer,
}
hostPlatform, err = nbi.AddPlatform(o.Ctx, hostPlatformStruct)
if err != nil {
return fmt.Errorf("failed adding oVirt Platform %v with error: %s", hostPlatform, err)
}
hostManufacturerStruct := &objects.Manufacturer{
Name: hostManufacturerName,
Slug: utils.Slugify(hostManufacturerName),
}
hostManufacturer, err := nbi.AddManufacturer(o.Ctx, hostManufacturerStruct)
if err != nil {
return nil, fmt.Errorf("failed adding oVirt Manufacturer %v with error: %s", hostManufacturerStruct, err)
}

var hostDescription string
if description, exists := host.Description(); exists {
hostDescription = description
}
var hostDeviceType *objects.DeviceType
hostDeviceTypeStruct := &objects.DeviceType{
Manufacturer: hostManufacturer,
Model: hostModel,
Slug: deviceSlug,
}
hostDeviceType, err = nbi.AddDeviceType(o.Ctx, hostDeviceTypeStruct)
if err != nil {
return nil, fmt.Errorf("failed adding oVirt DeviceType %v with error: %s", hostDeviceTypeStruct, err)
}

var hostComment string
if comment, exists := host.Comment(); exists {
hostComment = comment
var hostStatus *objects.DeviceStatus
ovirtStatus, exists := host.Status()
if exists {
switch ovirtStatus {
case ovirtsdk4.HOSTSTATUS_UP:
hostStatus = &objects.DeviceStatusActive
default:
hostStatus = &objects.DeviceStatusOffline
}
}

var hostCPUCores string
if cpu, exists := host.Cpu(); exists {
hostCPUCores, exists = cpu.Name()
if !exists {
o.Logger.Warning(o.Ctx, "oVirt hostCpuCores of ", hostName, " is empty.")
var hostPlatform *objects.Platform
osDistribution := constants.DefaultOSName
osVersion := constants.DefaultOSVersion
cpuArch := constants.DefaultCPUArch
if os, exists := host.Os(); exists {
if ovirtOsType, exists := os.Type(); exists {
osDistribution = ovirtOsType
}
if ovirtOsVersion, exists := os.Version(); exists {
if osMajorVersion, exists := ovirtOsVersion.Major(); exists {
osVersion = fmt.Sprintf("%d", osMajorVersion)
}
}

mem, _ := host.Memory()
mem /= (constants.KiB * constants.KiB * constants.KiB) // Value is in Bytes, we convert to GB

// Match host to a role. First test if user provided relations, if not
// use default server role.
var hostRole *objects.DeviceRole
if len(o.SourceConfig.HostRoleRelations) > 0 {
hostRole, err = common.MatchHostToRole(o.Ctx, nbi, hostName, o.SourceConfig.HostRoleRelations)
if err != nil {
return fmt.Errorf("match host to role: %s", err)
// We extract architecture from reported_kernel_cmdline
if reportedKernelCmdline, exists := os.ReportedKernelCmdline(); exists {
cpuArch = utils.ExtractCPUArch(reportedKernelCmdline)
if bitArch, ok := constants.Arch2Bit[cpuArch]; ok {
cpuArch = bitArch
}
}
if hostRole == nil {
hostRole, err = nbi.AddServerDeviceRole(o.Ctx)
if err != nil {
return fmt.Errorf("add server device role %s", err)
}
}
platformName := utils.GeneratePlatformName(osDistribution, osVersion, cpuArch)
hostPlatformStruct := &objects.Platform{
Name: platformName,
Slug: utils.Slugify(platformName),
Manufacturer: hostManufacturer,
}
hostPlatform, err = nbi.AddPlatform(o.Ctx, hostPlatformStruct)
if err != nil {
return nil, fmt.Errorf("failed adding oVirt Platform %v with error: %s", hostPlatform, err)
}

var hostDescription string
if description, exists := host.Description(); exists {
hostDescription = description
}

var hostComment string
if comment, exists := host.Comment(); exists {
hostComment = comment
}

var hostCPUCores string
if cpu, exists := host.Cpu(); exists {
hostCPUCores, exists = cpu.Name()
if !exists {
o.Logger.Warning(o.Ctx, "oVirt hostCpuCores of ", hostName, " is empty.")
}
}

nbHost := &objects.Device{
NetboxObject: objects.NetboxObject{
Description: hostDescription,
Tags: o.Config.SourceTags,
CustomFields: map[string]interface{}{
constants.CustomFieldSourceIDName: hostID,
constants.CustomFieldHostCPUCoresName: hostCPUCores,
constants.CustomFieldHostMemoryName: fmt.Sprintf("%d GB", mem),
constants.CustomFieldDeviceUUIDName: hostUUID,
},
},
Name: hostName,
Status: hostStatus,
Platform: hostPlatform,
DeviceRole: hostRole,
Site: hostSite,
Tenant: hostTenant,
Cluster: hostCluster,
Comments: hostComment,
SerialNumber: hostSerialNumber,
DeviceType: hostDeviceType,
}
nbHost, err = nbi.AddDevice(o.Ctx, nbHost)
mem, _ := host.Memory()
mem /= (constants.KiB * constants.KiB * constants.KiB) // Value is in Bytes, we convert to GB

// Match host to a role. First test if user provided relations, if not
// use default server role.
var hostRole *objects.DeviceRole
if len(o.SourceConfig.HostRoleRelations) > 0 {
hostRole, err = common.MatchHostToRole(o.Ctx, nbi, hostName, o.SourceConfig.HostRoleRelations)
if err != nil {
return fmt.Errorf("failed to add oVirt host %+v with error: %v", nbHost, err)
return nil, fmt.Errorf("match host to role: %s", err)
}

// We also need to sync nics separately, because nic is a separate object in netbox
err = o.syncHostNics(nbi, host, nbHost)
}
if hostRole == nil {
hostRole, err = nbi.AddServerDeviceRole(o.Ctx)
if err != nil {
return fmt.Errorf("failed to sync oVirt host %s nics with error: %v", hostName, err)
return nil, fmt.Errorf("add server device role %s", err)
}
}
return nil

return &objects.Device{
NetboxObject: objects.NetboxObject{
Description: hostDescription,
Tags: o.Config.SourceTags,
CustomFields: map[string]interface{}{
constants.CustomFieldSourceIDName: hostID,
constants.CustomFieldHostCPUCoresName: hostCPUCores,
constants.CustomFieldHostMemoryName: fmt.Sprintf("%d GB", mem),
constants.CustomFieldDeviceUUIDName: hostUUID,
},
},
Name: hostName,
Status: hostStatus,
Platform: hostPlatform,
DeviceRole: hostRole,
Site: hostSite,
Tenant: hostTenant,
Cluster: hostCluster,
Comments: hostComment,
SerialNumber: hostSerialNumber,
DeviceType: hostDeviceType,
}, nil
}

// syncHostNics syncs collected host nics from ovirt api to netbox inventory.
Expand Down

0 comments on commit 046efc0

Please sign in to comment.