|
| 1 | +"""Example script for working with workspace resources in Terraform Enterprise. |
| 2 | +
|
| 3 | +This script demonstrates how to list resources within a workspace. |
| 4 | +""" |
| 5 | + |
| 6 | +import argparse |
| 7 | +import sys |
| 8 | + |
| 9 | +from pytfe import TFEClient |
| 10 | +from pytfe.models import WorkspaceResourceListOptions |
| 11 | + |
| 12 | + |
| 13 | +def list_workspace_resources( |
| 14 | + client: TFEClient, |
| 15 | + workspace_id: str, |
| 16 | + page_number: int | None = None, |
| 17 | + page_size: int | None = None, |
| 18 | +) -> None: |
| 19 | + """List all resources in a workspace.""" |
| 20 | + try: |
| 21 | + print(f"Listing resources for workspace: {workspace_id}") |
| 22 | + |
| 23 | + # Prepare list options |
| 24 | + options = None |
| 25 | + if page_number or page_size: |
| 26 | + options = WorkspaceResourceListOptions() |
| 27 | + if page_number: |
| 28 | + options.page_number = page_number |
| 29 | + if page_size: |
| 30 | + options.page_size = page_size |
| 31 | + |
| 32 | + # List workspace resources (returns an iterator) |
| 33 | + resources = list(client.workspace_resources.list(workspace_id, options)) |
| 34 | + |
| 35 | + if not resources: |
| 36 | + print("No resources found in this workspace.") |
| 37 | + return |
| 38 | + |
| 39 | + print(f"\nFound {len(resources)} resource(s):") |
| 40 | + print("-" * 80) |
| 41 | + |
| 42 | + for resource in resources: |
| 43 | + print(f"ID: {resource.id}") |
| 44 | + print(f"Address: {resource.address}") |
| 45 | + print(f"Name: {resource.name}") |
| 46 | + print(f"Module: {resource.module}") |
| 47 | + print(f"Provider: {resource.provider}") |
| 48 | + print(f"Provider Type: {resource.provider_type}") |
| 49 | + print(f"Created At: {resource.created_at}") |
| 50 | + print(f"Updated At: {resource.updated_at}") |
| 51 | + print(f"Modified By State Version: {resource.modified_by_state_version_id}") |
| 52 | + if resource.name_index: |
| 53 | + print(f"Name Index: {resource.name_index}") |
| 54 | + print("-" * 80) |
| 55 | + |
| 56 | + except Exception as e: |
| 57 | + print(f"Error listing workspace resources: {e}", file=sys.stderr) |
| 58 | + sys.exit(1) |
| 59 | + |
| 60 | + |
| 61 | +def main(): |
| 62 | + """Main function to handle command line arguments and execute operations.""" |
| 63 | + parser = argparse.ArgumentParser( |
| 64 | + description="Manage workspace resources in Terraform Enterprise", |
| 65 | + formatter_class=argparse.RawDescriptionHelpFormatter, |
| 66 | + epilog=""" |
| 67 | +Examples: |
| 68 | + # List all resources in a workspace |
| 69 | + python workspace_resources.py --list --workspace-id ws-abc123 |
| 70 | +
|
| 71 | + # List with pagination |
| 72 | + python workspace_resources.py --list --workspace-id ws-abc123 --page-number 2 --page-size 50 |
| 73 | +
|
| 74 | +Environment variables: |
| 75 | + TFE_TOKEN: Your Terraform Enterprise API token |
| 76 | + TFE_URL: Your Terraform Enterprise URL (default: https://app.terraform.io) |
| 77 | + TFE_ORG: Your Terraform Enterprise organization name |
| 78 | + """, |
| 79 | + ) |
| 80 | + |
| 81 | + # Add command flags |
| 82 | + parser.add_argument( |
| 83 | + "--list", |
| 84 | + action="store_true", |
| 85 | + help="List workspace resources" |
| 86 | + ) |
| 87 | + parser.add_argument( |
| 88 | + "--workspace-id", |
| 89 | + required=True, |
| 90 | + help="ID of the workspace (required, e.g., ws-abc123)" |
| 91 | + ) |
| 92 | + parser.add_argument( |
| 93 | + "--page-number", |
| 94 | + type=int, |
| 95 | + help="Page number for pagination" |
| 96 | + ) |
| 97 | + parser.add_argument( |
| 98 | + "--page-size", |
| 99 | + type=int, |
| 100 | + help="Page size for pagination" |
| 101 | + ) |
| 102 | + |
| 103 | + args = parser.parse_args() |
| 104 | + |
| 105 | + if not args.list: |
| 106 | + parser.print_help() |
| 107 | + sys.exit(1) |
| 108 | + |
| 109 | + # Initialize TFE client |
| 110 | + try: |
| 111 | + client = TFEClient() |
| 112 | + except Exception as e: |
| 113 | + print(f"Error initializing TFE client: {e}", file=sys.stderr) |
| 114 | + print( |
| 115 | + "Make sure TFE_TOKEN and TFE_URL environment variables are set.", |
| 116 | + file=sys.stderr, |
| 117 | + ) |
| 118 | + sys.exit(1) |
| 119 | + |
| 120 | + # Execute the list command |
| 121 | + list_workspace_resources( |
| 122 | + client, |
| 123 | + args.workspace_id, |
| 124 | + args.page_number, |
| 125 | + args.page_size, |
| 126 | + ) |
| 127 | + |
| 128 | + |
| 129 | +if __name__ == "__main__": |
| 130 | + main() |
0 commit comments