diff --git a/auth0/data_source_auth0_client.go b/auth0/data_source_auth0_client.go new file mode 100644 index 00000000..ab4cf89b --- /dev/null +++ b/auth0/data_source_auth0_client.go @@ -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) +} diff --git a/auth0/data_source_auth0_client_test.go b/auth0/data_source_auth0_client_test.go new file mode 100644 index 00000000..4eb53306 --- /dev/null +++ b/auth0/data_source_auth0_client_test.go @@ -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"), + ), + }, + }, + }) +} diff --git a/auth0/provider.go b/auth0/provider.go index 3dbf23a2..55411fdd 100644 --- a/auth0/provider.go +++ b/auth0/provider.go @@ -68,6 +68,9 @@ func Provider() *schema.Provider { "auth0_action": newAction(), "auth0_trigger_binding": newTriggerBinding(), }, + DataSourcesMap: map[string]*schema.Resource{ + "auth0_client": newDataClient(), + }, } provider.ConfigureFunc = ConfigureProvider(provider.TerraformVersion) diff --git a/docs/datasources/client.md b/docs/datasources/client.md new file mode 100644 index 00000000..c82e8875 --- /dev/null +++ b/docs/datasources/client.md @@ -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. \ No newline at end of file