inventory

package
v1.2.3 Latest Latest
Warning

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

Go to latest
Published: May 30, 2024 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var MockExistingSites = map[string]*objects.Site{
	"existing_site1": {
		NetboxObject: objects.NetboxObject{
			ID:   1,
			Tags: []*objects.Tag{service.MockDefaultSsotTag},
		},
		Name: "existing_site1",
		Slug: "existing_site1",
	},
	"existing_site2": {
		NetboxObject: objects.NetboxObject{
			ID:   2,
			Tags: []*objects.Tag{service.MockDefaultSsotTag},
		},
		Name: "existing_site2",
		Slug: "existing_site2",
	},
}
View Source
var MockExistingTags = map[string]*objects.Tag{
	"existing_tag1": {
		Name:        "existing_tag1",
		Description: "Test exististing tag1",
		Slug:        "existing_tag1",
	},
	"existing_tag2": {
		Name:        "existing_tag2",
		Description: "Test exististing tag2",
		Slug:        "existing_tag2",
	},
}
View Source
var MockExistingTenants = map[string]*objects.Tenant{
	"existing_tenant1": {
		NetboxObject: objects.NetboxObject{
			ID:   1,
			Tags: []*objects.Tag{service.MockDefaultSsotTag},
		},
		Name: "existing_tenant1",
		Slug: "existing_tenant1",
	},
	"existing_tenant2": {
		NetboxObject: objects.NetboxObject{
			ID:   2,
			Tags: []*objects.Tag{service.MockDefaultSsotTag},
		},
		Name: "existing_tenant2",
		Slug: "existing_tenant2",
	},
}
View Source
var MockInventory = &NetboxInventory{
	Logger:             &logger.Logger{Logger: log.New(os.Stdout, "", log.LstdFlags)},
	TagsIndexByName:    MockExistingTags,
	TagsLock:           sync.Mutex{},
	TenantsIndexByName: MockExistingTenants,
	TenantsLock:        sync.Mutex{},
	SitesIndexByName:   MockExistingSites,
	SitesLock:          sync.Mutex{},
	NetboxAPI:          service.MockNetboxClient,
	SsotTag: &objects.Tag{
		ID:          0,
		Name:        "netbox-ssot",
		Slug:        "netbox-ssot",
		Description: "default netbox-ssot tag",
		Color:       "ffffff",
	},
}

Functions

This section is empty.

Types

type NetboxInventory

