diff options
author | Hervé Beraud <hberaud@redhat.com> | 2021-04-15 13:44:42 +0200 |
---|---|---|
committer | Daniel Bengtsson <dbengt@redhat.com> | 2021-04-22 12:31:48 +0200 |
commit | bb5e4cbeb973c6341bf51eea2ebe5d38366167e8 (patch) | |
tree | a1b54841832b596f19ca5efe2449c5d4d661a55c | |
parent | b38217691e3594c323d4a9d98a682e7acfec5a0a (diff) | |
download | oslo-config-bb5e4cbeb973c6341bf51eea2ebe5d38366167e8.tar.gz |
Adding the missing HostDomain config option
The ``HostDomain`` config type have been added few months ago [1]
however the config option have been forgotten and this new type
isn't importable.
When we try to import this type without defining a new related cfg
option we get the following issue:
```
AttributeError: module 'oslo_config.cfg' has no attribute 'HostDomain'
```
These changes allow us to import this new type and allow us to use
it in our configs:
```
>>> from oslo_config import cfg
>>> foo = cfg.HostDomain('foo')
>>> foo.type.__call__("1")
...
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "~/oslo.config/oslo_config/types.py", line 893, in __call__
raise ValueError(
ValueError: 1 is not a valid host address
>>> foo.type.__call__("host_name")
'host_name'
```
Also properly initialize HostDomain because The HostDomain class wasn't
calling super in it's __init__() method, which resulted in the type_name not
being set properly for instances of that class.
[1] https://opendev.org/openstack/oslo.config/commit/6480356928c9ae6169ea1e5a5b5f1df3d6e0dc75
Change-Id: Ie947803f61ba0ef080018e0447de894a400d7975
Closes-Bug: 1924283
-rw-r--r-- | oslo_config/cfg.py | 22 | ||||
-rw-r--r-- | oslo_config/tests/test_generator.py | 27 | ||||
-rw-r--r-- | oslo_config/types.py | 7 | ||||
-rw-r--r-- | releasenotes/notes/add-missing-config-option-9ee1992eea750200.yaml | 5 |
4 files changed, 61 insertions, 0 deletions
diff --git a/oslo_config/cfg.py b/oslo_config/cfg.py index 39a9878..35f0cfb 100644 --- a/oslo_config/cfg.py +++ b/oslo_config/cfg.py @@ -1175,6 +1175,28 @@ class HostAddressOpt(Opt): **kwargs) +class HostDomainOpt(Opt): + + r"""Option for either an IP or a hostname. + + Like HostAddress with the support of _ character. + + Option with ``type`` :class:`oslo_config.types.HostDomain` + + :param name: the option's name + :param version: one of either ``4``, ``6``, or ``None`` to specify + either version. + :param \*\*kwargs: arbitrary keyword arguments passed to :class:`Opt` + + .. versionadded:: 8.6 + """ + + def __init__(self, name, version=None, **kwargs): + super(HostDomainOpt, self).__init__(name, + type=types.HostDomain(version), + **kwargs) + + class URIOpt(Opt): r"""Opt with URI type diff --git a/oslo_config/tests/test_generator.py b/oslo_config/tests/test_generator.py index d75b4d8..6f95176 100644 --- a/oslo_config/tests/test_generator.py +++ b/oslo_config/tests/test_generator.py @@ -1872,5 +1872,32 @@ class HostAddressTestCase(base.BaseTestCase): self.assertEqual(expected, result) +class HostDomainTestCase(base.BaseTestCase): + + opts = [cfg.HostDomainOpt('foo', help='foo option', default='0.0.0.0')] + + def test_host_domain(self): + + config = [("namespace", [("alpha", self.opts)])] + groups = generator._get_groups(config) + + out = io.StringIO() + formatter = build_formatter(out) + generator._output_opts(formatter, 'alpha', groups.pop('alpha')) + result = out.getvalue() + + expected = textwrap.dedent(''' + [alpha] + + # + # From namespace + # + + # foo option (host domain value) + #foo = 0.0.0.0 + ''').lstrip() + self.assertEqual(expected, result) + + GeneratorTestCase.generate_scenarios() MachineReadableGeneratorTestCase.generate_scenarios() diff --git a/oslo_config/types.py b/oslo_config/types.py index 72242cb..8ebc6e6 100644 --- a/oslo_config/types.py +++ b/oslo_config/types.py @@ -871,6 +871,13 @@ class HostDomain(HostAddress): # DOMAIN_REGEX is HOSTNAME_REGEX with the _ character added DOMAIN_REGEX = '(?!-)[A-Z0-9-_]{1,63}(?<!-)$' + def __init__(self, version=None, type_name='host domain value'): + """Check for valid version in case an IP address is provided + + """ + + super(HostDomain, self).__init__(type_name=type_name) + def __call__(self, value): """Checks if is a valid IP/hostname. diff --git a/releasenotes/notes/add-missing-config-option-9ee1992eea750200.yaml b/releasenotes/notes/add-missing-config-option-9ee1992eea750200.yaml new file mode 100644 index 0000000..e6b1fc8 --- /dev/null +++ b/releasenotes/notes/add-missing-config-option-9ee1992eea750200.yaml @@ -0,0 +1,5 @@ +--- +fixes: + - | + Adding the missing ``HostDomain`` config option. Previously this available + type couldn't been imported because no related config option was defined. |