1+ #!/usr/bin/env python3
2+ """
3+ Organization Token Operations Example
4+
5+ Demonstrates usage of all 6 organization token operations:
6+ 1. create() - Create a new organization token, replacing any existing token
7+ 2. create_with_options() - Create with options like expiration date and token type
8+ 3. read() - Read the organization token
9+ 4. read_with_options() - Read with options like token type
10+ 5. delete() - Delete the organization token
11+ 6. delete_with_options() - Delete with options like token type
12+
13+ Usage:
14+ - Modify organization names as needed for your environment
15+ - Ensure you have proper TFE credentials and organization access
16+ - Organization tokens are used for organization-level API access
17+
18+ Prerequisites:
19+ - Set TFE_TOKEN and TFE_ADDRESS environment variables
20+ - You need an existing organization or admin permissions to create one
21+ - Appropriate permissions to manage organization tokens
22+ """
23+
24+ from datetime import datetime , timedelta
25+
26+ # Add the src directory to the path
27+ ##sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "src"))
28+ from pytfe import TFEClient , TFEConfig
29+ from pytfe .models import (
30+ OrganizationTokenCreateOptions ,
31+ OrganizationTokenDeleteOptions ,
32+ OrganizationTokenReadOptions ,
33+ TokenType ,
34+ )
35+
36+
37+ def redact_token (token_value : str | None ) -> str :
38+ """Redact token value for safe display."""
39+ if not token_value :
40+ return "None"
41+ if len (token_value ) <= 8 :
42+ return f"{ '*' * len (token_value )} "
43+ # Show first 3 and last 3 characters
44+ return f"{ token_value [:3 ]} ...{ token_value [- 3 :]} " .replace (
45+ token_value [3 :- 3 ], "*" * (len (token_value ) - 6 )
46+ )
47+
48+
49+ def redact_id (id_value : str | None ) -> str :
50+ """Redact ID for safe display."""
51+ if not id_value :
52+ return "None"
53+ if len (id_value ) <= 6 :
54+ return f"{ '*' * len (id_value )} "
55+ # Show first 3 and last 3 characters
56+ return f"{ id_value [:3 ]} ...{ id_value [- 3 :]} "
57+
58+
59+ def main ():
60+ """Execute organization token operation examples."""
61+
62+ print ("=" * 80 )
63+ print ("ORGANIZATION TOKEN OPERATIONS" )
64+ print ("=" * 80 )
65+
66+ # Initialize the TFE client
67+ client = TFEClient (TFEConfig .from_env ())
68+ organization_name = "prab-sandbox02"
69+ # =====================================================
70+ # 1. CREATE ORGANIZATION TOKEN (BASIC)
71+ # =====================================================
72+ print ("\n 1. create() - Create a new organization token:" )
73+ print ("-" * 40 )
74+ try :
75+ print (f"Creating token for organization: { organization_name } " )
76+ token = client .organization_tokens .create (organization_name )
77+
78+ print ("Token created successfully!" )
79+ print (f" Token ID: { redact_id (token .id )} " )
80+ print (f" Created At: { token .created_at } " )
81+ print (f" Description: { token .description } " )
82+ print (f" Token Value: { redact_token (token .token )} " )
83+ if token .expired_at :
84+ print (f" Expires At: { token .expired_at } " )
85+ print ()
86+
87+ except Exception as e :
88+ print (f" Error: { e } " )
89+ print ()
90+
91+ # =====================================================
92+ # 2. CREATE WITH OPTIONS (WITH EXPIRATION)
93+ # =====================================================
94+ print ("2. create_with_options() - Create token with expiration date:" )
95+ print ("-" * 40 )
96+ try :
97+ # Create a token that expires in 30 days
98+ expiry_date = datetime .utcnow () + timedelta (days = 30 )
99+ options = OrganizationTokenCreateOptions (expired_at = expiry_date )
100+
101+ print (f"Creating organization token with expiration date: { expiry_date } " )
102+ token = client .organization_tokens .create_with_options (
103+ organization_name , options
104+ )
105+
106+ print ("Token created with options successfully!" )
107+ print (f" Token ID: { redact_id (token .id )} " )
108+ print (f" Created At: { token .created_at } " )
109+ if token .expired_at :
110+ print (f" Expires At: { token .expired_at } " )
111+ print ()
112+
113+ except Exception as e :
114+ print (f" Error: { e } " )
115+ print ()
116+
117+ # =====================================================
118+ print ("3. create_with_options() - Create audit-trails token:" )
119+ print ("-" * 40 )
120+ try :
121+ options = OrganizationTokenCreateOptions (token_type = TokenType .AUDIT_TRAILS )
122+
123+ print (f"Creating audit-trails token for organization: { organization_name } " )
124+ token = client .organization_tokens .create_with_options (
125+ organization_name , options
126+ )
127+
128+ print (" Audit-trails token created successfully!" )
129+ print (f" Token ID: { redact_id (token .id )} " )
130+ print (f" Token Value: { redact_token (token .token )} " )
131+ print ()
132+
133+ except Exception as e :
134+ print (f"Error: { e } " )
135+ print ()
136+
137+ # =====================================================
138+ print ("4. read() - Read the organization token:" )
139+ print ("-" * 40 )
140+ try :
141+ print (f"Reading organization token for organization: { organization_name } " )
142+ token = client .organization_tokens .read (organization_name )
143+
144+ print ("Token read successfully!" )
145+ print (f" Token ID: { redact_id (token .id )} " )
146+ print (f" Created At: { token .created_at } " )
147+ print (f" Description: { token .description } " )
148+ if token .last_used_at :
149+ print (f" Last Used At: { token .last_used_at } " )
150+ if token .expired_at :
151+ print (f" Expires At: { token .expired_at } " )
152+ print ()
153+
154+ except Exception as e :
155+ print (f" Error: { e } " )
156+ print ()
157+
158+ # =====================================================
159+ print ("5. read_with_options() - Read audit-trails token:" )
160+ print ("-" * 40 )
161+ try :
162+ options = OrganizationTokenReadOptions (token_type = TokenType .AUDIT_TRAILS )
163+
164+ print (f"Reading audit-trails token for organization: { organization_name } " )
165+ token = client .organization_tokens .read_with_options (organization_name , options )
166+
167+ print (" Audit-trails token read successfully!" )
168+ print (f" Token ID: { redact_id (token .id )} " )
169+ print (f" Token Value: { redact_token (token .token )} " )
170+ print ()
171+
172+ except Exception as e :
173+ print (f" Error: { e } " )
174+ print ()
175+
176+ # =====================================================
177+ print ("6. delete() - Delete the organization token:" )
178+ print ("-" * 40 )
179+ try :
180+ print (f"Deleting organization token for organization: { organization_name } " )
181+ client .organization_tokens .delete (organization_name )
182+
183+ print (" Token deleted successfully!" )
184+ print ()
185+
186+ except Exception as e :
187+ print (f" Error: { e } " )
188+ print ()
189+
190+ # =====================================================
191+ print ("7. delete_with_options() - Delete audit-trails token:" )
192+ print ("-" * 40 )
193+ try :
194+ options = OrganizationTokenDeleteOptions (token_type = TokenType .AUDIT_TRAILS )
195+
196+ print (f"Deleting audit-trails token for organization: { organization_name } " )
197+ client .organization_tokens .delete_with_options (organization_name , options )
198+
199+ print (" Audit-trails token deleted successfully!" )
200+ print ()
201+
202+ except Exception as e :
203+ print (f"Error: { e } " )
204+ print ()
205+
206+ print ("=" * 80 )
207+ print ("ORGANIZATION TOKEN OPERATIONS COMPLETED" )
208+ print ("=" * 80 )
209+
210+
211+ if __name__ == "__main__" :
212+ main ()
0 commit comments