Skip to content

Commit

Permalink
TF-9703 project_ids and organization_scoped to oauth client (#1148)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Co-authored-by: Netra Mali <[email protected]>
  • Loading branch information
3 people committed Apr 22, 2024
1 parent 92e3165 commit e8bbfd2
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 1 deletion.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
16 changes: 16 additions & 0 deletions internal/provider/data_source_oauth_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
}
}
Expand Down Expand Up @@ -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:
Expand All @@ -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
}
65 changes: 65 additions & 0 deletions internal/provider/data_source_oauth_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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"),
),
},
},
Expand Down Expand Up @@ -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"),
),
},
},
Expand Down Expand Up @@ -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 = "[email protected]"
}
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" {
Expand All @@ -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
Expand All @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions website/docs/d/oauth_client.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.

0 comments on commit e8bbfd2

Please sign in to comment.