netbox

Netbox module.

exception spicerack.netbox.NetboxAPIError[source]

Bases: spicerack.netbox.NetboxError

Usually a wrapper for pynetbox.RequestError, errors that occur when accessing the API.

exception spicerack.netbox.NetboxError[source]

Bases: spicerack.exceptions.SpicerackError

General errors raised by this module.

exception spicerack.netbox.NetboxHostNotFoundError[source]

Bases: spicerack.netbox.NetboxError

Raised when a host is not found for an operation.

class spicerack.netbox.Netbox(url: str, token: str, *, dry_run: bool = True)[source]

Bases: object

Class which wraps Netbox API operations.

Create Netbox instance.

Parameters
  • url (str) -- The Netbox top level URL (with scheme and port if necessary)

  • token (str) -- A Netbox API token

  • dry_run (bool, optional) -- set to False to cause writes to Netbox to occur

fetch_host_detail(hostname: str) Dict[source]

Return a dict containing details about the host.

. deprecated:: v0.0.50

use spicerack.netbox.NetboxServer instead.

Parameters

hostname (str) -- the name of the host to retrieve.

Returns

data about the host

Return type

dict

Raises
fetch_host_status(hostname: str) str[source]

Return the current status of a host as a string.

. deprecated:: v0.0.50

use spicerack.netbox.NetboxServer instead.

Parameters

hostname (str) -- the name of the host status

Returns

the normalized status name

Return type

str

Raises
get_server(hostname: str) spicerack.netbox.NetboxServer[source]

Return a NetboxServer instance for the given hostname.

Parameters

hostname (str) -- the device hostname.

Raises
Returns

the server instance.

Return type

spicerack.netbox.NetboxServer

put_host_status(hostname: str, status: str) None[source]

Set the device status.

. deprecated:: v0.0.50

use spicerack.netbox.NetboxServer instead.

Note

This method does not operate on virtual machines since they are updated automatically from Ganeti into Netbox.

Parameters
  • hostname (str) -- the name of the host to operate on

  • status (str) -- A status label or name

Raises
property api: pynetbox.core.api.Api

Getter for the Netbox API property.

Todo

When feasible expose instead higher level functionalities.

Returns

the Netbox API instance.

Return type

pynetbox.api

class spicerack.netbox.NetboxServer(*, api: pynetbox.core.api.Api, server: Union[pynetbox.models.dcim.Devices, pynetbox.models.virtualization.VirtualMachines], dry_run: bool = True)[source]

Bases: object

Represent a Netbox device of role server or a virtual machine.

Initialize the instance.

Parameters
  • api (pynetbox.api) -- the API instance to connect to Netbox.

  • server (pynetbox.models.dcim.Devices, pynetbox.models.virtualization.VirtualMachines) -- the server object.

Raises

spicerack.netbox.NetboxError -- if the device is not of type server.

as_dict() Dict[source]

Return a dict containing details about the server.

Returns

with the whole data about the server.

Return type

dict

allowed_status_transitions = {'active': ('staged', 'decommissioned'), 'decommissioned': ('staged', 'spare'), 'failed': ('spare', 'planned', 'staged', 'decommissioned'), 'planned': ('staged', 'failed'), 'spare': ('planned', 'failed', 'decommissioned'), 'staged': ('failed', 'active', 'decommissioned')}

See https://wikitech.wikimedia.org/wiki/Server_Lifecycle#/media/File:Server_Lifecycle_Statuses.png

Type

dict

property asset_tag_fqdn: str

Return the management FQDN for the asset tag of the device.

Returns

the asset tag management FQDN.

Return type

str

Raises

spicerack.netbox.NetboxError -- for virtual servers or the server has no management FQDN defined in Netbox.

property fqdn: str

Return the FQDN of the device.

Returns

the FQDN.

Return type

str

Raises

spicerack.netbox.NetboxError -- if the server has no FQDN defined in Netbox.

property mgmt_fqdn: str

Return the management FQDN of the device.

Returns

the management FQDN.

Return type

str

Raises

spicerack.netbox.NetboxError -- for virtual servers or the server has no management FQDN defined in Netbox.

property status: str

Getter for the server status property.

Returns

the status name.

Return type

str

property virtual: bool

Getter to check if the server is physical or virtual.

Returns

True if the server is virtual, False if physical.

Return type

bool