Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [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 | |
Sergey Nikitin | ba67867 | 2016-02-25 12:55:51 +0300 | [diff] [blame] | 15 | import re |
| 16 | |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 17 | from oslo_serialization import jsonutils as json |
| 18 | from six.moves import urllib |
| 19 | |
| 20 | from tempest.lib.api_schema.response.compute.v2_1 import versions as schema |
| 21 | from tempest.lib.common import rest_client |
Ghanshyam | ee9af30 | 2016-02-25 06:12:43 +0900 | [diff] [blame] | 22 | from tempest.lib.services.compute import base_compute_client |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 23 | |
| 24 | |
Ghanshyam | ee9af30 | 2016-02-25 06:12:43 +0900 | [diff] [blame] | 25 | class VersionsClient(base_compute_client.BaseComputeClient): |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 26 | |
| 27 | def _get_base_version_url(self): |
Sergey Nikitin | ba67867 | 2016-02-25 12:55:51 +0300 | [diff] [blame] | 28 | # NOTE: The URL which is got from keystone's catalog contains |
| 29 | # API version and project-id like "/app-name/v2/{project-id}" or |
| 30 | # "/v2/{project-id}", but we need to access the URL which doesn't |
| 31 | # contain API version for getting API versions. For that, here |
| 32 | # should use raw_request() instead of get(). |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 33 | endpoint = self.base_url |
Sergey Nikitin | ba67867 | 2016-02-25 12:55:51 +0300 | [diff] [blame] | 34 | url = urllib.parse.urlsplit(endpoint) |
| 35 | new_path = re.split(r'(^|/)+v\d+(\.\d+)?', url.path)[0] |
| 36 | url = list(url) |
| 37 | url[2] = new_path + '/' |
| 38 | return urllib.parse.urlunsplit(url) |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 39 | |
| 40 | def list_versions(self): |
| 41 | version_url = self._get_base_version_url() |
| 42 | resp, body = self.raw_request(version_url, 'GET') |
Ken'ichi Ohmichi | 3b05049 | 2016-10-20 11:51:09 -0700 | [diff] [blame^] | 43 | self._error_checker(resp, body) |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 44 | body = json.loads(body) |
| 45 | self.validate_response(schema.list_versions, resp, body) |
| 46 | return rest_client.ResponseBody(resp, body) |
| 47 | |
| 48 | def get_version_by_url(self, version_url): |
| 49 | """Get the version document by url. |
| 50 | |
| 51 | This gets the version document for a url, useful in testing |
| 52 | the contents of things like /v2/ or /v2.1/ in Nova. That |
| 53 | controller needs authenticated access, so we have to get |
| 54 | ourselves a token before making the request. |
| 55 | |
| 56 | """ |
| 57 | # we need a token for this request |
| 58 | resp, body = self.raw_request(version_url, 'GET', |
| 59 | {'X-Auth-Token': self.token}) |
Ken'ichi Ohmichi | 3b05049 | 2016-10-20 11:51:09 -0700 | [diff] [blame^] | 60 | self._error_checker(resp, body) |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 61 | body = json.loads(body) |
| 62 | self.validate_response(schema.get_one_version, resp, body) |
| 63 | return rest_client.ResponseBody(resp, body) |