Package cm_api :: Package endpoints :: Module role_config_groups
[hide private]
[frames] | no frames]

Source Code for Module cm_api.endpoints.role_config_groups

  1  # Licensed to Cloudera, Inc. under one 
  2  # or more contributor license agreements.  See the NOTICE file 
  3  # distributed with this work for additional information 
  4  # regarding copyright ownership.  Cloudera, Inc. licenses this file 
  5  # to you under the Apache License, Version 2.0 (the 
  6  # "License"); you may not use this file except in compliance 
  7  # with the License.  You may obtain a copy of the License at 
  8  # 
  9  #     http://www.apache.org/licenses/LICENSE-2.0 
 10  # 
 11  # Unless required by applicable law or agreed to in writing, software 
 12  # distributed under the License is distributed on an "AS IS" BASIS, 
 13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 14  # See the License for the specific language governing permissions and 
 15  # limitations under the License. 
 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   
30 -def _get_role_config_groups_path(cluster_name, service_name):
31 if cluster_name: 32 return ROLE_CONFIG_GROUPS_PATH % (cluster_name, service_name) 33 else: 34 return CM_ROLE_CONFIG_GROUPS_PATH
35
36 -def _get_role_config_group_path(cluster_name, service_name, name):
37 path = _get_role_config_groups_path(cluster_name, service_name) 38 return "%s/%s" % (path, name)
39
40 -def create_role_config_groups(resource_root, service_name, apigroup_list, 41 cluster_name="default"):
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
55 -def create_role_config_group(resource_root, service_name, name, display_name, 56 role_type, cluster_name="default"):
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
72 -def get_role_config_group(resource_root, service_name, name, 73 cluster_name="default"):
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
85 -def _get_role_config_group(resource_root, path):
86 dic = resource_root.get(path) 87 return ApiRoleConfigGroup.from_json_dict(dic, resource_root)
88
89 -def get_all_role_config_groups(resource_root, service_name, 90 cluster_name="default"):
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
102 -def update_role_config_group(resource_root, service_name, name, apigroup, 103 cluster_name="default"):
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
118 -def delete_role_config_group(resource_root, service_name, name, 119 cluster_name="default"):
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
151 -def move_roles_to_base_role_config_group(resource_root, service_name, 152 role_names, cluster_name="default"):
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
169 -class ApiRoleConfigGroup(BaseApiObject):
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):
185 BaseApiObject.init(self, resource_root, locals())
186
187 - def __str__(self):
188 return "<ApiRoleConfigGroup>: %s (cluster: %s; service: %s)" % ( 189 self.name, self.serviceRef.clusterName, self.serviceRef.serviceName)
190
191 - def _path(self):
192 return _get_role_config_group_path(self.serviceRef.clusterName, 193 self.serviceRef.serviceName, 194 self.name)
195
196 - def get_config(self, view = None):
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
211 - def update_config(self, config):
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
222 - def get_all_roles(self):
223 """ 224 Retrieve the roles in this role config group. 225 226 @return List of roles in this role config group. 227 """ 228 path = self._path() + '/roles' 229 resp = self._get_resource_root().get(path) 230 return ApiList.from_json_dict(ApiRole, resp, self._get_resource_root())
231
232 - def move_roles(self, roles):
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