Ken'ichi Ohmichi | 2b6012b | 2015-09-03 01:56:19 +0000 | [diff] [blame] | 1 | # Copyright (c) 2015 Hewlett-Packard Development Company, L.P. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | # not use this file except in compliance with the License. You may obtain |
| 5 | # a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations |
| 13 | # under the License. |
| 14 | |
| 15 | from oslo_serialization import jsonutils as json |
| 16 | from six.moves import urllib |
| 17 | |
| 18 | from tempest.api_schema.response.compute.v2_1 import versions as schema |
| 19 | from tempest.common import service_client |
| 20 | |
| 21 | |
| 22 | class VersionsClient(service_client.ServiceClient): |
| 23 | |
Ken'ichi Ohmichi | 5085f06 | 2015-09-04 02:12:42 +0000 | [diff] [blame] | 24 | def _get_base_version_url(self): |
Ken'ichi Ohmichi | 2b6012b | 2015-09-03 01:56:19 +0000 | [diff] [blame] | 25 | # NOTE: The URL which is gotten from keystone's catalog contains |
| 26 | # API version and project-id like "v2/{project-id}", but we need |
| 27 | # to access the URL which doesn't contain them for getting API |
| 28 | # versions. For that, here should use raw_request() instead of |
| 29 | # get(). |
| 30 | endpoint = self.base_url |
| 31 | url = urllib.parse.urlparse(endpoint) |
Ken'ichi Ohmichi | 5085f06 | 2015-09-04 02:12:42 +0000 | [diff] [blame] | 32 | return '%s://%s/' % (url.scheme, url.netloc) |
Ken'ichi Ohmichi | 2b6012b | 2015-09-03 01:56:19 +0000 | [diff] [blame] | 33 | |
Ken'ichi Ohmichi | 5085f06 | 2015-09-04 02:12:42 +0000 | [diff] [blame] | 34 | def list_versions(self): |
| 35 | version_url = self._get_base_version_url() |
Ken'ichi Ohmichi | 2b6012b | 2015-09-03 01:56:19 +0000 | [diff] [blame] | 36 | resp, body = self.raw_request(version_url, 'GET') |
| 37 | body = json.loads(body) |
| 38 | self.validate_response(schema.list_versions, resp, body) |
| 39 | return service_client.ResponseBody(resp, body) |
Ken'ichi Ohmichi | 5085f06 | 2015-09-04 02:12:42 +0000 | [diff] [blame] | 40 | |
| 41 | def get_version_by_url(self, version_url): |
| 42 | """Get the version document by url. |
| 43 | |
| 44 | This gets the version document for a url, useful in testing |
| 45 | the contents of things like /v2/ or /v2.1/ in Nova. That |
| 46 | controller needs authenticated access, so we have to get |
| 47 | ourselves a token before making the request. |
| 48 | |
| 49 | """ |
| 50 | # we need a token for this request |
| 51 | resp, body = self.raw_request(version_url, 'GET', |
| 52 | {'X-Auth-Token': self.token}) |
| 53 | body = json.loads(body) |
| 54 | self.validate_response(schema.get_one_version, resp, body) |
| 55 | return service_client.ResponseBody(resp, body) |