function main_v43
Orchestrates a comprehensive SharePoint connection diagnostic tool that validates Azure AD authentication and SharePoint access by running multiple tests and reporting results.
/tf/active/vicechatdev/SPFCsync/diagnose_sharepoint.py
189 - 227
moderate
Purpose
This function serves as the entry point for a diagnostic tool that verifies SharePoint connectivity. It loads configuration, displays connection parameters, executes authentication tests (Azure token and SharePoint token), and provides detailed feedback on test results with troubleshooting guidance. Returns 0 on success or 1 on failure, making it suitable for use as a CLI tool exit code.
Source Code
def main():
"""Run all diagnostics."""
print("SharePoint Connection Diagnostic Tool")
print("=" * 50)
config = load_config()
if not config:
print("ā Could not load configuration")
return 1
print(f"SharePoint Site: {config.get('SHAREPOINT_SITE_URL', 'Not set')}")
print(f"Client ID: {config.get('AZURE_CLIENT_ID', 'Not set')[:8]}...")
print(f"Client Secret: {'Set' if config.get('AZURE_CLIENT_SECRET') else 'Not set'}")
print()
# Run tests
tests_passed = 0
total_tests = 2
if test_azure_token():
tests_passed += 1
if test_sharepoint_token():
tests_passed += 1
print("\n" + "=" * 50)
print(f"Diagnostic Results: {tests_passed}/{total_tests} tests passed")
if tests_passed == total_tests:
print("š All diagnostics passed! SharePoint connection should work.")
return 0
else:
print("ā Some diagnostics failed. Please check the guidance above.")
print("\nš Common Solutions:")
print("1. Verify Azure AD app permissions (Sites.Read.All)")
print("2. Ensure admin consent is granted")
print("3. Check client ID and secret are correct")
print("4. Verify SharePoint site URL is accessible")
return 1
Return Value
Returns an integer exit code: 0 if all diagnostic tests pass successfully, or 1 if any tests fail or configuration cannot be loaded. This follows standard Unix exit code conventions where 0 indicates success.
Dependencies
requests
Required Imports
import requests
import json
import base64
from urllib.parse import quote
import os
import sys
Usage Example
# Assuming load_config(), test_azure_token(), and test_sharepoint_token() are defined
# and environment variables or config file are set up
if __name__ == '__main__':
exit_code = main()
sys.exit(exit_code)
# Or simply:
# python diagnostic_tool.py
# The function will print diagnostic results and return appropriate exit code
Best Practices
- This function depends on three external functions: load_config(), test_azure_token(), and test_sharepoint_token() which must be defined in the same module
- Use the return value as a system exit code for proper CLI integration
- Ensure Azure AD application has proper permissions (Sites.Read.All) before running
- The function prints sensitive information (partial client ID), ensure output is not logged in production
- Configuration should be stored securely, preferably using environment variables or encrypted config files
- The function assumes exactly 2 tests; if adding more tests, update the total_tests variable accordingly
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function main_v47 81.0% similar
-
function main_v44 80.3% similar
-
function test_sharepoint_connection 77.7% similar
-
function main_v33 76.2% similar
-
function main_v17 75.0% similar