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
146
147
148
149
150
|
#########
# Copyright (c) 2015 GigaSpaces Technologies Ltd. All rights reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# * See the License for the specific language governing permissions and
# * limitations under the License.
from cloudify import ctx
from cloudify.decorators import operation
from cloudify.exceptions import NonRecoverableError
from openstack_plugin_common import (with_keystone_client,
with_nova_client,
with_cinder_client,
with_neutron_client,
get_resource_id,
use_external_resource,
delete_resource_and_runtime_properties,
validate_resource,
COMMON_RUNTIME_PROPERTIES_KEYS,
OPENSTACK_ID_PROPERTY,
OPENSTACK_TYPE_PROPERTY,
OPENSTACK_NAME_PROPERTY)
PROJECT_OPENSTACK_TYPE = 'project'
TENANT_QUOTA_TYPE = 'quota'
RUNTIME_PROPERTIES_KEYS = COMMON_RUNTIME_PROPERTIES_KEYS
@operation
@with_keystone_client
def create(keystone_client, **kwargs):
if use_external_resource(ctx, keystone_client, PROJECT_OPENSTACK_TYPE):
return
project_dict = {
'name': get_resource_id(ctx, PROJECT_OPENSTACK_TYPE),
'domain': 'default'
}
project_dict.update(ctx.node.properties['project'])
project = keystone_client.projects.create(**project_dict)
ctx.instance.runtime_properties[OPENSTACK_ID_PROPERTY] = project.id
ctx.instance.runtime_properties[OPENSTACK_TYPE_PROPERTY] = \
PROJECT_OPENSTACK_TYPE
ctx.instance.runtime_properties[OPENSTACK_NAME_PROPERTY] = project.name
@operation
@with_keystone_client
@with_nova_client
@with_cinder_client
@with_neutron_client
def start(keystone_client, nova_client, cinder_client, neutron_client,
**kwargs):
project_id = ctx.instance.runtime_properties[OPENSTACK_ID_PROPERTY]
users = ctx.node.properties['users']
validate_users(users, keystone_client)
assign_users(project_id, users, keystone_client)
quota = ctx.node.properties[TENANT_QUOTA_TYPE]
update_quota(project_id, quota, nova_client, 'nova')
update_quota(project_id, quota, neutron_client, 'neutron')
update_quota(project_id, quota, cinder_client, 'cinder')
@operation
@with_keystone_client
@with_nova_client
@with_cinder_client
@with_neutron_client
def delete(keystone_client, nova_client, cinder_client,
neutron_client, **kwargs):
tenant_id = ctx.instance.runtime_properties[OPENSTACK_ID_PROPERTY]
quota = ctx.node.properties[TENANT_QUOTA_TYPE]
delete_quota(tenant_id, quota, nova_client, 'nova')
delete_quota(tenant_id, quota, neutron_client, 'neutron')
delete_quota(tenant_id, quota, cinder_client, 'cinder')
delete_resource_and_runtime_properties(ctx, keystone_client,
RUNTIME_PROPERTIES_KEYS)
@operation
@with_keystone_client
def creation_validation(keystone_client, **kwargs):
validate_resource(ctx, keystone_client, PROJECT_OPENSTACK_TYPE)
def assign_users(project_id, users, keystone_client):
for user in users:
roles = user['roles']
u = keystone_client.users.find(name=user['name'])
for role in roles:
r = keystone_client.roles.find(name=role)
keystone_client.roles.grant(user=u.id,
project=project_id,
role=r.id)
def validate_users(users, keystone_client):
user_names = [user['name'] for user in users]
if len(user_names) > len(set(user_names)):
raise NonRecoverableError('Users are not unique')
for user_name in user_names:
keystone_client.users.find(name=user_name)
for user in users:
if len(user['roles']) > len(set(user['roles'])):
msg = 'Roles for user {} are not unique'
raise NonRecoverableError(msg.format(user['name']))
role_names = {role for user in users for role in user['roles']}
for role_name in role_names:
keystone_client.roles.find(name=role_name)
def update_quota(tenant_id, quota, client, what_quota):
updated_quota = quota.get(what_quota)
if updated_quota:
if what_quota == 'neutron':
new_quota = client.update_quota(tenant_id=tenant_id,
body={'quota': updated_quota})
else:
new_quota = client.quotas.update(tenant_id=tenant_id,
**updated_quota)
ctx.logger.info(
'Updated {0} quota: {1}'.format(what_quota, str(new_quota)))
def delete_quota(project_id, quota, client, what_quota):
deleting_quota = quota.get(what_quota)
if deleting_quota:
if what_quota == 'neutron':
client.delete_quota(tenant_id=project_id)
else:
client.quotas.delete(tenant_id=project_id)
|