type NetboxInventory struct {
	// Logger is the logger used for logging messages
	Logger *logger.Logger
	// NetboxConfig is the Netbox configuration
	NetboxConfig *parser.NetboxConfig
	// NetboxAPI is the Netbox API object, for communicating with the Netbox API
	NetboxAPI *service.NetboxClient
	// SourcePriority: if object is found on multiple sources, which source has the priority for the object attributes.
	SourcePriority map[string]int
	// TagsIndexByName is a map of all tags in the Netbox's inventory, indexed by their name
	TagsIndexByName map[string]*objects.Tag
	// ContactGroupsIndexByName is a map of all contact groups indexed by their names.
	ContactGroupsIndexByName map[string]*objects.ContactGroup
	// ContactRolesIndexByName is a map of all contact roles indexed by their names.
	ContactRolesIndexByName map[string]*objects.ContactRole
	// ContactsIndexByName is a map of all contacts in the Netbox's inventory, indexed by their names
	ContactsIndexByName map[string]*objects.Contact
	// ContactAssignmentsIndexByObjectTypeAndObjectIDAndContactIDAndRoleID is a map of all contact assignments indexed by their content type, object id, contact id and role id.
	ContactAssignmentsIndexByObjectTypeAndObjectIDAndContactIDAndRoleID map[objects.ObjectType]map[int]map[int]map[int]*objects.ContactAssignment
	// SitesIndexByName is a map of all sites in the Netbox's inventory, indexed by their name
	SitesIndexByName map[string]*objects.Site
	// ManufacturersIndexByName is a map of all manufacturers in the Netbox's inventory, indexed by their name
	ManufacturersIndexByName map[string]*objects.Manufacturer
	// PlatformsIndexByName is a map of all platforms in the Netbox's inventory, indexed by their name
	PlatformsIndexByName map[string]*objects.Platform
	// TenantsIndexByName is a map of all tenants in the Netbox's inventory, indexed by their name
	TenantsIndexByName map[string]*objects.Tenant
	// DeviceTypesIndexByModel is a map of all device types in the Netbox's inventory, indexed by their model
	DeviceTypesIndexByModel map[string]*objects.DeviceType
	// DevicesIndexByNameAndSiteID is a map of all devices in the Netbox's inventory, indexed by their name, and
	// site ID (This is because, netbox constraints: https://github.com/netbox-community/netbox/blob/3d941411d438f77b66d2036edf690c14b459af58/netbox/dcim/models/devices.py#L775)
	DevicesIndexByNameAndSiteID map[string]map[int]*objects.Device
	// VirtualDeviceContextsIndexByNameAndDeviceID is a map of all virtual device contexts in the Netbox's inventory indexed by their name and device ID.
	VirtualDeviceContextsIndexByNameAndDeviceID map[string]map[int]*objects.VirtualDeviceContext
	// PrefixesIndexByPrefix is a map of all prefixes in the Netbox's inventory, indexed by their prefix
	PrefixesIndexByPrefix map[string]*objects.Prefix
	// VlanGroupsIndexByName is a map of all VlanGroups in the Netbox's inventory, indexed by their name
	VlanGroupsIndexByName map[string]*objects.VlanGroup
	// VlansIndexByVlanGroupIDAndVID is a map of all vlans in the Netbox's inventory, indexed by their VlanGroup and vid.
	VlansIndexByVlanGroupIDAndVID map[int]map[int]*objects.Vlan
	// ClusterGroupsIndexByName is a map of all cluster groups in the Netbox's inventory, indexed by their name
	ClusterGroupsIndexByName map[string]*objects.ClusterGroup
	// ClusterTypesIndexByName is a map of all cluster types in the Netbox's inventory, indexed by their name
	ClusterTypesIndexByName map[string]*objects.ClusterType
	// ClustersIndexByName is a map of all clusters in the Netbox's inventory, indexed by their name
	ClustersIndexByName map[string]*objects.Cluster
	// Netbox's Device Roles is a map of all device roles in the inventory, indexed by name
	DeviceRolesIndexByName map[string]*objects.DeviceRole
	// CustomFieldsIndexByName is a map of all custom fields in the inventory, indexed by name
	CustomFieldsIndexByName map[string]*objects.CustomField
	// InterfacesIndexByDeviceAnName is a map of all interfaces in the inventory, indexed by their's
	// device id and their name.
	InterfacesIndexByDeviceIDAndName map[int]map[string]*objects.Interface
	// VMsIndexByNameAndClusterID is a map of all virtual machines in the inventory, indexed by their name and their cluster id
	VMsIndexByNameAndClusterID map[string]map[int]*objects.VM
	// VirtualMachineInterfacesIndexByVMAndName is a map of all virtual machine interfaces in the inventory, indexed by their's virtual machine id and their name
	VMInterfacesIndexByVMIdAndName map[int]map[string]*objects.VMInterface
	// IPAdressesIndexByAddress is a map of all IP addresses in the inventory, indexed by their address
	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
	ContactGroupsLock      sync.Mutex
	ContactsLock           sync.Mutex
	ContactAssignmentsLock sync.Mutex
	CustomFieldsLock       sync.Mutex
	ClusterGroupsLock      sync.Mutex
	ClusterTypesLock       sync.Mutex
	ClustersLock           sync.Mutex
	DeviceRolesLock        sync.Mutex
	ManufacturersLock      sync.Mutex
	DeviceTypesLock        sync.Mutex
	PlatformsLock          sync.Mutex
	DevicesLock            sync.Mutex
	VlanGroupsLock         sync.Mutex
	VlansLock              sync.Mutex
	InterfacesLock         sync.Mutex
	VMsLock                sync.Mutex
	VMInterfacesLock       sync.Mutex
	IPAddressesLock        sync.Mutex
	PrefixesLock           sync.Mutex

	// Orphan manager is a map of objectAPIPath to a set of managed ids for that object type.
	//
	// {
	//		"/api/dcim/devices/": {22: true, 3: true, ...},
	//		"/api/dcim/interface/": {15: true, 36: true, ...},
	//  	"/api/virtualization/clusters/": {121: true, 122: true, ...},
	//  	"...": [...]
	// }
	//
	// It stores which objects have been created by netbox-ssot and can be deleted
	// because they are not available in the sources anymore
	OrphanManager map[string]map[int]bool

	// OrphanObjectPriority is a map that stores priorities for each object. This is necessary
	// because map order is non deterministic and if we delete dependent object first we will
	// get the dependency error.
	//
	// {
	//   0: service.TagApiPath,
	//   1: service.CustomFieldApiPath,
	//   ...
	// }
	OrphanObjectPriority map[int]string

	// ArpDataLifeSpan determines the lifespan of arp entries in seconds.
	ArpDataLifeSpan int
	// Tag used by netbox-ssot to mark devices that are managed by it.
	SsotTag *objects.Tag
	// Default context for the inventory, we use it to pass sourcename to functions for logging.
	Ctx context.Context //nolint:containedctx
}

