dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 1 | # vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 2 | |
| 3 | # Copyright 2012 OpenStack, LLC |
| 4 | # All Rights Reserved. |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. You may obtain |
| 8 | # a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | # License for the specific language governing permissions and limitations |
| 16 | # under the License. |
| 17 | |
harika-vakadi | 2daed0a | 2013-01-01 20:51:39 +0530 | [diff] [blame] | 18 | import httplib2 |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 19 | import json |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 20 | import urllib |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 21 | |
| 22 | from tempest.common.rest_client import RestClient |
harika-vakadi | 2daed0a | 2013-01-01 20:51:39 +0530 | [diff] [blame] | 23 | from tempest import exceptions |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 24 | |
| 25 | |
| 26 | class AccountClient(RestClient): |
| 27 | def __init__(self, config, username, password, auth_url, tenant_name=None): |
| 28 | super(AccountClient, self).__init__(config, username, password, |
| 29 | auth_url, tenant_name) |
| 30 | self.service = self.config.object_storage.catalog_type |
| 31 | self.format = 'json' |
| 32 | |
| 33 | def list_account_metadata(self): |
| 34 | """ |
| 35 | HEAD on the storage URL |
| 36 | Returns all account metadata headers |
| 37 | """ |
| 38 | |
Jaroslav Henner | e27a4bf | 2012-10-23 17:16:46 +0200 | [diff] [blame] | 39 | headers = {"X-Storage-Token": self.token} |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 40 | resp, body = self.head('', headers=headers) |
| 41 | return resp, body |
| 42 | |
| 43 | def create_account_metadata(self, metadata, |
| 44 | metadata_prefix='X-Account-Meta-'): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 45 | """Creates an account metadata entry.""" |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 46 | headers = {} |
| 47 | for key in metadata: |
| 48 | headers[metadata_prefix + key] = metadata[key] |
| 49 | |
| 50 | resp, body = self.post('', headers=headers, body=None) |
| 51 | return resp, body |
| 52 | |
Larisa Ustalov | 6c3c780 | 2012-11-05 12:25:19 +0200 | [diff] [blame] | 53 | def delete_account_metadata(self, metadata, |
| 54 | metadata_prefix='X-Remove-Account-Meta-'): |
| 55 | """ |
| 56 | Deletes an account metadata entry. |
| 57 | """ |
| 58 | |
| 59 | headers = {"X-Storage-Token": self.token} |
| 60 | for item in metadata: |
| 61 | headers[metadata_prefix + item] = 'x' |
| 62 | resp, body = self.post('', headers=headers, body=None) |
| 63 | return resp, body |
| 64 | |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 65 | def list_account_containers(self, params=None): |
| 66 | """ |
| 67 | GET on the (base) storage URL |
| 68 | Given the X-Storage-URL and a valid X-Auth-Token, returns |
| 69 | a list of all containers for the account. |
| 70 | |
| 71 | Optional Arguments: |
| 72 | limit=[integer value N] |
| 73 | Limits the number of results to at most N values |
| 74 | DEFAULT: 10,000 |
| 75 | |
| 76 | marker=[string value X] |
| 77 | Given string value X, return object names greater in value |
| 78 | than the specified marker. |
| 79 | DEFAULT: No Marker |
| 80 | |
| 81 | format=[string value, either 'json' or 'xml'] |
| 82 | Specify either json or xml to return the respective serialized |
| 83 | response. |
| 84 | DEFAULT: Python-List returned in response body |
| 85 | """ |
| 86 | |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 87 | url = '?format=%s' % self.format |
| 88 | if params: |
| 89 | url += '&%s' + urllib.urlencode(params) |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 90 | |
| 91 | resp, body = self.get(url) |
| 92 | body = json.loads(body) |
| 93 | return resp, body |
harika-vakadi | 2daed0a | 2013-01-01 20:51:39 +0530 | [diff] [blame] | 94 | |
| 95 | |
| 96 | class AccountClientCustomizedHeader(RestClient): |
| 97 | |
| 98 | def __init__(self, config, username, password, auth_url, tenant_name=None): |
| 99 | super(AccountClientCustomizedHeader, self).__init__(config, username, |
| 100 | password, auth_url, |
| 101 | tenant_name) |
| 102 | #Overwrites json-specific header encoding in RestClient |
| 103 | self.service = self.config.object_storage.catalog_type |
| 104 | self.format = 'json' |
| 105 | |
| 106 | def request(self, method, url, headers=None, body=None, wait=None): |
| 107 | """A simple HTTP request interface.""" |
| 108 | self.http_obj = httplib2.Http() |
| 109 | if headers is None: |
| 110 | headers = {} |
| 111 | if self.base_url is None: |
| 112 | self._set_auth() |
| 113 | |
| 114 | req_url = "%s/%s" % (self.base_url, url) |
Attila Fazekas | 11d2a77 | 2013-01-29 17:46:52 +0100 | [diff] [blame^] | 115 | |
| 116 | self._log_request(method, req_url, headers, body) |
harika-vakadi | 2daed0a | 2013-01-01 20:51:39 +0530 | [diff] [blame] | 117 | resp, resp_body = self.http_obj.request(req_url, method, |
| 118 | headers=headers, body=body) |
Attila Fazekas | 11d2a77 | 2013-01-29 17:46:52 +0100 | [diff] [blame^] | 119 | self._log_response(resp, resp_body) |
harika-vakadi | 2daed0a | 2013-01-01 20:51:39 +0530 | [diff] [blame] | 120 | |
| 121 | if resp.status == 401 or resp.status == 403: |
harika-vakadi | 2daed0a | 2013-01-01 20:51:39 +0530 | [diff] [blame] | 122 | raise exceptions.Unauthorized() |
| 123 | |
| 124 | return resp, resp_body |
| 125 | |
| 126 | def list_account_containers(self, params=None, metadata=None): |
| 127 | """ |
| 128 | GET on the (base) storage URL |
| 129 | Given the X-Storage-URL and a valid X-Auth-Token, returns |
| 130 | a list of all containers for the account. |
| 131 | |
| 132 | Optional Arguments: |
| 133 | limit=[integer value N] |
| 134 | Limits the number of results to at most N values |
| 135 | DEFAULT: 10,000 |
| 136 | |
| 137 | marker=[string value X] |
| 138 | Given string value X, return object names greater in value |
| 139 | than the specified marker. |
| 140 | DEFAULT: No Marker |
| 141 | |
| 142 | format=[string value, either 'json' or 'xml'] |
| 143 | Specify either json or xml to return the respective serialized |
| 144 | response. |
| 145 | DEFAULT: Python-List returned in response body |
| 146 | """ |
| 147 | |
| 148 | url = '?format=%s' % self.format |
| 149 | if params: |
| 150 | url += '&%s' + urllib.urlencode(params) |
| 151 | |
| 152 | headers = {} |
| 153 | if metadata: |
| 154 | for key in metadata: |
| 155 | headers[str(key)] = metadata[key] |
| 156 | |
| 157 | resp, body = self.get(url, headers=headers) |
| 158 | return resp, body |