utils

package
v1.11.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 6, 2025 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ManufacturerMap = map[string]string{
	".*Cisco.*":    "Cisco",
	".*Fortinet.*": "Fortinet",
	".*Dell.*":     "Dell",
	"FTS Corp":     "Fujitsu",
	".*Fujitsu.*":  "Fujitsu",
	"^HP$":         "HPE",
	"^HP .*":       "HPE",
	".*Huawei.*":   "Huawei",
	".*Inspur.*":   "Inspur",
	".*Intel.*":    "Intel",
	"LEN":          "Lenovo",
	".*Nvidea.*":   "Nvidia",
	".*Samsung.*":  "Samsung",
}

ManufacturerMap maps regex of manufacturer names to manufacturer name. Manufacturer names are compatible with device type library. See internal/devices/combined_data.go for more info.

View Source
var SpecificOSMap = map[string]string{
	"rhcos_x64":                           "RHCOS (64bit)",
	".*Red Hat Enterprise Linux CoreOS.*": "RHCOS",

	".*windows_2022.*": "Microsoft Windows 2022",

	".*ol_5x64.*":              "Oracle Linux 5 (64-bit)",
	".*ol_6x64.*":              "Oracle Linux 6 (64-bit)",
	".*ol_7x64.*":              "Oracle Linux 7 (64-bit)",
	".*ol_8x64.*":              "Oracle Linux 8 (64-bit)",
	".*ol_9x64.*":              "Oracle Linux 9 (64-bit)",
	"Microsoft Windows Server": "Microsoft Windows Server",
}

SpecificOSMap maps regex of OS names to serialized OS names.

View Source
var UniversalOSMap = map[string]string{
	".*Red Hat Enterprise Linux.*": "RHEL",

	".*Windows.*": "Microsoft Windows",

	".*ol_.*":                 "Oracle Linux",
	"^Oracle$":                "Oracle Linux",
	".*Oracle Linux Server.*": "Oracle Linux",

	".*Centos.*": "Centos Linux",

	".*Rocky.*": "Rocky Linux",

	".*Alma.*": "Alma Linux",

	".*Ubuntu.*": "Ubuntu Linux",
}

Universal OSMap maps regex of OS names to serialized OS names.

Functions

func Alphanumeric

func Alphanumeric(name string) string

Converts string name to its alphanumeric representation with underscored only.

func CPUArchToBits

func CPUArchToBits(arch string) string

CPUArchToBits maps cpu architecture to corresponding bits of the architecture: x86_64 -> 64-bit, arm -> 32-bit.... CPUArchToBits maps cpu architecture to corresponding bits of the architecture.

func ConvertStringsToPairs

func ConvertStringsToPairs(input []string) map[string]string

Converts strings of format fieldName = value to map[fieldName] = value.

func ConvertStringsToRegexPairs

func ConvertStringsToRegexPairs(input []string) map[string]string

Converts array of strings, that are of form "regex = value", to a map where key is regex and value is value.

func ExtractCPUArch

func ExtractCPUArch(input string) string

extractCPUArch extracts the CPU architecture from an input string. If no CPU architecture was found, empty string is returned.

func ExtractFieldsFromDiffMap

func ExtractFieldsFromDiffMap(
	diffMap map[string]interface{},
	field []string,
) map[string]interface{}

func ExtractFunctionName

func ExtractFunctionName(i interface{}) string

ExtractFunctionName attempts to extract the name of a function regardless of its signature. Note: This function sacrifices type safety and assumes the caller ensures the correct usage.

func ExtractFunctionNameWithTrimPrefix

func ExtractFunctionNameWithTrimPrefix(i interface{}, prefix string) string

ExtractFunctionNameWithTrimPrefix extracts the function name and trims the prefix. Note: This function sacrifices type safety and assumes the caller ensures the correct usage.

func ExtractJSONTagsFromStruct

func ExtractJSONTagsFromStruct(inputStruct interface{}) []string

func ExtractJSONTagsFromStructIntoString

func ExtractJSONTagsFromStructIntoString(inputStruct interface{}) string

func FilterInterfaceName

func FilterInterfaceName(ifaceName string, ifaceFilter string) bool

Function that returns true if the given interface name should be filtered out, or false if it shouldn't be.

func GenerateDeviceTypeSlug

func GenerateDeviceTypeSlug(manufacturerName string, modelName string) string

GenerateDeviceTypeSlug generates a device type slug from the given manufacturer and model.

func GeneratePlatformName

func GeneratePlatformName(osDistribution string, osMajorVersion string, cpuArch string) string

Function that takes osDistribution (Linux, Windows, ...), osMajorVersion (8, 9, 10, ...) and cpuArch (x86_64, or 64bit, ....) and generates universal platform name in the format of "osDistrbution osMajorVersion (cpuArch)".

func GetIPVersion

func GetIPVersion(ipAddress string) int

GetIPVersion returns the version of the IP address. It returns 4 for IPv4, 6 for IPv6, and 0 if the IP address is invalid.

func GetPrefixAndMaskFromIPAddress

func GetPrefixAndMaskFromIPAddress(ipAddress string) (string, int, error)

GetmaskAndPrefixFromIPAddress extracts mask and prefix from a given ipAddress of format ip/mask. 192.168.1.1/24 --> (192.168.1.0/24, 24).

