|
| 1 | +# |
| 2 | +# We at Masterpoint found this python script to be useful when debugging import |
| 3 | +# issues with existing users and roles into the terraform module to confirm the |
| 4 | +# expected data values. For example, confirm data and formatting of |
| 5 | +# exsting users' custom schema keys, values, and json encoded strings. |
| 6 | +# |
| 7 | +# This is intended only for ad-hoc debugging purposes and has not been thorughly |
| 8 | +# reviewed or tested. Use at your own risk. |
| 9 | +# |
| 10 | + |
| 11 | +from google.oauth2 import service_account |
| 12 | +from googleapiclient.discovery import build |
| 13 | + |
| 14 | +# Path to your service account JSON key |
| 15 | +SERVICE_ACCOUNT_FILE = 'my-google-admin-api-key.json' |
| 16 | + |
| 17 | +# Replace with your impersonated Google Workspace admin email |
| 18 | +DELEGATED_ADMIN = 'first.last@your-company.io' |
| 19 | + |
| 20 | +SCOPES = [ |
| 21 | + "https://www.googleapis.com/auth/admin.directory.group", |
| 22 | + "https://www.googleapis.com/auth/admin.directory.user", |
| 23 | + "https://www.googleapis.com/auth/admin.directory.userschema", |
| 24 | + "https://www.googleapis.com/auth/apps.groups.settings", |
| 25 | + "https://www.googleapis.com/auth/iam", |
| 26 | +] |
| 27 | + |
| 28 | +# Load credentials and delegate to admin |
| 29 | +credentials = service_account.Credentials.from_service_account_file( |
| 30 | + SERVICE_ACCOUNT_FILE, |
| 31 | + scopes=SCOPES |
| 32 | +).with_subject(DELEGATED_ADMIN) |
| 33 | + |
| 34 | + |
| 35 | +# Build the service |
| 36 | +service = build('admin', 'directory_v1', credentials=credentials) |
| 37 | + |
| 38 | + |
| 39 | +# Call the Directory API to list all user schemas |
| 40 | +def list_user_schemas(customer_id='my_customer'): |
| 41 | + try: |
| 42 | + schemas = service.schemas().list(customerId=customer_id).execute() |
| 43 | + for schema in schemas.get('schemas', []): |
| 44 | + print(f"Schema ID: {schema['schemaId']}") |
| 45 | + print(f"Schema Name: {schema['schemaName']}") |
| 46 | + print(f"Fields:") |
| 47 | + for field in schema.get('fields', []): |
| 48 | + print(field) |
| 49 | + # print(f" - '{field['fieldName']}' ({field['fieldType']})") |
| 50 | + print(f" - '{field['fieldName']}': '{field['fieldValues']}'") |
| 51 | + except Exception as e: |
| 52 | + print(f"Failed to retrieve schemas: {e}") |
| 53 | + |
| 54 | + |
| 55 | +def get_user_custom_schemas(user_email): |
| 56 | + try: |
| 57 | + # Use projection='full' to include custom schemas in the response |
| 58 | + user = service.users().get(userKey=user_email, projection='full').execute() |
| 59 | + print(user) |
| 60 | + custom_schemas = user.get('customSchemas', {}) |
| 61 | + |
| 62 | + print(f"Custom schemas for {user_email}:") |
| 63 | + for schema_name, schema_data in custom_schemas.items(): |
| 64 | + print(f" Schema: {schema_name}") |
| 65 | + for field_name, field_value in schema_data.items(): |
| 66 | + print(f" {field_name}: {field_value}") |
| 67 | + |
| 68 | + return custom_schemas |
| 69 | + except Exception as e: |
| 70 | + print(f"Failed to retrieve user custom schemas: {e}") |
| 71 | + return None |
| 72 | + |
| 73 | + |
| 74 | +def list_group_members(group_email): |
| 75 | + results = service.members().list(groupKey=group_email).execute() |
| 76 | + members = results.get('members', []) |
| 77 | + for member in members: |
| 78 | + # print(member['email']) |
| 79 | + print(member) |
| 80 | + |
| 81 | + |
| 82 | +if __name__ == '__main__': |
| 83 | + # list_group_members('team@your-company.io') |
| 84 | + # list_user_schemas() |
| 85 | + get_user_custom_schemas('first.last@your-company.io') |
0 commit comments