config option 's parameter choices can check value before using it
Bug #1471168 reported by
ChangBo Guo(gcb)
This bug affects 1 person
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
OpenStack Compute (nova) |
Fix Released
|
Undecided
|
ChangBo Guo(gcb) |
Bug Description
In oslo.config StrOpt provide parameter choices, that means the option's value must be in the choices, otherewise an exception will be raised. The current exception is NoSuchOptError[1], a more meaningful exception should be raised [2].
In other word, if an exception will be raised if we set wrong value(not in choices). So in Nova code, we don't need check the value if is valid. The check code is duplicated , we can remove them.
[1]https:/
[2]https:/
an example the usage of choices:
[gcb@localhost my_code]$ cat test_oslo_config.py
import sys
from oslo_config import cfg
if __name__ == '__main__':
cfg.StrOpt( 'type',
default= 'kvm-x' ,
choices= ('kvm', 'lxc', 'qemu', 'uml', 'xen', 'parallels'),
help= 'Libvirt domain type'),
opts = [
]
cfg. CONF.register_ opts(opts) CONF(sys. argv[1: ], project='test', version='0.1', default_ config_ files=None) virt_type)
cfg.
virt_type = cfg.CONF.type
print(
[gcb@localhost my_code]$ python test_oslo_config.py python2. 7/site- packages/ oslo_config/ types.py( 57)__call_ _() config. py", line 14, in <module> python2. 7/site- packages/ oslo_config/ cfg.py" , line 1870, in __getattr__ name) cfg.NoSuchOptEr ror: no such option: type
> /usr/lib/
-> if self.quotes and value:
(Pdb) c
Traceback (most recent call last):
File "test_oslo_
virt_type = cfg.CONF.type
File "/usr/lib/
raise NoSuchOptError(
oslo_config.
I'm also fixing bug https:/ /bugs.launchpad .net/bugs/ 1471149 , that will result like config. py", line 14, in <module> python2. 7/site- packages/ oslo_config/ cfg.py" , line 1868, in __getattr__ python2. 7/site- packages/ oslo_config/ cfg.py" , line 2240, in _get python2. 7/site- packages/ oslo_config/ cfg.py" , line 2296, in _do_get opt.default) python2. 7/site- packages/ oslo_config/ cfg.py" , line 2272, in convert _substitute( value, group, namespace), opt) python2. 7/site- packages/ oslo_config/ cfg.py" , line 2341, in _convert_value python2. 7/site- packages/ oslo_config/ types.py" , line 68, in __call__
[gcb@localhost my_code]$ python test_oslo_config.py
Traceback (most recent call last):
File "test_oslo_
virt_type = cfg.CONF.type
File "/usr/lib/
return self._get(name)
File "/usr/lib/
value = self._do_get(name, group, namespace)
File "/usr/lib/
return convert(
File "/usr/lib/
self.
File "/usr/lib/
return opt.type(value)
File "/usr/lib/
repr(value)))
ValueError: Valid values are [kvm, lxc, qemu, uml, xen, parallels], but found 'kvm-x'