forked from hashicorp/go-tfe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask_result.go
93 lines (77 loc) · 3.24 KB
/
task_result.go
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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package tfe
import (
"context"
"fmt"
"time"
)
// Compile-time proof of interface implementation
var _ TaskResults = (*taskResults)(nil)
// TaskResults describes all the task result related methods that the HCP Terraform or Terraform Enterprise API supports.
type TaskResults interface {
// Read a task result by ID
Read(ctx context.Context, taskResultID string) (*TaskResult, error)
}
// taskResults implements TaskResults
type taskResults struct {
client *Client
}
// TaskResultStatus is an enum that represents all possible statuses for a task result
type TaskResultStatus string
const (
TaskPassed TaskResultStatus = "passed"
TaskFailed TaskResultStatus = "failed"
TaskPending TaskResultStatus = "pending"
TaskRunning TaskResultStatus = "running"
TaskUnreachable TaskResultStatus = "unreachable"
TaskErrored TaskResultStatus = "errored"
)
// TaskEnforcementLevel is an enum that describes the enforcement levels for a run task
type TaskEnforcementLevel string
const (
Advisory TaskEnforcementLevel = "advisory"
Mandatory TaskEnforcementLevel = "mandatory"
)
// TaskResultStatusTimestamps represents the set of timestamps recorded for a task result
type TaskResultStatusTimestamps struct {
ErroredAt time.Time `jsonapi:"attr,errored-at,rfc3339"`
RunningAt time.Time `jsonapi:"attr,running-at,rfc3339"`
CanceledAt time.Time `jsonapi:"attr,canceled-at,rfc3339"`
FailedAt time.Time `jsonapi:"attr,failed-at,rfc3339"`
PassedAt time.Time `jsonapi:"attr,passed-at,rfc3339"`
}
// TaskResult represents the result of a HCP Terraform or Terraform Enterprise run task
type TaskResult struct {
ID string `jsonapi:"primary,task-results"`
Status TaskResultStatus `jsonapi:"attr,status"`
Message string `jsonapi:"attr,message"`
StatusTimestamps TaskResultStatusTimestamps `jsonapi:"attr,status-timestamps"`
URL string `jsonapi:"attr,url"`
CreatedAt time.Time `jsonapi:"attr,created-at,iso8601"`
UpdatedAt time.Time `jsonapi:"attr,updated-at,iso8601"`
TaskID string `jsonapi:"attr,task-id"`
TaskName string `jsonapi:"attr,task-name"`
TaskURL string `jsonapi:"attr,task-url"`
WorkspaceTaskID string `jsonapi:"attr,workspace-task-id"`
WorkspaceTaskEnforcementLevel TaskEnforcementLevel `jsonapi:"attr,workspace-task-enforcement-level"`
// The task stage this result belongs to
TaskStage *TaskStage `jsonapi:"relation,task_stage"`
}
// Read a task result by ID
func (t *taskResults) Read(ctx context.Context, taskResultID string) (*TaskResult, error) {
if !validStringID(&taskResultID) {
return nil, ErrInvalidTaskResultID
}
u := fmt.Sprintf("task-results/%s", taskResultID)
req, err := t.client.NewRequest("GET", u, nil)
if err != nil {
return nil, err
}
r := &TaskResult{}
err = req.Do(ctx, r)
if err != nil {
return nil, err
}
return r, nil
}