Skip to content

Commit 976b812

Browse files
Netra2104netramali
andauthored
project relationship (#806)
added the project relationship and the post/delete support removed include changelog read with options generate mocks readWithOptions test Update CHANGELOG.md rename Co-authored-by: Netra Mali <[email protected]>
1 parent 51a9039 commit 976b812

File tree

5 files changed

+342
-8
lines changed

5 files changed

+342
-8
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
## Bug Fixes
1717
* Fixes an issue where the request body is not preserved during certain retry scenarios by @sebasslash [#813](https://github.com/hashicorp/go-tfe/pull/813)
1818

19+
## Enhancements
20+
* Added BETA support for including `projects` relationship to oauth_client on create by @Netra2104 [#806](https://github.com/hashicorp/go-tfe/pull/806)
21+
* Added BETA method `AddProjects` and `RemoveProjects` for attaching/detaching oauth_client to projects by Netra2104 [#806](https://github.com/hashicorp/go-tfe/pull/806)
22+
1923
# v1.39.0
2024

2125
## Features

helper_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,7 @@ func createUploadedPolicyWithOptions(t *testing.T, client *Client, pass bool, or
892892
}
893893
}
894894

895-
func createOAuthClient(t *testing.T, client *Client, org *Organization) (*OAuthClient, func()) {
895+
func createOAuthClient(t *testing.T, client *Client, org *Organization, projects []*Project) (*OAuthClient, func()) {
896896
var orgCleanup func()
897897

898898
if org == nil {
@@ -909,6 +909,7 @@ func createOAuthClient(t *testing.T, client *Client, org *Organization) (*OAuthC
909909
HTTPURL: String("https://github.com"),
910910
OAuthToken: String(githubToken),
911911
ServiceProvider: ServiceProvider(ServiceProviderGithub),
912+
Projects: projects,
912913
}
913914

914915
ctx := context.Background()
@@ -934,7 +935,7 @@ func createOAuthClient(t *testing.T, client *Client, org *Organization) (*OAuthC
934935
}
935936

936937
func createOAuthToken(t *testing.T, client *Client, org *Organization) (*OAuthToken, func()) {
937-
ocTest, ocTestCleanup := createOAuthClient(t, client, org)
938+
ocTest, ocTestCleanup := createOAuthClient(t, client, org, nil)
938939
return ocTest.OAuthTokens[0], ocTestCleanup
939940
}
940941

mocks/oauth_client_mocks.go

Lines changed: 43 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

oauth_client.go

Lines changed: 111 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,20 @@ type OAuthClients interface {
2828
// Read an OAuth client by its ID.
2929
Read(ctx context.Context, oAuthClientID string) (*OAuthClient, error)
3030

31+
// ReadWithOptions reads an oauth client by its ID using the options supplied.
32+
ReadWithOptions(ctx context.Context, oAuthClientID string, options *OAuthClientReadOptions) (*OAuthClient, error)
33+
3134
// Update an existing OAuth client by its ID.
3235
Update(ctx context.Context, oAuthClientID string, options OAuthClientUpdateOptions) (*OAuthClient, error)
3336

3437
// Delete an OAuth client by its ID.
3538
Delete(ctx context.Context, oAuthClientID string) error
39+
40+
// AddProjects add projects to an oauth client.
41+
AddProjects(ctx context.Context, oAuthClientID string, options OAuthClientAddProjectsOptions) error
42+
43+
// RemoveProjects remove projects from an oauth client.
44+
RemoveProjects(ctx context.Context, oAuthClientID string, options OAuthClientRemoveProjectsOptions) error
3645
}
3746

3847
// oAuthClients implements OAuthClients.
@@ -86,12 +95,18 @@ type OAuthClient struct {
8695
// Relations
8796
Organization *Organization `jsonapi:"relation,organization"`
8897
OAuthTokens []*OAuthToken `jsonapi:"relation,oauth-tokens"`
98+
// **Note: This field is still in BETA and subject to change.**
99+
// The projects to which the oauth client applies.
100+
Projects []*Project `jsonapi:"relation,projects"`
89101
}
90102

91103
// A list of relations to include
92104
type OAuthClientIncludeOpt string
93105

94-
const OauthClientOauthTokens OAuthClientIncludeOpt = "oauth_tokens"
106+
const (
107+
OauthClientOauthTokens OAuthClientIncludeOpt = "oauth_tokens"
108+
OauthClientProjects OAuthClientIncludeOpt = "projects"
109+
)
95110

96111
// OAuthClientListOptions represents the options for listing
97112
// OAuth clients.
@@ -101,6 +116,15 @@ type OAuthClientListOptions struct {
101116
Include []OAuthClientIncludeOpt `url:"include,omitempty"`
102117
}
103118

119+
// OAuthClientReadOptions are read options.
120+
// For a full list of relations, please see:
121+
// https://developer.hashicorp.com/terraform/cloud-docs/api-docs/oauth-clients#relationships
122+
type OAuthClientReadOptions struct {
123+
// Optional: A list of relations to include. See available resources
124+
// https://developer.hashicorp.com/terraform/cloud-docs/api-docs/oauth-clients#available-related-resources
125+
Include []OAuthClientIncludeOpt `url:"include,omitempty"`
126+
}
127+
104128
// OAuthClientCreateOptions represents the options for creating an OAuth client.
105129
type OAuthClientCreateOptions struct {
106130
// Type is a public field utilized by JSON:API to
@@ -124,6 +148,10 @@ type OAuthClientCreateOptions struct {
124148
// Optional: The token string you were given by your VCS provider.
125149
OAuthToken *string `jsonapi:"attr,oauth-token-string,omitempty"`
126150

151+
// **Note: This field is still in BETA and subject to change.**
152+
// Optional: The initial list of projects for which the oauth client should be associated with.
153+
Projects []*Project `jsonapi:"relation,projects,omitempty"`
154+
127155
// Optional: Private key associated with this vcs provider - only available for ado_server
128156
PrivateKey *string `jsonapi:"attr,private-key,omitempty"`
129157

@@ -173,6 +201,20 @@ type OAuthClientUpdateOptions struct {
173201
OrganizationScoped *bool `jsonapi:"attr,organization-scoped,omitempty"`
174202
}
175203

204+
// OAuthClientAddProjectsOptions represents the options for adding projects
205+
// to an oauth client.
206+
type OAuthClientAddProjectsOptions struct {
207+
// The projects to add to an oauth client.
208+
Projects []*Project
209+
}
210+
211+
// OAuthClientRemoveProjectsOptions represents the options for removing
212+
// projects from an oauth client.
213+
type OAuthClientRemoveProjectsOptions struct {
214+
// The projects to remove from an oauth client.
215+
Projects []*Project
216+
}
217+
176218
// List all the OAuth clients for a given organization.
177219
func (s *oAuthClients) List(ctx context.Context, organization string, options *OAuthClientListOptions) (*OAuthClientList, error) {
178220
if !validStringID(&organization) {
@@ -223,12 +265,19 @@ func (s *oAuthClients) Create(ctx context.Context, organization string, options
223265

224266
// Read an OAuth client by its ID.
225267
func (s *oAuthClients) Read(ctx context.Context, oAuthClientID string) (*OAuthClient, error) {
268+
return s.ReadWithOptions(ctx, oAuthClientID, nil)
269+
}
270+
271+
func (s *oAuthClients) ReadWithOptions(ctx context.Context, oAuthClientID string, options *OAuthClientReadOptions) (*OAuthClient, error) {
226272
if !validStringID(&oAuthClientID) {
227273
return nil, ErrInvalidOauthClientID
228274
}
275+
if err := options.valid(); err != nil {
276+
return nil, err
277+
}
229278

230279
u := fmt.Sprintf("oauth-clients/%s", url.QueryEscape(oAuthClientID))
231-
req, err := s.client.NewRequest("GET", u, nil)
280+
req, err := s.client.NewRequest("GET", u, options)
232281
if err != nil {
233282
return nil, err
234283
}
@@ -300,3 +349,63 @@ func (o OAuthClientCreateOptions) valid() error {
300349
func (o *OAuthClientListOptions) valid() error {
301350
return nil
302351
}
352+
353+
// AddProjects adds projects to a given oauth client.
354+
func (s *oAuthClients) AddProjects(ctx context.Context, oAuthClientID string, options OAuthClientAddProjectsOptions) error {
355+
if !validStringID(&oAuthClientID) {
356+
return ErrInvalidOauthClientID
357+
}
358+
if err := options.valid(); err != nil {
359+
return err
360+
}
361+
362+
u := fmt.Sprintf("oauth-clients/%s/relationships/projects", url.QueryEscape(oAuthClientID))
363+
req, err := s.client.NewRequest("POST", u, options.Projects)
364+
if err != nil {
365+
return err
366+
}
367+
368+
return req.Do(ctx, nil)
369+
}
370+
371+
// RemoveProjects removes projects from an oauth client.
372+
func (s *oAuthClients) RemoveProjects(ctx context.Context, oAuthClientID string, options OAuthClientRemoveProjectsOptions) error {
373+
if !validStringID(&oAuthClientID) {
374+
return ErrInvalidOauthClientID
375+
}
376+
if err := options.valid(); err != nil {
377+
return err
378+
}
379+
380+
u := fmt.Sprintf("oauth-clients/%s/relationships/projects", url.QueryEscape(oAuthClientID))
381+
req, err := s.client.NewRequest("DELETE", u, options.Projects)
382+
if err != nil {
383+
return err
384+
}
385+
386+
return req.Do(ctx, nil)
387+
}
388+
389+
func (o OAuthClientAddProjectsOptions) valid() error {
390+
if o.Projects == nil {
391+
return ErrRequiredProject
392+
}
393+
if len(o.Projects) == 0 {
394+
return ErrProjectMinLimit
395+
}
396+
return nil
397+
}
398+
399+
func (o OAuthClientRemoveProjectsOptions) valid() error {
400+
if o.Projects == nil {
401+
return ErrRequiredProject
402+
}
403+
if len(o.Projects) == 0 {
404+
return ErrProjectMinLimit
405+
}
406+
return nil
407+
}
408+
409+
func (o *OAuthClientReadOptions) valid() error {
410+
return nil
411+
}

0 commit comments

Comments
 (0)