2019-08-04 01:11:53 +02:00
|
|
|
import importlib
|
2019-08-04 09:48:42 +02:00
|
|
|
import importlib.machinery
|
2019-08-04 01:11:53 +02:00
|
|
|
|
2019-08-04 09:48:42 +02:00
|
|
|
from netbox_agent.config import DATACENTER_LOCATION, DATACENTER_LOCATION_DRIVER_FILE, \
|
2019-08-04 01:11:53 +02:00
|
|
|
DATACENTER_LOCATION_REGEX
|
|
|
|
|
|
|
|
|
|
|
|
class Datacenter():
|
|
|
|
"""
|
2019-08-04 09:55:10 +02:00
|
|
|
This class is used to guess the datacenter in order to push the information
|
|
|
|
in Netbox for a `Device`
|
|
|
|
|
|
|
|
A driver takes a `value` and evaluates a regex with a `named group`: `datacenter`.
|
|
|
|
|
|
|
|
There's embeded drivers such as `file` or `cmd` which read a file or return the
|
|
|
|
output of a file.
|
|
|
|
|
|
|
|
There's also a support for an external driver file outside of this project in case
|
|
|
|
the logic isn't supported here.
|
2019-08-04 01:11:53 +02:00
|
|
|
"""
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.driver = DATACENTER_LOCATION.split(':')[0]
|
|
|
|
self.driver_value = DATACENTER_LOCATION.split(':')[1]
|
2019-08-04 09:48:42 +02:00
|
|
|
self.driver_file = DATACENTER_LOCATION_DRIVER_FILE
|
2019-08-04 01:11:53 +02:00
|
|
|
|
2019-08-04 09:48:42 +02:00
|
|
|
if self.driver_file:
|
|
|
|
try:
|
|
|
|
# FIXME: Works with Python 3.3+, support older version?
|
|
|
|
loader = importlib.machinery.SourceFileLoader('driver_file', self.driver_file)
|
|
|
|
self.driver = loader.load_module()
|
|
|
|
except ImportError:
|
|
|
|
raise ImportError("Couldn't import {} as a module".format(self.driver_file))
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
self.driver = importlib.import_module(
|
|
|
|
'netbox_agent.drivers.datacenter_{}'.format(self.driver)
|
|
|
|
)
|
|
|
|
except ImportError:
|
|
|
|
raise ImportError("Driver {} doesn't exists".format(self.driver))
|
2019-08-04 01:11:53 +02:00
|
|
|
|
|
|
|
def get(self):
|
2019-08-04 19:18:11 +02:00
|
|
|
if not hasattr(self.driver, 'get'):
|
2019-08-04 19:32:52 +02:00
|
|
|
raise Exception(
|
|
|
|
"Your driver {} doesn't have a get() function, please fix it".format(self.driver)
|
|
|
|
)
|
2019-08-04 01:11:53 +02:00
|
|
|
return getattr(self.driver, 'get')(self.driver_value, DATACENTER_LOCATION_REGEX)
|