aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--config/config.py46
-rw-r--r--config/test-invalid-DB.cfg5
-rw-r--r--config/test.py7
-rw-r--r--vland.cfg3
4 files changed, 55 insertions, 6 deletions
diff --git a/config/config.py b/config/config.py
index fda34d8..08d78f8 100644
--- a/config/config.py
+++ b/config/config.py
@@ -31,16 +31,22 @@ if __name__ == '__main__':
from errors import CriticalError, InputError, ConfigError
+class DaemonConfigClass:
+ """ Simple container for stuff to make for nicer syntax """
+
+ def __repr__(self):
+ return "<DaemonConfig: port: %s>" % (self.port)
+
class DBConfigClass:
""" Simple container for stuff to make for nicer syntax """
def __repr__(self):
- return "<DBConfig: server: %s, port: %s, dbname: %s, username: %s, password: %s" % (self.server, self.port, self.dbname, self.username, self.password)
+ return "<DBConfig: server: %s, port: %s, dbname: %s, username: %s, password: %s>" % (self.server, self.port, self.dbname, self.username, self.password)
class SwitchConfigClass:
""" Simple container for stuff to make for nicer syntax """
def __repr__(self):
- return "<SwitchConfig: name: %s, section: %s, driver: %s, username: %s, password: %s, enable_password: %s" % (self.name, self.section, self.driver, self.username, self.password, self.enable_password)
+ return "<SwitchConfig: name: %s, section: %s, driver: %s, username: %s, password: %s, enable_password: %s>" % (self.name, self.section, self.driver, self.username, self.password, self.enable_password)
class VlanConfig:
"""VLANd config class"""
@@ -62,12 +68,20 @@ class VlanConfig:
# Parse out the config file
# Must have a [database] section
+ # May have a [vland] section
# May have a [logging] section
# May have multiple [switch 'foo'] sections
if not config.has_section('database'):
raise ConfigError('No database configuration section found')
+ # No DB-specific defaults to set
self.database = DBConfigClass()
+
+ # Set default port number
+ self.vland = DaemonConfigClass()
+ self.vland.port = 3080
+
+ # No switch-specific defaults to set
self.switches = {}
sw_regex = re.compile('(switch)\ (.*)', flags=re.I)
@@ -75,33 +89,56 @@ class VlanConfig:
if section == 'database':
try:
self.database.server = config.get(section, 'server')
+ except ConfigParser.NoOptionError:
+ pass
except:
raise ConfigError('Invalid database configuration (server)')
try:
- self.database.port = config.get(section, 'port')
+ port = config.get(section, 'port')
+ if port is not None:
+ self.database.port = config.getint(section, 'port')
+ except ConfigParser.NoOptionError:
+ pass
except:
raise ConfigError('Invalid database configuration (port)')
try:
self.database.dbname = config.get(section, 'dbname')
+ except ConfigParser.NoOptionError:
+ pass
except:
raise ConfigError('Invalid database configuration (dbname)')
try:
self.database.username = config.get(section, 'username')
+ except ConfigParser.NoOptionError:
+ pass
except:
raise ConfigError('Invalid database configuration (username)')
try:
self.database.password = config.get(section, 'password')
+ except ConfigParser.NoOptionError:
+ pass
except:
raise ConfigError('Invalid database configuration (password)')
- # Others are optional, but these are not
+
+ # Other database config options are optional, but these are not
if self.database.dbname is None or self.database.username is None:
raise ConfigError('Database configuration section incomplete')
+
elif section == 'logging':
pass # Fill this in later...
+
+ elif section == 'vland':
+ try:
+ self.vland.port = config.getint(section, 'port')
+ except ConfigParser.NoOptionError:
+ pass
+ except:
+ raise ConfigError('Invalid vland configuration (port)')
+
else:
match = sw_regex.match(section)
if match:
@@ -127,6 +164,7 @@ class VlanConfig:
if __name__ == '__main__':
config = VlanConfig(filenames=('./vland.cfg',))
print config.database
+ print config.vland
for switch in config.switches:
print config.switches[switch]
diff --git a/config/test-invalid-DB.cfg b/config/test-invalid-DB.cfg
index 5a5107a..a80fcbc 100644
--- a/config/test-invalid-DB.cfg
+++ b/config/test-invalid-DB.cfg
@@ -1,2 +1,5 @@
[database]
-foo = bar
+port = bar
+dbname = vland
+username = vland
+
diff --git a/config/test.py b/config/test.py
index de2b7ad..1c8361e 100644
--- a/config/test.py
+++ b/config/test.py
@@ -15,7 +15,7 @@ class MyTest(unittest.TestCase):
with self.assertRaisesRegexp(ConfigError, 'No database'):
config = VlanConfig(filenames=("/dev/null",))
- # Check that we raise on missing database config values
+ # Check that we raise on broken database config values
def test_missing_database_config(self):
with self.assertRaisesRegexp(ConfigError, 'Invalid database'):
config = VlanConfig(filenames=("test-invalid-DB.cfg",))
@@ -30,6 +30,11 @@ class MyTest(unittest.TestCase):
with self.assertRaisesRegexp(ConfigError, 'Database.*incomplete'):
config = VlanConfig(filenames=("test-missing-db-username.cfg",))
+ # Check that we raise on broken vland config values
+ def test_missing_vlan_config(self):
+ with self.assertRaisesRegexp(ConfigError, 'Invalid vland'):
+ config = VlanConfig(filenames=("test-invalid-vland.cfg",))
+
# Check that we raise on repeated switch names
def test_missing_repeated_switch_names(self):
with self.assertRaisesRegexp(ConfigError, 'same name'):
diff --git a/vland.cfg b/vland.cfg
index d1584cd..00ee9fc 100644
--- a/vland.cfg
+++ b/vland.cfg
@@ -7,6 +7,9 @@ dbname = vland
username = vland
#password=
+[vland]
+port = 3080
+
[switch foo]
name = 10.172.2.51
driver = CiscoSX300