Repository navigation
Expand file tree
/
Copy pathtenants.py
More file actions
145 lines (126 loc) · 4.2 KB
/
Copy pathtenants.py
File metadata and controls
145 lines (126 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env python
"""
Description: This script prepares new tenant with the following properties:
1. Creates new tenant.
2. Creates new user and this user is added _member_ to tenant. \
Password is second argument of script
3. Admin user will be added to tenant with role admin
Developer: gopal@onecloudinc.com
"""
import os
import pdb
from keystoneclient.v2_0 import client as ksclient
from credentials import get_credentials
from config import USER_COUNT, USER_PASSWORD
credentials = get_credentials()
keystone = ksclient.Client(**credentials)
assign_admin = True
def create_tenant(tenant_name):
"""
This method is used to create tenant and create number of users per tenant
"""
print "\n"
print "=" * 50
print " Initiated Tenant Creation for " + tenant_name
print "=" * 50
print "\n"
try:
try:
new_tenant = keystone.tenants.create(tenant_name=tenant_name,
description="Scale tenant \
created",
enabled=True)
print(' - Tenant %s created' % tenant_name)
except Exception:
new_tenant = keystone.tenants.find(name=tenant_name)
tenant_id = new_tenant.id
tenant_status = True
user_data = []
for j in range(USER_COUNT):
j += 1
user_name = tenant_name + '-user-' + str(j)
user_data.append(create_user(user_name, tenant_id))
except Exception:
tenant_status = False
print "\n"
msg = ('<== Completed Tenant Creation and Users per Tenant '
'with _member_ role ')
msg += 'Successfully ==>'
print msg
print "\n"
tenant_data = {'tenant_name': tenant_name,
'tenant_id': tenant_id,
'status': tenant_status}
return tenant_data
def discover_tenant():
"""
This method is used to discover tenant and discover number of users per tenant
"""
global tenant_id,tenant_status
try:
pdb.set_trace()
new_tenant = keystone.tenants.list()
tenant_id = new_tenant.id
tenant_status = True
print "\n"
print(' - Tenant %s Discovered' % tenant_name)
print(' - Tenant_ID %s Discovered' % tenant_id)
except Exception:
pass
tenant_data = {'tenant_name': tenant_name,
'tenant_id': tenant_id,
'status': tenant_status}
return tenant_data
def create_user(user_name, tenant_id):
"""
This method is to create users per Tenant
"""
try:
new_user = keystone.users.create(name=user_name,
password=USER_PASSWORD,
tenant_id=tenant_id)
print(' - Created User %s' % user_name)
except Exception:
new_user = keystone.users.find(name=user_name)
member_role = keystone.roles.find(name='_member_')
try:
keystone.roles.add_user_role(new_user, member_role, tenant_id)
except Exception:
pass
if assign_admin:
admin_user = keystone.users.find(name='admin')
admin_role = keystone.roles.find(name='admin')
try:
keystone.roles.add_user_role(admin_user, admin_role, tenant_id)
except Exception:
pass
user_data = {'name': new_user.name,
'id': new_user.id}
return user_data
def delete_tenant(tenant_name):
"""
This method is used to delete tenant and users
"""
try:
tenant = keystone.tenants.find(name=tenant_name)
for j in range(USER_COUNT):
j += 1
user_name = tenant_name + '-user-' + str(j)
delete_user(user_name, tenant.id)
tenant.delete()
print(' - Deleted Tenant %s ' % tenant_name)
except Exception:
pass
return True
def delete_user(user_name, tenant_id):
"""
This method is to delete users per Tenant
"""
try:
user = keystone.users.find(name=user_name)
user.delete()
except Exception:
print(" - User Not Found: %s" % user_name)
pass
print(' - Deleted User %s' % user_name)
return True