blob: fec273cd2c40962b2dc2d1a74e7d086b008fe034 [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 url = '?format=%s' % self.format
88 if params:
89 url += '&%s' + urllib.urlencode(params)
dwalleck5d734432012-10-04 01:11:47 -050090
91 resp, body = self.get(url)
92 body = json.loads(body)
93 return resp, body
harika-vakadi2daed0a2013-01-01 20:51:39 +053094
95
96class 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 Fazekas11d2a772013-01-29 17:46:52 +0100115
116 self._log_request(method, req_url, headers, body)
harika-vakadi2daed0a2013-01-01 20:51:39 +0530117 resp, resp_body = self.http_obj.request(req_url, method,
118 headers=headers, body=body)
Attila Fazekas11d2a772013-01-29 17:46:52 +0100119 self._log_response(resp, resp_body)
harika-vakadi2daed0a2013-01-01 20:51:39 +0530120
121 if resp.status == 401 or resp.status == 403:
harika-vakadi2daed0a2013-01-01 20:51:39 +0530122 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