44Tests: list, list_all, create, read, update, and delete operations.
55"""
66
7- import sys
87import os
8+ import sys
99import time
1010
1111# Add the src directory to the path
12- sys .path .insert (0 , os .path .join (os .path .dirname (__file__ ), '..' , ' src' ))
12+ sys .path .insert (0 , os .path .join (os .path .dirname (__file__ ), ".." , " src" ))
1313
1414from tfe import TFEClient , TFEConfig
15- from tfe .types import VariableCreateOptions , VariableUpdateOptions , CategoryType
15+ from tfe .types import CategoryType , VariableCreateOptions , VariableUpdateOptions
1616
1717
1818def main ():
1919 """Test all variable operations in a workspace."""
20-
20+
2121 # Initialize the TFE client
2222 client = TFEClient (TFEConfig .from_env ())
23-
23+
2424 # Replace this with your actual workspace ID
2525 workspace_id = "ws-example123456789" # Get this from your TFE workspace
26-
26+
2727 print (f"Testing all variable operations in workspace: { workspace_id } " )
2828 print ("=" * 60 )
29-
29+
3030 # Track created variables for cleanup
3131 created_variables = []
32-
32+
3333 try :
3434 # 1. Test CREATE function - COMMENTED OUT (already have variables from previous run)
3535 print ("\n 1. Testing CREATE operation:" )
3636 print ("-" * 30 )
37-
37+
3838 # Create a Terraform variable
3939 terraform_var = VariableCreateOptions (
4040 key = "test_terraform_var" ,
4141 value = "production" ,
4242 description = "Test Terraform variable" ,
4343 category = CategoryType .TERRAFORM ,
4444 hcl = False ,
45- sensitive = False
45+ sensitive = False ,
4646 )
47-
47+
4848 try :
4949 variable = client .variables .create (workspace_id , terraform_var )
5050 created_variables .append (variable .id )
5151 print (f"✓ Created Terraform variable: { variable .key } = { variable .value } " )
5252 print (f" ID: { variable .id } , Category: { variable .category } " )
5353 except Exception as e :
5454 print (f"✗ Error creating Terraform variable: { e } " )
55-
55+
5656 # Create an environment variable
5757 env_var = VariableCreateOptions (
5858 key = "TEST_LOG_LEVEL" ,
5959 value = "DEBUG" ,
6060 description = "Test environment variable" ,
6161 category = CategoryType .ENV ,
6262 hcl = False ,
63- sensitive = False
63+ sensitive = False ,
6464 )
65-
65+
6666 try :
6767 variable = client .variables .create (workspace_id , env_var )
6868 created_variables .append (variable .id )
6969 print (f"✓ Created environment variable: { variable .key } = { variable .value } " )
7070 print (f" ID: { variable .id } , Category: { variable .category } " )
7171 except Exception as e :
7272 print (f"✗ Error creating environment variable: { e } " )
73-
73+
7474 # Create a sensitive variable
7575 secret_var = VariableCreateOptions (
7676 key = "TEST_API_KEY" ,
7777 value = "super-secret-key-12345" ,
7878 description = "Test sensitive variable" ,
7979 category = CategoryType .ENV ,
8080 hcl = False ,
81- sensitive = True
81+ sensitive = True ,
8282 )
83-
83+
8484 try :
8585 variable = client .variables .create (workspace_id , secret_var )
8686 created_variables .append (variable .id )
8787 print (f"✓ Created sensitive variable: { variable .key } = ***HIDDEN***" )
8888 print (f" ID: { variable .id } , Category: { variable .category } " )
8989 except Exception as e :
9090 print (f"✗ Error creating sensitive variable: { e } " )
91-
91+
9292 # Small delay to ensure variables are created
9393 time .sleep (1 )
94-
94+
9595 # 2. Test LIST function (workspace-only variables) - COMMENTED OUT
9696 print ("\n 2. Testing LIST operation (workspace variables only):" )
9797 print ("-" * 50 )
98-
98+
9999 try :
100100 variables = list (client .variables .list (workspace_id ))
101101 print (f"Found { len (variables )} workspace variables:" )
102102 for var in variables :
103103 value_display = "***SENSITIVE***" if var .sensitive else var .value
104- print (f" • { var .key } = { value_display } ({ var .category } ) [ID: { var .id } ]" )
104+ print (
105+ f" • { var .key } = { value_display } ({ var .category } ) [ID: { var .id } ]"
106+ )
105107 except Exception as e :
106108 print (f"✗ Error listing variables: { e } " )
107-
109+
108110 # 3. Test LIST_ALL function (includes inherited variables from variable sets)
109111 print ("\n 3. Testing LIST_ALL operation (includes variable sets):" )
110112 print ("-" * 55 )
111-
113+
112114 try :
113115 all_variables = list (client .variables .list_all (workspace_id ))
114116 print (f"Found { len (all_variables )} total variables (including inherited):" )
115117 for var in all_variables :
116118 value_display = "***SENSITIVE***" if var .sensitive else var .value
117- print (f" • { var .key } = { value_display } ({ var .category } ) [ID: { var .id } ]" )
119+ print (
120+ f" • { var .key } = { value_display } ({ var .category } ) [ID: { var .id } ]"
121+ )
118122 except Exception as e :
119123 print (f"✗ Error listing all variables: { e } " )
120-
124+
121125 # Test READ function with specific variable ID - COMMENTED OUT
122126 print ("\n 4. Testing READ operation with specific variable ID:" )
123127 print ("-" * 50 )
124-
128+
125129 # Replace this with actual variable ID to test reading
126130 test_variable_id = "var-example123456789"
127131 print (f"Testing READ with variable ID: { test_variable_id } " )
128-
132+
129133 try :
130134 variable = client .variables .read (workspace_id , test_variable_id )
131135 # For testing, show actual values even for sensitive variables
@@ -138,63 +142,67 @@ def main():
138142 print (f" Category: { variable .category } " )
139143 print (f" HCL: { variable .hcl } " )
140144 print (f" Sensitive: { variable .sensitive } " )
141- if hasattr (variable , ' version_id' ):
145+ if hasattr (variable , " version_id" ):
142146 print (f" Version ID: { variable .version_id } " )
143147 except Exception as e :
144148 print (f"✗ Error reading variable { test_variable_id } : { e } " )
145-
149+
146150 # Test UPDATE function with specific variable ID - COMMENTED OUT
147151 print ("\n 5. Testing UPDATE operation with specific variable ID:" )
148152 print ("-" * 55 )
149-
153+
150154 # Replace this with actual variable ID to test updating
151155 test_variable_id = "var-example123456789"
152156 print (f"Testing UPDATE with variable ID: { test_variable_id } " )
153- print (f "Setting value to: 'npe'" )
154-
157+ print ("Setting value to: 'npe'" )
158+
155159 try :
156160 # First read the current variable to get its details
157161 current_var = client .variables .read (workspace_id , test_variable_id )
158162 print (f"Current value: { current_var .value } " )
159163 print (f"Current key: { current_var .key } " )
160-
164+
161165 # Update the variable value to "npe"
162166 update_options = VariableUpdateOptions (
163167 key = current_var .key ,
164168 value = "npe" ,
165169 description = current_var .description ,
166170 hcl = current_var .hcl ,
167- sensitive = current_var .sensitive
171+ sensitive = current_var .sensitive ,
172+ )
173+
174+ updated_variable = client .variables .update (
175+ workspace_id , test_variable_id , update_options
176+ )
177+ print (
178+ f"✓ Updated variable: { updated_variable .key } = { updated_variable .value } "
168179 )
169-
170- updated_variable = client .variables .update (workspace_id , test_variable_id , update_options )
171- print (f"✓ Updated variable: { updated_variable .key } = { updated_variable .value } " )
172180 print (f" Description: { updated_variable .description } " )
173181 print (f" Category: { updated_variable .category } " )
174182 print (f" HCL: { updated_variable .hcl } " )
175183 print (f" Sensitive: { updated_variable .sensitive } " )
176184 print (f" ID: { updated_variable .id } " )
177185 except Exception as e :
178186 print (f"✗ Error updating variable { test_variable_id } : { e } " )
179-
187+
180188 # Test DELETE function with specific variable ID
181189 print ("\n 6. Testing DELETE operation with specific variable ID:" )
182190 print ("-" * 55 )
183-
191+
184192 # Replace this with actual variable ID to test deletion
185193 test_variable_id = "var-example123456789"
186194 print (f"Testing DELETE with variable ID: { test_variable_id } " )
187-
195+
188196 try :
189197 # First read the variable to confirm it exists before deletion
190198 variable = client .variables .read (workspace_id , test_variable_id )
191199 print (f"Variable to delete: { variable .key } = { variable .value } " )
192200 print (f" ID: { variable .id } " )
193-
201+
194202 # Delete the variable
195203 client .variables .delete (workspace_id , test_variable_id )
196204 print (f"✓ Successfully deleted variable with ID: { test_variable_id } " )
197-
205+
198206 # Try to read it again to verify deletion
199207 print ("Verifying deletion..." )
200208 try :
@@ -205,20 +213,21 @@ def main():
205213 print ("✓ Confirmed: Variable no longer exists" )
206214 else :
207215 print (f"✗ Unexpected error verifying deletion: { read_error } " )
208-
216+
209217 except Exception as e :
210218 print (f"✗ Error deleting variable { test_variable_id } : { e } " )
211-
212-
219+
213220 # 4. Test READ function
214221 print ("\n 4. Testing READ operation:" )
215222 print ("-" * 25 )
216-
223+
217224 if created_variables :
218225 test_var_id = created_variables [0 ] # Use the first created variable
219226 try :
220227 variable = client .variables .read (workspace_id , test_var_id )
221- value_display = "***SENSITIVE***" if variable .sensitive else variable .value
228+ value_display = (
229+ "***SENSITIVE***" if variable .sensitive else variable .value
230+ )
222231 print (f"✓ Read variable: { variable .key } = { value_display } " )
223232 print (f" ID: { variable .id } " )
224233 print (f" Description: { variable .description } " )
@@ -229,65 +238,75 @@ def main():
229238 print (f"✗ Error reading variable { test_var_id } : { e } " )
230239 else :
231240 print ("No variables available to read" )
232-
241+
233242 # 5. Test UPDATE function
234243 print ("\n 5. Testing UPDATE operation:" )
235244 print ("-" * 27 )
236-
245+
237246 if created_variables :
238247 test_var_id = created_variables [0 ] # Use the first created variable
239248 try :
240249 # First read the current variable to get its details
241250 current_var = client .variables .read (workspace_id , test_var_id )
242-
251+
243252 # Update the variable
244253 update_options = VariableUpdateOptions (
245254 key = current_var .key ,
246255 value = "updated_value_123" ,
247256 description = "Updated test variable description" ,
248257 hcl = False ,
249- sensitive = False
258+ sensitive = False ,
259+ )
260+
261+ updated_variable = client .variables .update (
262+ workspace_id , test_var_id , update_options
263+ )
264+ print (
265+ f"✓ Updated variable: { updated_variable .key } = { updated_variable .value } "
250266 )
251-
252- updated_variable = client .variables .update (workspace_id , test_var_id , update_options )
253- print (f"✓ Updated variable: { updated_variable .key } = { updated_variable .value } " )
254267 print (f" New description: { updated_variable .description } " )
255268 print (f" ID: { updated_variable .id } " )
256269 except Exception as e :
257270 print (f"✗ Error updating variable { test_var_id } : { e } " )
258271 else :
259272 print ("No variables available to update" )
260-
273+
261274 # 6. Test DELETE function
262275 print ("\n 6. Testing DELETE operation:" )
263276 print ("-" * 27 )
264-
277+
265278 # Delete all created variables
266279 for var_id in created_variables :
267280 try :
268281 client .variables .delete (workspace_id , var_id )
269282 print (f"✓ Deleted variable with ID: { var_id } " )
270283 except Exception as e :
271284 print (f"✗ Error deleting variable { var_id } : { e } " )
272-
285+
273286 # Verify deletion by listing variables again
274287 print ("\n Verifying deletion - listing variables after cleanup:" )
275288 try :
276289 remaining_variables = list (client .variables .list (workspace_id ))
277290 # Filter out the variables we just deleted
278- remaining_test_vars = [v for v in remaining_variables if v .key .startswith ("test_" ) or v .key .startswith ("TEST_" )]
291+ remaining_test_vars = [
292+ v
293+ for v in remaining_variables
294+ if v .key .startswith ("test_" ) or v .key .startswith ("TEST_" )
295+ ]
279296 if remaining_test_vars :
280- print (f"Warning: { len (remaining_test_vars )} test variables still exist:" )
297+ print (
298+ f"Warning: { len (remaining_test_vars )} test variables still exist:"
299+ )
281300 for var in remaining_test_vars :
282301 print (f" • { var .key } [ID: { var .id } ]" )
283302 else :
284303 print ("✓ All test variables successfully deleted" )
285304 except Exception as e :
286305 print (f"✗ Error verifying deletion: { e } " )
287-
306+
288307 except Exception as e :
289308 print (f"✗ Unexpected error during testing: { e } " )
290-
309+
291310 print ("\n " + "=" * 60 )
292311 print ("Variable testing complete!" )
293312
0 commit comments