ZhiQiang Fan | 39f9722 | 2013-09-20 04:49:44 +0800 | [diff] [blame] | 1 | # Copyright 2012 OpenStack Foundation |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 2 | # All Rights Reserved. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | # not use this file except in compliance with the License. You may obtain |
| 6 | # a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | # License for the specific language governing permissions and limitations |
| 14 | # under the License. |
| 15 | |
| 16 | import json |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 17 | import urllib |
Matthew Treinish | 96e9e88 | 2014-06-09 18:37:19 -0400 | [diff] [blame] | 18 | from xml.etree import ElementTree as etree |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 19 | |
Ken'ichi Ohmichi | de398ac | 2014-12-17 08:34:34 +0000 | [diff] [blame] | 20 | from tempest.services.object_storage import base |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 21 | |
| 22 | |
Ken'ichi Ohmichi | de398ac | 2014-12-17 08:34:34 +0000 | [diff] [blame] | 23 | class ContainerClient(base.ObjectStorageClient): |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 24 | |
Daisuke Morita | 1d59158 | 2014-01-14 19:49:23 +0900 | [diff] [blame] | 25 | def create_container( |
| 26 | self, container_name, |
| 27 | metadata=None, |
| 28 | remove_metadata=None, |
| 29 | metadata_prefix='X-Container-Meta-', |
| 30 | remove_metadata_prefix='X-Remove-Container-Meta-'): |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 31 | """ |
| 32 | Creates a container, with optional metadata passed in as a |
Chang Bo Guo | cc1623c | 2013-09-13 20:11:27 -0700 | [diff] [blame] | 33 | dictionary |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 34 | """ |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 35 | url = str(container_name) |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 36 | headers = {} |
| 37 | |
| 38 | if metadata is not None: |
| 39 | for key in metadata: |
| 40 | headers[metadata_prefix + key] = metadata[key] |
Daisuke Morita | 1d59158 | 2014-01-14 19:49:23 +0900 | [diff] [blame] | 41 | if remove_metadata is not None: |
| 42 | for key in remove_metadata: |
| 43 | headers[remove_metadata_prefix + key] = remove_metadata[key] |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 44 | |
| 45 | resp, body = self.put(url, body=None, headers=headers) |
JordanP | a84dde3 | 2014-11-14 15:47:42 +0100 | [diff] [blame] | 46 | self.expected_success([201, 202], resp.status) |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 47 | return resp, body |
| 48 | |
| 49 | def delete_container(self, container_name): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 50 | """Deletes the container (if it's empty).""" |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 51 | url = str(container_name) |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 52 | resp, body = self.delete(url) |
JordanP | a84dde3 | 2014-11-14 15:47:42 +0100 | [diff] [blame] | 53 | self.expected_success(204, resp.status) |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 54 | return resp, body |
| 55 | |
Daisuke Morita | 1d59158 | 2014-01-14 19:49:23 +0900 | [diff] [blame] | 56 | def update_container_metadata( |
| 57 | self, container_name, |
| 58 | metadata=None, |
| 59 | remove_metadata=None, |
| 60 | metadata_prefix='X-Container-Meta-', |
| 61 | remove_metadata_prefix='X-Remove-Container-Meta-'): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 62 | """Updates arbitrary metadata on container.""" |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 63 | url = str(container_name) |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 64 | headers = {} |
| 65 | |
| 66 | if metadata is not None: |
| 67 | for key in metadata: |
| 68 | headers[metadata_prefix + key] = metadata[key] |
Daisuke Morita | 1d59158 | 2014-01-14 19:49:23 +0900 | [diff] [blame] | 69 | if remove_metadata is not None: |
| 70 | for key in remove_metadata: |
| 71 | headers[remove_metadata_prefix + key] = remove_metadata[key] |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 72 | |
| 73 | resp, body = self.post(url, body=None, headers=headers) |
JordanP | a84dde3 | 2014-11-14 15:47:42 +0100 | [diff] [blame] | 74 | self.expected_success(204, resp.status) |
Larisa Ustalov | 6c3c780 | 2012-11-05 12:25:19 +0200 | [diff] [blame] | 75 | return resp, body |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 76 | |
Larisa Ustalov | 6c3c780 | 2012-11-05 12:25:19 +0200 | [diff] [blame] | 77 | def delete_container_metadata(self, container_name, metadata, |
| 78 | metadata_prefix='X-Remove-Container-Meta-'): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 79 | """Deletes arbitrary metadata on container.""" |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 80 | url = str(container_name) |
Larisa Ustalov | 6c3c780 | 2012-11-05 12:25:19 +0200 | [diff] [blame] | 81 | headers = {} |
| 82 | |
| 83 | if metadata is not None: |
| 84 | for item in metadata: |
Daisuke Morita | 1d59158 | 2014-01-14 19:49:23 +0900 | [diff] [blame] | 85 | headers[metadata_prefix + item] = metadata[item] |
Larisa Ustalov | 6c3c780 | 2012-11-05 12:25:19 +0200 | [diff] [blame] | 86 | |
| 87 | resp, body = self.post(url, body=None, headers=headers) |
JordanP | a84dde3 | 2014-11-14 15:47:42 +0100 | [diff] [blame] | 88 | self.expected_success(204, resp.status) |
Larisa Ustalov | 6c3c780 | 2012-11-05 12:25:19 +0200 | [diff] [blame] | 89 | return resp, body |
| 90 | |
| 91 | def list_container_metadata(self, container_name): |
| 92 | """ |
| 93 | Retrieves container metadata headers |
| 94 | """ |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 95 | url = str(container_name) |
Chmouel Boudjnah | dcf40ea | 2014-01-06 18:35:34 -0800 | [diff] [blame] | 96 | resp, body = self.head(url) |
JordanP | a84dde3 | 2014-11-14 15:47:42 +0100 | [diff] [blame] | 97 | self.expected_success(204, resp.status) |
Larisa Ustalov | 6c3c780 | 2012-11-05 12:25:19 +0200 | [diff] [blame] | 98 | return resp, body |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 99 | |
| 100 | def list_all_container_objects(self, container, params=None): |
| 101 | """ |
| 102 | Returns complete list of all objects in the container, even if |
| 103 | item count is beyond 10,000 item listing limit. |
Chang Bo Guo | cc1623c | 2013-09-13 20:11:27 -0700 | [diff] [blame] | 104 | Does not require any parameters aside from container name. |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 105 | """ |
Chang Bo Guo | cc1623c | 2013-09-13 20:11:27 -0700 | [diff] [blame] | 106 | # TODO(dwalleck): Rewrite using json format to avoid newlines at end of |
Attila Fazekas | a8b5fe7 | 2013-08-01 16:59:06 +0200 | [diff] [blame] | 107 | # obj names. Set limit to API limit - 1 (max returned items = 9999) |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 108 | limit = 9999 |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 109 | if params is not None: |
| 110 | if 'limit' in params: |
| 111 | limit = params['limit'] |
| 112 | |
| 113 | if 'marker' in params: |
| 114 | limit = params['marker'] |
| 115 | |
Daisuke Morita | 1d59158 | 2014-01-14 19:49:23 +0900 | [diff] [blame] | 116 | resp, objlist = self.list_container_contents( |
| 117 | container, |
| 118 | params={'limit': limit, 'format': 'json'}) |
JordanP | a84dde3 | 2014-11-14 15:47:42 +0100 | [diff] [blame] | 119 | self.expected_success(200, resp.status) |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 120 | return objlist |
| 121 | """tmp = [] |
| 122 | for obj in objlist: |
| 123 | tmp.append(obj['name']) |
| 124 | objlist = tmp |
| 125 | |
| 126 | if len(objlist) >= limit: |
| 127 | |
Attila Fazekas | a8b5fe7 | 2013-08-01 16:59:06 +0200 | [diff] [blame] | 128 | # Increment marker |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 129 | marker = objlist[len(objlist) - 1] |
| 130 | |
Attila Fazekas | a8b5fe7 | 2013-08-01 16:59:06 +0200 | [diff] [blame] | 131 | # Get the next chunk of the list |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 132 | objlist.extend(_list_all_container_objects(container, |
| 133 | params={'marker': marker, |
| 134 | 'limit': limit})) |
| 135 | return objlist |
| 136 | else: |
Attila Fazekas | a8b5fe7 | 2013-08-01 16:59:06 +0200 | [diff] [blame] | 137 | # Return final, complete list |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 138 | return objlist""" |
| 139 | |
| 140 | def list_container_contents(self, container, params=None): |
| 141 | """ |
| 142 | List the objects in a container, given the container name |
| 143 | |
| 144 | Returns the container object listing as a plain text list, or as |
| 145 | xml or json if that option is specified via the 'format' argument. |
| 146 | |
| 147 | Optional Arguments: |
| 148 | limit = integer |
| 149 | For an integer value n, limits the number of results to at most |
| 150 | n values. |
| 151 | |
| 152 | marker = 'string' |
| 153 | Given a string value x, return object names greater in value |
| 154 | than the specified marker. |
| 155 | |
| 156 | prefix = 'string' |
| 157 | For a string value x, causes the results to be limited to names |
| 158 | beginning with the substring x. |
| 159 | |
| 160 | format = 'json' or 'xml' |
| 161 | Specify either json or xml to return the respective serialized |
| 162 | response. |
| 163 | If json, returns a list of json objects |
| 164 | if xml, returns a string of xml |
| 165 | |
| 166 | path = 'string' |
| 167 | For a string value x, return the object names nested in the |
| 168 | pseudo path (assuming preconditions are met - see below). |
| 169 | |
| 170 | delimiter = 'character' |
| 171 | For a character c, return all the object names nested in the |
| 172 | container (without the need for the directory marker objects). |
| 173 | """ |
| 174 | |
| 175 | url = str(container) |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 176 | if params: |
Daisuke Morita | 1d59158 | 2014-01-14 19:49:23 +0900 | [diff] [blame] | 177 | url += '?' |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 178 | url += '&%s' % urllib.urlencode(params) |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 179 | |
vponomaryov | 67b58fe | 2014-02-06 19:05:41 +0200 | [diff] [blame] | 180 | resp, body = self.get(url, headers={}) |
Daisuke Morita | 1d59158 | 2014-01-14 19:49:23 +0900 | [diff] [blame] | 181 | if params and params.get('format') == 'json': |
| 182 | body = json.loads(body) |
| 183 | elif params and params.get('format') == 'xml': |
| 184 | body = etree.fromstring(body) |
JordanP | a84dde3 | 2014-11-14 15:47:42 +0100 | [diff] [blame] | 185 | self.expected_success([200, 204], resp.status) |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 186 | return resp, body |