diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f6d81174..a5d1a6368 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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: diff --git a/tfe/data_source_oauth_client.go b/tfe/data_source_oauth_client.go new file mode 100644 index 000000000..97a878bea --- /dev/null +++ b/tfe/data_source_oauth_client.go @@ -0,0 +1,60 @@ +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, + }, + "api_url": { + Type: schema.TypeString, + Computed: true, + }, + "http_url": { + Type: schema.TypeString, + Computed: true, + }, + "oauth_token_id": { + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func dataSourceTFEOAuthClientRead(d *schema.ResourceData, meta interface{}) error { + ctx := context.TODO() + 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) + } + + d.SetId(oc.ID) + _ = d.Set("api_url", oc.APIURL) + _ = d.Set("http_url", oc.HTTPURL) + + switch len(oc.OAuthTokens) { + case 0: + d.Set("oauth_token_id", "") + case 1: + d.Set("oauth_token_id", oc.OAuthTokens[0].ID) + default: + return fmt.Errorf("Unexpected number of OAuth tokens: %d", len(oc.OAuthTokens)) + } + + return nil +} diff --git a/tfe/data_source_oauth_client_test.go b/tfe/data_source_oauth_client_test.go new file mode 100644 index 000000000..e6dbee90e --- /dev/null +++ b/tfe/data_source_oauth_client_test.go @@ -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", "oauth_token_id"), + ), + }, + }, + }) +} + +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) +} diff --git a/tfe/provider.go b/tfe/provider.go index ba9b6c8e3..b425be492 100644 --- a/tfe/provider.go +++ b/tfe/provider.go @@ -62,6 +62,7 @@ func Provider() terraform.ResourceProvider { }, DataSourcesMap: map[string]*schema.Resource{ + "tfe_oauth_client": dataSourceTFEOAuthClient(), "tfe_organization_membership": dataSourceTFEOrganizationMembership(), "tfe_ssh_key": dataSourceTFESSHKey(), "tfe_team": dataSourceTFETeam(), diff --git a/website/docs/d/oauth_client.html.markdown b/website/docs/d/oauth_client.html.markdown new file mode 100644 index 000000000..a934ff74c --- /dev/null +++ b/website/docs/d/oauth_client.html.markdown @@ -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. This will match `oauth_client_id`. +* `api_url` - The client's API URL. +* `http_url` - The client's HTTP URL. +* `oauth_token_id` - The ID of the OAuth token associated with the OAuth client. +* `ssh_key` - The SSH key assigned to the OAuth client. diff --git a/website/tfe.erb b/website/tfe.erb index 06a1ea53b..6b875f53f 100644 --- a/website/tfe.erb +++ b/website/tfe.erb @@ -13,6 +13,9 @@ > Data Sources