NetboxInventory is a singleton class to manage a inventory of NetBoxObject objects.

func NewNetboxInventory

func NewNetboxInventory(ctx context.Context, logger *logger.Logger, nbConfig *parser.NetboxConfig) *NetboxInventory

NewNetboxInventory creates a new NetBoxInventory object. It takes a logger and a NetboxConfig as parameters, and returns a pointer to the newly created NetBoxInventory. The logger is used for logging messages, and the NetboxConfig is used to configure the NetBoxInventory.

func (*NetboxInventory) AddCluster

func (nbi *NetboxInventory) AddCluster(ctx context.Context, newCluster *objects.Cluster) (*objects.Cluster, error)

AddCluster adds a new cluster to the Netbox inventory. It takes a context and a pointer to a Cluster object as input. It returns the newly created cluster object and an error, if any. If the cluster already exists in Netbox, it checks if the existing cluster is up to date. If it is not up to date, it patches the existing cluster with the changes from the new cluster. If the cluster does not exist in Netbox, it creates a new cluster.

func (*NetboxInventory) AddClusterGroup

func (nbi *NetboxInventory) AddClusterGroup(ctx context.Context, newCg *objects.ClusterGroup) (*objects.ClusterGroup, error)

AddClusterGroup adds a new cluster group to the Netbox inventory. It takes a context and a newCg object as input and returns the newly created cluster group and an error (if any). If the cluster group already exists in Netbox, it checks if it is up to date and patches it if necessary. If the cluster group does not exist, it creates a new one. The function also updates the cluster group index by name and removes the ID from the orphan manager.

func (*NetboxInventory) AddClusterType

func (nbi *NetboxInventory) AddClusterType(ctx context.Context, newClusterType *objects.ClusterType) (*objects.ClusterType, error)

AddClusterType adds a new cluster type to the Netbox inventory. It takes a context and a newClusterType object as input and returns the created or updated cluster type object and an error, if any. If the cluster type already exists in Netbox, it checks if it is up to date. If not, it patches the existing cluster type. If the cluster type does not exist, it creates a new one.

func (*NetboxInventory) AddContact

func (nbi *NetboxInventory) AddContact(ctx context.Context, newContact *objects.Contact) (*objects.Contact, error)

AddContact adds a contact to the local netbox inventory.

func (*NetboxInventory) AddContactAssignment

func (nbi *NetboxInventory) AddContactAssignment(ctx context.Context, newCA *objects.ContactAssignment) (*objects.ContactAssignment, error)

AddContact assignment adds a contact assignment to the local netbox inventory. TODO: Make index check less code and more universal, checking each level is ugly.

func (*NetboxInventory) AddContactGroup

func (nbi *NetboxInventory) AddContactGroup(ctx context.Context, newContactGroup *objects.ContactGroup) (*objects.ContactGroup, error)

AddContactGroup adds contact group to the local netbox inventory.

func (*NetboxInventory) AddContactRole

func (nbi *NetboxInventory) AddContactRole(ctx context.Context, newContactRole *objects.ContactRole) (*objects.ContactRole, error)

AddContactRole adds the newContactRole to the local netbox inventory.

func (*NetboxInventory) AddCustomField

func (nbi *NetboxInventory) AddCustomField(ctx context.Context, newCf *objects.CustomField) (*objects.CustomField, error)

AddCustomField adds a custom field to the Netbox inventory. It takes a context and a newCf object as input and returns the created or patched custom field along with any error encountered. If the custom field already exists in Netbox but is out of date, it will be patched with the new values. If the custom field does not exist, it will be created.

func (*NetboxInventory) AddDevice

func (nbi *NetboxInventory) AddDevice(ctx context.Context, newDevice *objects.Device) (*objects.Device, error)

func (*NetboxInventory) AddDeviceRole

func (nbi *NetboxInventory) AddDeviceRole(ctx context.Context, newDeviceRole *objects.DeviceRole) (*objects.DeviceRole, error)

AddDeviceRole adds a new device role to the Netbox inventory. It takes a context and a newDeviceRole object as input and returns the created device role object and an error, if any. If the device role already exists in Netbox, it checks if it is up to date and patches it if necessary. If the device role does not exist, it creates a new one.

func (*NetboxInventory) AddDeviceType

func (nbi *NetboxInventory) AddDeviceType(ctx context.Context, newDeviceType *objects.DeviceType) (*objects.DeviceType, error)

