blob: 886ce7b0ce2c3e9a60090b715ab15d86eea3ddd7 [file] [log] [blame]
Kurt Taylor6a6f5be2013-04-02 18:53:47 -04001# Copyright 2012 IBM Corp.
Vincent Hou6b8a7b72012-08-25 01:24:33 +08002# 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.
Matthew Treinish28f164c2014-03-04 18:55:06 +000015from tempest.common import xml_utils as xml
Matthew Treinish684d8992014-01-30 16:27:40 +000016from tempest import config
vponomaryov67b58fe2014-02-06 19:05:41 +020017from tempest.services.identity.json import identity_client
Matthew Treinisha83a16e2012-12-07 13:44:02 -050018
Matthew Treinish684d8992014-01-30 16:27:40 +000019CONF = config.CONF
Vincent Hou6b8a7b72012-08-25 01:24:33 +080020
21XMLNS = "http://docs.openstack.org/identity/api/v2.0"
22
23
vponomaryov67b58fe2014-02-06 19:05:41 +020024class IdentityClientXML(identity_client.IdentityClientJSON):
25 TYPE = "xml"
Vincent Hou6b8a7b72012-08-25 01:24:33 +080026
27 def create_role(self, name):
Sean Daguef237ccb2013-01-04 15:19:14 -050028 """Create a role."""
vponomaryov67b58fe2014-02-06 19:05:41 +020029 create_role = xml.Element("role", xmlns=XMLNS, name=name)
30 resp, body = self.post('OS-KSADM/roles',
31 str(xml.Document(create_role)))
32 return resp, self._parse_resp(body)
Vincent Hou6b8a7b72012-08-25 01:24:33 +080033
Gong Zhangcb6b8862014-02-20 15:14:05 +080034 def get_role(self, role_id):
35 """Get a role by its id."""
36 resp, body = self.get('OS-KSADM/roles/%s' % role_id)
37 return resp, self._parse_resp(body)
38
Vincent Hou6b8a7b72012-08-25 01:24:33 +080039 def create_tenant(self, name, **kwargs):
40 """
41 Create a tenant
42 name (required): New tenant name
43 description: Description of new tenant (default is none)
44 enabled <true|false>: Initial tenant status (default is true)
45 """
46 en = kwargs.get('enabled', 'true')
vponomaryov67b58fe2014-02-06 19:05:41 +020047 create_tenant = xml.Element("tenant",
48 xmlns=XMLNS,
49 name=name,
50 description=kwargs.get('description', ''),
51 enabled=str(en).lower())
52 resp, body = self.post('tenants', str(xml.Document(create_tenant)))
53 return resp, self._parse_resp(body)
Vincent Hou6b8a7b72012-08-25 01:24:33 +080054
55 def list_tenants(self):
Sean Daguef237ccb2013-01-04 15:19:14 -050056 """Returns tenants."""
vponomaryov67b58fe2014-02-06 19:05:41 +020057 resp, body = self.get('tenants')
58 return resp, self._parse_resp(body)
Matthew Treinishbf41e102013-01-08 15:56:28 -050059
Vincent Hou6b8a7b72012-08-25 01:24:33 +080060 def update_tenant(self, tenant_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -050061 """Updates a tenant."""
Vincent Hou6b8a7b72012-08-25 01:24:33 +080062 resp, body = self.get_tenant(tenant_id)
63 name = kwargs.get('name', body['name'])
64 desc = kwargs.get('description', body['description'])
65 en = kwargs.get('enabled', body['enabled'])
vponomaryov67b58fe2014-02-06 19:05:41 +020066 update_tenant = xml.Element("tenant",
67 xmlns=XMLNS,
68 id=tenant_id,
69 name=name,
70 description=desc,
71 enabled=str(en).lower())
Vincent Hou6b8a7b72012-08-25 01:24:33 +080072
73 resp, body = self.post('tenants/%s' % tenant_id,
vponomaryov67b58fe2014-02-06 19:05:41 +020074 str(xml.Document(update_tenant)))
75 return resp, self._parse_resp(body)
Vincent Hou6b8a7b72012-08-25 01:24:33 +080076
huangtianhuafc8db4f2013-10-08 12:05:58 +080077 def create_user(self, name, password, tenant_id, email, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -050078 """Create a user."""
vponomaryov67b58fe2014-02-06 19:05:41 +020079 create_user = xml.Element("user",
80 xmlns=XMLNS,
81 name=name,
82 password=password,
vponomaryov67b58fe2014-02-06 19:05:41 +020083 email=email)
Brant Knudsona4cfe0c2014-03-15 09:36:45 -050084 if tenant_id:
85 create_user.add_attr('tenantId', tenant_id)
huangtianhuafc8db4f2013-10-08 12:05:58 +080086 if 'enabled' in kwargs:
87 create_user.add_attr('enabled', str(kwargs['enabled']).lower())
88
vponomaryov67b58fe2014-02-06 19:05:41 +020089 resp, body = self.post('users', str(xml.Document(create_user)))
90 return resp, self._parse_resp(body)
Vincent Hou6b8a7b72012-08-25 01:24:33 +080091
Chang Bo Guob36b2f12013-09-13 04:52:00 -070092 def update_user(self, user_id, **kwargs):
93 """Updates a user."""
94 if 'enabled' in kwargs:
95 kwargs['enabled'] = str(kwargs['enabled']).lower()
vponomaryov67b58fe2014-02-06 19:05:41 +020096 update_user = xml.Element("user", xmlns=XMLNS, **kwargs)
Chang Bo Guob36b2f12013-09-13 04:52:00 -070097
98 resp, body = self.put('users/%s' % user_id,
vponomaryov67b58fe2014-02-06 19:05:41 +020099 str(xml.Document(update_user)))
100 return resp, self._parse_resp(body)
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800101
102 def enable_disable_user(self, user_id, enabled):
Sean Daguef237ccb2013-01-04 15:19:14 -0500103 """Enables or disables a user."""
vponomaryov67b58fe2014-02-06 19:05:41 +0200104 enable_user = xml.Element("user", enabled=str(enabled).lower())
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800105 resp, body = self.put('users/%s/enabled' % user_id,
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200106 str(xml.Document(enable_user)))
vponomaryov67b58fe2014-02-06 19:05:41 +0200107 return resp, self._parse_resp(body)
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800108
vponomaryov67b58fe2014-02-06 19:05:41 +0200109 def create_service(self, name, service_type, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500110 """Create a service."""
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800111 OS_KSADM = "http://docs.openstack.org/identity/api/ext/OS-KSADM/v1.0"
vponomaryov67b58fe2014-02-06 19:05:41 +0200112 create_service = xml.Element("service",
113 xmlns=OS_KSADM,
114 name=name,
115 type=service_type,
116 description=kwargs.get('description'))
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800117 resp, body = self.post('OS-KSADM/services',
vponomaryov67b58fe2014-02-06 19:05:41 +0200118 str(xml.Document(create_service)))
119 return resp, self._parse_resp(body)
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800120
Abhijeet.Jainff5c3542014-05-06 16:07:30 +0530121 def update_user_password(self, user_id, new_pass):
122 """Update User Password."""
123 put_body = xml.Element("user",
124 id=user_id,
125 password=new_pass)
126 resp, body = self.put('users/%s/OS-KSADM/password' % user_id,
127 str(xml.Document(put_body)))
128 return resp, self._parse_resp(body)
129
Abhijeet.Jain3f49b842014-05-20 12:06:20 +0530130 def list_extensions(self):
131 """List all the extensions."""
132 resp, body = self.get('/extensions')
133 return resp, self._parse_resp(body)
134
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800135
vponomaryov67b58fe2014-02-06 19:05:41 +0200136class TokenClientXML(identity_client.TokenClientJSON):
137 TYPE = "xml"
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800138
Brant Knudsona4cfe0c2014-03-15 09:36:45 -0500139 def auth(self, user, password, tenant=None):
140 passwordCreds = xml.Element('passwordCredentials',
vponomaryov67b58fe2014-02-06 19:05:41 +0200141 username=user,
142 password=password)
Brant Knudsona4cfe0c2014-03-15 09:36:45 -0500143 auth_kwargs = {}
144 if tenant:
145 auth_kwargs['tenantName'] = tenant
146 auth = xml.Element('auth', **auth_kwargs)
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800147 auth.append(passwordCreds)
vponomaryov67b58fe2014-02-06 19:05:41 +0200148 resp, body = self.post(self.auth_url, body=str(xml.Document(auth)))
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000149 return resp, body['access']
Brant Knudsona4cfe0c2014-03-15 09:36:45 -0500150
151 def auth_token(self, token_id, tenant=None):
152 tokenCreds = xml.Element('token', id=token_id)
153 auth_kwargs = {}
154 if tenant:
155 auth_kwargs['tenantName'] = tenant
156 auth = xml.Element('auth', **auth_kwargs)
157 auth.append(tokenCreds)
158 resp, body = self.post(self.auth_url, body=str(xml.Document(auth)))
159 return resp, body['access']