From b83377557c0f8078bb7159924ac69a7b17d23a8d Mon Sep 17 00:00:00 2001 From: "A. Craig West" Date: Mon, 28 Oct 2024 17:28:22 -0400 Subject: [PATCH] add test for provider --- docs/data-sources/provider.md | 20 +++++++ .../encryptionkeymanager/resource_test.go | 11 ---- internal/framework/provider/data_source.go | 54 +++++++++++++++++++ .../framework/provider/data_source_test.go | 29 ++++++++++ internal/framework/provider/provider.go | 4 +- internal/provider/provider.go | 20 ------- 6 files changed, 106 insertions(+), 32 deletions(-) create mode 100644 docs/data-sources/provider.md create mode 100644 internal/framework/provider/data_source.go create mode 100644 internal/framework/provider/data_source_test.go diff --git a/docs/data-sources/provider.md b/docs/data-sources/provider.md new file mode 100644 index 00000000..aab38ae3 --- /dev/null +++ b/docs/data-sources/provider.md @@ -0,0 +1,20 @@ +--- +page_title: "Data Source: auth0_provider" +description: |- + A data source for retrieving basic information about the provider. +--- + +# Data Source: auth0_provider + +A data source for retrieving basic information about the provider. + + + + +## Schema + +### Read-Only + +- `provider_version` (String) The version of the provider. + + diff --git a/internal/framework/auth0/encryptionkeymanager/resource_test.go b/internal/framework/auth0/encryptionkeymanager/resource_test.go index 7e5347c5..73dfb6d8 100644 --- a/internal/framework/auth0/encryptionkeymanager/resource_test.go +++ b/internal/framework/auth0/encryptionkeymanager/resource_test.go @@ -80,17 +80,6 @@ resource "auth0_encryption_key_manager" "my_key_manager" { } ` -func TestAccFrameworkEncryptionKeyManagerEmpty(t *testing.T) { - resource.UnitTest(t, resource.TestCase{ - ProtoV6ProviderFactories: acctest.TestProviderFactories(), - Steps: []resource.TestStep{ - { - Config: `resource "auth0_encryption_key_manager" "my_key_manager" {}`, - }, - }, - }) -} - func TestAccFrameworkEncryptionKeyManagerRotation(t *testing.T) { initialKey := make(map[string]string) firstRotationKey := make(map[string]string) diff --git a/internal/framework/provider/data_source.go b/internal/framework/provider/data_source.go new file mode 100644 index 00000000..a425ebc9 --- /dev/null +++ b/internal/framework/provider/data_source.go @@ -0,0 +1,54 @@ +package provider + +import ( + "context" + + "github.com/hashicorp/terraform-plugin-framework/datasource" + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" + "github.com/hashicorp/terraform-plugin-framework/path" + + "github.com/auth0/terraform-provider-auth0/internal/config" +) + +type providerDataSource struct { + cfg *config.Config +} + +// NewDataSource will return a new auth0_provider data source. +func NewDataSource() datasource.DataSource { + return &providerDataSource{} +} + +// Configure will be called by the framework to configure the auth0_provider data source. +func (r *providerDataSource) Configure(_ context.Context, request datasource.ConfigureRequest, _ *datasource.ConfigureResponse) { + if request.ProviderData != nil { + r.cfg = request.ProviderData.(*config.Config) + } +} + +// Metadata will be called by the framework to get the type name for the auth0_encryption_key_manager datasource. +func (r *providerDataSource) Metadata(_ context.Context, _ datasource.MetadataRequest, response *datasource.MetadataResponse) { + response.TypeName = "auth0_provider" +} + +// Schema will be called by the framework to get the schema name for the auth0_encryption_key_manager datasource. +func (r *providerDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, response *datasource.SchemaResponse) { + if response != nil { + response.Schema = schema.Schema{ + Description: "A data source for retrieving basic information about the provider.", + MarkdownDescription: "A data source for retrieving basic information about the provider.", + Attributes: map[string]schema.Attribute{ + "provider_version": schema.StringAttribute{ + Computed: true, + Description: "The version of the provider. ", + MarkdownDescription: "The version of the provider. ", + }, + }, + } + } +} + +// Read will be called by the framework to read an auth0_encryption_key_manager data source. +func (r *providerDataSource) Read(ctx context.Context, _ datasource.ReadRequest, response *datasource.ReadResponse) { + response.Diagnostics.Append(response.State.SetAttribute(ctx, path.Root("provider_version"), config.GetProviderVersion())...) +} diff --git a/internal/framework/provider/data_source_test.go b/internal/framework/provider/data_source_test.go new file mode 100644 index 00000000..bf1e66bc --- /dev/null +++ b/internal/framework/provider/data_source_test.go @@ -0,0 +1,29 @@ +package provider_test + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-testing/helper/resource" + + "github.com/auth0/terraform-provider-auth0/internal/acctest" +) + +const testAccDataSourceProvider = ` +data "auth0_provider" "my_provider" { +} +` + +func TestAccFrameworkDataSourceProvider(t *testing.T) { + resource.UnitTest(t, resource.TestCase{ + // This is primarily here to allow us to test the regular provider instantiation flow. + ProtoV6ProviderFactories: acctest.TestProviderFactories(), + Steps: []resource.TestStep{ + { + Config: acctest.ParseTestName(testAccDataSourceProvider, t.Name()), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("data.auth0_provider.my_provider", "provider_version"), + ), + }, + }, + }) +} diff --git a/internal/framework/provider/provider.go b/internal/framework/provider/provider.go index e5103de2..4b1dd13c 100644 --- a/internal/framework/provider/provider.go +++ b/internal/framework/provider/provider.go @@ -104,7 +104,9 @@ func (p *Auth0Provider) Configure(ctx context.Context, request provider.Configur // DataSources will be called by the framework to configure the auth0 provider data sources. func (p *Auth0Provider) DataSources(_ context.Context) []func() datasource.DataSource { - return []func() datasource.DataSource{} + return []func() datasource.DataSource{ + NewDataSource, + } } // Resources will be called by the framework to configure the auth0 provider resources. diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 9f26bd55..28075ac7 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -2,7 +2,6 @@ package provider import ( "context" - "fmt" "os" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" @@ -175,25 +174,6 @@ func New() *schema.Provider { } provider.ConfigureContextFunc = func(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) { - var diags diag.Diagnostics - - // Check required environment variables. - requiredEnvVars := []string{"AUTH0_DOMAIN", "AUTH0_CLIENT_ID", "AUTH0_CLIENT_SECRET"} - for _, varName := range requiredEnvVars { - value, exists := os.LookupEnv(varName) - if !exists || value == "" { - diags = append(diags, diag.Diagnostic{ - Severity: diag.Error, - Summary: fmt.Sprintf("Missing environment variable: %s", varName), - Detail: fmt.Sprintf("The environment variable %s must be set and cannot be empty.", varName), - }) - } - } - - if len(diags) > 0 { - return nil, diags - } - // Call the original configuration function if no errors. return config.ConfigureProvider(&provider.TerraformVersion)(ctx, d) }