func (*NetboxInventory) AddIPAddress

func (nbi *NetboxInventory) AddIPAddress(ctx context.Context, newIPAddress *objects.IPAddress) (*objects.IPAddress, error)

func (*NetboxInventory) AddInterface

func (nbi *NetboxInventory) AddInterface(ctx context.Context, newInterface *objects.Interface) (*objects.Interface, error)

func (*NetboxInventory) AddManufacturer

func (nbi *NetboxInventory) AddManufacturer(ctx context.Context, newManufacturer *objects.Manufacturer) (*objects.Manufacturer, error)

AddManufacturer adds a new manufacturer to the Netbox inventory. It takes a context, `ctx`, and a pointer to a `newManufacturer` object as input. The function returns a pointer to the newly created manufacturer and an error, if any. If the manufacturer already exists in Netbox, the function checks if it is up to date. If it is not up to date, the function patches the existing manufacturer with the updated information. If the manufacturer does not exist in Netbox, the function creates a new one.

func (*NetboxInventory) AddPlatform

func (nbi *NetboxInventory) AddPlatform(ctx context.Context, newPlatform *objects.Platform) (*objects.Platform, error)

func (*NetboxInventory) AddPrefix

func (nbi *NetboxInventory) AddPrefix(ctx context.Context, newPrefix *objects.Prefix) (*objects.Prefix, error)

func (*NetboxInventory) AddSite

func (nbi *NetboxInventory) AddSite(ctx context.Context, newSite *objects.Site) (*objects.Site, error)

AddContact adds a contact to the local netbox inventory.

func (*NetboxInventory) AddTag

func (nbi *NetboxInventory) AddTag(ctx context.Context, newTag *objects.Tag) (*objects.Tag, error)

AddTag adds the newTag from source sourceName to the local inventory.

func (*NetboxInventory) AddTenant added in v0.2.2

func (nbi *NetboxInventory) AddTenant(ctx context.Context, newTenant *objects.Tenant) (*objects.Tenant, error)

AddTenants adds a new tenant to the local netbox inventory.

func (*NetboxInventory) AddVM

func (nbi *NetboxInventory) AddVM(ctx context.Context, newVM *objects.VM) (*objects.VM, error)

func (*NetboxInventory) AddVMInterface

func (nbi *NetboxInventory) AddVMInterface(ctx context.Context, newVMInterface *objects.VMInterface) (*objects.VMInterface, error)

func (*NetboxInventory) AddVirtualDeviceContext added in v0.4.0

func (nbi *NetboxInventory) AddVirtualDeviceContext(ctx context.Context, newVDC *objects.VirtualDeviceContext) (*objects.VirtualDeviceContext, error)

AddVirtualDeviceContext adds new virtual device context to the local inventory.

func (*NetboxInventory) AddVlan

func (nbi *NetboxInventory) AddVlan(ctx context.Context, newVlan *objects.Vlan) (*objects.Vlan, error)

func (*NetboxInventory) AddVlanGroup

func (nbi *NetboxInventory) AddVlanGroup(ctx context.Context, newVlanGroup *objects.VlanGroup) (*objects.VlanGroup, error)

func (*NetboxInventory) DeleteOrphans

func (nbi *NetboxInventory) DeleteOrphans(ctx context.Context) error

func (*NetboxInventory) Init

func (nbi *NetboxInventory) Init() error

Init function that initializes the NetBoxInventory object with objects from Netbox.

func (*NetboxInventory) InitAdminContactRole

func (nbi *NetboxInventory) InitAdminContactRole(ctx context.Context) error

Initializes default admin contact role used for adding admin contacts of vms.

func (*NetboxInventory) InitClusterGroups

func (nbi *NetboxInventory) InitClusterGroups(ctx context.Context) error

Collects all nbClusters from Netbox API and stores them in the NetBoxInventory.

func (*NetboxInventory) InitClusterTypes

func (nbi *NetboxInventory) InitClusterTypes(ctx context.Context) error

Collects all ClusterTypes from Netbox API and stores them in the NetBoxInventory.

func (*NetboxInventory) InitClusters

func (nbi *NetboxInventory) InitClusters(ctx context.Context) error

Collects all clusters from Netbox API and stores them to local inventory.

func (*NetboxInventory) InitContactAssignments

func (nbi *NetboxInventory) InitContactAssignments(ctx context.Context) error

func (*NetboxInventory) InitContactGroups

func (nbi *NetboxInventory) InitContactGroups(ctx context.Context) error

Collects all contact groups from Netbox API and store them in the NetBoxInventory.

