1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 try:
18 import json
19 except ImportError:
20 import simplejson as json
21
22 from cm_api.endpoints.types import *
23 from cm_api.endpoints.roles import ApiRole
24
25 __docformat__ = "epytext"
26
27 ROLE_CONFIG_GROUPS_PATH = "/clusters/%s/services/%s/roleConfigGroups"
28 CM_ROLE_CONFIG_GROUPS_PATH = "/cm/service/roleConfigGroups"
29
35
39
42 """
43 Create role config groups.
44 @param resource_root: The root Resource object.
45 @param service_name: Service name.
46 @param apigroup_list: List of role config groups to create.
47 @param cluster_name: Cluster name.
48 @return: New ApiRoleConfigGroup object.
49 """
50 body = json.dumps(apigroup_list.to_json_dict())
51 resp = resource_root.post(_get_role_config_groups_path(
52 cluster_name, service_name), data=body)
53 return ApiList.from_json_dict(ApiRoleConfigGroup, resp, resource_root)
54
57 """
58 Create a role config group.
59 @param resource_root: The root Resource object.
60 @param service_name: Service name.
61 @param name: The name of the new group.
62 @param display_name: The display name of the new group.
63 @param role_type: The role type of the new group.
64 @param cluster_name: Cluster name.
65 @return: List of created role config groups.
66 """
67 apigroup = ApiRoleConfigGroup(resource_root, name, display_name, role_type)
68 apigroup_list = ApiList([apigroup])
69 return create_role_config_groups(resource_root, service_name, apigroup_list,
70 cluster_name)[0]
71
74 """
75 Find a role config group by name.
76 @param resource_root: The root Resource object.
77 @param service_name: Service name.
78 @param name: Role config group name.
79 @param cluster_name: Cluster name.
80 @return: An ApiRoleConfigGroup object.
81 """
82 return _get_role_config_group(resource_root, _get_role_config_group_path(
83 cluster_name, service_name, name))
84
88
91 """
92 Get all role config groups in the specified service.
93 @param resource_root: The root Resource object.
94 @param service_name: Service name.
95 @param cluster_name: Cluster name.
96 @return: A list of ApiRoleConfigGroup objects.
97 """
98 dic = resource_root.get(_get_role_config_groups_path(
99 cluster_name, service_name))
100 return ApiList.from_json_dict(ApiRoleConfigGroup, dic, resource_root)
101
104 """
105 Update a role config group by name.
106 @param resource_root: The root Resource object.
107 @param service_name: Service name.
108 @param name: Role config group name.
109 @param apigroup: The updated role config group.
110 @param cluster_name: Cluster name.
111 @return: The updated ApiRoleConfigGroup object.
112 """
113 body = json.dumps(apigroup.to_json_dict())
114 resp = resource_root.put(_get_role_config_group_path(
115 cluster_name, service_name, name), data=body)
116 return ApiRoleConfigGroup.from_json_dict(resp, resource_root)
117
120 """
121 Delete a role config group by name.
122 @param resource_root: The root Resource object.
123 @param service_name: Service name.
124 @param name: Role config group name.
125 @param cluster_name: Cluster name.
126 @return: The deleted ApiRoleConfigGroup object.
127 """
128 resp = resource_root.delete(_get_role_config_group_path(
129 cluster_name, service_name, name))
130 return ApiRoleConfigGroup.from_json_dict(resp, resource_root)
131
132 -def move_roles(resource_root, service_name, name, role_names,
133 cluster_name="default"):
134 """
135 Moves roles to the specified role config group.
136
137 The roles can be moved from any role config group belonging
138 to the same service. The role type of the destination group
139 must match the role type of the roles.
140
141 @param name: The name of the group the roles will be moved to.
142 @param role_names: The names of the roles to move.
143 @return List of roles which have been moved successfully.
144 """
145 path = _get_role_config_group_path(
146 cluster_name, service_name, name) + '/roles'
147 resp = resource_root.put(path,
148 data=json.dumps({ApiList.LIST_KEY : role_names}))
149 return ApiList.from_json_dict(ApiRole, resp, resource_root)
150
153 """
154 Moves roles to the base role config group.
155
156 The roles can be moved from any role config group belonging to the same
157 service. The role type of the roles may vary. Each role will be moved to
158 its corresponding base group depending on its role type.
159
160 @param role_names The names of the roles to move.
161 @return List of roles which have been moved successfully.
162 """
163 path = _get_role_config_groups_path(cluster_name, service_name) + '/roles'
164 resp = resource_root.put(path,
165 data=json.dumps({ApiList.LIST_KEY : role_names}))
166 return ApiList.from_json_dict(ApiRole, resp, resource_root)
167
168
170 """
171 name is RW only temporarily; once all RCG names are unique,
172 this property will be auto-generated and Read-only
173 """
174 _ATTRIBUTES = {
175 'name' : None,
176 'displayName' : None,
177 'roleType' : None,
178 'config' : Attr(ApiConfig),
179 'base' : ROAttr(),
180 'serviceRef' : ROAttr(ApiServiceRef),
181 }
182
183 - def __init__(self, resource_root, name=None, displayName=None, roleType=None,
184 config=None):
186
188 return "<ApiRoleConfigGroup>: %s (cluster: %s; service: %s)" % (
189 self.name, self.serviceRef.clusterName, self.serviceRef.serviceName)
190
195
197 """
198 Retrieve the group's configuration.
199
200 The 'summary' view contains strings as the dictionary values. The full
201 view contains ApiConfig instances as the values.
202
203 @param view: View to materialize ('full' or 'summary').
204 @return Dictionary with configuration data.
205 """
206 path = self._path() + '/config'
207 resp = self._get_resource_root().get(path,
208 params = view and dict(view=view) or None)
209 return json_to_config(resp, view == 'full')
210
212 """
213 Update the group's configuration.
214
215 @param config Dictionary with configuration to update.
216 @return Dictionary with updated configuration.
217 """
218 path = self._path() + '/config'
219 resp = self._get_resource_root().put(path, data = config_to_json(config))
220 return json_to_config(resp)
221
231
233 """
234 Moves roles to this role config group.
235
236 The roles can be moved from any role config group belonging
237 to the same service. The role type of the destination group
238 must match the role type of the roles.
239
240 @param roles: The names of the roles to move.
241 @return List of roles which have been moved successfully.
242 """
243 return move_roles(self._get_resource_root(), self.serviceRef.serviceName,
244 self.name, roles, self.serviceRef.clusterName)
245