Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 25 additions & 25 deletions examples/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,41 +36,41 @@ def main():
address = os.environ.get("TFE_ADDRESS", "https://app.terraform.io")

if not token:
print("TFE_TOKEN environment variable is required")
print("TFE_TOKEN environment variable is required")
return 1

if not org:
print("TFE_ORG environment variable is required")
print("TFE_ORG environment variable is required")
return 1

# Create TFE client
config = TFEConfig(token=token, address=address)
client = TFEClient(config)

print(f"🔗 Connected to: {address}")
print(f"🏢 Organization: {org}")
print(f"Connected to: {address}")
print(f" Organization: {org}")

try:
# Example 1: Find agent pools to demonstrate agent operations
print("\n📋 Finding agent pools...")
print("\n Finding agent pools...")
agent_pools = client.agent_pools.list(org)

# Convert to list to check if empty and get count
pool_list = list(agent_pools)
if not pool_list:
print("⚠️ No agent pools found. Create an agent pool first.")
print("No agent pools found. Create an agent pool first.")
return 1

print(f"Found {len(pool_list)} agent pools:")
for pool in pool_list:
print(f" - {pool.name} (ID: {pool.id}, Agents: {pool.agent_count})")

# Example 2: List agents in each pool
print("\n🤖 Listing agents in each pool...")
print("\n Listing agents in each pool...")
total_agents = 0

for pool in pool_list:
print(f"\n📂 Agents in pool '{pool.name}':")
print(f"\n Agents in pool '{pool.name}':")

# Use optional parameters for listing
list_options = AgentListOptions(page_size=10) # Optional parameter
Expand All @@ -81,45 +81,45 @@ def main():
if agent_list:
total_agents += len(agent_list)
for agent in agent_list:
print(f" - Agent {agent.id}")
print(f" Name: {agent.name or 'Unnamed'}")
print(f" Status: {agent.status}")
print(f" Version: {agent.version or 'Unknown'}")
print(f" IP: {agent.ip_address or 'Unknown'}")
print(f" Last Ping: {agent.last_ping_at or 'Never'}")
print(f"Agent {agent.id}")
print(f"Name: {agent.name or 'Unnamed'}")
print(f"Status: {agent.status}")
print(f"Version: {agent.version or 'Unknown'}")
print(f"IP: {agent.ip_address or 'Unknown'}")
print(f"Last Ping: {agent.last_ping_at or 'Never'}")

# Example 3: Read detailed agent information
try:
agent_details = client.agents.read(agent.id)
print("Agent details retrieved successfully")
print(f" Full name: {agent_details.name or 'Unnamed'}")
print(f" Current status: {agent_details.status}")
print("Agent details retrieved successfully")
print(f"Full name: {agent_details.name or 'Unnamed'}")
print(f"Current status: {agent_details.status}")
except NotFound:
print(" ⚠️ Agent details not accessible")
print("Agent details not accessible")
except Exception as e:
print(f"Error reading agent details: {e}")
print(f"Error reading agent details: {e}")

print("")
else:
print(" No agents found in this pool")
print("No agents found in this pool")

if total_agents == 0:
print("\n⚠️ No agents found in any pools.")
print("\n No agents found in any pools.")
print("To see agents in action:")
print("1. Create an agent pool")
print("2. Run a Terraform Enterprise agent binary connected to the pool")
print("3. Run this example again")
else:
print(f"\n📊 Total agents found across all pools: {total_agents}")
print(f"\n Total agents found across all pools: {total_agents}")

print("\n🎉 Agent operations completed successfully!")
print("\n Agent operations completed successfully!")
return 0

except NotFound as e:
print(f" Resource not found: {e}")
print(f" Resource not found: {e}")
return 1
except Exception as e:
print(f" Error: {e}")
print(f" Error: {e}")
return 1


Expand Down
46 changes: 23 additions & 23 deletions examples/agent_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,23 @@ def main():
address = os.environ.get("TFE_ADDRESS", "https://app.terraform.io")

if not token:
print("TFE_TOKEN environment variable is required")
print("TFE_TOKEN environment variable is required")
return 1

if not org:
print("TFE_ORG environment variable is required")
print("TFE_ORG environment variable is required")
return 1

# Create TFE client
config = TFEConfig(token=token, address=address)
client = TFEClient(config=config)

