Skip to content

Commit 6d3aa76

Browse files
Examples working fine
1 parent fd2229b commit 6d3aa76

4 files changed

Lines changed: 55 additions & 24 deletions

File tree

examples/organization_token.py

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,10 @@
2121
- Appropriate permissions to manage organization tokens
2222
"""
2323

24-
import os
25-
import sys
2624
from datetime import datetime, timedelta
2725

2826
# Add the src directory to the path
29-
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "src"))
30-
27+
##sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "src"))
3128
from pytfe import TFEClient, TFEConfig
3229
from pytfe.models import (
3330
OrganizationTokenCreateOptions,
@@ -37,6 +34,28 @@
3734
)
3835

3936

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+
4059
def main():
4160
"""Execute organization token operation examples."""
4261

@@ -46,8 +65,7 @@ def main():
4665

4766
# Initialize the TFE client
4867
client = TFEClient(TFEConfig.from_env())
49-
organization_name = "my-org" # Change to your organization name
50-
68+
organization_name = "prab-sandbox01"
5169
# =====================================================
5270
# 1. CREATE ORGANIZATION TOKEN (BASIC)
5371
# =====================================================
@@ -58,16 +76,16 @@ def main():
5876
token = client.organization_tokens.create(organization_name)
5977

6078
print("Token created successfully!")
61-
print(f" Token ID: {token.id}")
79+
print(f" Token ID: {redact_id(token.id)}")
6280
print(f" Created At: {token.created_at}")
6381
print(f" Description: {token.description}")
64-
print(f" Token Value: {token.token}")
82+
print(f" Token Value: {redact_token(token.token)}")
6583
if token.expired_at:
6684
print(f" Expires At: {token.expired_at}")
6785
print()
6886

6987
except Exception as e:
70-
print(f" Error: {e}")
88+
print(f" Error: {e}")
7189
print()
7290

7391
# =====================================================
@@ -86,7 +104,7 @@ def main():
86104
)
87105

88106
print("Token created with options successfully!")
89-
print(f" Token ID: {token.id}")
107+
print(f" Token ID: {redact_id(token.id)}")
90108
print(f" Created At: {token.created_at}")
91109
if token.expired_at:
92110
print(f" Expires At: {token.expired_at}")
@@ -108,12 +126,12 @@ def main():
108126
)
109127

110128
print(" Audit-trails token created successfully!")
111-
print(f" Token ID: {token.id}")
112-
print(f" Token Value: {token.token}")
129+
print(f" Token ID: {redact_id(token.id)}")
130+
print(f" Token Value: {redact_token(token.token)}")
113131
print()
114132

115133
except Exception as e:
116-
print(f"Error: {e}")
134+
print(f"Error: {e}")
117135
print()
118136

119137
# =====================================================
@@ -124,7 +142,7 @@ def main():
124142
token = client.organization_tokens.read(organization_name)
125143

126144
print("Token read successfully!")
127-
print(f" Token ID: {token.id}")
145+
print(f" Token ID: {redact_id(token.id)}")
128146
print(f" Created At: {token.created_at}")
129147
print(f" Description: {token.description}")
130148
if token.last_used_at:
@@ -134,7 +152,7 @@ def main():
134152
print()
135153

136154
except Exception as e:
137-
print(f" Error: {e}")
155+
print(f" Error: {e}")
138156
print()
139157

140158
# =====================================================
@@ -147,12 +165,12 @@ def main():
147165
token = client.organization_tokens.read_with_options(organization_name, options)
148166

149167
print(" Audit-trails token read successfully!")
150-
print(f" Token ID: {token.id}")
151-
print(f" Token Value: {token.token}")
168+
print(f" Token ID: {redact_id(token.id)}")
169+
print(f" Token Value: {redact_token(token.token)}")
152170
print()
153171

154172
except Exception as e:
155-
print(f" Error: {e}")
173+
print(f" Error: {e}")
156174
print()
157175

158176
# =====================================================

src/pytfe/models/__init__.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,15 @@
9393
OrganizationMembershipStatus,
9494
OrgMembershipIncludeOpt,
9595
)
96+
97+
# ── Organization Token ────────────────────────────────────────────────────────
98+
from .organization_token import (
99+
OrganizationToken,
100+
OrganizationTokenCreateOptions,
101+
OrganizationTokenDeleteOptions,
102+
OrganizationTokenReadOptions,
103+
TokenType,
104+
)
96105
from .policy import (
97106
Policy,
98107
PolicyCreateOptions,
@@ -287,7 +296,6 @@
287296
from .ssh_key import (
288297
SSHKey,
289298
SSHKeyCreateOptions,
290-
SSHKeyList,
291299
SSHKeyListOptions,
292300
SSHKeyUpdateOptions,
293301
)
@@ -382,7 +390,6 @@
382390
# SSH keys
383391
"SSHKey",
384392
"SSHKeyCreateOptions",
385-
"SSHKeyList",
386393
"SSHKeyListOptions",
387394
"SSHKeyUpdateOptions",
388395
# Reserved tag keys
@@ -486,6 +493,12 @@
486493
"OrganizationMembershipReadOptions",
487494
"OrganizationMembershipStatus",
488495
"OrgMembershipIncludeOpt",
496+
# Organization tokens
497+
"OrganizationToken",
498+
"OrganizationTokenCreateOptions",
499+
"OrganizationTokenDeleteOptions",
500+
"OrganizationTokenReadOptions",
501+
"TokenType",
489502
"OrganizationAccess",
490503
"Team",
491504
"TeamPermissions",

src/pytfe/models/organization_token.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ class OrganizationToken(BaseModel):
2323

2424
id: str = Field(..., description="Organization token ID")
2525
created_at: datetime = Field(..., description="Creation timestamp")
26-
description: str = Field("", description="Token description")
26+
description: str | None = Field(None, description="Token description")
2727
last_used_at: datetime | None = Field(None, description="Last usage timestamp")
28-
token: str = Field("", description="The actual token value")
28+
token: str | None = Field(None, description="The actual token value")
2929
expired_at: datetime | None = Field(None, description="Token expiration timestamp")
3030
created_by: Any | None = Field(
3131
None, description="The entity that created this token"

src/pytfe/resources/organization_token.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def create(self, organization: str) -> OrganizationToken:
3030
Raises:
3131
ValueError: If the organization name is invalid
3232
"""
33-
return self.create_with_options(organization, None)
33+
return self.create_with_options(organization)
3434

3535
def create_with_options(
3636
self,
@@ -57,7 +57,7 @@ def create_with_options(
5757
# Build request body
5858
body: dict[str, Any] = {
5959
"data": {
60-
"type": "authentication-tokens",
60+
"type": "authentication-token",
6161
"attributes": {},
6262
}
6363
}

0 commit comments

Comments
 (0)