Skip to content

Commit

Permalink
add test for provider
Browse files Browse the repository at this point in the history
  • Loading branch information
acwest committed Oct 28, 2024
1 parent 8144093 commit b833775
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 32 deletions.
20 changes: 20 additions & 0 deletions docs/data-sources/provider.md
Original file line number Diff line number Diff line change
@@ -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 generated by tfplugindocs -->
## Schema

### Read-Only

- `provider_version` (String) The version of the provider.


11 changes: 0 additions & 11 deletions internal/framework/auth0/encryptionkeymanager/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
54 changes: 54 additions & 0 deletions internal/framework/provider/data_source.go
Original file line number Diff line number Diff line change
@@ -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())...)
}
29 changes: 29 additions & 0 deletions internal/framework/provider/data_source_test.go
Original file line number Diff line number Diff line change
@@ -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"),
),
},
},
})
}
4 changes: 3 additions & 1 deletion internal/framework/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
20 changes: 0 additions & 20 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package provider

import (
"context"
"fmt"
"os"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit b833775

Please sign in to comment.