|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# |
| 4 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 5 | +# contributor license agreements. See the NOTICE file distributed with |
| 6 | +# this work for additional information regarding copyright ownership. |
| 7 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 8 | +# (the "License"); you may not use this file except in compliance with |
| 9 | +# the License. You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, software |
| 14 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +# See the License for the specific language governing permissions and |
| 17 | +# limitations under the License. |
| 18 | + |
| 19 | +from apache_ranger.client.ranger_client import * |
| 20 | +from apache_ranger.utils import * |
| 21 | +from apache_ranger.model.ranger_user_mgmt import * |
| 22 | +from apache_ranger.client.ranger_user_mgmt_client import * |
| 23 | + |
| 24 | + |
| 25 | +class TestUserManagement: |
| 26 | + def __init__(self, ranger_url, username, password): |
| 27 | + self.ranger = RangerClient(ranger_url, (username, password)) |
| 28 | + self.ranger.session.verify = False |
| 29 | + self.ugclient = RangerUserMgmtClient(self.ranger) |
| 30 | + return |
| 31 | + |
| 32 | + ROBOT_LIBRARY_SCOPE = 'SUITE' |
| 33 | + |
| 34 | + def find_users(self): |
| 35 | + print('Listing all users!') |
| 36 | + users = self.ugclient.find_users() |
| 37 | + print(f'{len(users.list)} users found') |
| 38 | + return users |
| 39 | + |
| 40 | + def find_groups(self): |
| 41 | + print('Listing all groups!') |
| 42 | + groups = self.ugclient.find_groups() |
| 43 | + print(f'{len(groups.list)} groups found') |
| 44 | + return groups |
| 45 | + |
| 46 | + def create_user(self, user_name): |
| 47 | + user = RangerUser({'name': user_name, |
| 48 | + 'firstName': user_name, |
| 49 | + 'lastName': 'lnu', |
| 50 | + 'emailAddress': user_name + '@test.org', |
| 51 | + 'password': 'Welcome1', |
| 52 | + 'userRoleList': ['ROLE_USER'], |
| 53 | + 'otherAttributes': '{ "dept": "test" }'}) |
| 54 | + |
| 55 | + created_user = self.ugclient.create_user(user) |
| 56 | + print(f'User {created_user.name} created!') |
| 57 | + return created_user |
| 58 | + |
| 59 | + def create_group(self, group_name): |
| 60 | + group = RangerGroup({'name': group_name, 'otherAttributes': '{ "dept": "test" }'}) |
| 61 | + created_group = self.ugclient.create_group(group) |
| 62 | + print(f'Group {created_group.name} created!') |
| 63 | + return created_group |
| 64 | + |
| 65 | + def add_to_group(self, group_name, group_id, user_id): |
| 66 | + group_user = RangerGroupUser({'name': group_name, 'parentGroupId': group_id, 'userId': user_id}) |
| 67 | + created_group_user = self.ugclient.create_group_user(group_user) |
| 68 | + print(f'Created group-user: {created_group_user}') |
| 69 | + return created_group_user |
| 70 | + |
| 71 | + def list_users_in_group(self, group_name): |
| 72 | + users = self.ugclient.get_users_in_group(group_name) |
| 73 | + return users |
| 74 | + |
| 75 | + def list_groups_for_user(self, user_name): |
| 76 | + groups = self.ugclient.get_groups_for_user(user_name) |
| 77 | + return groups |
| 78 | + |
| 79 | + def list_group_users(self): |
| 80 | + group_users = self.ugclient.find_group_users() |
| 81 | + print(f'{len(group_users.list)} group-users found') |
| 82 | + |
| 83 | + for group_user in group_users.list: |
| 84 | + print(f'id: {group_user.id}, groupId: {group_user.parentGroupId}, userId: {group_user.userId}') |
| 85 | + return group_users |
| 86 | + |
| 87 | + def delete_user_by_id(self, id): |
| 88 | + self.ugclient.delete_user_by_id(id, True) |
| 89 | + return |
| 90 | + |
| 91 | + def delete_group_by_id(self, id): |
| 92 | + self.ugclient.delete_group_by_id(id, True) |
| 93 | + return |
| 94 | + |
| 95 | + def delete_group_user_by_id(self, id): |
| 96 | + self.ugclient.delete_group_user_by_id(id) |
| 97 | + return |
| 98 | + |
0 commit comments