func (*NetboxInventory) InitContactRoles

func (nbi *NetboxInventory) InitContactRoles(ctx context.Context) error

Collects all contact roles from Netbox API and store them in the NetBoxInventory.

func (*NetboxInventory) InitContacts

func (nbi *NetboxInventory) InitContacts(ctx context.Context) error

Collects all contacts from Netbox API and store them in the NetBoxInventory.

func (*NetboxInventory) InitCustomFields

func (nbi *NetboxInventory) InitCustomFields(ctx context.Context) error

func (*NetboxInventory) InitDefaultSite added in v0.3.6

func (nbi *NetboxInventory) InitDefaultSite(ctx context.Context) error

InitDefaultSite inits default site, which is used for hosts that have no corresponding site. This is because site is required for adding new hosts.

func (*NetboxInventory) InitDefaultVlanGroup

func (nbi *NetboxInventory) InitDefaultVlanGroup(ctx context.Context) error

Inits default VlanGroup, which is required to group all Vlans that are not part of other vlangroups into it. Each vlan is indexed by their (vlanGroup, vid).

func (*NetboxInventory) InitDeviceRoles

func (nbi *NetboxInventory) InitDeviceRoles(ctx context.Context) error

Collects all deviceRoles from Netbox API and store them in the NetBoxInventory.

func (*NetboxInventory) InitDeviceTypes

func (nbi *NetboxInventory) InitDeviceTypes(ctx context.Context) error

func (*NetboxInventory) InitDevices

func (nbi *NetboxInventory) InitDevices(ctx context.Context) error

Collect all devices from Netbox API and store them in the NetBoxInventory.

func (*NetboxInventory) InitIPAddresses

func (nbi *NetboxInventory) InitIPAddresses(ctx context.Context) error

Collects all IP addresses from Netbox API and stores them to local inventory.

func (*NetboxInventory) InitInterfaces

func (nbi *NetboxInventory) InitInterfaces(ctx context.Context) error

Collects all interfaces from Netbox API and stores them to local inventory.

func (*NetboxInventory) InitManufacturers

func (nbi *NetboxInventory) InitManufacturers(ctx context.Context) error

Collects all manufacturers from Netbox API and store them in NetBoxInventory.

func (*NetboxInventory) InitPlatforms

func (nbi *NetboxInventory) InitPlatforms(ctx context.Context) error

Collects all platforms from Netbox API and store them in the NetBoxInventory.

func (*NetboxInventory) InitPrefixes

func (nbi *NetboxInventory) InitPrefixes(ctx context.Context) error

Collects all Prefixes from Netbox API and stores them to local inventory.

func (*NetboxInventory) InitSites

func (nbi *NetboxInventory) InitSites(ctx context.Context) error

Collects all sites from Netbox API and store them in the NetBoxInventory.

func (*NetboxInventory) InitSsotCustomFields

func (nbi *NetboxInventory) InitSsotCustomFields(ctx context.Context) error

This function Initializes all custom fields required for servers and other objects Currently these are two: - host_cpu_cores - host_memory - sourceId - this is used to store the ID of the source object in Netbox (interfaces).

func (*NetboxInventory) InitTags

func (nbi *NetboxInventory) InitTags(ctx context.Context) error

Collect all tags from Netbox API and store them in the NetBoxInventory.

func (*NetboxInventory) InitTenants

func (nbi *NetboxInventory) InitTenants(ctx context.Context) error

Collects all tenants from Netbox API and store them in the NetBoxInventory.

func (*NetboxInventory) InitVMInterfaces

func (nbi *NetboxInventory) InitVMInterfaces(ctx context.Context) error

Collects all VMInterfaces from Netbox API and stores them to local inventory.

func (*NetboxInventory) InitVMs

func (nbi *NetboxInventory) InitVMs(ctx context.Context) error

Collects all vms from Netbox API and stores them to local inventory.

func (*NetboxInventory) InitVirtualDeviceContexts added in v0.4.0

func (nbi *NetboxInventory) InitVirtualDeviceContexts(ctx context.Context) error

Collect all devices from Netbox API and store them in the NetBoxInventory.

func (*NetboxInventory) InitVlanGroups

func (nbi *NetboxInventory) InitVlanGroups(ctx context.Context) error

Collects all vlans from Netbox API and stores them to local inventory.

func (*NetboxInventory) InitVlans

func (nbi *NetboxInventory) InitVlans(ctx context.Context) error

Collects all vlans from Netbox API and stores them to local inventory.

func (*NetboxInventory) String

func (nbi *NetboxInventory) String() string

Func string representation.

Jump to

Keyboard shortcuts

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