From ff940bcfbecce2aa3e602798e59348905210ebad Mon Sep 17 00:00:00 2001 From: Devin Brenton Date: Fri, 22 Nov 2024 15:50:00 -0500 Subject: [PATCH] Move clients data source files into client package --- .../data_source_clients.go} | 11 +++--- .../data_source_clients_test.go} | 12 ++++--- internal/auth0/client/flatten.go | 29 +++++++++++++++ internal/auth0/clients/flatten.go | 35 ------------------- internal/provider/provider.go | 3 +- 5 files changed, 42 insertions(+), 48 deletions(-) rename internal/auth0/{clients/data_source.go => client/data_source_clients.go} (92%) rename internal/auth0/{clients/data_source_test.go => client/data_source_clients_test.go} (86%) delete mode 100644 internal/auth0/clients/flatten.go diff --git a/internal/auth0/clients/data_source.go b/internal/auth0/client/data_source_clients.go similarity index 92% rename from internal/auth0/clients/data_source.go rename to internal/auth0/client/data_source_clients.go index 34806fe2..63488a27 100644 --- a/internal/auth0/clients/data_source.go +++ b/internal/auth0/client/data_source_clients.go @@ -1,4 +1,4 @@ -package clients +package client import ( "context" @@ -11,12 +11,11 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" - "github.com/auth0/terraform-provider-auth0/internal/auth0/client" "github.com/auth0/terraform-provider-auth0/internal/config" ) -// NewDataSource will return a new auth0_clients data source. -func NewDataSource() *schema.Resource { +// NewClientsDataSource will return a new auth0_clients data source. +func NewClientsDataSource() *schema.Resource { return &schema.Resource{ ReadContext: readClientsForDataSource, Description: "Data source to retrieve a list of Auth0 application clients with optional filtering.", @@ -32,7 +31,7 @@ func NewDataSource() *schema.Resource { Description: "Filter clients by application types.", Elem: &schema.Schema{ Type: schema.TypeString, - ValidateFunc: validation.StringInSlice(client.ValidAppTypes, false), + ValidateFunc: validation.StringInSlice(ValidAppTypes, false), }, }, "is_first_party": { @@ -53,7 +52,7 @@ func NewDataSource() *schema.Resource { } func coreClientDataSourceSchema() map[string]*schema.Schema { - clientSchema := client.NewDataSource().Schema + clientSchema := dataSourceSchema() // Remove unused fields from the client schema. fieldsToRemove := []string{ diff --git a/internal/auth0/clients/data_source_test.go b/internal/auth0/client/data_source_clients_test.go similarity index 86% rename from internal/auth0/clients/data_source_test.go rename to internal/auth0/client/data_source_clients_test.go index d4c66bf1..b28b61fb 100644 --- a/internal/auth0/clients/data_source_test.go +++ b/internal/auth0/client/data_source_clients_test.go @@ -1,4 +1,4 @@ -package clients_test +package client_test import ( "fmt" @@ -10,7 +10,7 @@ import ( "github.com/auth0/terraform-provider-auth0/internal/acctest" ) -const testAccGivenAClient = ` +const testAccGivenSomeClients = ` resource "auth0_client" "my_client_1" { name = "Acceptance Test 1 - {{.testName}}" app_type = "non_interactive" @@ -66,7 +66,7 @@ func TestAccDataClientsNameFilter(t *testing.T) { PreventPostDestroyRefresh: true, Steps: []resource.TestStep{ { - Config: acctest.ParseTestName(testAccGivenAClient+testAccDataClientsWithNameFilter, t.Name()), + Config: acctest.ParseTestName(testAccGivenSomeClients+testAccDataClientsWithNameFilter, t.Name()), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrSet("data.auth0_clients.test", "id"), resource.TestCheckResourceAttr("data.auth0_clients.test", "clients.#", "2"), @@ -87,7 +87,7 @@ func TestAccDataClientsAppTypeFilter(t *testing.T) { PreventPostDestroyRefresh: true, Steps: []resource.TestStep{ { - Config: acctest.ParseTestName(testAccGivenAClient+testAccDataClientsWithAppTypeFilter, t.Name()), + Config: acctest.ParseTestName(testAccGivenSomeClients+testAccDataClientsWithAppTypeFilter, t.Name()), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrSet("data.auth0_clients.test", "id"), resource.TestCheckResourceAttr("data.auth0_clients.test", "clients.#", "1"), @@ -104,10 +104,12 @@ func TestAccDataClientsIsFirstPartyFilter(t *testing.T) { PreventPostDestroyRefresh: true, Steps: []resource.TestStep{ { - Config: acctest.ParseTestName(testAccGivenAClient+testAccDataClientsWithIsFirstPartyFilter, t.Name()), + Config: acctest.ParseTestName(testAccGivenSomeClients+testAccDataClientsWithIsFirstPartyFilter, t.Name()), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrSet("data.auth0_clients.test", "id"), + resource.TestCheckResourceAttr("data.auth0_clients.test", "clients.#", "1"), resource.TestCheckResourceAttr("data.auth0_clients.test", "clients.0.is_first_party", "true"), + resource.TestCheckResourceAttr("data.auth0_clients.test", "clients.0.name", fmt.Sprintf("Acceptance Test 1 - %v", t.Name())), ), }, }, diff --git a/internal/auth0/client/flatten.go b/internal/auth0/client/flatten.go index ec4ffffd..fc25e3e8 100644 --- a/internal/auth0/client/flatten.go +++ b/internal/auth0/client/flatten.go @@ -801,3 +801,32 @@ func flattenCredentials( return stateCredentials, nil } + +func flattenClientList(data *schema.ResourceData, clients []*management.Client) error { + if clients == nil { + return data.Set("clients", make([]map[string]interface{}, 0)) + } + + clientList := make([]map[string]interface{}, 0, len(clients)) + for _, client := range clients { + clientMap := map[string]interface{}{ + "client_id": client.GetClientID(), + "client_secret": client.GetClientSecret(), + "name": client.GetName(), + "description": client.GetDescription(), + "app_type": client.GetAppType(), + "is_first_party": client.GetIsFirstParty(), + "is_token_endpoint_ip_header_trusted": client.GetIsTokenEndpointIPHeaderTrusted(), + "callbacks": client.GetCallbacks(), + "allowed_logout_urls": client.GetAllowedLogoutURLs(), + "allowed_origins": client.GetAllowedOrigins(), + "allowed_clients": client.GetAllowedClients(), + "grant_types": client.GetGrantTypes(), + "web_origins": client.GetWebOrigins(), + "client_metadata": client.GetClientMetadata(), + } + clientList = append(clientList, clientMap) + } + + return data.Set("clients", clientList) +} diff --git a/internal/auth0/clients/flatten.go b/internal/auth0/clients/flatten.go deleted file mode 100644 index c60bd687..00000000 --- a/internal/auth0/clients/flatten.go +++ /dev/null @@ -1,35 +0,0 @@ -package clients - -import ( - "github.com/auth0/go-auth0/management" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" -) - -func flattenClientList(data *schema.ResourceData, clients []*management.Client) error { - if clients == nil { - return data.Set("clients", make([]map[string]interface{}, 0)) - } - - clientList := make([]map[string]interface{}, 0, len(clients)) - for _, client := range clients { - clientMap := map[string]interface{}{ - "client_id": client.GetClientID(), - "client_secret": client.GetClientSecret(), - "name": client.GetName(), - "description": client.GetDescription(), - "app_type": client.GetAppType(), - "is_first_party": client.GetIsFirstParty(), - "is_token_endpoint_ip_header_trusted": client.GetIsTokenEndpointIPHeaderTrusted(), - "callbacks": client.GetCallbacks(), - "allowed_logout_urls": client.GetAllowedLogoutURLs(), - "allowed_origins": client.GetAllowedOrigins(), - "allowed_clients": client.GetAllowedClients(), - "grant_types": client.GetGrantTypes(), - "web_origins": client.GetWebOrigins(), - "client_metadata": client.GetClientMetadata(), - } - clientList = append(clientList, clientMap) - } - - return data.Set("clients", clientList) -} diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 0b63f33d..7369bb9b 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -15,7 +15,6 @@ import ( "github.com/auth0/terraform-provider-auth0/internal/auth0/attackprotection" "github.com/auth0/terraform-provider-auth0/internal/auth0/branding" "github.com/auth0/terraform-provider-auth0/internal/auth0/client" - "github.com/auth0/terraform-provider-auth0/internal/auth0/clients" "github.com/auth0/terraform-provider-auth0/internal/auth0/connection" "github.com/auth0/terraform-provider-auth0/internal/auth0/customdomain" "github.com/auth0/terraform-provider-auth0/internal/auth0/email" @@ -156,7 +155,7 @@ func New() *schema.Provider { "auth0_branding": branding.NewDataSource(), "auth0_branding_theme": branding.NewThemeDataSource(), "auth0_client": client.NewDataSource(), - "auth0_clients": clients.NewDataSource(), + "auth0_clients": client.NewClientsDataSource(), "auth0_connection": connection.NewDataSource(), "auth0_connection_scim_configuration": connection.NewSCIMConfigurationDataSource(), "auth0_custom_domain": customdomain.NewDataSource(),