/
project.go
211 lines (169 loc) · 4.97 KB
/
project.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package tfe
import (
"context"
"fmt"
"net/url"
)
// Compile-time proof of interface implementation.
var _ Projects = (*projects)(nil)
// Projects describes all the project related methods that the Terraform
// Enterprise API supports
//
// TFE API docs: (TODO: ADD DOCS URL)
// **Note: This functionality is still in BETA and subject to change.**
type Projects interface {
// List all projects in the given organization
List(ctx context.Context, organization string, options *ProjectListOptions) (*ProjectList, error)
// Create a new project.
Create(ctx context.Context, organization string, options ProjectCreateOptions) (*Project, error)
// Read a project by its ID.
Read(ctx context.Context, projectID string) (*Project, error)
// Update a project.
Update(ctx context.Context, projectID string, options ProjectUpdateOptions) (*Project, error)
// Delete a project.
Delete(ctx context.Context, projectID string) error
}
// projects implements Projects
type projects struct {
client *Client
}
// ProjectList represents a list of projects
type ProjectList struct {
*Pagination
Items []*Project
}
// Project represents a Terraform Enterprise project
type Project struct {
ID string `jsonapi:"primary,projects"`
Name string `jsonapi:"attr,name"`
// Relations
Organization *Organization `jsonapi:"relation,organization"`
}
// ProjectListOptions represents the options for listing projects
type ProjectListOptions struct {
ListOptions
}
// ProjectCreateOptions represents the options for creating a project
type ProjectCreateOptions struct {
// Type is a public field utilized by JSON:API to
// set the resource type via the field tag.
// It is not a user-defined value and does not need to be set.
// https://jsonapi.org/format/#crud-creating
Type string `jsonapi:"primary,projects"`
// Required: A name to identify the project.
Name string `jsonapi:"attr,name"`
}
// ProjectUpdateOptions represents the options for updating a project
type ProjectUpdateOptions struct {
// Type is a public field utilized by JSON:API to
// set the resource type via the field tag.
// It is not a user-defined value and does not need to be set.
// https://jsonapi.org/format/#crud-creating
Type string `jsonapi:"primary,projects"`
// Optional: A name to identify the project
Name *string `jsonapi:"attr,name,omitempty"`
}
// List all projects.
func (s *projects) List(ctx context.Context, organization string, options *ProjectListOptions) (*ProjectList, error) {
if !validStringID(&organization) {
return nil, ErrInvalidOrg
}
if err := options.valid(); err != nil {
return nil, err
}
u := fmt.Sprintf("organizations/%s/projects", url.QueryEscape(organization))
req, err := s.client.NewRequest("GET", u, options)
if err != nil {
return nil, err
}
p := &ProjectList{}
err = req.Do(ctx, p)
if err != nil {
return nil, err
}
return p, nil
}
func (o *ProjectListOptions) valid() error {
if o == nil || o.PageNumber == 0 || o.PageSize == 0 {
return ErrInvalidPagination
}
return nil
}
// Create a project with the given options
func (s *projects) Create(ctx context.Context, organization string, options ProjectCreateOptions) (*Project, error) {
if !validStringID(&organization) {
return nil, ErrInvalidOrg
}
if err := options.valid(); err != nil {
return nil, err
}
u := fmt.Sprintf("organizations/%s/projects", url.QueryEscape(organization))
req, err := s.client.NewRequest("POST", u, &options)
if err != nil {
return nil, err
}
p := &Project{}
err = req.Do(ctx, p)
if err != nil {
return nil, err
}
return p, nil
}
// Read a single project by its ID.
func (s *projects) Read(ctx context.Context, projectID string) (*Project, error) {
if !validStringID(&projectID) {
return nil, ErrInvalidProjectID
}
u := fmt.Sprintf("projects/%s", url.QueryEscape(projectID))
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, err
}
p := &Project{}
err = req.Do(ctx, p)
if err != nil {
return nil, err
}
return p, nil
}
// Update a project by its ID
func (s *projects) Update(ctx context.Context, projectID string, options ProjectUpdateOptions) (*Project, error) {
if !validStringID(&projectID) {
return nil, ErrInvalidProjectID
}
if err := options.valid(); err != nil {
return nil, err
}
u := fmt.Sprintf("projects/%s", url.QueryEscape(projectID))
req, err := s.client.NewRequest("PATCH", u, &options)
if err != nil {
return nil, err
}
p := &Project{}
err = req.Do(ctx, p)
if err != nil {
return nil, err
}
return p, nil
}
// Delete a project by its ID
func (s *projects) Delete(ctx context.Context, projectID string) error {
if !validStringID(&projectID) {
return ErrInvalidProjectID
}
u := fmt.Sprintf("projects/%s", url.QueryEscape(projectID))
req, err := s.client.NewRequest("DELETE", u, nil)
if err != nil {
return err
}
return req.Do(ctx, nil)
}
func (o ProjectCreateOptions) valid() error {
if !validString(&o.Name) {
return ErrRequiredName
}
return nil
}
func (o ProjectUpdateOptions) valid() error {
return nil
}