From e8bbfd2ce984585223bc55f3915a650d06ab83d2 Mon Sep 17 00:00:00 2001 From: Netra Mali <42544158+Netra2104@users.noreply.github.com> Date: Mon, 22 Apr 2024 09:59:48 -0400 Subject: [PATCH] TF-9703 project_ids and organization_scoped to oauth client (#1148) * init * markdown changes * changelog * Update data_source_oauth_client_test.go * Update data_source_oauth_client_test.go * test fix --------- Co-authored-by: Netra Mali Co-authored-by: Netra Mali <104793044+netramali@users.noreply.github.com> --- CHANGELOG.md | 4 +- internal/provider/data_source_oauth_client.go | 16 +++++ .../provider/data_source_oauth_client_test.go | 65 +++++++++++++++++++ website/docs/d/oauth_client.html.markdown | 2 + 4 files changed, 86 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e60d2027a..09640e772 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,8 +3,10 @@ * Adds `post_apply` to list of possible `stages` for Run Tasks by @carolinaborim [#1307](https://github.com/hashicorp/terraform-provider-tfe/pull/1307) ### Features +* `d/tfe_oauth_client`: Add `project_ids` attribute, by @Netra2104 [1148](https://github.com/hashicorp/terraform-provider-tfe/pull/1148) +* `d/tfe_oauth_client`: Add `organization_scoped` attribute, by @Netra2104 [1148](https://github.com/hashicorp/terraform-provider-tfe/pull/1148) * **New Resource**: `r/tfe_project_oauth_client` attaches/detaches an existing `project` to an existing `oauth client`, by @Netra2104 [1144](https://github.com/hashicorp/terraform-provider-tfe/pull/1144) - + ## v0.53.0 FEATURES: diff --git a/internal/provider/data_source_oauth_client.go b/internal/provider/data_source_oauth_client.go index 5f977dca2..6c4f92cf2 100644 --- a/internal/provider/data_source_oauth_client.go +++ b/internal/provider/data_source_oauth_client.go @@ -80,6 +80,15 @@ func dataSourceTFEOAuthClient() *schema.Resource { Type: schema.TypeString, Computed: true, }, + "organization_scoped": { + Type: schema.TypeBool, + Computed: true, + }, + "project_ids": { + Type: schema.TypeSet, + Elem: &schema.Schema{Type: schema.TypeString}, + Computed: true, + }, }, } } @@ -132,6 +141,7 @@ func dataSourceTFEOAuthClientRead(d *schema.ResourceData, meta interface{}) erro } d.Set("service_provider", oc.ServiceProvider) d.Set("service_provider_display_name", oc.ServiceProviderName) + d.Set("organization_scoped", oc.OrganizationScoped) switch len(oc.OAuthTokens) { case 0: @@ -142,5 +152,11 @@ func dataSourceTFEOAuthClientRead(d *schema.ResourceData, meta interface{}) erro return fmt.Errorf("unexpected number of OAuth tokens: %d", len(oc.OAuthTokens)) } + var projectIDs []interface{} + for _, project := range oc.Projects { + projectIDs = append(projectIDs, project.ID) + } + d.Set("project_ids", projectIDs) + return nil } diff --git a/internal/provider/data_source_oauth_client_test.go b/internal/provider/data_source_oauth_client_test.go index b9df39804..562289096 100644 --- a/internal/provider/data_source_oauth_client_test.go +++ b/internal/provider/data_source_oauth_client_test.go @@ -20,6 +20,28 @@ func testAccTFEOAuthClientDataSourcePreCheck(t *testing.T) { } } +func TestAccTFEOAuthClientDataSource_basic(t *testing.T) { + rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int() + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccTFEOAuthClientDataSourcePreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccTFEOAuthClientDataSourceConfig_basic(rInt), + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttrSet("data.tfe_oauth_client.client", "id"), + resource.TestCheckResourceAttr( + "data.tfe_oauth_client.client", "organization_scoped", "true"), + resource.TestCheckResourceAttr( + "data.tfe_oauth_client.client", "project_ids.#", "1"), + ), + }, + }, + }, + ) +} + func TestAccTFEOAuthClientDataSource_findByID(t *testing.T) { rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int() resource.Test(t, resource.TestCase{ @@ -41,6 +63,9 @@ func TestAccTFEOAuthClientDataSource_findByID(t *testing.T) { resource.TestCheckResourceAttrPair( "tfe_oauth_client.test", "oauth_token_id", "data.tfe_oauth_client.client", "oauth_token_id"), + resource.TestCheckResourceAttrPair( + "tfe_oauth_client.test", "organization_scoped", + "data.tfe_oauth_client.client", "organization_scoped"), ), }, }, @@ -68,6 +93,9 @@ func TestAccTFEOAuthClientDataSource_findByName(t *testing.T) { resource.TestCheckResourceAttrPair( "tfe_oauth_client.test", "oauth_token_id", "data.tfe_oauth_client.client", "oauth_token_id"), + resource.TestCheckResourceAttrPair( + "tfe_oauth_client.test", "organization_scoped", + "data.tfe_oauth_client.client", "organization_scoped"), ), }, }, @@ -185,6 +213,41 @@ func TestAccTFEOAuthClientDataSource_sameServiceProvider(t *testing.T) { }) } +func testAccTFEOAuthClientDataSourceConfig_basic(rInt int) string { + return fmt.Sprintf(` +resource "tfe_organization" "foobar" { + name = "tst-terraform-%d" + email = "admin@company.com" +} + +resource "tfe_project" "foobar" { + name = "project-foo-%d" + organization = tfe_organization.foobar.name +} + +resource "tfe_oauth_client" "test" { + organization = tfe_organization.foobar.name + api_url = "https://api.github.com" + http_url = "https://github.com" + oauth_token = "%s" + service_provider = "github" + organization_scoped = true +} + +resource "tfe_project_oauth_client" "foobar" { + oauth_client_id = tfe_oauth_client.test.id + project_id = tfe_project.foobar.id +} + +data "tfe_oauth_client" "client" { + name = tfe_oauth_client.test.name + organization = tfe_organization.foobar.name + oauth_client_id = tfe_oauth_client.test.id + depends_on=[tfe_project_oauth_client.foobar] +} +`, rInt, rInt, envGithubToken) +} + func testAccTFEOAuthClientDataSourceConfig_findByID(rInt int) string { return fmt.Sprintf(` resource "tfe_organization" "foobar" { @@ -197,6 +260,7 @@ resource "tfe_oauth_client" "test" { http_url = "https://github.com" oauth_token = "%s" service_provider = "github" + organization_scoped = true } data "tfe_oauth_client" "client" { oauth_client_id = tfe_oauth_client.test.id @@ -217,6 +281,7 @@ resource "tfe_oauth_client" "test" { name = "tst-github-%d" oauth_token = "%s" service_provider = "github" + organization_scoped = true } data "tfe_oauth_client" "client" { organization = "tst-terraform-%d" diff --git a/website/docs/d/oauth_client.html.markdown b/website/docs/d/oauth_client.html.markdown index 0864c5bd6..3488a8a61 100644 --- a/website/docs/d/oauth_client.html.markdown +++ b/website/docs/d/oauth_client.html.markdown @@ -66,3 +66,5 @@ In addition to all arguments above, the following attributes are exported: * `organization` - The organization in which the OAuth client is registered. * `service_provider` - The API identifier of the OAuth service provider. * `service_provider_display_name` - The display name of the OAuth service provider. +* `organization_scoped` - Whether or not the agent pool can be used by all workspaces and projects in the organization. +* `project_ids` - IDs of the projects that use the oauth client. \ No newline at end of file