Skip to content

Commit 83b9747

Browse files
fix: docs, add python script I used in debugging the MP setup (#12)
## what - add debugging python script to the import example # why - planning to reference this in the blog post <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Introduced a Python script to help inspect Google Workspace users, roles, and custom schema data for debugging purposes. - Enables listing of user schemas, viewing custom schema data for specific users, and listing group members. - Provides detailed output and error handling for easier troubleshooting during Terraform module imports. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 94a6ecb commit 83b9747

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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

Comments
 (0)