add datacenter location awareness with pseudo-driver (cmd and file) #4

Merged
Solvik merged 10 commits from feature/datacenter into master 2019-08-04 20:09:29 +02:00
2 changed files with 20 additions and 7 deletions
Showing only changes of commit 6b24b4b000 - Show all commits

View file

@ -11,5 +11,6 @@ netbox_instance = pynetbox.api(
)
DATACENTER_LOCATION = config['datacenter_location']['driver']
DATACENTER_LOCATION_REGEX = config['datacenter_location']['regex']
DATACENTER_LOCATION_DRIVER_FILE = config.get('datacenter_location').get('driver_file')
DATACENTER_LOCATION = config.get('datacenter_location').get('driver')
DATACENTER_LOCATION_REGEX = config.get('datacenter_location').get('regex')
blotus commented 2019-08-04 18:58:28 +02:00 (Migrated from github.com)
Review

Probably need some sanity check as the drivers (for now) require the regex to have a datacenter named group.
I think there is 3 solutions :

  • Check if the regex has a datacenter named group (not a big fan)
  • Remove the requirement of the named capture group, and just grab whatever is in the first capturing group of the regex.
  • Allow user to configure the name of group or the position in the configuration file (and defaulting to the 1st capturing group)
Probably need some sanity check as the drivers (for now) require the regex to have a `datacenter` named group. I think there is 3 solutions : - Check if the regex has a `datacenter` named group (not a big fan) - Remove the requirement of the named capture group, and just grab whatever is in the first capturing group of the regex. - Allow user to configure the name of group or the position in the configuration file (and defaulting to the 1st capturing group)
Solvik commented 2019-08-04 19:52:31 +02:00 (Migrated from github.com)
Review

fixed in 9f0db28
for the sanity check, will address that in a PR around configuration file

fixed in 9f0db28 for the sanity check, will address that in a PR around configuration file

View file

@ -1,6 +1,7 @@
import importlib
import importlib.machinery
from netbox_agent.config import DATACENTER_LOCATION, \
from netbox_agent.config import DATACENTER_LOCATION, DATACENTER_LOCATION_DRIVER_FILE, \
DATACENTER_LOCATION_REGEX
@ -10,11 +11,22 @@ class Datacenter():
def __init__(self, *args, **kwargs):
self.driver = DATACENTER_LOCATION.split(':')[0]
self.driver_value = DATACENTER_LOCATION.split(':')[1]
self.driver_file = DATACENTER_LOCATION_DRIVER_FILE
try:
self.driver = importlib.import_module('netbox_agent.drivers.datacenter_' + self.driver)
except ImportError:
raise ImportError("Driver {} doesn't exists".format(self.driver))
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))
def get(self):
return getattr(self.driver, 'get')(self.driver_value, DATACENTER_LOCATION_REGEX)