-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathquery_run.py
More file actions
293 lines (224 loc) · 9.11 KB
/
Copy pathquery_run.py
File metadata and controls
293 lines (224 loc) · 9.11 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#!/usr/bin/env python3
# Copyright IBM Corp. 2025, 2026
# SPDX-License-Identifier: MPL-2.0
"""
Query Run Individual Function Tests
This file provides individual test functions for each query run operation.
You can run specific functions to test individual parts of the API.
Functions available:
- run_list() - List query runs in a workspace
- run_create() - Create a new query run
- run_read() - Read a specific query run
- run_logs() - Retrieve logs for a query run
- run_cancel() - Cancel a query run
- run_force_cancel() - Force cancel a query run
Usage:
python query_run.py
Note: Query Runs require Terraform ~>1.14 which includes the 'terraform query' command.
These tests may fail with error status since the feature is not fully available yet.
"""
import os
import time
from pytfe import TFEClient, TFEConfig
from pytfe.models import (
QueryRunCreateOptions,
QueryRunListOptions,
QueryRunSource,
)
def get_client_and_workspace():
"""Initialize client and get workspace ID."""
client = TFEClient(TFEConfig.from_env())
organization = os.getenv("TFE_ORG", "aayush-test")
workspace_name = "query-test" # Default workspace for testing
# Get workspace
workspace = client.workspaces.read(workspace_name, organization=organization)
return client, workspace
def run_list():
"""Test 1: List query runs in a workspace."""
print("=== Test 1: List Query Runs ===")
client, workspace = get_client_and_workspace()
try:
# Simple list
query_runs = list(client.query_runs.list(workspace.id))
print(f"Found {len(query_runs)} query runs in workspace '{workspace.name}'")
for i, qr in enumerate(query_runs[:5], 1):
print(f" {i}. {qr.id}")
print(f" Status: {qr.status}")
print(f" Created: {qr.created_at}")
print()
# List with options
options = QueryRunListOptions(page_size=5)
limited_runs = list(client.query_runs.list(workspace.id, options))
print(f"Retrieved {len(limited_runs)} query runs (page_size=5)")
return query_runs
except Exception as e:
print(f"Error: {e}")
return []
def run_create():
"""Test 2: Create a new query run."""
print("\n=== Test 2: Create Query Run ===")
client, workspace = get_client_and_workspace()
try:
# Get the latest configuration version
config_versions = list(client.configuration_versions.list(workspace.id))
if not config_versions:
print("ERROR: No configuration versions found in workspace")
return None
config_version = config_versions[0]
print(f"Using configuration version: {config_version.id}")
# Create query run
options = QueryRunCreateOptions(
source=QueryRunSource.API,
workspace_id=workspace.id,
configuration_version_id=config_version.id,
)
query_run = client.query_runs.create(options)
print(f"Created query run: {query_run.id}")
print(f" Status: {query_run.status}")
print(f" Source: {query_run.source}")
print(f" Created: {query_run.created_at}")
return query_run
except Exception as e:
print(f"Error: {e}")
return None
def run_read(query_run_id=None):
"""Test 3: Read a specific query run."""
print("\n=== Test 3: Read Query Run ===")
client, workspace = get_client_and_workspace()
try:
# If no query_run_id provided, get the first one from the list
if not query_run_id:
query_runs = list(client.query_runs.list(workspace.id))
if not query_runs:
print("ERROR: No query runs found to read")
return None
query_run_id = query_runs[0].id
print(f"Using first query run from list: {query_run_id}")
# Read the query run
query_run = client.query_runs.read(query_run_id)
print(f"Read query run: {query_run.id}")
print(f" Status: {query_run.status}")
print(f" Source: {query_run.source}")
print(f" Created: {query_run.created_at}")
if query_run.status_timestamps:
print(" Status Timestamps:")
if query_run.status_timestamps.queued_at:
print(f" Queued: {query_run.status_timestamps.queued_at}")
if query_run.status_timestamps.running_at:
print(f" Running: {query_run.status_timestamps.running_at}")
if query_run.status_timestamps.finished_at:
print(f" Finished: {query_run.status_timestamps.finished_at}")
if query_run.status_timestamps.errored_at:
print(f" Errored: {query_run.status_timestamps.errored_at}")
return query_run
except Exception as e:
print(f"Error: {e}")
return None
def run_logs(query_run_id=None):
"""Test 4: Retrieve logs for a query run."""
print("\n=== Test 4: Get Query Run Logs ===")
client, workspace = get_client_and_workspace()
try:
# If no query_run_id provided, get the first one from the list
if not query_run_id:
query_runs = list(client.query_runs.list(workspace.id))
if not query_runs:
print("ERROR: No query runs found to get logs")
return None
query_run_id = query_runs[0].id
print(f"Using first query run from list: {query_run_id}")
# Get logs
logs = client.query_runs.logs(query_run_id)
log_content = logs.read().decode("utf-8")
print(f"Retrieved logs for query run: {query_run_id}")
print(f" Log size: {len(log_content)} bytes")
print("\n--- Log Preview (first 500 chars) ---")
print(log_content[:500])
if len(log_content) > 500:
print(f"\n... ({len(log_content) - 500} more characters)")
print("--- End of Log Preview ---")
return log_content
except Exception as e:
print(f"Error: {e}")
print(" Note: Logs may not be available if the query run hasn't started yet")
return None
def run_cancel(query_run_id=None):
"""Test 5: Cancel a query run."""
print("\n=== Test 5: Cancel Query Run ===")
client, workspace = get_client_and_workspace()
try:
# If no query_run_id provided, create a new one
if not query_run_id:
print("Creating a new query run to cancel...")
new_run = run_create()
if not new_run:
print("ERROR: Could not create query run to cancel")
return False
query_run_id = new_run.id
time.sleep(1) # Give it a moment to start
# Cancel the query run
client.query_runs.cancel(query_run_id)
print(f"Cancel requested for query run: {query_run_id}")
# Verify cancellation
time.sleep(2)
query_run = client.query_runs.read(query_run_id)
print(f" Status after cancel: {query_run.status}")
return True
except Exception as e:
print(f"Error: {e}")
print(" Note: Query run may not be in a cancelable state")
return False
def run_force_cancel(query_run_id=None):
"""Test 6: Force cancel a query run."""
print("\n=== Test 6: Force Cancel Query Run ===")
client, workspace = get_client_and_workspace()
try:
# If no query_run_id provided, create a new one
if not query_run_id:
print("Creating a new query run to force cancel...")
new_run = run_create()
if not new_run:
print("ERROR: Could not create query run to force cancel")
return False
query_run_id = new_run.id
time.sleep(1) # Give it a moment to start
# Force cancel the query run
client.query_runs.force_cancel(query_run_id)
print(f"Force cancel requested for query run: {query_run_id}")
# Verify force cancellation
time.sleep(2)
query_run = client.query_runs.read(query_run_id)
print(f" Status after force cancel: {query_run.status}")
return True
except Exception as e:
print(f"Error: {e}")
print(" Note: Query run may not be in a force-cancelable state")
return False
def main():
"""Run all tests in sequence."""
print("=" * 80)
print("QUERY RUN FUNCTION TESTS")
print("=" * 80)
print("Testing all Query Run API operations")
print()
print("NOTE: Query Runs require Terraform 1.10+ with 'terraform query' command.")
print(" Most query runs will error since this feature is not yet available.")
print("=" * 80)
# Test 1: List query runs
query_runs = run_list()
# Test 2: Create a query run
new_query_run = run_create()
# Test 3: Read a query run
if query_runs:
run_read(query_runs[0].id)
elif new_query_run:
run_read(new_query_run.id)
# Test 4: Get logs (use first query run from list)
if query_runs:
run_logs(query_runs[0].id)
# Test 5: Cancel a query run (creates new one)
run_cancel()
# Test 6: Force cancel a query run (creates new one)
run_force_cancel()
if __name__ == "__main__":
main()