This repository has been archived by the owner on Mar 8, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 149
DXCDT-20: Client data source #511
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7e01459
Reintroducing client data source that was originally written by Yinza…
9ec55d3
Tweaking and re-integrating client data source back into provider
051e42c
Removing unnecessary checkDataSourceStateMatchesResourceState function
070c51e
Updating documentation
4905251
Simplifying the newDataClient generator
56121f7
Reusing existing testAccClientConfig client resource configuration
b24dd52
Fixing integration tests after switching back to the testAccClientConfig
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package auth0 | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"gopkg.in/auth0.v5" | ||
"gopkg.in/auth0.v5/management" | ||
) | ||
|
||
func newDataClient() *schema.Resource { | ||
clientSchema := datasourceSchemaFromResourceSchema(newClient().Schema) | ||
delete(clientSchema, "client_secret_rotation_trigger") | ||
|
||
addOptionalFieldsToSchema(clientSchema, "name", "client_id") | ||
|
||
return &schema.Resource{ | ||
Read: readDataClient, | ||
Schema: clientSchema, | ||
} | ||
} | ||
|
||
func readDataClient(d *schema.ResourceData, m interface{}) error { | ||
clientId := auth0.StringValue(String(d, "client_id")) | ||
if clientId != "" { | ||
d.SetId(clientId) | ||
return readClient(d, m) | ||
} | ||
|
||
//If not provided ID, perform looking of client by name | ||
name := auth0.StringValue(String(d, "name")) | ||
if name == "" { | ||
return errors.New("no 'client_id' or 'name' was specified") | ||
} | ||
|
||
api := m.(*management.Management) | ||
clients, err := api.Client.List(management.IncludeFields("client_id", "name")) | ||
if err != nil { | ||
return err | ||
} | ||
for _, client := range clients.Clients { | ||
if auth0.StringValue(client.Name) == name { | ||
clientId = auth0.StringValue(client.ClientID) | ||
d.SetId(clientId) | ||
return readClient(d, m) | ||
} | ||
} | ||
return fmt.Errorf("no client found with 'name' = '%s'", name) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package auth0 | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/alexkappa/terraform-provider-auth0/auth0/internal/random" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/terraform" | ||
) | ||
|
||
const testAccDataClientConfigByName = ` | ||
%v | ||
data auth0_client test { | ||
name = "Acceptance Test - {{.random}}" | ||
} | ||
` | ||
|
||
const testAccDataClientConfigById = ` | ||
%v | ||
data auth0_client test { | ||
client_id = auth0_client.my_client.client_id | ||
} | ||
` | ||
|
||
func TestAccDataClientByName(t *testing.T) { | ||
rand := random.String(6) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
Providers: map[string]terraform.ResourceProvider{ | ||
"auth0": Provider(), | ||
}, | ||
PreventPostDestroyRefresh: true, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: random.Template(testAccClientConfig, rand), // must initialize resource before reading with data source | ||
}, | ||
{ | ||
Config: random.Template(fmt.Sprintf(testAccDataClientConfigByName, testAccClientConfig), rand), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttrSet("data.auth0_client.test", "client_id"), | ||
resource.TestCheckResourceAttr("data.auth0_client.test", "name", fmt.Sprintf("Acceptance Test - %v", rand)), | ||
resource.TestCheckResourceAttr("data.auth0_client.test", "app_type", "non_interactive"), // Arbitrary property selection | ||
resource.TestCheckNoResourceAttr("data.auth0_client.test", "client_secret_rotation_trigger"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccDataClientById(t *testing.T) { | ||
rand := random.String(6) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
Providers: map[string]terraform.ResourceProvider{ | ||
"auth0": Provider(), | ||
}, | ||
PreventPostDestroyRefresh: true, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: random.Template(testAccClientConfig, rand), | ||
}, | ||
{ | ||
Config: random.Template(fmt.Sprintf(testAccDataClientConfigById, testAccClientConfig), rand), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttrSet("data.auth0_client.test", "id"), | ||
resource.TestCheckResourceAttrSet("data.auth0_client.test", "name"), | ||
resource.TestCheckNoResourceAttr("data.auth0_client.test", "client_secret_rotation_trigger"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just checking for the absence of this particular property because we explicitly excluded it from the schema definition. |
||
), | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
--- | ||
layout: "auth0" | ||
page_title: "Data Source: auth0_client" | ||
description: |- | ||
Data source to retrieve a specific Auth0 Application client by 'client_id' or 'name' | ||
--- | ||
|
||
# Data Source: auth0_client | ||
|
||
Data source to retrieve a specific Auth0 Application client by 'client_id' or 'name' | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "auth0_client" "some-client-by-name" { | ||
name = "Name of my Application" | ||
} | ||
data "auth0_client" "some-client-by-id" { | ||
client_id = "abcdefghkijklmnopqrstuvwxyz0123456789" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
At least one of the following arguments required: | ||
|
||
- `client_id` - (Optional) String. client_id of the application. | ||
- `name` - (Optional) String. Name of the application. Ignored if `client_id` is also specified. | ||
|
||
## Attribute Reference | ||
|
||
The client data source possesses the same attributes as the `auth0_client` resource, with the exception of `client_secret_rotation_trigger`. Refer to the [auth0_client resource documentation](../resources/client.md) for a list of returned attributes. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems more reasonable than just duplicating the properties again, it'll just become more of a maintenance liability. However, I'm unsure if this will interfere with other documentation, for example the TF registry docs. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're currently excluding the
client_secret_rotation_trigger
property when building from the client resource schema. This was proposed in #363 and I'm assuming that the exclusion is still valid but need to verify that no other properties should also be excluded.