blob: 0047b50f3864a1018b7eb26a698ae28c34f01aca [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-vakadi1a9ad612012-12-14 19:12:08 +053018import httplib2
19import json
dwalleck5d734432012-10-04 01:11:47 -050020import re
21from tempest.common.rest_client import RestClient
harika-vakadi6ab397b2012-12-20 12:16:17 +053022from tempest import exceptions
dwalleck5d734432012-10-04 01:11:47 -050023
24
25class ObjectClient(RestClient):
26 def __init__(self, config, username, password, auth_url, tenant_name=None):
27 super(ObjectClient, self).__init__(config, username, password,
28 auth_url, tenant_name)
29
30 self.service = self.config.object_storage.catalog_type
31
32 def create_object(self, container, object_name, data):
33 """Create storage object"""
34
35 url = "%s/%s" % (str(container), str(object_name))
36 resp, body = self.put(url, data, self.headers)
37 return resp, body
38
39 def update_object(self, container, object_name, data):
40 """Upload data to replace current storage object"""
41 return create_object(container, object_name, data)
42
43 def delete_object(self, container, object_name):
44 """Delete storage object"""
45 url = "%s/%s" % (str(container), str(object_name))
46 resp, body = self.delete(url)
47 return resp, body
48
49 def update_object_metadata(self, container, object_name, metadata,
50 metadata_prefix='X-Object-Meta-'):
51 """Add, remove, or change X-Object-Meta metadata for storage object"""
52
53 headers = {}
54 for key in metadata:
55 headers["%s%s" % (str(metadata_prefix), str(key))] = metadata[key]
56
57 url = "%s/%s" % (str(container), str(object_name))
58 resp, body = self.post(url, None, headers=headers)
59 return resp, body
60
61 def list_object_metadata(self, container, object_name):
62 """List all storage object X-Object-Meta- metadata"""
63
64 url = "%s/%s" % (str(container), str(object_name))
65 resp, body = self.head(url)
66 return resp, body
Larisa Ustalov6c3c7802012-11-05 12:25:19 +020067
68 def get_object(self, container, object_name):
69 """Retrieve object's data."""
70
71 url = "{0}/{1}".format(container, object_name)
72 resp, body = self.get(url)
73 return resp, body
74
rajalakshmi-ganesan925e2392012-11-30 19:17:24 +053075 def copy_object_in_same_container(self, container, src_object_name,
76 dest_object_name, metadata=None):
Larisa Ustalov6c3c7802012-11-05 12:25:19 +020077 """Copy storage object's data to the new object using PUT"""
78
79 url = "{0}/{1}".format(container, dest_object_name)
80 headers = {}
81 headers['X-Copy-From'] = "%s/%s" % (str(container),
82 str(src_object_name))
83 headers['content-length'] = '0'
84 if metadata:
85 for key in metadata:
86 headers[str(key)] = metadata[key]
87
88 resp, body = self.put(url, None, headers=headers)
89 return resp, body
90
rajalakshmi-ganesan925e2392012-11-30 19:17:24 +053091 def copy_object_across_containers(self, src_container, src_object_name,
92 dst_container, dst_object_name,
93 metadata=None):
94 """Copy storage object's data to the new object using PUT"""
95
96 url = "{0}/{1}".format(dst_container, dst_object_name)
97 headers = {}
98 headers['X-Copy-From'] = "%s/%s" % (str(src_container),
99 str(src_object_name))
100 headers['content-length'] = '0'
101 if metadata:
102 for key in metadata:
103 headers[str(key)] = metadata[key]
104
105 resp, body = self.put(url, None, headers=headers)
106 return resp, body
107
Larisa Ustalov6c3c7802012-11-05 12:25:19 +0200108 def copy_object_2d_way(self, container, src_object_name, dest_object_name,
109 metadata=None):
110 """Copy storage object's data to the new object using COPY"""
111
112 url = "{0}/{1}".format(container, src_object_name)
113 headers = {}
114 headers['Destination'] = "%s/%s" % (str(container),
115 str(dest_object_name))
116 if metadata:
117 for key in metadata:
118 headers[str(key)] = metadata[key]
119
120 resp, body = self.copy(url, headers=headers)
121 return resp, body
harika-vakadi1a9ad612012-12-14 19:12:08 +0530122
123
124class ObjectClientCustomizedHeader(RestClient):
125
126 def __init__(self, config, username, password, auth_url, tenant_name=None):
127 super(ObjectClientCustomizedHeader, self).__init__(config, username,
128 password, auth_url,
129 tenant_name)
130 #Overwrites json-specific header encoding in RestClient
131 self.service = self.config.object_storage.catalog_type
132 self.format = 'json'
133
134 def request(self, method, url, headers=None, body=None, wait=None):
135 """A simple HTTP request interface."""
136 self.http_obj = httplib2.Http()
137 if headers is None:
138 headers = {}
139 if self.base_url is None:
140 self._set_auth()
141
142 req_url = "%s/%s" % (self.base_url, url)
143 resp, resp_body = self.http_obj.request(req_url, method,
144 headers=headers, body=body)
harika-vakadi6ab397b2012-12-20 12:16:17 +0530145
146 if resp.status == 401 or resp.status == 403:
147 self._log(req_url, body, resp, resp_body)
148 raise exceptions.Unauthorized()
149
harika-vakadi1a9ad612012-12-14 19:12:08 +0530150 return resp, resp_body
151
152 def get_object(self, container, object_name, metadata=None):
153 """Retrieve object's data."""
154 headers = {}
155 if metadata:
156 for key in metadata:
157 headers[str(key)] = metadata[key]
158
159 url = "{0}/{1}".format(container, object_name)
160 resp, body = self.get(url, headers=headers)
161 return resp, body
harika-vakadi6ab397b2012-12-20 12:16:17 +0530162
163 def create_object(self, container, object_name, data, metadata=None):
164 """Create storage object"""
165
166 headers = {}
167 if metadata:
168 for key in metadata:
169 headers[str(key)] = metadata[key]
170
171 url = "%s/%s" % (str(container), str(object_name))
172 resp, body = self.put(url, data, headers=headers)
173 return resp, body
174
175 def delete_object(self, container, object_name):
176 """Delete storage object"""
177
178 url = "%s/%s" % (str(container), str(object_name))
179 resp, body = self.delete(url)
180 return resp, body