blob: 81846daec26c1b64103b6aa97b0db91783f5ea7f [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 Treinish684d8992014-01-30 16:27:40 +000015from tempest import config
vponomaryov67b58fe2014-02-06 19:05:41 +020016from tempest.services.compute.xml import common as xml
17from 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
34 def create_tenant(self, name, **kwargs):
35 """
36 Create a tenant
37 name (required): New tenant name
38 description: Description of new tenant (default is none)
39 enabled <true|false>: Initial tenant status (default is true)
40 """
41 en = kwargs.get('enabled', 'true')
vponomaryov67b58fe2014-02-06 19:05:41 +020042 create_tenant = xml.Element("tenant",
43 xmlns=XMLNS,
44 name=name,
45 description=kwargs.get('description', ''),
46 enabled=str(en).lower())
47 resp, body = self.post('tenants', str(xml.Document(create_tenant)))
48 return resp, self._parse_resp(body)
Vincent Hou6b8a7b72012-08-25 01:24:33 +080049
50 def list_tenants(self):
Sean Daguef237ccb2013-01-04 15:19:14 -050051 """Returns tenants."""
vponomaryov67b58fe2014-02-06 19:05:41 +020052 resp, body = self.get('tenants')
53 return resp, self._parse_resp(body)
Matthew Treinishbf41e102013-01-08 15:56:28 -050054
Vincent Hou6b8a7b72012-08-25 01:24:33 +080055 def update_tenant(self, tenant_id, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -050056 """Updates a tenant."""
Vincent Hou6b8a7b72012-08-25 01:24:33 +080057 resp, body = self.get_tenant(tenant_id)
58 name = kwargs.get('name', body['name'])
59 desc = kwargs.get('description', body['description'])
60 en = kwargs.get('enabled', body['enabled'])
vponomaryov67b58fe2014-02-06 19:05:41 +020061 update_tenant = xml.Element("tenant",
62 xmlns=XMLNS,
63 id=tenant_id,
64 name=name,
65 description=desc,
66 enabled=str(en).lower())
Vincent Hou6b8a7b72012-08-25 01:24:33 +080067
68 resp, body = self.post('tenants/%s' % tenant_id,
vponomaryov67b58fe2014-02-06 19:05:41 +020069 str(xml.Document(update_tenant)))
70 return resp, self._parse_resp(body)
Vincent Hou6b8a7b72012-08-25 01:24:33 +080071
huangtianhuafc8db4f2013-10-08 12:05:58 +080072 def create_user(self, name, password, tenant_id, email, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -050073 """Create a user."""
vponomaryov67b58fe2014-02-06 19:05:41 +020074 create_user = xml.Element("user",
75 xmlns=XMLNS,
76 name=name,
77 password=password,
78 tenantId=tenant_id,
79 email=email)
huangtianhuafc8db4f2013-10-08 12:05:58 +080080 if 'enabled' in kwargs:
81 create_user.add_attr('enabled', str(kwargs['enabled']).lower())
82
vponomaryov67b58fe2014-02-06 19:05:41 +020083 resp, body = self.post('users', str(xml.Document(create_user)))
84 return resp, self._parse_resp(body)
Vincent Hou6b8a7b72012-08-25 01:24:33 +080085
Chang Bo Guob36b2f12013-09-13 04:52:00 -070086 def update_user(self, user_id, **kwargs):
87 """Updates a user."""
88 if 'enabled' in kwargs:
89 kwargs['enabled'] = str(kwargs['enabled']).lower()
vponomaryov67b58fe2014-02-06 19:05:41 +020090 update_user = xml.Element("user", xmlns=XMLNS, **kwargs)
Chang Bo Guob36b2f12013-09-13 04:52:00 -070091
92 resp, body = self.put('users/%s' % user_id,
vponomaryov67b58fe2014-02-06 19:05:41 +020093 str(xml.Document(update_user)))
94 return resp, self._parse_resp(body)
Vincent Hou6b8a7b72012-08-25 01:24:33 +080095
96 def enable_disable_user(self, user_id, enabled):
Sean Daguef237ccb2013-01-04 15:19:14 -050097 """Enables or disables a user."""
vponomaryov67b58fe2014-02-06 19:05:41 +020098 enable_user = xml.Element("user", enabled=str(enabled).lower())
Vincent Hou6b8a7b72012-08-25 01:24:33 +080099 resp, body = self.put('users/%s/enabled' % user_id,
vponomaryov67b58fe2014-02-06 19:05:41 +0200100 str(xml.Document(enable_user)), self.headers)
101 return resp, self._parse_resp(body)
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800102
vponomaryov67b58fe2014-02-06 19:05:41 +0200103 def create_service(self, name, service_type, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500104 """Create a service."""
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800105 OS_KSADM = "http://docs.openstack.org/identity/api/ext/OS-KSADM/v1.0"
vponomaryov67b58fe2014-02-06 19:05:41 +0200106 create_service = xml.Element("service",
107 xmlns=OS_KSADM,
108 name=name,
109 type=service_type,
110 description=kwargs.get('description'))
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800111 resp, body = self.post('OS-KSADM/services',
vponomaryov67b58fe2014-02-06 19:05:41 +0200112 str(xml.Document(create_service)))
113 return resp, self._parse_resp(body)
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800114
115
vponomaryov67b58fe2014-02-06 19:05:41 +0200116class TokenClientXML(identity_client.TokenClientJSON):
117 TYPE = "xml"
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800118
119 def auth(self, user, password, tenant):
vponomaryov67b58fe2014-02-06 19:05:41 +0200120 passwordCreds = xml.Element("passwordCredentials",
121 username=user,
122 password=password)
123 auth = xml.Element("auth", tenantName=tenant)
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800124 auth.append(passwordCreds)
vponomaryov67b58fe2014-02-06 19:05:41 +0200125 resp, body = self.post(self.auth_url, body=str(xml.Document(auth)))
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000126 return resp, body['access']