|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import argparse |
| 4 | +import os |
| 5 | + |
| 6 | +from pytfe import TFEClient, TFEConfig |
| 7 | +from pytfe.models import ( |
| 8 | + TeamCreateOptions, |
| 9 | + TeamIncludeOpt, |
| 10 | + TeamListOptions, |
| 11 | + TeamUpdateOptions, |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +def _print_header(title: str): |
| 16 | + print("\n" + "=" * 80) |
| 17 | + print(title) |
| 18 | + print("=" * 80) |
| 19 | + |
| 20 | + |
| 21 | +def main(): |
| 22 | + parser = argparse.ArgumentParser(description="Teams list demo for python-tfe SDK") |
| 23 | + parser.add_argument( |
| 24 | + "--address", default=os.getenv("TFE_ADDRESS", "https://app.terraform.io") |
| 25 | + ) |
| 26 | + parser.add_argument("--token", default=os.getenv("TFE_TOKEN", "")) |
| 27 | + parser.add_argument( |
| 28 | + "--org", |
| 29 | + required=True, |
| 30 | + help="Organization name", |
| 31 | + ) |
| 32 | + parser.add_argument( |
| 33 | + "--page-size", |
| 34 | + type=int, |
| 35 | + default=20, |
| 36 | + help="Page size for fetching teams", |
| 37 | + ) |
| 38 | + parser.add_argument( |
| 39 | + "--query", |
| 40 | + default=None, |
| 41 | + help="Optional q filter for team search", |
| 42 | + ) |
| 43 | + parser.add_argument( |
| 44 | + "--names", |
| 45 | + nargs="+", |
| 46 | + default=None, |
| 47 | + help="Optional team names filter (space-separated)", |
| 48 | + ) |
| 49 | + parser.add_argument( |
| 50 | + "--include-users", |
| 51 | + action="store_true", |
| 52 | + help="Include related users", |
| 53 | + ) |
| 54 | + parser.add_argument( |
| 55 | + "--include-memberships", |
| 56 | + action="store_true", |
| 57 | + help="Include related organization-memberships", |
| 58 | + ) |
| 59 | + parser.add_argument( |
| 60 | + "--create", |
| 61 | + action="store_true", |
| 62 | + help="Create a new team before listing", |
| 63 | + ) |
| 64 | + parser.add_argument( |
| 65 | + "--name", |
| 66 | + default=None, |
| 67 | + help="Team name for create operation", |
| 68 | + ) |
| 69 | + parser.add_argument( |
| 70 | + "--visibility", |
| 71 | + default="secret", |
| 72 | + help="Team visibility for create operation (secret or organization)", |
| 73 | + ) |
| 74 | + parser.add_argument( |
| 75 | + "--sso-team-id", |
| 76 | + default=None, |
| 77 | + help="Optional SSO team ID for create operation", |
| 78 | + ) |
| 79 | + parser.add_argument( |
| 80 | + "--allow-member-token-management", |
| 81 | + action="store_true", |
| 82 | + help="Enable member token management on create/update", |
| 83 | + ) |
| 84 | + parser.add_argument( |
| 85 | + "--update", |
| 86 | + action="store_true", |
| 87 | + help="Update a team before listing", |
| 88 | + ) |
| 89 | + parser.add_argument( |
| 90 | + "--read", |
| 91 | + action="store_true", |
| 92 | + help="Read a team by ID before listing", |
| 93 | + ) |
| 94 | + parser.add_argument( |
| 95 | + "--delete", |
| 96 | + action="store_true", |
| 97 | + help="Delete a team by ID before listing", |
| 98 | + ) |
| 99 | + parser.add_argument( |
| 100 | + "--team-id", |
| 101 | + default=None, |
| 102 | + help="Team ID for read/update/delete operation", |
| 103 | + ) |
| 104 | + args = parser.parse_args() |
| 105 | + |
| 106 | + cfg = TFEConfig(address=args.address, token=args.token) |
| 107 | + client = TFEClient(cfg) |
| 108 | + |
| 109 | + if args.create: |
| 110 | + if not args.name: |
| 111 | + print("Error: --name is required when using --create") |
| 112 | + return |
| 113 | + |
| 114 | + _print_header(f"Creating team in organization: {args.org}") |
| 115 | + create_options = TeamCreateOptions( |
| 116 | + name=args.name, |
| 117 | + visibility=args.visibility, |
| 118 | + sso_team_id=args.sso_team_id, |
| 119 | + allow_member_token_management=args.allow_member_token_management, |
| 120 | + ) |
| 121 | + new_team = client.teams.create(args.org, create_options) |
| 122 | + print(f"Created Team ID: {new_team.id}") |
| 123 | + print(f"Name: {new_team.name}") |
| 124 | + print(f"Visibility: {new_team.visibility}") |
| 125 | + print( |
| 126 | + f"Allow Member Token Management: {new_team.allow_member_token_management}" |
| 127 | + ) |
| 128 | + print() |
| 129 | + |
| 130 | + if args.update: |
| 131 | + if not args.team_id: |
| 132 | + print("Error: --team-id is required when using --update") |
| 133 | + return |
| 134 | + |
| 135 | + _print_header(f"Updating team: {args.team_id}") |
| 136 | + update_options = TeamUpdateOptions( |
| 137 | + name=args.name, |
| 138 | + visibility=args.visibility, |
| 139 | + sso_team_id=args.sso_team_id, |
| 140 | + allow_member_token_management=args.allow_member_token_management, |
| 141 | + ) |
| 142 | + updated_team = client.teams.update(args.team_id, update_options) |
| 143 | + print(f"Updated Team ID: {updated_team.id}") |
| 144 | + print(f"Name: {updated_team.name}") |
| 145 | + print(f"Visibility: {updated_team.visibility}") |
| 146 | + print( |
| 147 | + f"Allow Member Token Management: {updated_team.allow_member_token_management}" |
| 148 | + ) |
| 149 | + print() |
| 150 | + |
| 151 | + if args.read: |
| 152 | + if not args.team_id: |
| 153 | + print("Error: --team-id is required when using --read") |
| 154 | + return |
| 155 | + |
| 156 | + _print_header(f"Reading team: {args.team_id}") |
| 157 | + team = client.teams.read(args.team_id) |
| 158 | + print(f"Team ID: {team.id}") |
| 159 | + print(f"Name: {team.name}") |
| 160 | + print(f"Visibility: {team.visibility}") |
| 161 | + print(f"Is Unified: {team.is_unified}") |
| 162 | + print(f"User Count: {team.user_count}") |
| 163 | + print(f"Allow Member Token Management: {team.allow_member_token_management}") |
| 164 | + |
| 165 | + if team.organization_access: |
| 166 | + print("Organization Access:") |
| 167 | + print(f" - manage_workspaces={team.organization_access.manage_workspaces}") |
| 168 | + print(f" - read_workspaces={team.organization_access.read_workspaces}") |
| 169 | + print(f" - manage_projects={team.organization_access.manage_projects}") |
| 170 | + |
| 171 | + if team.permissions: |
| 172 | + print("Permissions:") |
| 173 | + print(f" - can_update_membership={team.permissions.can_update_membership}") |
| 174 | + print(f" - can_destroy={team.permissions.can_destroy}") |
| 175 | + |
| 176 | + print(f"Users included: {len(team.users)}") |
| 177 | + print( |
| 178 | + f"Organization memberships included: {len(team.organization_memberships)}" |
| 179 | + ) |
| 180 | + print() |
| 181 | + |
| 182 | + if args.delete: |
| 183 | + if not args.team_id: |
| 184 | + print("Error: --team-id is required when using --delete") |
| 185 | + return |
| 186 | + |
| 187 | + _print_header(f"Deleting team: {args.team_id}") |
| 188 | + client.teams.delete(args.team_id) |
| 189 | + print(f"Deleted Team ID: {args.team_id}") |
| 190 | + print() |
| 191 | + |
| 192 | + includes: list[TeamIncludeOpt] = [] |
| 193 | + if args.include_users: |
| 194 | + includes.append(TeamIncludeOpt.TEAM_USERS) |
| 195 | + if args.include_memberships: |
| 196 | + includes.append(TeamIncludeOpt.TEAM_ORGANIZATION_MEMBERSHIPS) |
| 197 | + |
| 198 | + options = TeamListOptions( |
| 199 | + page_size=args.page_size, |
| 200 | + query=args.query, |
| 201 | + names=args.names, |
| 202 | + include=includes or None, |
| 203 | + ) |
| 204 | + |
| 205 | + _print_header(f"Listing teams for organization: {args.org}") |
| 206 | + print("Options:") |
| 207 | + print(f"- page_size={args.page_size}") |
| 208 | + print(f"- query={args.query}") |
| 209 | + print(f"- names={args.names}") |
| 210 | + print(f"- include={[item.value for item in includes] if includes else None}") |
| 211 | + print() |
| 212 | + |
| 213 | + count = 0 |
| 214 | + for team in client.teams.list(args.org, options): |
| 215 | + count += 1 |
| 216 | + print(f"[{count}] Team ID: {team.id}") |
| 217 | + print(f"Name: {team.name}") |
| 218 | + print(f"Visibility: {team.visibility}") |
| 219 | + print(f"Is Unified: {team.is_unified}") |
| 220 | + print(f"User Count: {team.user_count}") |
| 221 | + print(f"Allow Member Token Management: {team.allow_member_token_management}") |
| 222 | + |
| 223 | + if team.organization_access: |
| 224 | + print("Organization Access:") |
| 225 | + print(f" - manage_workspaces={team.organization_access.manage_workspaces}") |
| 226 | + print(f" - read_workspaces={team.organization_access.read_workspaces}") |
| 227 | + print(f" - manage_projects={team.organization_access.manage_projects}") |
| 228 | + |
| 229 | + if team.permissions: |
| 230 | + print("Permissions:") |
| 231 | + print(f" - can_update_membership={team.permissions.can_update_membership}") |
| 232 | + print(f" - can_destroy={team.permissions.can_destroy}") |
| 233 | + |
| 234 | + print(f"Users included: {len(team.users)}") |
| 235 | + print( |
| 236 | + f"Organization memberships included: {len(team.organization_memberships)}" |
| 237 | + ) |
| 238 | + print() |
| 239 | + |
| 240 | + if count == 0: |
| 241 | + print("No teams found.") |
| 242 | + else: |
| 243 | + print(f"Total teams: {count}") |
| 244 | + |
| 245 | + |
| 246 | +if __name__ == "__main__": |
| 247 | + main() |
0 commit comments