forked from hashicorp/go-tfe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_event.go
135 lines (109 loc) · 3.53 KB
/
run_event.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
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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package tfe
import (
"context"
"fmt"
"net/url"
"time"
)
// Compile-time proof of interface implementation.
var _ RunEvents = (*runEvents)(nil)
// RunEvents describes all the run events that the Terraform Enterprise
// API supports.
//
// TFE API docs: https://developer.hashicorp.com/terraform/cloud-docs/api-docs/run
type RunEvents interface {
// List all the runs events of the given run.
List(ctx context.Context, runID string, options *RunEventListOptions) (*RunEventList, error)
// Read a run event by its ID.
Read(ctx context.Context, runEventID string) (*RunEvent, error)
// ReadWithOptions reads a run event by its ID using the options supplied
ReadWithOptions(ctx context.Context, runEventID string, options *RunEventReadOptions) (*RunEvent, error)
}
// runEvents implements RunEvents.
type runEvents struct {
client *Client
}
// RunEventList represents a list of run events.
type RunEventList struct {
// Pagination is not supported by the API
*Pagination
Items []*RunEvent
}
// RunEvent represents a Terraform Enterprise run event.
type RunEvent struct {
ID string `jsonapi:"primary,run-events"`
Action string `jsonapi:"attr,action"`
CreatedAt time.Time `jsonapi:"attr,created-at,iso8601"`
Description string `jsonapi:"attr,description"`
// Relations - Note that `target` is not supported yet
Actor *User `jsonapi:"relation,actor"`
Comment *Comment `jsonapi:"relation,comment"`
}
// RunEventIncludeOpt represents the available options for include query params.
type RunEventIncludeOpt string
const (
RunEventComment RunEventIncludeOpt = "comment"
RunEventActor RunEventIncludeOpt = "actor"
)
// RunEventListOptions represents the options for listing run events.
type RunEventListOptions struct {
// Optional: A list of relations to include. See available resources:
Include []RunEventIncludeOpt `url:"include,omitempty"`
}
// RunEventReadOptions represents the options for reading a run event.
type RunEventReadOptions struct {
// Optional: A list of relations to include. See available resources:
Include []RunEventIncludeOpt `url:"include,omitempty"`
}
// List all the run events of the given run.
func (s *runEvents) List(ctx context.Context, runID string, options *RunEventListOptions) (*RunEventList, error) {
if !validStringID(&runID) {
return nil, ErrInvalidRunID
}
if err := options.valid(); err != nil {
return nil, err
}
u := fmt.Sprintf("runs/%s/run-events", url.PathEscape(runID))
req, err := s.client.NewRequest("GET", u, options)
if err != nil {
return nil, err
}
rl := &RunEventList{}
err = req.Do(ctx, rl)
if err != nil {
return nil, err
}
return rl, nil
}
// Read a run by its ID.
func (s *runEvents) Read(ctx context.Context, runEventID string) (*RunEvent, error) {
return s.ReadWithOptions(ctx, runEventID, nil)
}
// ReadWithOptions reads a run by its ID with the given options.
func (s *runEvents) ReadWithOptions(ctx context.Context, runEventID string, options *RunEventReadOptions) (*RunEvent, error) {
if !validStringID(&runEventID) {
return nil, ErrInvalidRunEventID
}
if err := options.valid(); err != nil {
return nil, err
}
u := fmt.Sprintf("run-events/%s", url.PathEscape(runEventID))
req, err := s.client.NewRequest("GET", u, options)
if err != nil {
return nil, err
}
r := &RunEvent{}
err = req.Do(ctx, r)
if err != nil {
return nil, err
}
return r, nil
}
func (o *RunEventReadOptions) valid() error {
return nil
}
func (o *RunEventListOptions) valid() error {
return nil
}