|
1 | 1 | import sys |
2 | 2 | import os |
3 | 3 | import requests |
| 4 | +import json |
| 5 | + |
| 6 | +def check_team_exists(backend_url, team_id, user_principal_id): |
| 7 | + """ |
| 8 | + Check if a team already exists in the database. |
| 9 | + |
| 10 | + Args: |
| 11 | + backend_url: The backend endpoint URL |
| 12 | + team_id: The team ID to check |
| 13 | + user_principal_id: User principal ID for authentication |
| 14 | + |
| 15 | + Returns: |
| 16 | + exists: bool |
| 17 | + """ |
| 18 | + check_endpoint = backend_url.rstrip('/') + f'/api/v3/team_configs/{team_id}' |
| 19 | + headers = { |
| 20 | + 'x-ms-client-principal-id': user_principal_id |
| 21 | + } |
| 22 | + |
| 23 | + try: |
| 24 | + response = requests.get(check_endpoint, headers=headers) |
| 25 | + if response.status_code == 200: |
| 26 | + return True |
| 27 | + elif response.status_code == 404: |
| 28 | + return False |
| 29 | + else: |
| 30 | + print(f"Error checking team {team_id}: Status {response.status_code}, Response: {response.text}") |
| 31 | + return False |
| 32 | + except Exception as e: |
| 33 | + print(f"Exception checking team {team_id}: {str(e)}") |
| 34 | + return False |
4 | 35 |
|
5 | 36 | if len(sys.argv) < 2: |
6 | 37 | print("Usage: python upload_team_config.py <backend_endpoint> <directory_path> [<user_principal_id>]") |
|
28 | 59 | file_path = os.path.join(directory_path, filename) |
29 | 60 | if os.path.isfile(file_path): |
30 | 61 | print(f"Uploading file: {filename}") |
| 62 | + # Check if team already exists |
| 63 | + team_exists = check_team_exists(backend_url, team_id, user_principal_id) |
| 64 | + if team_exists: |
| 65 | + try: |
| 66 | + with open(file_path, 'r', encoding='utf-8') as f: |
| 67 | + team_data = json.load(f) |
| 68 | + team_name = team_data.get('name', 'Unknown') |
| 69 | + print(f"Team '{team_name}' (ID: {team_id}) already exists!") |
| 70 | + continue |
| 71 | + except Exception as e: |
| 72 | + print(f"Error reading {filename}: {str(e)}") |
| 73 | + continue |
| 74 | + |
31 | 75 | try: |
32 | 76 | with open(file_path, 'rb') as file_data: |
33 | 77 | files = { |
|
0 commit comments