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