diff --git a/oauth_client.go b/oauth_client.go index 327c1f230..f71302c4c 100644 --- a/oauth_client.go +++ b/oauth_client.go @@ -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"` + // **Note: This field is still in BETA and subject to change.** // The projects to which the oauth client applies. Projects []*Project `jsonapi:"relation,projects"` @@ -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. diff --git a/oauth_client_integration_test.go b/oauth_client_integration_test.go index 037abd395..a2151d68a 100644 --- a/oauth_client_integration_test.go +++ b/oauth_client_integration_test.go @@ -224,6 +224,77 @@ 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) { + // This requires access to Private VCS feature and tfc-agent running locally + t.Skip() + 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()