diff options
author | Flavio Percoco <flaper87@gmail.com> | 2014-12-10 10:47:40 +0100 |
---|---|---|
committer | Flavio Percoco <flaper87@gmail.com> | 2014-12-10 13:23:36 +0100 |
commit | 3989cd202de5d122da011be4f3bb9cede2fe8d45 (patch) | |
tree | 860308aeb2568192cfaab1d8a80b1014be701cdc /glanceclient/common/utils.py | |
parent | 9829d7b6b9f2232bda8039b6db54be1d8885e7a0 (diff) | |
download | python-glanceclient-3989cd202de5d122da011be4f3bb9cede2fe8d45.tar.gz |
Support schema types with non-str value
Change I75da1e9309e0f7ef8839dea3ec9c99c58edc5d63 introduced some
properties' types which are not string. This broke the `schema_args`
utility since lists are not hashable and there was no support for such
type values.
This patch fixes this issue with a very glance specific strategy in
which this values are assumed to have a `null` type and another type -
string, integer, etc. The fix ignores `null` options and it takes the
first non-null type as the valid one.
The patch adds support for enum types that accept `None`
Closes-bug: #1401032
Change-Id: I250e8912aca262a56c54ac59bb24f917e5d8cfce
Diffstat (limited to 'glanceclient/common/utils.py')
-rw-r--r-- | glanceclient/common/utils.py | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/glanceclient/common/utils.py b/glanceclient/common/utils.py index c0d16a5..fe7a51a 100644 --- a/glanceclient/common/utils.py +++ b/glanceclient/common/utils.py @@ -81,6 +81,17 @@ def schema_args(schema_getter, omit=None): kwargs = {} type_str = property.get('type', 'string') + + if isinstance(type_str, list): + # NOTE(flaper87): This means the server has + # returned something like `['null', 'string']`, + # therfore we use the first non-`null` type as + # the valid type. + for t in type_str: + if t != 'null': + type_str = t + break + if type_str == 'array': items = property.get('items') kwargs['type'] = typemap.get(items.get('type')) @@ -97,8 +108,13 @@ def schema_args(schema_getter, omit=None): if 'enum' in property: if len(description): description += " " - description += ("Valid values: " + - ', '.join(property.get('enum'))) + + # NOTE(flaper87): Make sure all values are `str/unicode` + # for the `join` to succeed. Enum types can also be `None` + # therfore, join's call would fail without the following + # list comprehension + vals = [six.text_type(val) for val in property.get('enum')] + description += ('Valid values: ' + ', '.join(vals)) kwargs['help'] = description func.__dict__.setdefault('arguments', |