Parcourir la source

[2380] allow -c omittable.

JINMEI Tatuya il y a 12 ans
Parent
commit
437477418c

+ 2 - 0
doc/guide/bind10-guide.xml

@@ -451,6 +451,8 @@ var/
           <para>Load desired zone file(s), for example:
             <screen>$ <userinput>b10-loadzone <replaceable>-c '{"database_file": "/usr/local/var/bind10/zone.sqlite3"}'</replaceable> <replaceable>your.zone.example.org</replaceable> <replaceable>your.zone.file</replaceable></userinput></screen>
           </para>
+	  (If you use the sqlite3 data source with the default DB
+	  file, you can omit the -c option).
         </listitem>
 
         <listitem>

+ 24 - 20
src/bin/loadzone/b10-loadzone.xml

@@ -44,11 +44,11 @@
   <refsynopsisdiv>
     <cmdsynopsis>
       <command>b10-loadzone</command>
+      <arg><option>-c <replaceable class="parameter">datasrc_config</replaceable></option></arg>
       <arg><option>-d <replaceable class="parameter">debug_level</replaceable></option></arg>
       <arg><option>-i <replaceable class="parameter">report_interval</replaceable></option></arg>
       <arg><option>-t <replaceable class="parameter">datasrc_type</replaceable></option></arg>
       <arg><option>-C <replaceable class="parameter">zone_class</replaceable></option></arg>
-      <arg choice="req">-c <replaceable class="parameter">datasrc_config</replaceable></arg>
       <arg choice="req">zone name</arg>
       <arg choice="req">zone file</arg>
     </cmdsynopsis>
@@ -110,6 +110,27 @@
     <title>ARGUMENTS</title>
 
     <variablelist>
+      <varlistentry>
+        <term>-c <replaceable class="parameter">datasrc_config</replaceable></term>
+        <listitem><para>
+          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"}'.
+	  <note>
+	    <simpara>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).
+	  </simpara></note>
+        </para></listitem>
+      </varlistentry>
 
       <varlistentry>
         <term>-d <replaceable class="parameter">debug_level</replaceable> </term>
@@ -154,24 +175,6 @@
       </varlistentry>
 
       <varlistentry>
-        <term>-c <replaceable class="parameter">datasrc_config</replaceable></term>
-        <listitem><para>
-          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"}'.
-	  <note>
-	    <simpara>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).
-	  </simpara></note>
-        </para></listitem>
-      </varlistentry>
-
-      <varlistentry>
         <term><replaceable class="parameter">zone name</replaceable></term>
         <listitem><para>
           The name of the zone to create or update.  This must be a valid DNS
@@ -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.
-
+    </para>
+    <para>
       There are some other issues noted in the DESCRIPTION section.
     </para>
   </refsect1>

+ 26 - 6
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.
 

+ 5 - 0
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.

+ 22 - 14
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