utils

package
v0.6.2 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2024 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Alphanumeric

func Alphanumeric(name string) string

Converts string name to its alphanumeric representation with underscored only.

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 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 ExtractPrefixFromIPAddress added in v0.4.0

func ExtractPrefixFromIPAddress(ipAddress string) (string, error)

ExtractPrefixFromIPAddress extracts network with mask from the given ipAddress of format ip/mask, e.g. 172.16.2.1/16 -> 172.16.0.0/16.

func FilterInterfaceName added in v0.3.8

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 GeneratePlatformName

func GeneratePlatformName(osType string, osVersion string) string

Function that takes osType and osVersion and returns a an universal platform name that then can be shared between multiple objects.

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 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 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)

This function 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 ReverseLookup

func ReverseLookup(ipAddress string) string

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

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{}

Function that 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

Function that 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 SubnetsContainIPAddress added in v0.3.7

func SubnetsContainIPAddress(ipAddress string, subnets []string) bool

SubnetsContainIPAddress checks if array of subnets contain, the ip address.

func ValidateRegexRelations

func ValidateRegexRelations(regexRelations []string) error

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

func VerifySubnet added in v0.3.7

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