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

New datasource: tfe_oauth_client #212

Merged
merged 11 commits into from
Sep 23, 2020
Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
## 0.22.0 (Unreleased)
* **New Data Source:** d/tfe_oauth_client ([#212](https://github.com/terraform-providers/terraform-provider-tfe/pull/212))
## 0.21.0 (August 19, 2020)

ENHANCEMENTS:
Expand Down
58 changes: 58 additions & 0 deletions tfe/data_source_oauth_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package tfe

import (
"context"
"fmt"

"github.com/hashicorp/go-tfe"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func dataSourceTFEOAuthClient() *schema.Resource {
return &schema.Resource{
Read: dataSourceTFEOAuthClientRead,
Schema: map[string]*schema.Schema{
"oauth_client_id": {
Type: schema.TypeString,
Required: true,
},
"ssh_key": {
Type: schema.TypeString,
Computed: true,
},
"token_id": {
Type: schema.TypeString,
Computed: true,
},
"api_url": {
Type: schema.TypeString,
Computed: true,
},
"http_url": {
Type: schema.TypeString,
Computed: true,
},
koikonom marked this conversation as resolved.
Show resolved Hide resolved
},
}
}

func dataSourceTFEOAuthClientRead(d *schema.ResourceData, meta interface{}) error {
ctx := context.TODO()
Copy link
Contributor

Choose a reason for hiding this comment

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

just a question for my own learning: i've never seen this before. i read about it in the docs and it makes sense. is this something we should be doing in all of our resources/data sources that have a blank/empty context?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I created an empty context because I needed one to pass to the tfe client.

Perhaps in the long run we could (should?) revisit the codebase and set a context with a timeout (https://godoc.org/context#WithTimeout).

tfeClient := meta.(*tfe.Client)

ocID := d.Get("oauth_client_id").(string)

oc, err := tfeClient.OAuthClients.Read(ctx, ocID)
if err != nil {
return fmt.Errorf("Error retrieving OAuth client: %v", err)
}

tokenID := oc.OAuthTokens[0].ID
Copy link
Contributor

Choose a reason for hiding this comment

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

do you think it is worth handling this case the way we do in the resource? in case the oauth client doesn't have an oauth token yet?

like this: https://github.com/terraform-providers/terraform-provider-tfe/blob/master/tfe/resource_tfe_oauth_client.go#L130

Copy link
Contributor

Choose a reason for hiding this comment

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

can confirm that we need to handle this similar to how we handle it in the resource. this panics if you give it the oauth client id of an unfinished client (so a client that doesn't have an oauth token yet)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the hint!

d.SetId(oc.ID)
_ = d.Set("ssh_key", oc.RSAPublicKey)
_ = d.Set("token_id", tokenID)
koikonom marked this conversation as resolved.
Show resolved Hide resolved
_ = d.Set("api_url", oc.APIURL)
_ = d.Set("http_url", oc.HTTPURL)

return nil
}
52 changes: 52 additions & 0 deletions tfe/data_source_oauth_client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package tfe

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccTFEOAuthClientDataSource_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccTFEOAuthClientDataSourceConfig(),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrPair(
"tfe_oauth_client.test", "api_url",
"data.tfe_oauth_client.client", "api_url"),
resource.TestCheckResourceAttrPair(
"tfe_oauth_client.test", "http_url",
"data.tfe_oauth_client.client", "http_url"),
resource.TestCheckResourceAttrPair(
"tfe_oauth_client.test", "oauth_token_id",
"data.tfe_oauth_client.client", "token_id"),
koikonom marked this conversation as resolved.
Show resolved Hide resolved
),
},
},
})
}

func testAccTFEOAuthClientDataSourceConfig() string {
return fmt.Sprintf(`
resource "tfe_organization" "foobar" {
name = "tst-terraform"
email = "admin@company.com"
}

resource "tfe_oauth_client" "test" {
organization = "${tfe_organization.foobar.id}"
api_url = "https://api.github.com"
http_url = "https://github.com"
oauth_token = "%s"
service_provider = "github"
}

data "tfe_oauth_client" "client" {
oauth_client_id = "${tfe_oauth_client.test.id}"
}
`, GITHUB_TOKEN)
}
1 change: 1 addition & 0 deletions tfe/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func Provider() terraform.ResourceProvider {
"tfe_team_access": dataSourceTFETeamAccess(),
"tfe_workspace": dataSourceTFEWorkspace(),
"tfe_workspace_ids": dataSourceTFEWorkspaceIDs(),
"tfe_oauth_client": dataSourceTFEOAuthClient(),
koikonom marked this conversation as resolved.
Show resolved Hide resolved
},

ResourcesMap: map[string]*schema.Resource{
Expand Down
35 changes: 35 additions & 0 deletions website/docs/d/oauth_client.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
layout: "tfe"
page_title: "Terraform Enterprise: tfe_oauth_client"
sidebar_current: "docs-datasource-tfe-oauth-client-x"
description: |-
Get information on an OAuth client.
---

# Data Source: tfe_oauth_client

Use this data source to get information about an OAuth client.

## Example Usage

```hcl
data "tfe_oauth_client" "client" {
oauth_client_id = "oc-XXXXXXX"
}
```

## Argument Reference

The following arguments are supported:

* `oauth_client_id` - (Required) ID of the OAuth client.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

* `id` - The OAuth client ID.
* `ssh_key` - The SSH key assigned to the OAuth client.
* `token_id` - The ID of the OAuth token associated with te OAuth client.
* `api_url` - The client's API URL.
* `api_url` - The client's HTTP URL.
koikonom marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 3 additions & 0 deletions website/tfe.erb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
<li<%= sidebar_current("docs-datasource-tfe-workspace-ids") %>>
<a href="/docs/providers/tfe/d/workspace_ids.html">tfe_workspace_ids</a>
</li>
<li<%= sidebar_current("docs-datasource-tfe-oauth-client") %>>
<a href="/docs/providers/tfe/d/oauth_client.html">tfe_oauth_client</a>
</li>
koikonom marked this conversation as resolved.
Show resolved Hide resolved
</ul>
</li>

Expand Down