blob: 9932b4aaecb32c4b9b2a3ea60a6f571d7c0b2edd [file] [log] [blame]
ZhiQiang Fan39f97222013-09-20 04:49:44 +08001# Copyright 2012 OpenStack Foundation
dwalleck5d734432012-10-04 01:11:47 -05002# 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 Treinish96e9e882014-06-09 18:37:19 -040016from xml.etree import ElementTree as etree
dwalleck5d734432012-10-04 01:11:47 -050017
Matthew Treinish21905512015-07-13 10:33:35 -040018from oslo_serialization import jsonutils as json
Matthew Treinish89128142015-04-23 10:44:30 -040019from six.moves.urllib import parse as urllib
20
Ken'ichi Ohmichi19f68812016-03-02 14:09:17 +090021from tempest.lib.common import rest_client
Matthew Treinish684d8992014-01-30 16:27:40 +000022
dwalleck5d734432012-10-04 01:11:47 -050023
Ken'ichi Ohmichi19f68812016-03-02 14:09:17 +090024class AccountClient(rest_client.RestClient):
dwalleck5d734432012-10-04 01:11:47 -050025
Daisuke Morita499bba32013-11-28 18:44:49 +090026 def create_account(self, data=None,
27 params=None,
Ghanshyam2a180b82014-06-16 13:54:22 +090028 metadata=None,
29 remove_metadata=None,
Daisuke Morita499bba32013-11-28 18:44:49 +090030 metadata_prefix='X-Account-Meta-',
31 remove_metadata_prefix='X-Remove-Account-Meta-'):
32 """Create an account."""
Ghanshyam2a180b82014-06-16 13:54:22 +090033 if metadata is None:
34 metadata = {}
35 if remove_metadata is None:
36 remove_metadata = {}
Daisuke Morita499bba32013-11-28 18:44:49 +090037 url = ''
38 if params:
39 url += '?%s' % urllib.urlencode(params)
40
41 headers = {}
42 for key in metadata:
43 headers[metadata_prefix + key] = metadata[key]
44 for key in remove_metadata:
45 headers[remove_metadata_prefix + key] = remove_metadata[key]
46
47 resp, body = self.put(url, data, headers)
JordanPa84dde32014-11-14 15:47:42 +010048 self.expected_success(200, resp.status)
Daisuke Morita499bba32013-11-28 18:44:49 +090049 return resp, body
50
51 def delete_account(self, data=None, params=None):
52 """Delete an account."""
53 url = ''
54 if params:
Daisuke Morita499bba32013-11-28 18:44:49 +090055 url = '?%s%s' % (url, urllib.urlencode(params))
56
vponomaryov67b58fe2014-02-06 19:05:41 +020057 resp, body = self.delete(url, headers={}, body=data)
JordanPa84dde32014-11-14 15:47:42 +010058 self.expected_success(200, resp.status)
Daisuke Morita499bba32013-11-28 18:44:49 +090059 return resp, body
60
dwalleck5d734432012-10-04 01:11:47 -050061 def list_account_metadata(self):
Ken'ichi Ohmichib2790842015-11-17 11:46:13 +000062 """HEAD on the storage URL
63
dwalleck5d734432012-10-04 01:11:47 -050064 Returns all account metadata headers
65 """
Chmouel Boudjnahdcf40ea2014-01-06 18:35:34 -080066 resp, body = self.head('')
JordanPa84dde32014-11-14 15:47:42 +010067 self.expected_success(204, resp.status)
dwalleck5d734432012-10-04 01:11:47 -050068 return resp, body
69
70 def create_account_metadata(self, metadata,
Daisuke Moritae3f6ed32014-08-25 11:34:21 +090071 metadata_prefix='X-Account-Meta-',
72 data=None, params=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050073 """Creates an account metadata entry."""
dwalleck5d734432012-10-04 01:11:47 -050074 headers = {}
Daisuke Moritae3f6ed32014-08-25 11:34:21 +090075 if metadata:
76 for key in metadata:
77 headers[metadata_prefix + key] = metadata[key]
dwalleck5d734432012-10-04 01:11:47 -050078
Daisuke Moritae3f6ed32014-08-25 11:34:21 +090079 url = ''
80 if params:
81 url = '?%s%s' % (url, urllib.urlencode(params))
82
83 resp, body = self.post(url, headers=headers, body=data)
JordanPa84dde32014-11-14 15:47:42 +010084 self.expected_success([200, 204], resp.status)
dwalleck5d734432012-10-04 01:11:47 -050085 return resp, body
86
Larisa Ustalov6c3c7802012-11-05 12:25:19 +020087 def delete_account_metadata(self, metadata,
88 metadata_prefix='X-Remove-Account-Meta-'):
Ken'ichi Ohmichib2790842015-11-17 11:46:13 +000089 """Deletes an account metadata entry."""
Larisa Ustalov6c3c7802012-11-05 12:25:19 +020090
Chmouel Boudjnahdcf40ea2014-01-06 18:35:34 -080091 headers = {}
Larisa Ustalov6c3c7802012-11-05 12:25:19 +020092 for item in metadata:
Daisuke Morita83441282014-01-14 18:56:17 +090093 headers[metadata_prefix + item] = metadata[item]
94 resp, body = self.post('', headers=headers, body=None)
JordanPa84dde32014-11-14 15:47:42 +010095 self.expected_success(204, resp.status)
Daisuke Morita83441282014-01-14 18:56:17 +090096 return resp, body
97
98 def create_and_delete_account_metadata(
99 self,
100 create_metadata=None,
101 delete_metadata=None,
102 create_metadata_prefix='X-Account-Meta-',
103 delete_metadata_prefix='X-Remove-Account-Meta-'):
Ken'ichi Ohmichib2790842015-11-17 11:46:13 +0000104 """Creates and deletes an account metadata entry."""
Daisuke Morita83441282014-01-14 18:56:17 +0900105 headers = {}
106 for key in create_metadata:
107 headers[create_metadata_prefix + key] = create_metadata[key]
108 for key in delete_metadata:
109 headers[delete_metadata_prefix + key] = delete_metadata[key]
110
Larisa Ustalov6c3c7802012-11-05 12:25:19 +0200111 resp, body = self.post('', headers=headers, body=None)
JordanPa84dde32014-11-14 15:47:42 +0100112 self.expected_success(204, resp.status)
Larisa Ustalov6c3c7802012-11-05 12:25:19 +0200113 return resp, body
114
dwalleck5d734432012-10-04 01:11:47 -0500115 def list_account_containers(self, params=None):
Ken'ichi Ohmichib2790842015-11-17 11:46:13 +0000116 """GET on the (base) storage URL
117
Chmouel Boudjnahdcf40ea2014-01-06 18:35:34 -0800118 Given valid X-Auth-Token, returns a list of all containers for the
119 account.
dwalleck5d734432012-10-04 01:11:47 -0500120
121 Optional Arguments:
122 limit=[integer value N]
123 Limits the number of results to at most N values
124 DEFAULT: 10,000
125
126 marker=[string value X]
127 Given string value X, return object names greater in value
128 than the specified marker.
129 DEFAULT: No Marker
130
131 format=[string value, either 'json' or 'xml']
132 Specify either json or xml to return the respective serialized
133 response.
134 DEFAULT: Python-List returned in response body
135 """
Daisuke Morita83441282014-01-14 18:56:17 +0900136 url = '?%s' % urllib.urlencode(params) if params else ''
dwalleck5d734432012-10-04 01:11:47 -0500137
Daisuke Morita83441282014-01-14 18:56:17 +0900138 resp, body = self.get(url, headers={})
Daisuke Morita499bba32013-11-28 18:44:49 +0900139 if params and params.get('format') == 'json':
140 body = json.loads(body)
Daisuke Morita83441282014-01-14 18:56:17 +0900141 elif params and params.get('format') == 'xml':
142 body = etree.fromstring(body)
143 else:
144 body = body.strip().splitlines()
JordanPa84dde32014-11-14 15:47:42 +0100145 self.expected_success([200, 204], resp.status)
dwalleck5d734432012-10-04 01:11:47 -0500146 return resp, body