|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Reserved Tag Keys Example Script. |
| 3 | +
|
| 4 | +This script demonstrates how to use the Reserved Tag Keys API to: |
| 5 | +1. List all reserved tag keys for an organization |
| 6 | +2. Create a new reserved tag key |
| 7 | +3. Update a reserved tag key |
| 8 | +4. Delete a reserved tag key |
| 9 | +
|
| 10 | +Before running this script: |
| 11 | +1. Set TFE_TOKEN environment variable with your Terraform Cloud API token |
| 12 | +2. Set TFE_ORG environment variable with your organization name |
| 13 | +""" |
| 14 | + |
| 15 | +import os |
| 16 | +import sys |
| 17 | + |
| 18 | +# Add the source directory to the path for direct execution |
| 19 | +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "src")) |
| 20 | + |
| 21 | +from tfe import TFEClient, TFEConfig |
| 22 | +from tfe.errors import TFEError |
| 23 | +from tfe.models import ( |
| 24 | + ReservedTagKeyCreateOptions, |
| 25 | + ReservedTagKeyListOptions, |
| 26 | + ReservedTagKeyUpdateOptions, |
| 27 | +) |
| 28 | + |
| 29 | +# Configuration |
| 30 | +TFE_TOKEN = os.getenv("TFE_TOKEN") |
| 31 | +TFE_ORG = os.getenv("TFE_ORG") |
| 32 | + |
| 33 | + |
| 34 | +def main(): |
| 35 | + """Main function demonstrating Reserved Tag Keys API usage.""" |
| 36 | + |
| 37 | + # Validate environment variables |
| 38 | + if not TFE_TOKEN: |
| 39 | + print("❌ Error: TFE_TOKEN environment variable is required") |
| 40 | + sys.exit(1) |
| 41 | + |
| 42 | + if not TFE_ORG: |
| 43 | + print("❌ Error: TFE_ORG environment variable is required") |
| 44 | + sys.exit(1) |
| 45 | + |
| 46 | + # Initialize the TFE client |
| 47 | + config = TFEConfig(token=TFE_TOKEN) |
| 48 | + client = TFEClient(config) |
| 49 | + |
| 50 | + print(f"Reserved Tag Keys API Example for organization: {TFE_ORG}") |
| 51 | + print("=" * 60) |
| 52 | + |
| 53 | + try: |
| 54 | + # 1. List existing reserved tag keys |
| 55 | + print("\n1. Listing reserved tag keys...") |
| 56 | + reserved_tag_keys = client.reserved_tag_key.list(TFE_ORG) |
| 57 | + print(f"✅ Found {len(reserved_tag_keys.items)} reserved tag keys:") |
| 58 | + for rtk in reserved_tag_keys.items: |
| 59 | + print( |
| 60 | + f" - ID: {rtk.id}, Key: {rtk.key}, Disable Overrides: {rtk.disable_overrides}" |
| 61 | + ) |
| 62 | + |
| 63 | + # 2. Create a new reserved tag key |
| 64 | + print("\n2. Creating a new reserved tag key...") |
| 65 | + create_options = ReservedTagKeyCreateOptions( |
| 66 | + key="python-tfe-example", disable_overrides=False |
| 67 | + ) |
| 68 | + |
| 69 | + new_rtk = client.reserved_tag_key.create(TFE_ORG, create_options) |
| 70 | + print(f"✅ Created reserved tag key: {new_rtk.id} - {new_rtk.key}") |
| 71 | + print(f" Disable Overrides: {new_rtk.disable_overrides}") |
| 72 | + |
| 73 | + # 3. Update the reserved tag key |
| 74 | + print("\n3. Updating the reserved tag key...") |
| 75 | + update_options = ReservedTagKeyUpdateOptions( |
| 76 | + key="python-tfe-example-updated", disable_overrides=True |
| 77 | + ) |
| 78 | + |
| 79 | + updated_rtk = client.reserved_tag_key.update(new_rtk.id, update_options) |
| 80 | + print(f"✅ Updated reserved tag key: {updated_rtk.id} - {updated_rtk.key}") |
| 81 | + print(f" Disable Overrides: {updated_rtk.disable_overrides}") |
| 82 | + |
| 83 | + # 4. Delete the reserved tag key |
| 84 | + print("\n4. Deleting the reserved tag key...") |
| 85 | + client.reserved_tag_key.delete(new_rtk.id) |
| 86 | + print(f"✅ Deleted reserved tag key: {new_rtk.id}") |
| 87 | + |
| 88 | + # 5. Verify deletion by listing again |
| 89 | + print("\n5. Verifying deletion...") |
| 90 | + reserved_tag_keys_after = client.reserved_tag_key.list(TFE_ORG) |
| 91 | + print( |
| 92 | + f"✅ Reserved tag keys after deletion: {len(reserved_tag_keys_after.items)}" |
| 93 | + ) |
| 94 | + |
| 95 | + # 6. Demonstrate pagination with options |
| 96 | + print("\n6. Demonstrating pagination options...") |
| 97 | + list_options = ReservedTagKeyListOptions(page_size=5, page_number=1) |
| 98 | + paginated_rtks = client.reserved_tag_key.list(TFE_ORG, list_options) |
| 99 | + print(f"✅ Page 1 with page size 5: {len(paginated_rtks.items)} keys") |
| 100 | + print(f" Total pages: {paginated_rtks.total_pages}") |
| 101 | + print(f" Total count: {paginated_rtks.total_count}") |
| 102 | + |
| 103 | + print("\n🎉 Reserved Tag Keys API example completed successfully!") |
| 104 | + |
| 105 | + except NotImplementedError as e: |
| 106 | + print(f"\n⚠️ Note: {e}") |
| 107 | + print("This is expected - the read operation is not supported by the API.") |
| 108 | + |
| 109 | + except TFEError as e: |
| 110 | + print(f"\n❌ TFE API Error: {e}") |
| 111 | + if hasattr(e, "status"): |
| 112 | + if e.status == 403: |
| 113 | + print("💡 Permission denied - check token permissions") |
| 114 | + elif e.status == 401: |
| 115 | + print("💡 Authentication failed - check token validity") |
| 116 | + elif e.status == 422: |
| 117 | + print("💡 Validation error - check reserved tag key format") |
| 118 | + sys.exit(1) |
| 119 | + |
| 120 | + except Exception as e: |
| 121 | + print(f"\n❌ Unexpected error: {e}") |
| 122 | + sys.exit(1) |
| 123 | + |
| 124 | + |
| 125 | +if __name__ == "__main__": |
| 126 | + main() |
0 commit comments