diff options
| author | Flavio Percoco <flaper87@gmail.com> | 2015-09-01 23:28:11 +0200 |
|---|---|---|
| committer | Flavio Percoco <flaper87@gmail.com> | 2015-09-04 13:31:55 +0200 |
| commit | 47423ebbb2dfcb0d63aefcb87a9bec85420a7a99 (patch) | |
| tree | 9bc204ce43097c4ff9c45bd7f51d458cc762d910 /glanceclient/shell.py | |
| parent | 90b7dc4141d18445ea40d7b6b4c3d25a2c163284 (diff) | |
| download | python-glanceclient-47423ebbb2dfcb0d63aefcb87a9bec85420a7a99.tar.gz | |
Check if v2 is available and fallback
We have a basic implementation for a fallback mechanism that will use v1
rather than v2 when downloading schema files from glance-api fails.
However, this is not sound. If the schemas are cached already, we won't
check if v2 is available and fail to fallback.
This patch fixes the aforementioned issue by getting the list of
available versions from the server only when the API versions was not
explicitly specified through the CLI. That is, for all commands that
don't pass `--os-image-api-version 2`, we'll check v2's availability and
we'll fallback to v1 if it isn't available.
This patch also changes how we handle `/versions` calls in the client.
The server has been, incorrectly, replying to requests to `/version`
with a 300 error, which ended up in the client re-raising such
exception. While I think 300 shouldn't raise an exception, I think we
should handle that in a spearate patch. Therefore, this patch just
avoids raising such exception when `/version` is explicitly called.
This fallback behaviour and the check on `/versions` will be removed in
future versions of the client. The later depends on this bug[0] being
fixed.
[0] https://bugs.launchpad.net/glance/+bug/1491350
Closes-bug: #1489381
Change-Id: Ibeba6bc86db2a97b8a2b4bd042248464cd792e5e
Diffstat (limited to 'glanceclient/shell.py')
| -rwxr-xr-x | glanceclient/shell.py | 24 |
1 files changed, 16 insertions, 8 deletions
diff --git a/glanceclient/shell.py b/glanceclient/shell.py index cba0010..0c134a4 100755 --- a/glanceclient/shell.py +++ b/glanceclient/shell.py @@ -553,7 +553,7 @@ class OpenStackImagesShell(object): client = glanceclient.Client(api_version, endpoint, **kwargs) return client - def _cache_schemas(self, options, home_dir='~/.glanceclient'): + def _cache_schemas(self, options, client, home_dir='~/.glanceclient'): homedir = os.path.expanduser(home_dir) path_prefix = homedir if options.os_auth_url: @@ -573,16 +573,11 @@ class OpenStackImagesShell(object): schema_file_paths = [os.path.join(path_prefix, x + '_schema.json') for x in ['image', 'namespace', 'resource_type']] - client = None failed_download_schema = 0 for resource, schema_file_path in zip(resources, schema_file_paths): if (not os.path.exists(schema_file_path)) or options.get_schema: try: - if not client: - client = self._get_versioned_client('2', options, - force_auth=True) schema = client.schemas.get(resource) - with open(schema_file_path, 'w') as f: f.write(json.dumps(schema.raw())) except Exception: @@ -624,8 +619,21 @@ class OpenStackImagesShell(object): "Supported values are %s" % SUPPORTED_VERSIONS) utils.exit(msg=msg) - if api_version == 2: - switch_version = self._cache_schemas(options) + if not options.os_image_api_version and api_version == 2: + switch_version = True + client = self._get_versioned_client('2', options, + force_auth=True) + + resp, body = client.http_client.get('/versions') + + for version in body['versions']: + if version['id'].startswith('v2'): + # NOTE(flaper87): We know v2 is enabled in the server, + # which means we should be able to get the schemas and + # move on. + switch_version = self._cache_schemas(options, client) + break + if switch_version: print('WARNING: The client is falling back to v1 because' ' the accessing to v2 failed. This behavior will' |
