From: JINMEI Tatuya Date: Tue, 18 Dec 2012 07:46:05 +0000 (-0800) Subject: [2380] allow -c omittable. X-Git-Tag: bind10-1.0.0-beta-release~9^2~14^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=437477418c413c8a56adcb59441984dd5399f8a5;p=thirdparty%2Fkea.git [2380] allow -c omittable. --- diff --git a/doc/guide/bind10-guide.xml b/doc/guide/bind10-guide.xml index 72892d9378..336cc9bb4c 100644 --- a/doc/guide/bind10-guide.xml +++ b/doc/guide/bind10-guide.xml @@ -451,6 +451,8 @@ var/ Load desired zone file(s), for example: $ b10-loadzone -c '{"database_file": "/usr/local/var/bind10/zone.sqlite3"}' your.zone.example.org your.zone.file + (If you use the sqlite3 data source with the default DB + file, you can omit the -c option). diff --git a/src/bin/loadzone/b10-loadzone.xml b/src/bin/loadzone/b10-loadzone.xml index 5174f440bc..d1815036de 100644 --- a/src/bin/loadzone/b10-loadzone.xml +++ b/src/bin/loadzone/b10-loadzone.xml @@ -44,11 +44,11 @@ b10-loadzone + - -c datasrc_config zone name zone file @@ -110,6 +110,27 @@ ARGUMENTS + + -c datasrc_config + + Specifies configuration of the data source in the JSON + format. The configuration contents depend on the type of + the data source, and that's the same as what would be + specified for the BIND 10 servers (see the data source + configuration section of the BIND 10 guide). For example, + for an SQLite3 data source, it would look like + '{"database_file": "path-to-sqlite3-db-file"}'. + + For SQLite3 data source with the default DB file, + this option can be omitted; in other cases including + for any other types of data sources when supported, + this option is currently mandatory in practice. + In a future version it will be possible to retrieve the + configuration from the BIND 10 server configuration (if + it exists). + + + -d debug_level @@ -153,24 +174,6 @@ - - -c datasrc_config - - Specifies configuration of the data source in the JSON - format. The configuration contents depend on the type of - the data source, and that's the same as what would be - specified for the BIND 10 servers (see the data source - configuration section of the BIND 10 guide). For example, - for an SQLite3 data source, it would look like - '{"database_file": "path-to-sqlite3-db-file"}'. - - This option currently cannot be omitted. In a future - version it will be possible to retrieve the configuration from - the BIND 10 server configuration (if it exists). - - - - zone name @@ -233,7 +236,8 @@ responsibility to fix the errors and reload it. When the library is updated with the post load checks, it will be more sophisticated and the such zone won't be successfully loaded. - + + There are some other issues noted in the DESCRIPTION section. diff --git a/src/bin/loadzone/loadzone.py.in b/src/bin/loadzone/loadzone.py.in index b30ed148fb..fce1b20ef4 100755 --- a/src/bin/loadzone/loadzone.py.in +++ b/src/bin/loadzone/loadzone.py.in @@ -56,9 +56,8 @@ def set_cmd_options(parser): ''' parser.add_option("-c", "--datasrc-conf", dest="conf", action="store", - help="""(Mandatory) configuration of datasrc to load -the zone in. Example: -'{"database_file": "/path/to/dbfile/db.sqlite3"}'""", + help="""configuration of datasrc to load the zone in. +Example: '{"database_file": "/path/to/dbfile/db.sqlite3"}'""", metavar='CONFIG') parser.add_option("-d", "--debug", dest="debug_level", type='int', action="store", default=None, @@ -149,10 +148,10 @@ class LoadZoneRunner: self._log_debuglevel) self._config_log() - if options.conf is None: - raise BadArgument('data source config option cannot be omitted') - self._datasrc_config = options.conf self._datasrc_type = options.datasrc_type + self._datasrc_config = options.conf + if options.conf is None: + self._datasrc_config = self._get_datasrc_config(self._datasrc_type) try: self._zone_class = RRClass(options.zone_class) except isc.dns.InvalidRRClass as ex: @@ -177,6 +176,27 @@ class LoadZoneRunner: str(ex)) self._zone_file = args[1] + def _get_datasrc_config(self, datasrc_type): + ''''Return the default data source configuration of given type. + + Right now, it only supports SQLite3, and hardcodes the syntax + of the default configuration. It's a kind of workaround to balance + convenience of users and minimizing hardcoding of data source + specific logic in the entire tool. In future this should be + more sophisticated. + + This is essentially a private helper method for _parse_arg(), + but defined as "protected" so tests can use it directly. + + ''' + if datasrc_type != 'sqlite3': + raise BadArgument('default config is not available for ' + + datasrc_type) + + default_db_file = bind10_config.DATA_PATH + '/zone.sqlite3' + logger.info(LOADZONE_SQLITE3_USING_DEFAULT_CONFIG, default_db_file) + return '{"database_file": "' + default_db_file + '"}' + def __cancel_create(self): '''sqlite3-only hack: delete the zone just created on load failure. diff --git a/src/bin/loadzone/loadzone_messages.mes b/src/bin/loadzone/loadzone_messages.mes index 3dfdaa51ba..86800795b3 100644 --- a/src/bin/loadzone/loadzone_messages.mes +++ b/src/bin/loadzone/loadzone_messages.mes @@ -65,3 +65,8 @@ have been cleanly canceled in this case, too. The specified zone to b10-loadzone to load does not exist in the specified data source. b10-loadzone has created a new empty zone in the data source. + +% LOADZONE_SQLITE3_USING_DEFAULT_CONFIG Using default configuration with SQLite3 DB file %1 +The SQLite3 data source is specified as the data source type without a +data source configuration. b10-loadzone uses the default +configuration with the default DB file for the BIND 10 system. diff --git a/src/bin/loadzone/tests/loadzone_test.py b/src/bin/loadzone/tests/loadzone_test.py index 9817e12d4d..4f13c5d458 100755 --- a/src/bin/loadzone/tests/loadzone_test.py +++ b/src/bin/loadzone/tests/loadzone_test.py @@ -20,6 +20,7 @@ from loadzone import * from isc.dns import * from isc.datasrc import * import isc.log +import bind10_config import os import shutil @@ -89,36 +90,28 @@ class TestLoadZoneRunner(unittest.TestCase): self.assertEqual(1, runner._log_debuglevel) def test_parse_bad_args(self): - # -c cannot be omitted (right now) - self.assertRaises(BadArgument, - LoadZoneRunner(['example', 'example.zone']). - _parse_args) - - copt = ['-c', '0'] # template for the mandatory -c option - # There must be exactly 2 non-option arguments: zone name and zone file - self.assertRaises(BadArgument, LoadZoneRunner(copt)._parse_args) - self.assertRaises(BadArgument, LoadZoneRunner(copt + ['example']). + self.assertRaises(BadArgument, LoadZoneRunner([])._parse_args) + self.assertRaises(BadArgument, LoadZoneRunner(['example']). _parse_args) self.assertRaises(BadArgument, LoadZoneRunner(self.__args + ['0']). _parse_args) # Bad zone name + args = ['example.org', 'example.zone'] # otherwise valid args self.assertRaises(BadArgument, - LoadZoneRunner(copt + ['bad..name', 'example.zone']). + LoadZoneRunner(['bad..name', 'example.zone'] + args). _parse_args) # Bad class name self.assertRaises(BadArgument, - LoadZoneRunner(copt + ['-C', 'badclass']). + LoadZoneRunner(['-C', 'badclass'] + args). _parse_args) # Unsupported class self.assertRaises(BadArgument, - LoadZoneRunner(copt + ['-C', 'CH']). - _parse_args) + LoadZoneRunner(['-C', 'CH'] + args)._parse_args) # bad debug level - args = copt + ['example.org', 'example.zone'] # otherwise valid args self.assertRaises(BadArgument, LoadZoneRunner(['-d', '-10'] + args)._parse_args) @@ -126,6 +119,21 @@ class TestLoadZoneRunner(unittest.TestCase): self.assertRaises(BadArgument, LoadZoneRunner(['-i', '-5'] + args)._parse_args) + # -c cannot be omitted unless it's type sqlite3 (right now) + self.assertRaises(BadArgument, + LoadZoneRunner(['-t', 'memory'] + args)._parse_args) + + def test_get_datasrc_config(self): + # For sqlite3, we use the config with the well-known DB file. + expected_conf = \ + '{"database_file": "' + bind10_config.DATA_PATH + '/zone.sqlite3"}' + self.assertEqual(expected_conf, + self.__runner._get_datasrc_config('sqlite3')) + + # For other types, config must be given by hand for now + self.assertRaises(BadArgument, self.__runner._get_datasrc_config, + 'memory') + def __common_load_setup(self): self.__runner._zone_class = RRClass.IN() self.__runner._zone_name = TEST_ZONE_NAME