-
Notifications
You must be signed in to change notification settings - Fork 94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add support for run task results callback #867
Open
karvounis-form3
wants to merge
11
commits into
hashicorp:main
Choose a base branch
from
karvounis-form3:main
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7c96573
feat: add support for run task results callback
karvounis-form3 75e6c68
chore: set Run Tasks Integration as supported
karvounis-form3 fb8a55e
Merge branch 'main' into main
karvounis-form3 12b0240
Merge branch 'main' into main
karvounis-form3 3d73bb1
Merge branch 'main' into main
karvounis-form3 8e5aad7
chore: replace json structs with jsonapi ones
karvounis-form3 5b9e41c
chore: endpoint validation uses access token verification
karvounis-form3 d935548
Merge branch 'main' of github.com:karvounis-form3/go-tfe
karvounis-form3 d9e1f41
chore(task-results-options): validate options in Update function
karvounis-form3 05ed8c6
Merge branch 'main' into main
karvounis-form3 4b819b6
Merge branch 'main' into main
karvounis-form3 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package tfe | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
const ( | ||
// VerificationToken is a nonsense Terraform Cloud API token that should NEVER be valid. | ||
VerificationToken = "test-token" | ||
) | ||
|
||
// RunTaskRequest is the payload object that TFC/E sends to the Run Task's URL. | ||
// https://developer.hashicorp.com/terraform/enterprise/api-docs/run-tasks/run-tasks-integration#common-properties | ||
type RunTaskRequest struct { | ||
AccessToken string `json:"access_token"` | ||
Capabilitites RunTaskRequestCapabilitites `json:"capabilitites,omitempty"` | ||
ConfigurationVersionDownloadURL string `json:"configuration_version_download_url,omitempty"` | ||
ConfigurationVersionID string `json:"configuration_version_id,omitempty"` | ||
IsSpeculative bool `json:"is_speculative"` | ||
OrganizationName string `json:"organization_name"` | ||
PayloadVersion int `json:"payload_version"` | ||
PlanJSONAPIURL string `json:"plan_json_api_url,omitempty"` // Specific to post_plan, pre_apply or post_apply stage | ||
RunAppURL string `json:"run_app_url"` | ||
RunCreatedAt time.Time `json:"run_created_at"` | ||
RunCreatedBy string `json:"run_created_by"` | ||
RunID string `json:"run_id"` | ||
RunMessage string `json:"run_message"` | ||
Stage string `json:"stage"` | ||
TaskResultCallbackURL string `json:"task_result_callback_url"` | ||
TaskResultEnforcementLevel string `json:"task_result_enforcement_level"` | ||
TaskResultID string `json:"task_result_id"` | ||
VcsBranch string `json:"vcs_branch,omitempty"` | ||
VcsCommitURL string `json:"vcs_commit_url,omitempty"` | ||
VcsPullRequestURL string `json:"vcs_pull_request_url,omitempty"` | ||
VcsRepoURL string `json:"vcs_repo_url,omitempty"` | ||
WorkspaceAppURL string `json:"workspace_app_url"` | ||
WorkspaceID string `json:"workspace_id"` | ||
WorkspaceName string `json:"workspace_name"` | ||
WorkspaceWorkingDirectory string `json:"workspace_working_directory,omitempty"` | ||
} | ||
|
||
// RunTaskRequestCapabilitites defines the capabilities that the caller supports. | ||
type RunTaskRequestCapabilitites struct { | ||
Outcomes bool `json:"outcomes"` | ||
} | ||
|
||
// IsEndpointValidation returns true if this is a Request from TFC/E to validate and register this API endpoint. | ||
// Function copied from: https://github.com/hashicorp/terraform-run-task-scaffolding-go/blob/d7ed63b7d8eacf0897ab687d35d353386e4bd0ac/internal/sdk/api/structs.go#L55-L60 | ||
func (r RunTaskRequest) IsEndpointValidation() bool { | ||
return r.AccessToken == VerificationToken | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package tfe | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
) | ||
|
||
// Compile-time proof of interface implementation. | ||
var _ RunTasksCallback = (*taskResultsCallback)(nil) | ||
|
||
// RunTasksCallback describes all the Run Tasks Integration Callback API methods. | ||
// | ||
// TFE API docs: | ||
// https://developer.hashicorp.com/terraform/enterprise/api-docs/run-tasks/run-tasks-integration | ||
type RunTasksCallback interface { | ||
// Update sends updates to TFC/E Run Task Callback URL | ||
Update(ctx context.Context, callbackURL string, accessToken string, options TaskResultCallbackRequestOptions) error | ||
} | ||
|
||
// taskResultsCallback implements RunTasksCallback. | ||
type taskResultsCallback struct { | ||
client *Client | ||
} | ||
|
||
// Update sends updates to TFC/E Run Task Callback URL | ||
func (s *taskResultsCallback) Update(ctx context.Context, callbackURL string, accessToken string, options TaskResultCallbackRequestOptions) error { | ||
if !validString(&callbackURL) { | ||
return ErrInvalidCallbackURL | ||
} | ||
if !validString(&accessToken) { | ||
return ErrInvalidAccessToken | ||
} | ||
if err := options.valid(); err != nil { | ||
return err | ||
} | ||
req, err := s.client.NewRequest(http.MethodPatch, callbackURL, &options) | ||
if err != nil { | ||
return err | ||
} | ||
// The PATCH request must use the token supplied in the originating request (access_token) for authentication. | ||
// https://developer.hashicorp.com/terraform/enterprise/api-docs/run-tasks/run-tasks-integration#request-headers-1 | ||
req.Header.Set("Authorization", "Bearer "+accessToken) | ||
return req.Do(ctx, nil) | ||
} | ||
|
||
// TaskResultCallbackRequestOptions represents the TFC/E Task result callback request | ||
// https://developer.hashicorp.com/terraform/enterprise/api-docs/run-tasks/run-tasks-integration#request-body-1 | ||
type TaskResultCallbackRequestOptions struct { | ||
Type string `jsonapi:"primary,task-results"` | ||
Status TaskResultStatus `jsonapi:"attr,status"` | ||
Message string `jsonapi:"attr,message,omitempty"` | ||
URL string `jsonapi:"attr,url,omitempty"` | ||
Outcomes []*TaskResultOutcome `jsonapi:"relation,outcomes,omitempty"` | ||
} | ||
|
||
// TaskResultOutcome represents a detailed TFC/E run task outcome, which improves result visibility and content in the TFC/E UI. | ||
// https://developer.hashicorp.com/terraform/enterprise/api-docs/run-tasks/run-tasks-integration#outcomes-payload-body | ||
type TaskResultOutcome struct { | ||
Type string `jsonapi:"primary,task-result-outcomes"` | ||
OutcomeID string `jsonapi:"attr,outcome-id,omitempty"` | ||
Description string `jsonapi:"attr,description,omitempty"` | ||
Body string `jsonapi:"attr,body,omitempty"` | ||
URL string `jsonapi:"attr,url,omitempty"` | ||
Tags map[string][]*TaskResultTag `jsonapi:"attr,tags,omitempty"` | ||
} | ||
|
||
// TaskResultTag can be used to enrich outcomes display list in TFC/E. | ||
// https://developer.hashicorp.com/terraform/enterprise/api-docs/run-tasks/run-tasks-integration#severity-and-status-tags | ||
type TaskResultTag struct { | ||
Label string `json:"label"` | ||
Level *string `json:"level,omitempty"` | ||
} | ||
|
||
func (o *TaskResultCallbackRequestOptions) valid() error { | ||
if !validStringID(String(string(o.Status))) || (o.Status != TaskFailed && o.Status != TaskPassed && o.Status != TaskRunning) { | ||
return ErrInvalidTaskResultsCallbackStatus | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move these above the
Update()
call for visibility. The style ordering for a file is: