Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds Agent Pool to OAuth Client API #841

Merged
merged 16 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions oauth_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ type OAuthClient struct {
// Relations
Organization *Organization `jsonapi:"relation,organization"`
OAuthTokens []*OAuthToken `jsonapi:"relation,oauth-tokens"`
AgentPool *AgentPool `jsonapi:"relation,agent-pool"`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears that you've also added the inverse relationship to the API (each agent pool has_many oauth-clients). What do you think about adding include_data = false to that new relationship in the API? Is there a good reason to serialize all of the oauth-clients associated with an agent pool?

Copy link
Contributor Author

@roleesinhaHC roleesinhaHC Feb 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, added those changes https://github.com/hashicorp/atlas/pull/18747


// **Note: This field is still in BETA and subject to change.**
// The projects to which the oauth client applies.
Projects []*Project `jsonapi:"relation,projects"`
Expand Down Expand Up @@ -165,6 +167,9 @@ type OAuthClientCreateOptions struct {
// Required: The VCS provider being connected with.
ServiceProvider *ServiceProviderType `jsonapi:"attr,service-provider"`

// Optional: AgentPool to associate the VCS Provider with, for PrivateVCS support
AgentPool *AgentPool `jsonapi:"relation,agent-pool,omitempty"`

// **Note: This field is still in BETA and subject to change.**
// Optional: Whether the OAuthClient is available to all workspaces in the organization.
// True if the oauth client is organization scoped, false otherwise.
Expand Down
70 changes: 70 additions & 0 deletions oauth_client_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,76 @@ func TestOAuthClientsCreate_rsaKeyPair(t *testing.T) {
})
}

func TestOAuthClientsCreate_agentPool(t *testing.T) {
client := testClient(t)
ctx := context.Background()

githubToken := os.Getenv("OAUTH_CLIENT_GITHUB_TOKEN")
if githubToken == "" {
t.Skip("Export a valid OAUTH_CLIENT_GITHUB_TOKEN before running this test!")
}

t.Run("with valid agent pool external id", func(t *testing.T) {
t.Skip()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some comments here would be appropriate

orgTestRead, errOrg := client.Organizations.Read(ctx, "xxxxx")
require.NoError(t, errOrg)
agentPoolTestRead, errAgentPool := client.AgentPools.Read(ctx, "xxxxx")
require.NoError(t, errAgentPool)
options := OAuthClientCreateOptions{
APIURL: String("https://githubenterprise.xxxxx"),
HTTPURL: String("https://githubenterprise.xxxxx"),
OAuthToken: String(githubToken),
ServiceProvider: ServiceProvider(ServiceProviderGithubEE),
AgentPool: agentPoolTestRead,
}
oc, errCreate := client.OAuthClients.Create(ctx, orgTestRead.Name, options)
require.NoError(t, errCreate)
assert.NotEmpty(t, oc.ID)
assert.Equal(t, "https://githubenterprise.xxxxx", oc.APIURL)
assert.Equal(t, "https://githubenterprise.xxxxx", oc.HTTPURL)
assert.Equal(t, 1, len(oc.OAuthTokens))
assert.Equal(t, ServiceProviderGithubEE, oc.ServiceProvider)
assert.Equal(t, agentPoolTestRead.ID, oc.AgentPool.ID)
})

t.Run("with an invalid agent pool", func(t *testing.T) {
orgTest, orgTestCleanup := createOrganization(t, client)
defer orgTestCleanup()
agentPoolTest, agentPoolCleanup := createAgentPool(t, client, orgTest)
defer agentPoolCleanup()
agentPoolID := agentPoolTest.ID
agentPoolTest.ID = badIdentifier
options := OAuthClientCreateOptions{
APIURL: String("https://githubenterprise.xxxxx"),
HTTPURL: String("https://githubenterprise.xxxxx"),
OAuthToken: String(githubToken),
ServiceProvider: ServiceProvider(ServiceProviderGithubEE),
AgentPool: agentPoolTest,
}
_, errCreate := client.OAuthClients.Create(ctx, orgTest.Name, options)
require.Error(t, errCreate)
assert.Contains(t, errCreate.Error(), "the provided agent pool does not exist or you are not authorized to use it")
agentPoolTest.ID = agentPoolID
})

t.Run("with no agents connected", func(t *testing.T) {
orgTest, orgTestCleanup := createOrganization(t, client)
defer orgTestCleanup()
agentPoolTest, agentPoolCleanup := createAgentPool(t, client, orgTest)
defer agentPoolCleanup()
options := OAuthClientCreateOptions{
APIURL: String("https://githubenterprise.xxxxx"),
HTTPURL: String("https://githubenterprise.xxxxx"),
OAuthToken: String(githubToken),
ServiceProvider: ServiceProvider(ServiceProviderGithubEE),
AgentPool: agentPoolTest,
}
_, errCreate := client.OAuthClients.Create(ctx, orgTest.Name, options)
assert.Contains(t, errCreate.Error(), "the organization does not have private VCS enabled")
require.Error(t, errCreate)
})
}

func TestOAuthClientsRead(t *testing.T) {
client := testClient(t)
ctx := context.Background()
Expand Down
Loading