1515
1616Usage:
1717 python query_run.py
18-
18+
1919Note: Query Runs require Terraform ~>1.14 which includes the 'terraform query' command.
2020 These tests may fail with error status since the feature is not fully available yet.
2121"""
@@ -36,7 +36,7 @@ def get_client_and_workspace():
3636 client = TFEClient (TFEConfig .from_env ())
3737 organization = os .getenv ("TFE_ORG" , "aayush-test" )
3838 workspace_name = "query-test" # Default workspace for testing
39-
39+
4040 # Get workspace
4141 workspace = client .workspaces .read (workspace_name , organization = organization )
4242 return client , workspace
@@ -45,27 +45,27 @@ def get_client_and_workspace():
4545def test_list ():
4646 """Test 1: List query runs in a workspace."""
4747 print ("=== Test 1: List Query Runs ===" )
48-
48+
4949 client , workspace = get_client_and_workspace ()
50-
50+
5151 try :
5252 # Simple list
5353 query_runs = list (client .query_runs .list (workspace .id ))
5454 print (f"Found { len (query_runs )} query runs in workspace '{ workspace .name } '" )
55-
55+
5656 for i , qr in enumerate (query_runs [:5 ], 1 ):
5757 print (f" { i } . { qr .id } " )
5858 print (f" Status: { qr .status } " )
5959 print (f" Created: { qr .created_at } " )
6060 print ()
61-
61+
6262 # List with options
6363 options = QueryRunListOptions (page_size = 5 )
6464 limited_runs = list (client .query_runs .list (workspace .id , options ))
6565 print (f"Retrieved { len (limited_runs )} query runs (page_size=5)" )
66-
66+
6767 return query_runs
68-
68+
6969 except Exception as e :
7070 print (f"Error: { e } " )
7171 return []
@@ -74,34 +74,34 @@ def test_list():
7474def test_create ():
7575 """Test 2: Create a new query run."""
7676 print ("\n === Test 2: Create Query Run ===" )
77-
77+
7878 client , workspace = get_client_and_workspace ()
79-
79+
8080 try :
8181 # Get the latest configuration version
8282 config_versions = list (client .configuration_versions .list (workspace .id ))
8383 if not config_versions :
8484 print ("ERROR: No configuration versions found in workspace" )
8585 return None
86-
86+
8787 config_version = config_versions [0 ]
8888 print (f"Using configuration version: { config_version .id } " )
89-
89+
9090 # Create query run
9191 options = QueryRunCreateOptions (
9292 source = QueryRunSource .API ,
9393 workspace_id = workspace .id ,
9494 configuration_version_id = config_version .id ,
9595 )
96-
96+
9797 query_run = client .query_runs .create (options )
9898 print (f"Created query run: { query_run .id } " )
9999 print (f" Status: { query_run .status } " )
100100 print (f" Source: { query_run .source } " )
101101 print (f" Created: { query_run .created_at } " )
102-
102+
103103 return query_run
104-
104+
105105 except Exception as e :
106106 print (f"Error: { e } " )
107107 return None
@@ -110,9 +110,9 @@ def test_create():
110110def test_read (query_run_id = None ):
111111 """Test 3: Read a specific query run."""
112112 print ("\n === Test 3: Read Query Run ===" )
113-
113+
114114 client , workspace = get_client_and_workspace ()
115-
115+
116116 try :
117117 # If no query_run_id provided, get the first one from the list
118118 if not query_run_id :
@@ -122,16 +122,16 @@ def test_read(query_run_id=None):
122122 return None
123123 query_run_id = query_runs [0 ].id
124124 print (f"Using first query run from list: { query_run_id } " )
125-
125+
126126 # Read the query run
127127 query_run = client .query_runs .read (query_run_id )
128128 print (f"Read query run: { query_run .id } " )
129129 print (f" Status: { query_run .status } " )
130130 print (f" Source: { query_run .source } " )
131131 print (f" Created: { query_run .created_at } " )
132-
132+
133133 if query_run .status_timestamps :
134- print (f " Status Timestamps:" )
134+ print (" Status Timestamps:" )
135135 if query_run .status_timestamps .queued_at :
136136 print (f" Queued: { query_run .status_timestamps .queued_at } " )
137137 if query_run .status_timestamps .running_at :
@@ -140,9 +140,9 @@ def test_read(query_run_id=None):
140140 print (f" Finished: { query_run .status_timestamps .finished_at } " )
141141 if query_run .status_timestamps .errored_at :
142142 print (f" Errored: { query_run .status_timestamps .errored_at } " )
143-
143+
144144 return query_run
145-
145+
146146 except Exception as e :
147147 print (f"Error: { e } " )
148148 return None
@@ -151,9 +151,9 @@ def test_read(query_run_id=None):
151151def test_logs (query_run_id = None ):
152152 """Test 4: Retrieve logs for a query run."""
153153 print ("\n === Test 4: Get Query Run Logs ===" )
154-
154+
155155 client , workspace = get_client_and_workspace ()
156-
156+
157157 try :
158158 # If no query_run_id provided, get the first one from the list
159159 if not query_run_id :
@@ -163,33 +163,33 @@ def test_logs(query_run_id=None):
163163 return None
164164 query_run_id = query_runs [0 ].id
165165 print (f"Using first query run from list: { query_run_id } " )
166-
166+
167167 # Get logs
168168 logs = client .query_runs .logs (query_run_id )
169169 log_content = logs .read ().decode ("utf-8" )
170-
170+
171171 print (f"Retrieved logs for query run: { query_run_id } " )
172172 print (f" Log size: { len (log_content )} bytes" )
173- print (f "\n --- Log Preview (first 500 chars) ---" )
173+ print ("\n --- Log Preview (first 500 chars) ---" )
174174 print (log_content [:500 ])
175175 if len (log_content ) > 500 :
176176 print (f"\n ... ({ len (log_content ) - 500 } more characters)" )
177177 print ("--- End of Log Preview ---" )
178-
178+
179179 return log_content
180-
180+
181181 except Exception as e :
182182 print (f"Error: { e } " )
183- print (f " Note: Logs may not be available if the query run hasn't started yet" )
183+ print (" Note: Logs may not be available if the query run hasn't started yet" )
184184 return None
185185
186186
187187def test_cancel (query_run_id = None ):
188188 """Test 5: Cancel a query run."""
189189 print ("\n === Test 5: Cancel Query Run ===" )
190-
190+
191191 client , workspace = get_client_and_workspace ()
192-
192+
193193 try :
194194 # If no query_run_id provided, create a new one
195195 if not query_run_id :
@@ -200,30 +200,30 @@ def test_cancel(query_run_id=None):
200200 return False
201201 query_run_id = new_run .id
202202 time .sleep (1 ) # Give it a moment to start
203-
203+
204204 # Cancel the query run
205205 client .query_runs .cancel (query_run_id )
206206 print (f"Cancel requested for query run: { query_run_id } " )
207-
207+
208208 # Verify cancellation
209209 time .sleep (2 )
210210 query_run = client .query_runs .read (query_run_id )
211211 print (f" Status after cancel: { query_run .status } " )
212-
212+
213213 return True
214-
214+
215215 except Exception as e :
216216 print (f"Error: { e } " )
217- print (f " Note: Query run may not be in a cancelable state" )
217+ print (" Note: Query run may not be in a cancelable state" )
218218 return False
219219
220220
221221def test_force_cancel (query_run_id = None ):
222222 """Test 6: Force cancel a query run."""
223223 print ("\n === Test 6: Force Cancel Query Run ===" )
224-
224+
225225 client , workspace = get_client_and_workspace ()
226-
226+
227227 try :
228228 # If no query_run_id provided, create a new one
229229 if not query_run_id :
@@ -234,21 +234,21 @@ def test_force_cancel(query_run_id=None):
234234 return False
235235 query_run_id = new_run .id
236236 time .sleep (1 ) # Give it a moment to start
237-
237+
238238 # Force cancel the query run
239239 client .query_runs .force_cancel (query_run_id )
240240 print (f"Force cancel requested for query run: { query_run_id } " )
241-
241+
242242 # Verify force cancellation
243243 time .sleep (2 )
244244 query_run = client .query_runs .read (query_run_id )
245245 print (f" Status after force cancel: { query_run .status } " )
246-
246+
247247 return True
248-
248+
249249 except Exception as e :
250250 print (f"Error: { e } " )
251- print (f " Note: Query run may not be in a force-cancelable state" )
251+ print (" Note: Query run may not be in a force-cancelable state" )
252252 return False
253253
254254
@@ -262,26 +262,26 @@ def main():
262262 print ("NOTE: Query Runs require Terraform 1.10+ with 'terraform query' command." )
263263 print (" Most query runs will error since this feature is not yet available." )
264264 print ("=" * 80 )
265-
265+
266266 # Test 1: List query runs
267267 query_runs = test_list ()
268-
268+
269269 # Test 2: Create a query run
270270 new_query_run = test_create ()
271-
271+
272272 # Test 3: Read a query run
273273 if query_runs :
274274 test_read (query_runs [0 ].id )
275275 elif new_query_run :
276276 test_read (new_query_run .id )
277-
277+
278278 # Test 4: Get logs (use first query run from list)
279279 if query_runs :
280280 test_logs (query_runs [0 ].id )
281-
281+
282282 # Test 5: Cancel a query run (creates new one)
283283 test_cancel ()
284-
284+
285285 # Test 6: Force cancel a query run (creates new one)
286286 test_force_cancel ()
287287
0 commit comments