print(f"🔗 Connected to: {address}")
print(f"🏢 Organization: {org}")
print(f"Connected to: {address}")
print(f" Organization: {org}")

try:
# Example 1: List existing agent pools
print("\n📋 Listing existing agent pools...")
print("\n Listing existing agent pools...")
list_options = AgentPoolListOptions(page_size=10) # Optional parameters
agent_pools = client.agent_pools.list(org, options=list_options)

Expand All @@ -66,7 +66,7 @@ def main():
print(f" - {pool.name} (ID: {pool.id}, Agents: {pool.agent_count})")

# Example 2: Create a new agent pool
print("\n🆕 Creating a new agent pool...")
print("\n Creating a new agent pool...")
unique_name = f"sdk-example-pool-{uuid.uuid4().hex[:8]}"

create_options = AgentPoolCreateOptions(
Expand All @@ -76,39 +76,39 @@ def main():
)

new_pool = client.agent_pools.create(org, create_options)
print(f"Created agent pool: {new_pool.name} (ID: {new_pool.id})")
print(f"Created agent pool: {new_pool.name} (ID: {new_pool.id})")

# Example 3: Read the agent pool
print("\n📖 Reading agent pool details...")
print("\n Reading agent pool details...")
pool_details = client.agent_pools.read(new_pool.id)
print(f" Name: {pool_details.name}")
print(f" Organization Scoped: {pool_details.organization_scoped}")
print(f" Policy: {pool_details.allowed_workspace_policy}")
print(f" Agent Count: {pool_details.agent_count}")
print(f"Name: {pool_details.name}")
print(f"Organization Scoped: {pool_details.organization_scoped}")
print(f"Policy: {pool_details.allowed_workspace_policy}")
print(f"Agent Count: {pool_details.agent_count}")

# Example 4: Update the agent pool
print("\n✏️ Updating agent pool...")
print("\n Updating agent pool...")
update_options = AgentPoolUpdateOptions(
name=f"{unique_name}-updated",
organization_scoped=False, # Making this optional parameter different
)

updated_pool = client.agent_pools.update(new_pool.id, update_options)
print(f"Updated agent pool name to: {updated_pool.name}")
print(f"Updated agent pool name to: {updated_pool.name}")

# Example 5: Create an agent token
print("\n🔑 Creating agent token...")
print("\n Creating agent token...")
token_options = AgentTokenCreateOptions(
description="SDK example token" # Optional description
)

agent_token = client.agent_tokens.create(new_pool.id, token_options)
print(f"Created agent token: {agent_token.id}")
print(f"Created agent token: {agent_token.id}")
if agent_token.token:
print(f" Token (first 10 chars): {agent_token.token[:10]}...")

# Example 6: List agent tokens
print("\n📝 Listing agent tokens...")
print("\n Listing agent tokens...")
tokens = client.agent_tokens.list(new_pool.id)

# Convert to list to get count and iterate
Expand All @@ -118,21 +118,21 @@ def main():
print(f" - {token.description or 'No description'} (ID: {token.id})")

# Example 7: Clean up - delete the token and pool
print("\n🧹 Cleaning up...")
print("\n Cleaning up...")
client.agent_tokens.delete(agent_token.id)
print("Deleted agent token")
print("Deleted agent token")

client.agent_pools.delete(new_pool.id)
print("Deleted agent pool")
print("Deleted agent pool")

print("\n🎉 Agent pool operations completed successfully!")
print("\n Agent pool operations completed successfully!")
return 0

except NotFound as e:
print(f" Resource not found: {e}")
print(f" Resource not found: {e}")
return 1
except Exception as e:
print(f" Error: {e}")
print(f" Error: {e}")
return 1


Expand Down
6 changes: 3 additions & 3 deletions examples/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ def main():

# Display timestamp details if available
if apply.status_timestamps:
print(f" Queued At: {apply.status_timestamps.queued_at}")
print(f" Started At: {apply.status_timestamps.started_at}")
print(f" Finished At: {apply.status_timestamps.finished_at}")
print(f"Queued At: {apply.status_timestamps.queued_at}")
print(f"Started At: {apply.status_timestamps.started_at}")
print(f"Finished At: {apply.status_timestamps.finished_at}")
except Exception as e:
print(f"Error reading apply: {e}")
return 1
Expand Down
Loading
Loading