forked from hashicorp/python-tfe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_task.py
More file actions
235 lines (200 loc) · 8.41 KB
/
run_task.py
File metadata and controls
235 lines (200 loc) · 8.41 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
# Copyright IBM Corp. 2025, 2026
# SPDX-License-Identifier: MPL-2.0
"""
Terraform Cloud/Enterprise Run Task Management Example
This example demonstrates comprehensive run task operations using the python-tfe SDK.
It provides a command-line interface for managing TFE run tasks with various operations
including create, read, update, delete, and advanced listing capabilities.
Prerequisites:
- Set TFE_TOKEN environment variable with your Terraform Cloud API token
- Ensure you have access to the target organization
Basic Usage:
python examples/run_task.py --help
Core Operations:
1. List Run Tasks (default operation):
python examples/run_task.py --org my-org
python examples/run_task.py --org my-org --page-size 20
python examples/run_task.py --org my-org --page 2 --page-size 10
2. Create New Run Task:
python examples/run_task.py --org my-org --create
3. Read Run Task Details:
python examples/run_task.py --org my-org --task-id "rt-abc123xyz"
python examples/run_task.py --org my-org --task-id "rt-abc123xyz" --include-workspace-tasks
4. Update Run Task Settings:
python examples/run_task.py --org my-org --task-id "rt-abc123xyz" --update
5. Delete Run Task:
python examples/run_task.py --org my-org --task-id "rt-abc123xyz" --delete
"""
from __future__ import annotations
import argparse
import os
import time
from datetime import datetime
from pytfe import TFEClient, TFEConfig
from pytfe.models import (
RunTaskCreateOptions,
RunTaskIncludeOptions,
RunTaskListOptions,
RunTaskReadOptions,
RunTaskUpdateOptions,
)
def _print_header(title: str) -> None:
"""Print a formatted header for operations."""
print("\n" + "=" * 80)
print(title)
print("=" * 80)
def main():
parser = argparse.ArgumentParser(description="Run Task demo for python-tfe SDK")
parser.add_argument(
"--address", default=os.getenv("TFE_ADDRESS", "https://app.terraform.io")
)
parser.add_argument("--token", default=os.getenv("TFE_TOKEN", ""))
parser.add_argument("--org", required=True, help="Organization name")
parser.add_argument(
"--task-id", help="Run Task ID for read/update/delete operations"
)
parser.add_argument("--create", action="store_true", help="Create a new run task")
parser.add_argument(
"--update", action="store_true", help="Update run task settings"
)
parser.add_argument("--delete", action="store_true", help="Delete the run task")
parser.add_argument(
"--include-workspace-tasks",
action="store_true",
help="Include workspace task relationships in read operations",
)
parser.add_argument("--page", type=int, default=1, help="Page number for listing")
parser.add_argument(
"--page-size", type=int, default=10, help="Page size for listing"
)
args = parser.parse_args()
cfg = TFEConfig(address=args.address, token=args.token)
client = TFEClient(cfg)
# 1) List run tasks in the organization
_print_header("Listing run tasks")
try:
# Create options for listing run tasks with pagination
options = RunTaskListOptions(
page_number=args.page,
page_size=args.page_size,
include=[
RunTaskIncludeOptions.RUN_TASK_WORKSPACE_TASKS,
RunTaskIncludeOptions.RUN_TASK_WORKSPACE,
],
)
print(
f"Fetching run tasks from organization '{args.org}' (page {args.page}, size {args.page_size})..."
)
# Get run tasks and convert to list safely
run_task_list = list(client.run_tasks.list(args.org, options))
print(f"Found {len(run_task_list)} run tasks")
print()
if not run_task_list:
print("No run tasks found in this organization.")
else:
for i, task in enumerate(run_task_list, 1):
print(f"{i:2d}. {task.name}")
print(f"ID: {task.id}")
print(f"URL: {task.url}")
print(f"Category: {task.category}")
print(f"Enabled: {task.enabled}")
if task.description:
print(f"Description: {task.description}")
print()
except Exception as e:
print(f"Error listing run tasks: {e}")
return
# 2) Create a new run task if requested
if args.create:
_print_header("Creating a new run task")
try:
timestamp = int(time.time())
task_name = f"demo-run-task-{timestamp}"
create_options = RunTaskCreateOptions(
name=task_name,
url="https://httpbin.org/post",
category="task",
description=f"Demo run task created at {datetime.now()}",
enabled=True,
hmac_key=f"demo-secret-key-{timestamp}",
)
print(f"Creating run task '{task_name}' in organization '{args.org}'...")
run_task = client.run_tasks.create(args.org, create_options)
print("Successfully created run task!")
print(f"Name: {run_task.name}")
print(f"ID: {run_task.id}")
print(f"URL: {run_task.url}")
print(f"Category: {run_task.category}")
print(f"Enabled: {run_task.enabled}")
print(f"Description: {run_task.description}")
print(f"HMAC Key: {'[CONFIGURED]' if run_task.hmac_key else 'None'}")
print()
args.task_id = run_task.id # Use the created task for other operations
except Exception as e:
print(f"Error creating run task: {e}")
return
# 3) Read run task details if task ID is provided
if args.task_id:
_print_header(f"Reading run task: {args.task_id}")
try:
if args.include_workspace_tasks:
read_options = RunTaskReadOptions(
include=[RunTaskIncludeOptions.RUN_TASK_WORKSPACE_TASKS]
)
run_task = client.run_tasks.read_with_options(
args.task_id, read_options
)
print("Reading run task with workspace task relationships...")
else:
run_task = client.run_tasks.read(args.task_id)
print("Reading run task details...")
print("Successfully read run task!")
print(f"Name: {run_task.name}")
print(f"ID: {run_task.id}")
print(f"URL: {run_task.url}")
print(f"Category: {run_task.category}")
print(f"Enabled: {run_task.enabled}")
print(f"Description: {run_task.description or 'None'}")
print(f"HMAC Key: {'[SET]' if run_task.hmac_key else 'None'}")
if run_task.organization:
print(f"Organization: {run_task.organization.id}")
if run_task.workspace_run_tasks:
print(f"Workspace Run Tasks: {len(run_task.workspace_run_tasks)} items")
print()
except Exception as e:
print(f"Error reading run task: {e}")
return
# 4) Update run task if requested
if args.update and args.task_id:
_print_header(f"Updating run task: {args.task_id}")
try:
update_options = RunTaskUpdateOptions(
name=f"updated-task-{int(time.time())}",
description=f"Updated run task at {datetime.now()}",
url="https://httpbin.org/anything",
enabled=True,
)
print(f"Updating run task '{args.task_id}'...")
updated_task = client.run_tasks.update(args.task_id, update_options)
print("Successfully updated run task!")
print(f"Name: {updated_task.name}")
print(f"Description: {updated_task.description}")
print(f"URL: {updated_task.url}")
print(f"Enabled: {updated_task.enabled}")
print()
except Exception as e:
print(f"Error updating run task: {e}")
return
# 5) Delete run task if requested (should be last operation)
if args.delete and args.task_id:
_print_header(f"Deleting run task: {args.task_id}")
try:
print(f"Deleting run task '{args.task_id}'...")
client.run_tasks.delete(args.task_id)
print(f"Successfully deleted run task: {args.task_id}")
print()
except Exception as e:
print(f"Error deleting run task: {e}")
return
if __name__ == "__main__":
main()