-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathrun_task_integration.py
More file actions
130 lines (95 loc) · 3.44 KB
/
Copy pathrun_task_integration.py
File metadata and controls
130 lines (95 loc) · 3.44 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
# Copyright IBM Corp. 2025, 2026
# SPDX-License-Identifier: MPL-2.0
"""Example Run Task callback integration.
This example sends a callback result back to Terraform after a Run Task
webhook is received.
Required environment variables:
- TFE_ADDRESS
Terraform address (for example: https://app.terraform.io)
- TFE_TOKEN
Your Terraform API token used to initialize the SDK client.
- TFE_CALLBACK_URL
The task_result_callback_url received in the Run Task webhook payload.
- TFE_CALLBACK_TOKEN
The access_token received in the same webhook payload.
This token is used for the callback request and is different from
your regular Terraform API token.
Local testing flow:
1. Start the webhook server:
uvicorn examples.run_task_webhook_server:app --reload --port 8000
2. Expose the server publicly:
ngrok http 8000
3. Create a Run Task in Terraform Cloud / Enterprise using the ngrok URL.
4. Attach the Run Task to a workspace and trigger a run.
5. The webhook payload will include values similar to:
{
"task_result_callback_url": "https://app.terraform.io/...",
"access_token": "v1.xxxxx..."
}
6. Export those values locally and run this example script,
or call client.run_task_integrations.callback(...) directly
inside your webhook handler.
Example:
export TFE_ADDRESS=https://app.terraform.io
export TFE_TOKEN=<your-api-token>
export TFE_CALLBACK_URL=<from-webhook-payload>
export TFE_CALLBACK_TOKEN=<from-webhook-payload>
python examples/run_task_integration.py
"""
from __future__ import annotations
import os
from pytfe import TFEClient, TFEConfig
from pytfe.models import (
TaskResultCallbackRequestOptions,
TaskResultCallbackStatus,
TaskResultOutcome,
TaskResultTag,
)
def main() -> None:
callback_url = os.getenv("TFE_CALLBACK_URL")
access_token = os.getenv("TFE_CALLBACK_TOKEN")
if not callback_url or not access_token:
print("Missing TFE_CALLBACK_URL or TFE_CALLBACK_TOKEN")
return
# TFE_ADDRESS and TFE_TOKEN are loaded from the environment.
# The callback request itself uses the short-lived webhook token.
client = TFEClient(TFEConfig.from_env())
outcome = TaskResultOutcome(
description="Example outcome",
body="All checks passed successfully",
tags={"severity": [TaskResultTag(label="low", level="info")]},
)
# Example status values:
#
# - passed: marks the run task as successful
# - failed: fails the run task
# - running: reports progress before sending a final result
#
# Example: send an in-progress update
#
# options = TaskResultCallbackRequestOptions(
# status=TaskResultStatus.running,
# message="Security scan in progress",
# )
#
# Example: report a failure
#
# options = TaskResultCallbackRequestOptions(
# status=TaskResultStatus.failed,
# message="Found critical vulnerabilities",
# )
options = TaskResultCallbackRequestOptions(
status=TaskResultCallbackStatus.passed,
message="Run task completed successfully",
url="https://example.com/results",
outcomes=[outcome],
)
print(f"Sending callback to: {callback_url}")
client.run_task_integrations.callback(
callback_url=callback_url,
access_token=access_token,
options=options,
)
print("Run task callback sent successfully")
if __name__ == "__main__":
main()