forked from hashicorp/go-tfe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolicy_set_version.go
160 lines (131 loc) · 5.09 KB
/
policy_set_version.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
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
package tfe
import (
"context"
"fmt"
"net/url"
"time"
)
// Compile-time proof of interface implementation.
var _ PolicySetVersions = (*policySetVersions)(nil)
// PolicySetVersions describes all the Policy Set Version related methods that the Terraform
// Enterprise API supports.
//
// TFE API docs: https://www.terraform.io/docs/cloud/api/policy-sets.html#create-a-policy-set-version
type PolicySetVersions interface {
// Create is used to create a new Policy Set Version.
Create(ctx context.Context, policySetID string) (*PolicySetVersion, error)
// Read is used to read a Policy Set Version by its ID.
Read(ctx context.Context, policySetVersionID string) (*PolicySetVersion, error)
// Upload uploads policy files. It takes a Policy Set Version and a path
// to the set of sentinel files, which will be packaged by hashicorp/go-slug
// before being uploaded.
Upload(ctx context.Context, psv PolicySetVersion, path string) error
}
// policySetVersions implements PolicySetVersions.
type policySetVersions struct {
client *Client
}
// PolicySetVersionSource represents a source type of a policy set version.
type PolicySetVersionSource string
// List all available sources for a Policy Set Version.
const (
PolicySetVersionSourceAPI PolicySetVersionSource = "tfe-api"
PolicySetVersionSourceADO PolicySetVersionSource = "ado"
PolicySetVersionSourceBitBucket PolicySetVersionSource = "bitbucket"
PolicySetVersionSourceGitHub PolicySetVersionSource = "github"
PolicySetVersionSourceGitLab PolicySetVersionSource = "gitlab"
)
// PolicySetVersionStatus represents a policy set version status.
type PolicySetVersionStatus string
// List all available policy set version statuses.
const (
PolicySetVersionErrored PolicySetVersionStatus = "errored"
PolicySetVersionIngressing PolicySetVersionStatus = "ingressing"
PolicySetVersionPending PolicySetVersionStatus = "pending"
PolicySetVersionReady PolicySetVersionStatus = "ready"
)
// PolicySetVersionStatusTimestamps holds the timestamps for individual policy
// set version statuses.
type PolicySetVersionStatusTimestamps struct {
PendingAt time.Time `jsonapi:"attr,pending-at,rfc3339"`
IngressingAt time.Time `jsonapi:"attr,ingressing-at,rfc3339"`
ReadyAt time.Time `jsonapi:"attr,ready-at,rfc3339"`
ErroredAt time.Time `jsonapi:"attr,errored-at,rfc3339"`
}
// PolicySetVersion represents a Terraform Enterprise Policy Set Version
type PolicySetVersion struct {
ID string `jsonapi:"primary,policy-set-versions"`
Source PolicySetVersionSource `jsonapi:"attr,source"`
Status PolicySetVersionStatus `jsonapi:"attr,status"`
StatusTimestamps PolicySetVersionStatusTimestamps `jsonapi:"attr,status-timestamps"`
Error string `jsonapi:"attr,error"`
ErrorMessage string `jsonapi:"attr,error-message"`
CreatedAt time.Time `jsonapi:"attr,created-at,iso8601"`
UpdatedAt time.Time `jsonapi:"attr,updated-at,iso8601"`
// Relations
PolicySet *PolicySet `jsonapi:"relation,policy-set"`
// Links
Links map[string]interface{} `jsonapi:"links,omitempty"`
}
func (p PolicySetVersion) uploadURL() (string, error) {
uploadURL, ok := p.Links["upload"].(string)
if !ok {
return uploadURL, fmt.Errorf("the Policy Set Version does not contain an upload link")
}
if uploadURL == "" {
return uploadURL, fmt.Errorf("the Policy Set Version upload URL is empty")
}
return uploadURL, nil
}
// Create is used to create a new Policy Set Version.
func (p *policySetVersions) Create(ctx context.Context, policySetID string) (*PolicySetVersion, error) {
if !validStringID(&policySetID) {
return nil, ErrInvalidPolicySetID
}
u := fmt.Sprintf("policy-sets/%s/versions", url.QueryEscape(policySetID))
req, err := p.client.newRequest("POST", u, nil)
if err != nil {
return nil, err
}
psv := &PolicySetVersion{}
err = p.client.do(ctx, req, psv)
if err != nil {
return nil, err
}
return psv, nil
}
// Read is used to read a Policy Set Version by its ID.
func (p *policySetVersions) Read(ctx context.Context, policySetVersionID string) (*PolicySetVersion, error) {
if !validStringID(&policySetVersionID) {
return nil, ErrInvalidPolicySetID
}
u := fmt.Sprintf("policy-set-versions/%s", url.QueryEscape(policySetVersionID))
req, err := p.client.newRequest("GET", u, nil)
if err != nil {
return nil, err
}
psv := &PolicySetVersion{}
err = p.client.do(ctx, req, psv)
if err != nil {
return nil, err
}
return psv, nil
}
// Upload uploads policy files. It takes a Policy Set Version and a path
// to the set of sentinel files, which will be packaged by hashicorp/go-slug
// before being uploaded.
func (p *policySetVersions) Upload(ctx context.Context, psv PolicySetVersion, path string) error {
uploadURL, err := psv.uploadURL()
if err != nil {
return err
}
body, err := packContents(path)
if err != nil {
return err
}
req, err := p.client.newRequest("PUT", uploadURL, body)
if err != nil {
return err
}
return p.client.do(ctx, req, nil)
}