blob: ce4b127786d6250018b148c4fb693e2f1e8fcc3d [file] [log] [blame]
dwalleck5d734432012-10-04 01:11:47 -05001# 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-vakadi2daed0a2013-01-01 20:51:39 +053018import httplib2
dwalleck5d734432012-10-04 01:11:47 -050019import json
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050020import urllib
dwalleck5d734432012-10-04 01:11:47 -050021
22from tempest.common.rest_client import RestClient
harika-vakadi2daed0a2013-01-01 20:51:39 +053023from tempest import exceptions
dwalleck5d734432012-10-04 01:11:47 -050024
25
26class 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 Hennere27a4bf2012-10-23 17:16:46 +020039 headers = {"X-Storage-Token": self.token}
dwalleck5d734432012-10-04 01:11:47 -050040 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 Daguef237ccb2013-01-04 15:19:14 -050045 """Creates an account metadata entry."""
dwalleck5d734432012-10-04 01:11:47 -050046 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 Ustalov6c3c7802012-11-05 12:25:19 +020053 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
dwalleck5d734432012-10-04 01:11:47 -050065 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 Treinish26dd0fa2012-12-04 17:14:37 -050087 if params:
Fabien Boucherfaab4312013-08-06 15:36:23 +020088 if 'format' not in params:
89 params['format'] = self.format
90 else:
91 params = {'format': self.format}
dwalleck5d734432012-10-04 01:11:47 -050092
Fabien Boucherfaab4312013-08-06 15:36:23 +020093 url = '?' + urllib.urlencode(params)
dwalleck5d734432012-10-04 01:11:47 -050094 resp, body = self.get(url)
95 body = json.loads(body)
96 return resp, body
harika-vakadi2daed0a2013-01-01 20:51:39 +053097
98
99class AccountClientCustomizedHeader(RestClient):
100
101 def __init__(self, config, username, password, auth_url, tenant_name=None):
102 super(AccountClientCustomizedHeader, self).__init__(config, username,
103 password, auth_url,
104 tenant_name)
105 #Overwrites json-specific header encoding in RestClient
106 self.service = self.config.object_storage.catalog_type
107 self.format = 'json'
108
Attila Fazekasb8aa7592013-01-26 01:25:45 +0100109 def request(self, method, url, headers=None, body=None):
harika-vakadi2daed0a2013-01-01 20:51:39 +0530110 """A simple HTTP request interface."""
111 self.http_obj = httplib2.Http()
112 if headers is None:
113 headers = {}
114 if self.base_url is None:
115 self._set_auth()
116
117 req_url = "%s/%s" % (self.base_url, url)
Attila Fazekas11d2a772013-01-29 17:46:52 +0100118
119 self._log_request(method, req_url, headers, body)
harika-vakadi2daed0a2013-01-01 20:51:39 +0530120 resp, resp_body = self.http_obj.request(req_url, method,
121 headers=headers, body=body)
Attila Fazekas11d2a772013-01-29 17:46:52 +0100122 self._log_response(resp, resp_body)
harika-vakadi2daed0a2013-01-01 20:51:39 +0530123
124 if resp.status == 401 or resp.status == 403:
harika-vakadi2daed0a2013-01-01 20:51:39 +0530125 raise exceptions.Unauthorized()
126
127 return resp, resp_body
128
129 def list_account_containers(self, params=None, metadata=None):
130 """
131 GET on the (base) storage URL
132 Given the X-Storage-URL and a valid X-Auth-Token, returns
133 a list of all containers for the account.
134
135 Optional Arguments:
136 limit=[integer value N]
137 Limits the number of results to at most N values
138 DEFAULT: 10,000
139
140 marker=[string value X]
141 Given string value X, return object names greater in value
142 than the specified marker.
143 DEFAULT: No Marker
144
145 format=[string value, either 'json' or 'xml']
146 Specify either json or xml to return the respective serialized
147 response.
148 DEFAULT: Python-List returned in response body
149 """
150
151 url = '?format=%s' % self.format
152 if params:
153 url += '&%s' + urllib.urlencode(params)
154
155 headers = {}
156 if metadata:
157 for key in metadata:
158 headers[str(key)] = metadata[key]
159
160 resp, body = self.get(url, headers=headers)
161 return resp, body