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

Source Code for Module cm_api.endpoints.users

  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 BaseApiObject, ApiList 
 23   
 24  USERS_PATH = "/users" 
 25   
26 -def get_all_users(resource_root, view=None):
27 """ 28 Get all users. 29 30 @param resource_root: The root Resource object 31 @param view: View to materialize ('full' or 'summary'). 32 @return: A list of ApiUser objects. 33 """ 34 dic = resource_root.get(USERS_PATH, 35 params=view and dict(view=view) or None) 36 return ApiList.from_json_dict(ApiUser, dic, resource_root)
37
38 -def get_user(resource_root, username):
39 """ 40 Look up a user by username. 41 42 @param resource_root: The root Resource object 43 @param username: Username to look up 44 @return: An ApiUser object 45 """ 46 dic = resource_root.get('%s/%s' % (USERS_PATH, username)) 47 return ApiUser.from_json_dict(dic, resource_root)
48
49 -def _grant_admin_role(resource_root, username):
50 """ 51 Grant admin access to a user. If the user already has admin access, this 52 does nothing. 53 54 @param resource_root: The root Resource object 55 @param username: Username to give admin access to. 56 @return: An ApiUser object 57 """ 58 apiuser = ApiUser(resource_root, username, roles=['ROLE_ADMIN']) 59 body = json.dumps(apiuser.to_json_dict()) 60 resp = resource_root.put('%s/%s' % (USERS_PATH, username), data=body) 61 return ApiUser.from_json_dict(resp, resource_root)
62
63 -def _revoke_admin_role(resource_root, username):
64 """ 65 Revoke admin access from a user. If the user does not have admin access, 66 this does nothing. 67 68 @param resource_root: The root Resource object 69 @param username: Username to give admin access to. 70 @return: An ApiUser object 71 """ 72 apiuser = ApiUser(resource_root, username, roles=[]) 73 body = json.dumps(apiuser.to_json_dict()) 74 resp = resource_root.put('%s/%s' % (USERS_PATH, username), data=body) 75 return ApiUser.from_json_dict(resp, resource_root)
76
77 -def create_user(resource_root, username, password, roles):
78 """ 79 Create a user. 80 81 @param resource_root: The root Resource object 82 @param username: Username 83 @param password: Password 84 @param roles: List of roles for the user. This should be [] for a 85 regular user, or ['ROLE_ADMIN'] for an admin. 86 @return: An ApiUser object 87 """ 88 apiuser = ApiUser(resource_root, username, password=password, roles=roles) 89 apiuser_list = ApiList([apiuser]) 90 body = json.dumps(apiuser_list.to_json_dict()) 91 resp = resource_root.post(USERS_PATH, data=body) 92 return ApiList.from_json_dict(ApiUser, resp, resource_root)[0]
93
94 -def delete_user(resource_root, username):
95 """ 96 Delete user by username. 97 98 @param resource_root: The root Resource object 99 @param: username Username 100 @return: An ApiUser object 101 """ 102 resp = resource_root.delete('%s/%s' % (USERS_PATH, username)) 103 return ApiUser.from_json_dict(resp, resource_root)
104
105 -class ApiUser(BaseApiObject):
106 _ATTRIBUTES = { 107 'name' : None, 108 'password' : None, 109 'roles' : None, 110 } 111
112 - def __init__(self, resource_root, name=None, password=None, roles=None):
113 BaseApiObject.init(self, resource_root, locals())
114
115 - def grant_admin_role(self):
116 """ 117 Grant admin access to a user. If the user already has admin access, this 118 does nothing. 119 120 @return: An ApiUser object 121 """ 122 return _grant_admin_role(self._get_resource_root(), self.name)
123
124 - def revoke_admin_role(self):
125 """ 126 Revoke admin access from a user. If the user does not have admin access, 127 this does nothing. 128 129 @return: An ApiUser object 130 """ 131 return _revoke_admin_role(self._get_resource_root(), self.name)
132