|
| 1 | +"""Simple Individual Agent operations example with the TFE Python SDK. |
| 2 | +
|
| 3 | +This example demonstrates: |
| 4 | +1. Listing agents within agent pools |
| 5 | +2. Reading individual agent details |
| 6 | +3. Agent status monitoring |
| 7 | +4. Using the organization SDK client |
| 8 | +
|
| 9 | +Note: Individual agents are created by running the agent binary, not through the API. |
| 10 | +This example shows how to manage agents that have already connected to agent pools. |
| 11 | +
|
| 12 | +Make sure to set the following environment variables: |
| 13 | +- TFE_TOKEN: Your Terraform Cloud/Enterprise API token |
| 14 | +- TFE_ADDRESS: Your Terraform Cloud/Enterprise URL (optional, defaults to https://app.terraform.io) |
| 15 | +- TFE_ORG: Your organization name |
| 16 | +
|
| 17 | +Usage: |
| 18 | + export TFE_TOKEN="your-token-here" |
| 19 | + export TFE_ORG="your-organization" |
| 20 | + python examples/agent_simple.py |
| 21 | +""" |
| 22 | + |
| 23 | +import os |
| 24 | + |
| 25 | +from tfe.client import TFEClient |
| 26 | +from tfe.config import TFEConfig |
| 27 | +from tfe.errors import NotFound |
| 28 | +from tfe.models.agent import AgentListOptions |
| 29 | + |
| 30 | + |
| 31 | +def main(): |
| 32 | + """Main function demonstrating agent operations.""" |
| 33 | + # Get environment variables |
| 34 | + token = os.environ.get("TFE_TOKEN") |
| 35 | + org = os.environ.get("TFE_ORG") |
| 36 | + address = os.environ.get("TFE_ADDRESS", "https://app.terraform.io") |
| 37 | + |
| 38 | + if not token: |
| 39 | + print("❌ TFE_TOKEN environment variable is required") |
| 40 | + return 1 |
| 41 | + |
| 42 | + if not org: |
| 43 | + print("❌ TFE_ORG environment variable is required") |
| 44 | + return 1 |
| 45 | + |
| 46 | + # Create TFE client |
| 47 | + config = TFEConfig(token=token, address=address) |
| 48 | + client = TFEClient(config=config) |
| 49 | + |
| 50 | + print(f"🔗 Connected to: {address}") |
| 51 | + print(f"🏢 Organization: {org}") |
| 52 | + |
| 53 | + try: |
| 54 | + # Example 1: Find agent pools to demonstrate agent operations |
| 55 | + print("\n📋 Finding agent pools...") |
| 56 | + agent_pools = client.agent_pools.list(org) |
| 57 | + |
| 58 | + # Convert to list to check if empty and get count |
| 59 | + pool_list = list(agent_pools) |
| 60 | + if not pool_list: |
| 61 | + print("⚠️ No agent pools found. Create an agent pool first.") |
| 62 | + return 1 |
| 63 | + |
| 64 | + print(f"Found {len(pool_list)} agent pools:") |
| 65 | + for pool in pool_list: |
| 66 | + print(f" - {pool.name} (ID: {pool.id}, Agents: {pool.agent_count})") |
| 67 | + |
| 68 | + # Example 2: List agents in each pool |
| 69 | + print("\n🤖 Listing agents in each pool...") |
| 70 | + total_agents = 0 |
| 71 | + |
| 72 | + for pool in pool_list: |
| 73 | + print(f"\n📂 Agents in pool '{pool.name}':") |
| 74 | + |
| 75 | + # Use optional parameters for listing |
| 76 | + list_options = AgentListOptions(page_size=10) # Optional parameter |
| 77 | + agents = client.agents.list(pool.id, options=list_options) |
| 78 | + |
| 79 | + # Convert to list to check if empty and iterate |
| 80 | + agent_list = list(agents) |
| 81 | + if agent_list: |
| 82 | + total_agents += len(agent_list) |
| 83 | + for agent in agent_list: |
| 84 | + print(f" - Agent {agent.id}") |
| 85 | + print(f" Name: {agent.name or 'Unnamed'}") |
| 86 | + print(f" Status: {agent.status}") |
| 87 | + print(f" Version: {agent.version or 'Unknown'}") |
| 88 | + print(f" IP: {agent.ip_address or 'Unknown'}") |
| 89 | + print(f" Last Ping: {agent.last_ping_at or 'Never'}") |
| 90 | + |
| 91 | + # Example 3: Read detailed agent information |
| 92 | + try: |
| 93 | + agent_details = client.agents.read(agent.id) |
| 94 | + print(" ✅ Agent details retrieved successfully") |
| 95 | + print(f" Full name: {agent_details.name or 'Unnamed'}") |
| 96 | + print(f" Current status: {agent_details.status}") |
| 97 | + except NotFound: |
| 98 | + print(" ⚠️ Agent details not accessible") |
| 99 | + except Exception as e: |
| 100 | + print(f" ❌ Error reading agent details: {e}") |
| 101 | + |
| 102 | + print("") |
| 103 | + else: |
| 104 | + print(" No agents found in this pool") |
| 105 | + |
| 106 | + if total_agents == 0: |
| 107 | + print("\n⚠️ No agents found in any pools.") |
| 108 | + print("To see agents in action:") |
| 109 | + print("1. Create an agent pool") |
| 110 | + print("2. Run a Terraform Enterprise agent binary connected to the pool") |
| 111 | + print("3. Run this example again") |
| 112 | + else: |
| 113 | + print(f"\n📊 Total agents found across all pools: {total_agents}") |
| 114 | + |
| 115 | + print("\n🎉 Agent operations completed successfully!") |
| 116 | + return 0 |
| 117 | + |
| 118 | + except NotFound as e: |
| 119 | + print(f"❌ Resource not found: {e}") |
| 120 | + return 1 |
| 121 | + except Exception as e: |
| 122 | + print(f"❌ Error: {e}") |
| 123 | + return 1 |
| 124 | + |
| 125 | + |
| 126 | +if __name__ == "__main__": |
| 127 | + exit(main()) |
0 commit comments