func IsPermittedIPAddress

func IsPermittedIPAddress(
	ipAddress string,
	permittedSubnets []string,
	ignoredSubnets []string,
) bool

func JSONDiffMapExceptID

func JSONDiffMapExceptID(
	newObj, existingObj interface{},
	resetFields bool,
	source2priority map[string]int,
) (map[string]interface{}, error)

JSONDiffMapExceptID compares two objects and returns a map of fields (represented by their JSON tag names) that are different with their values from newObj. If resetFields is set to true, the function will also include fields that are empty in newObj but might have a value in existingObj. Also we check for priority, if newObject has priority over existingObject we use the fields from newObject, otherwise we use the fields from exisingObject.

func LoadExtraCert

func LoadExtraCert(certPath string) (*x509.CertPool, error)

Function that loads additional certs from the given certDirPath. In case empty certPath is provided, deefault cert pool is returned.

func LoadExtraCertInTransportConfig

func LoadExtraCertInTransportConfig(certPath string) (*http.Transport, error)

Function that returns Transport config with extra cert from cert path loaded.

func Lookup

func Lookup(hostname string) string

Function that receives hostname and performs a forward lookup to get the IP address. If the forward lookup fails, it returns an empty string.

func MaskToBits

func MaskToBits(mask string) (int, error)

Function that converts string representation of ipv4 mask (e.g. 255.255.255.128) to bit representation (e.g. 25).

func MatchNamesWithEmails

func MatchNamesWithEmails(
	ctx context.Context,
	names []string,
	emails []string,
	logger *logger.Logger,
) map[string]string

Function that matches array of names to array of emails (which could be subset of it). It returns a map of name -> email.

E.g. names = ["John Doe", "Jane Doe"], emails = ["jane.doe@example"] Output: map["Jane Doe"] = "jane.doe@example".

func MatchStringToValue

func MatchStringToValue(input string, patterns map[string]string) (string, error)

Matches input string to a regex from input map patterns, and returns the value. If there is no match, it returns an empty string.

func NetboxJSONMarshal

func NetboxJSONMarshal(obj interface{}) ([]byte, error)

NetboxJSONMarshal takes an object pointer, and returns a json body, that can be used to create that object in netbox API. This is essential because default marshal of the object isn't compatible with netbox API when attributes have nested objects.

func NewHTTPClient

func NewHTTPClient(validateCert bool, caFile string) (*http.Client, error)

NewHTTPClient creates an http client with tls config depending on validateCert and caFile parameter.

func RemoveZoneIndexFromIPAddress

func RemoveZoneIndexFromIPAddress(ipAddress string) string

RemoveZoneIndexFromIPAddress removes zone index from the IPv6 address: e.g. 2001:db8::1%eth0 -> 2001:db8::1. e.g. 2001:db8::1%2/64 -> 2001:db8::1/64.

func ReverseLookup

func ReverseLookup(ipAddress string) string

func SerializeEmails

func SerializeEmails(emails []string) []string

SerializeEmails receieve an array of emails and transforms each array to lowercase strings and removes any whitespace.

func SerializeManufacturerName

func SerializeManufacturerName(manufacturer string) string

GetManufactuerFromString returns manufacturer name from the given string.

func SerializeMask

func SerializeMask(mask string) string

SerializeMask serializes mask into a bit representation. If a mask is already in bit rerpesentation, it returns the mask: mask "24" -> "24", mask "255.255.255.0" -> "24".

func SerializeOSName

func SerializeOSName(os string) string

SerializeOSName returns serialized OS name from the given string.

func SerializeOwner

func SerializeOwner(owner string) string

SerializeOwner receives an owner string and serializes it in the following way. If owner string is composed of name and surname, it returns the name and surname capitalized and separated by a space. If owner string is composed of only one word, it returns the word capitalized.

func SerializeOwners

func SerializeOwners(owners []string) []string

SerializeOwners receives an array of owners and serializes each owner in the following way. If owner string is composed of name and surname, it returns the name and surname capitalized and separated by a space.

func Slugify

func Slugify(name string) string

Converts string name to its slugified version. Slugified version can only contain: lowercase letters, numbers, underscores or hyphens. e.g. "My Name" -> "my-name" e.g. " @Test@ " -> "test".

func StructToNetboxJSONMap

func StructToNetboxJSONMap(obj interface{}) map[string]interface{}

StructToNetboxJSONMap converts an object to a map[string]interface{} which can be used to create a json body for netbox API, especially for POST requests.

func SubnetContainsIPAddress

func SubnetContainsIPAddress(ipAddress string, subnet string) bool

SubnetContainsIPAddress checks if given IP address is part of the given subnet (e.g. ipAddress "172.31.4.129" and subnet "172.31.4.145/25").

func ValidateRegexRelations

func ValidateRegexRelations(regexRelations []string) error

Validates array of regex relations Regex relation is a string of format "regex = value".

func VerifySubnet

func VerifySubnet(subnet string) bool

VerifySubnet checks if a given subnet is valid.

Types

type IDObject

type IDObject struct {
	ID int `json:"id"`
}

Struct used for patching objects, when attributes are structs, or slices we only need object with an ID of original object for patching.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL