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 | |
Matthew Treinish | 96e9e88 | 2014-06-09 18:37:19 -0400 | [diff] [blame] | 16 | from xml.etree import ElementTree as etree |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 17 | |
Matthew Treinish | 2190551 | 2015-07-13 10:33:35 -0400 | [diff] [blame] | 18 | from oslo_serialization import jsonutils as json |
Matthew Treinish | 8912814 | 2015-04-23 10:44:30 -0400 | [diff] [blame] | 19 | from six.moves.urllib import parse as urllib |
| 20 | |
Ken'ichi Ohmichi | 19f6881 | 2016-03-02 14:09:17 +0900 | [diff] [blame] | 21 | from tempest.lib.common import rest_client |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 22 | |
| 23 | |
Ken'ichi Ohmichi | 19f6881 | 2016-03-02 14:09:17 +0900 | [diff] [blame] | 24 | class ContainerClient(rest_client.RestClient): |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 25 | |
Daisuke Morita | 1d59158 | 2014-01-14 19:49:23 +0900 | [diff] [blame] | 26 | def create_container( |
| 27 | self, container_name, |
| 28 | metadata=None, |
| 29 | remove_metadata=None, |
| 30 | metadata_prefix='X-Container-Meta-', |
| 31 | remove_metadata_prefix='X-Remove-Container-Meta-'): |
Ken'ichi Ohmichi | b279084 | 2015-11-17 11:46:13 +0000 | [diff] [blame] | 32 | """Creates a container |
| 33 | |
| 34 | with optional metadata passed in as a dictionary |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 35 | """ |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 36 | url = str(container_name) |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 37 | headers = {} |
| 38 | |
| 39 | if metadata is not None: |
| 40 | for key in metadata: |
| 41 | headers[metadata_prefix + key] = metadata[key] |
Daisuke Morita | 1d59158 | 2014-01-14 19:49:23 +0900 | [diff] [blame] | 42 | if remove_metadata is not None: |
| 43 | for key in remove_metadata: |
| 44 | headers[remove_metadata_prefix + key] = remove_metadata[key] |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 45 | |
| 46 | resp, body = self.put(url, body=None, headers=headers) |
JordanP | a84dde3 | 2014-11-14 15:47:42 +0100 | [diff] [blame] | 47 | self.expected_success([201, 202], resp.status) |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 48 | return resp, body |
| 49 | |
| 50 | def delete_container(self, container_name): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 51 | """Deletes the container (if it's empty).""" |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 52 | url = str(container_name) |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 53 | resp, body = self.delete(url) |
JordanP | a84dde3 | 2014-11-14 15:47:42 +0100 | [diff] [blame] | 54 | self.expected_success(204, resp.status) |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 55 | return resp, body |
| 56 | |
Daisuke Morita | 1d59158 | 2014-01-14 19:49:23 +0900 | [diff] [blame] | 57 | def update_container_metadata( |
| 58 | self, container_name, |
| 59 | metadata=None, |
| 60 | remove_metadata=None, |
| 61 | metadata_prefix='X-Container-Meta-', |
| 62 | remove_metadata_prefix='X-Remove-Container-Meta-'): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 63 | """Updates arbitrary metadata on container.""" |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 64 | url = str(container_name) |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 65 | headers = {} |
| 66 | |
| 67 | if metadata is not None: |
| 68 | for key in metadata: |
| 69 | headers[metadata_prefix + key] = metadata[key] |
Daisuke Morita | 1d59158 | 2014-01-14 19:49:23 +0900 | [diff] [blame] | 70 | if remove_metadata is not None: |
| 71 | for key in remove_metadata: |
| 72 | headers[remove_metadata_prefix + key] = remove_metadata[key] |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 73 | |
| 74 | resp, body = self.post(url, body=None, headers=headers) |
JordanP | a84dde3 | 2014-11-14 15:47:42 +0100 | [diff] [blame] | 75 | self.expected_success(204, resp.status) |
Larisa Ustalov | 6c3c780 | 2012-11-05 12:25:19 +0200 | [diff] [blame] | 76 | return resp, body |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 77 | |
Larisa Ustalov | 6c3c780 | 2012-11-05 12:25:19 +0200 | [diff] [blame] | 78 | def delete_container_metadata(self, container_name, metadata, |
| 79 | metadata_prefix='X-Remove-Container-Meta-'): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 80 | """Deletes arbitrary metadata on container.""" |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 81 | url = str(container_name) |
Larisa Ustalov | 6c3c780 | 2012-11-05 12:25:19 +0200 | [diff] [blame] | 82 | headers = {} |
| 83 | |
| 84 | if metadata is not None: |
| 85 | for item in metadata: |
Daisuke Morita | 1d59158 | 2014-01-14 19:49:23 +0900 | [diff] [blame] | 86 | headers[metadata_prefix + item] = metadata[item] |
Larisa Ustalov | 6c3c780 | 2012-11-05 12:25:19 +0200 | [diff] [blame] | 87 | |
| 88 | resp, body = self.post(url, body=None, headers=headers) |
JordanP | a84dde3 | 2014-11-14 15:47:42 +0100 | [diff] [blame] | 89 | self.expected_success(204, resp.status) |
Larisa Ustalov | 6c3c780 | 2012-11-05 12:25:19 +0200 | [diff] [blame] | 90 | return resp, body |
| 91 | |
| 92 | def list_container_metadata(self, container_name): |
Ken'ichi Ohmichi | b279084 | 2015-11-17 11:46:13 +0000 | [diff] [blame] | 93 | """Retrieves container metadata headers""" |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 94 | url = str(container_name) |
Chmouel Boudjnah | dcf40ea | 2014-01-06 18:35:34 -0800 | [diff] [blame] | 95 | resp, body = self.head(url) |
JordanP | a84dde3 | 2014-11-14 15:47:42 +0100 | [diff] [blame] | 96 | self.expected_success(204, resp.status) |
Larisa Ustalov | 6c3c780 | 2012-11-05 12:25:19 +0200 | [diff] [blame] | 97 | return resp, body |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 98 | |
| 99 | def list_all_container_objects(self, container, params=None): |
Ken'ichi Ohmichi | b279084 | 2015-11-17 11:46:13 +0000 | [diff] [blame] | 100 | """Returns complete list of all objects in the container |
| 101 | |
| 102 | even if item count is beyond 10,000 item listing limit. |
| 103 | Does not require any parameters aside from container name. |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 104 | """ |
Chang Bo Guo | cc1623c | 2013-09-13 20:11:27 -0700 | [diff] [blame] | 105 | # TODO(dwalleck): Rewrite using json format to avoid newlines at end of |
Attila Fazekas | a8b5fe7 | 2013-08-01 16:59:06 +0200 | [diff] [blame] | 106 | # obj names. Set limit to API limit - 1 (max returned items = 9999) |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 107 | limit = 9999 |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 108 | if params is not None: |
| 109 | if 'limit' in params: |
| 110 | limit = params['limit'] |
| 111 | |
| 112 | if 'marker' in params: |
| 113 | limit = params['marker'] |
| 114 | |
Daisuke Morita | 1d59158 | 2014-01-14 19:49:23 +0900 | [diff] [blame] | 115 | resp, objlist = self.list_container_contents( |
| 116 | container, |
| 117 | params={'limit': limit, 'format': 'json'}) |
JordanP | a84dde3 | 2014-11-14 15:47:42 +0100 | [diff] [blame] | 118 | self.expected_success(200, resp.status) |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 119 | return objlist |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 120 | |
| 121 | def list_container_contents(self, container, params=None): |
Ken'ichi Ohmichi | b279084 | 2015-11-17 11:46:13 +0000 | [diff] [blame] | 122 | """List the objects in a container, given the container name |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 123 | |
| 124 | Returns the container object listing as a plain text list, or as |
| 125 | xml or json if that option is specified via the 'format' argument. |
| 126 | |
| 127 | Optional Arguments: |
| 128 | limit = integer |
| 129 | For an integer value n, limits the number of results to at most |
| 130 | n values. |
| 131 | |
| 132 | marker = 'string' |
| 133 | Given a string value x, return object names greater in value |
| 134 | than the specified marker. |
| 135 | |
| 136 | prefix = 'string' |
| 137 | For a string value x, causes the results to be limited to names |
| 138 | beginning with the substring x. |
| 139 | |
| 140 | format = 'json' or 'xml' |
| 141 | Specify either json or xml to return the respective serialized |
| 142 | response. |
| 143 | If json, returns a list of json objects |
| 144 | if xml, returns a string of xml |
| 145 | |
| 146 | path = 'string' |
| 147 | For a string value x, return the object names nested in the |
| 148 | pseudo path (assuming preconditions are met - see below). |
| 149 | |
| 150 | delimiter = 'character' |
| 151 | For a character c, return all the object names nested in the |
| 152 | container (without the need for the directory marker objects). |
| 153 | """ |
| 154 | |
| 155 | url = str(container) |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 156 | if params: |
Daisuke Morita | 1d59158 | 2014-01-14 19:49:23 +0900 | [diff] [blame] | 157 | url += '?' |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 158 | url += '&%s' % urllib.urlencode(params) |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 159 | |
vponomaryov | 67b58fe | 2014-02-06 19:05:41 +0200 | [diff] [blame] | 160 | resp, body = self.get(url, headers={}) |
Daisuke Morita | 1d59158 | 2014-01-14 19:49:23 +0900 | [diff] [blame] | 161 | if params and params.get('format') == 'json': |
| 162 | body = json.loads(body) |
| 163 | elif params and params.get('format') == 'xml': |
| 164 | body = etree.fromstring(body) |
JordanP | a84dde3 | 2014-11-14 15:47:42 +0100 | [diff] [blame] | 165 | self.expected_success([200, 204], resp.status) |
dwalleck | 5d73443 | 2012-10-04 01:11:47 -0500 | [diff] [blame] | 166 | return resp, body |