Skip to content

Commit

Permalink
Merge pull request #26027 from hashicorp/f-terraform-plugin-framework…
Browse files Browse the repository at this point in the history
…-simple-data-source

d/aws_arn: Migrate to Terraform Plugin Framework
  • Loading branch information
ewbankkit authored Aug 1, 2022
2 parents e162cdf + 06c1276 commit 8ad8580
Show file tree
Hide file tree
Showing 32 changed files with 629 additions and 109 deletions.
20 changes: 15 additions & 5 deletions internal/acctest/acctest.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,12 @@ var Provider *schema.Provider
var testAccProviderConfigure sync.Once

func init() {
Provider = provider.Provider()
var err error
Provider, err = provider.New(context.Background())

if err != nil {
panic(err)
}

// Always allocate a new provider instance each invocation, otherwise gRPC
// ProviderConfigure() can overwrite configuration during concurrent testing.
Expand All @@ -117,11 +122,16 @@ func protoV5ProviderFactoriesInit(providerNames ...string) map[string]func() (tf
return factories
}

func factoriesInit(providers *[]*schema.Provider, providerNames []string) map[string]func() (*schema.Provider, error) {
func factoriesInit(t *testing.T, providers *[]*schema.Provider, providerNames []string) map[string]func() (*schema.Provider, error) {
ctx := context.Background()
var factories = make(map[string]func() (*schema.Provider, error), len(providerNames))

for _, name := range providerNames {
p := provider.Provider()
p, err := provider.New(ctx)

if err != nil {
t.Fatal(err)
}

factories[name] = func() (*schema.Provider, error) { //nolint:unparam
return p, nil
Expand All @@ -140,8 +150,8 @@ func factoriesInit(providers *[]*schema.Provider, providerNames []string) map[st
// For cross-region testing: Typically paired with PreCheckMultipleRegion and ConfigAlternateRegionProvider.
//
// For cross-account testing: Typically paired with PreCheckAlternateAccount and ConfigAlternateAccountProvider.
func FactoriesAlternate(providers *[]*schema.Provider) map[string]func() (*schema.Provider, error) {
return factoriesInit(providers, []string{
func FactoriesAlternate(t *testing.T, providers *[]*schema.Provider) map[string]func() (*schema.Provider, error) {
return factoriesInit(t, providers, []string{
ProviderName,
ProviderNameAlternate,
})
Expand Down
14 changes: 10 additions & 4 deletions internal/acctest/ec2_classic.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,22 @@ func PreCheckEC2Classic(t *testing.T) {
// Since we are outside the scope of the Terraform configuration we must
// call Configure() to properly initialize the provider configuration.
testAccProviderEc2ClassicConfigure.Do(func() {
ProviderEC2Classic = provider.Provider()
ctx := context.Background()
var err error
ProviderEC2Classic, err = provider.New(ctx)

if err != nil {
t.Fatal(err)
}

config := map[string]interface{}{
"region": EC2ClassicRegion(),
}

err := ProviderEC2Classic.Configure(context.Background(), terraform.NewResourceConfigRaw(config))
diags := ProviderEC2Classic.Configure(ctx, terraform.NewResourceConfigRaw(config))

if err != nil {
t.Fatal(err)
if diags.HasError() {
t.Fatal(diags)
}
})

Expand Down
22 changes: 16 additions & 6 deletions internal/acctest/provider_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package acctest_test

import (
"context"
"fmt"
"reflect"
"strings"
Expand All @@ -19,13 +20,17 @@ import (
)

func TestProvider(t *testing.T) {
if err := provider.Provider().InternalValidate(); err != nil {
t.Fatalf("err: %s", err)
p, err := provider.New(context.Background())

if err != nil {
t.Fatal(err)
}
}

func TestProvider_impl(t *testing.T) {
var _ *schema.Provider = provider.Provider()
err = p.InternalValidate()

if err != nil {
t.Fatal(err)
}
}

func TestReverseDNS(t *testing.T) {
Expand Down Expand Up @@ -523,7 +528,12 @@ func TestAccProvider_AssumeRole_empty(t *testing.T) {

// Replaces FactoriesInternal.
func testAccProviderFactoriesInternal(t *testing.T, v **schema.Provider) map[string]func() (*schema.Provider, error) { //nolint:unparam
p := provider.Provider()
p, err := provider.New(context.Background())

if err != nil {
t.Fatal(err)
}

*v = p

return map[string]func() (*schema.Provider, error){
Expand Down
7 changes: 6 additions & 1 deletion internal/provider/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ import (
// ProtoV5ProviderServerFactory returns a muxed terraform-plugin-go protocol v5 provider factory function.
// This factory function is suitable for use with the terraform-plugin-go Serve function.
func ProtoV5ProviderServerFactory(ctx context.Context) (func() tfprotov5.ProviderServer, error) {
primary := Provider()
primary, err := New(ctx)

if err != nil {
return nil, err
}

servers := []func() tfprotov5.ProviderServer{
primary.GRPCProvider,
providerserver.NewProtocol5(fwprovider.New(primary)),
Expand Down
1 change: 1 addition & 0 deletions internal/provider/fwprovider/duration.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// TODO: Move this to a shared 'types' package.
package fwprovider

import (
Expand Down
1 change: 1 addition & 0 deletions internal/provider/fwprovider/duration_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// TODO: Move this to a shared 'types' package.
package fwprovider_test

import (
Expand Down
14 changes: 13 additions & 1 deletion internal/provider/fwprovider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import (
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-provider-aws/internal/service/meta"
"github.com/hashicorp/terraform-provider-aws/names"
)

// New returns a new Terraform Plugin Framework-style provider instance.
// New returns a new, initialized Terraform Plugin Framework-style provider instance.
// The provider instance is fully configured once the `Configure` method has been called.
func New(primary interface{ Meta() interface{} }) tfsdk.Provider {
return &provider{
Primary: primary,
Expand Down Expand Up @@ -308,6 +310,16 @@ func (p *provider) GetDataSources(ctx context.Context) (map[string]tfsdk.DataSou
var diags diag.Diagnostics
dataSources := make(map[string]tfsdk.DataSourceType)

// TODO: This should be done via service-level self-registration and initializatin in the primary provider.
t, err := meta.NewDataSourceARNType(ctx)

if err != nil {
diags.AddError("UhOh", err.Error())
return nil, diags
}

dataSources["aws_arn"] = t

return dataSources, diags
}

Expand Down
12 changes: 5 additions & 7 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,9 @@ import (
"github.com/hashicorp/terraform-provider-aws/names"
)

// Provider returns a *schema.Provider.
func Provider() *schema.Provider {
// TODO: Move the validation to this, requires conditional schemas
// TODO: Move the configuration to this, requires validation

// New returns a new, initialized Terraform Plugin SDK v2-style provider instance.
// The provider instance is fully configured once the `ConfigureContextFunc` has been called.
func New(_ context.Context) (*schema.Provider, error) {
// The actual provider
provider := &schema.Provider{
// This schema must match exactly the Terraform Protocol v6 (Terraform Plugin Framework) provider's schema.
Expand Down Expand Up @@ -740,7 +738,7 @@ func Provider() *schema.Provider {
"aws_location_route_calculator": location.DataSourceRouteCalculator(),
"aws_location_tracker": location.DataSourceTracker(),

"aws_arn": meta.DataSourceARN(),
// "aws_arn": meta.DataSourceARN(), // Now implemented using Terraform Plugin Framework.
"aws_billing_service_account": meta.DataSourceBillingServiceAccount(),
"aws_default_tags": meta.DataSourceDefaultTags(),
"aws_ip_ranges": meta.DataSourceIPRanges(),
Expand Down Expand Up @@ -2119,7 +2117,7 @@ func Provider() *schema.Provider {
return providerConfigure(ctx, d, terraformVersion)
}

return provider
return provider, nil
}

func providerConfigure(ctx context.Context, d *schema.ResourceData, terraformVersion string) (interface{}, diag.Diagnostics) {
Expand Down
10 changes: 8 additions & 2 deletions internal/service/apigateway/acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,19 @@ func testAccPreCheckEdgeDomainName(t *testing.T) {
// Since we are outside the scope of the Terraform configuration we must
// call Configure() to properly initialize the provider configuration.
testAccProviderEdgeDomainNameConfigure.Do(func() {
testAccProviderEdgeDomainName = provider.Provider()
ctx := context.Background()
var err error
testAccProviderEdgeDomainName, err = provider.New(ctx)

if err != nil {
t.Fatal(err)
}

config := map[string]interface{}{
"region": region,
}

diags := testAccProviderEdgeDomainName.Configure(context.Background(), terraform.NewResourceConfigRaw(config))
diags := testAccProviderEdgeDomainName.Configure(ctx, terraform.NewResourceConfigRaw(config))

if diags != nil && diags.HasError() {
for _, d := range diags {
Expand Down
10 changes: 8 additions & 2 deletions internal/service/cognitoidp/acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,19 @@ func testAccPreCheckUserPoolCustomDomain(t *testing.T) {
// Since we are outside the scope of the Terraform configuration we must
// call Configure() to properly initialize the provider configuration.
testAccProviderCognitoUserPoolCustomDomainConfigure.Do(func() {
testAccProviderCognitoUserPoolCustomDomain = provider.Provider()
ctx := context.Background()
var err error
testAccProviderCognitoUserPoolCustomDomain, err = provider.New(ctx)

if err != nil {
t.Fatal(err)
}

config := map[string]interface{}{
"region": region,
}

diags := testAccProviderCognitoUserPoolCustomDomain.Configure(context.Background(), terraform.NewResourceConfigRaw(config))
diags := testAccProviderCognitoUserPoolCustomDomain.Configure(ctx, terraform.NewResourceConfigRaw(config))

if diags != nil && diags.HasError() {
for _, d := range diags {
Expand Down
10 changes: 8 additions & 2 deletions internal/service/cur/acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,19 @@ func testAccPreCheck(t *testing.T) {
// Since we are outside the scope of the Terraform configuration we must
// call Configure() to properly initialize the provider configuration.
testAccProviderCurConfigure.Do(func() {
testAccProviderCur = provider.Provider()
ctx := context.Background()
var err error
testAccProviderCur, err = provider.New(ctx)

if err != nil {
t.Fatal(err)
}

config := map[string]interface{}{
"region": testAccGetRegion(),
}

diags := testAccProviderCur.Configure(context.Background(), terraform.NewResourceConfigRaw(config))
diags := testAccProviderCur.Configure(ctx, terraform.NewResourceConfigRaw(config))

if diags != nil && diags.HasError() {
for _, d := range diags {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestAccDirectConnectConnectionConfirmation_basic(t *testing.T) {
acctest.PreCheckAlternateAccount(t)
},
ErrorCheck: acctest.ErrorCheck(t, directconnect.EndpointsID),
ProviderFactories: acctest.FactoriesAlternate(&providers),
ProviderFactories: acctest.FactoriesAlternate(t, &providers),
CheckDestroy: testAccCheckHostedConnectionDestroy(altProviderFunc),
Steps: []resource.TestStep{
{
Expand Down
2 changes: 1 addition & 1 deletion internal/service/docdb/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ func TestAccDocDBCluster_GlobalClusterIdentifier_PrimarySecondaryClusters(t *tes
testAccPreCheckGlobalCluster(t)
},
ErrorCheck: acctest.ErrorCheck(t, docdb.EndpointsID),
ProviderFactories: acctest.FactoriesAlternate(&providers),
ProviderFactories: acctest.FactoriesAlternate(t, &providers),
CheckDestroy: testAccCheckClusterDestroy,
Steps: []resource.TestStep{
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestAccVPCPeeringConnectionAccepter_differentRegionSameAccount(t *testing.T
acctest.PreCheckMultipleRegion(t, 2)
},
ErrorCheck: acctest.ErrorCheck(t, ec2.EndpointsID),
ProviderFactories: acctest.FactoriesAlternate(&providers),
ProviderFactories: acctest.FactoriesAlternate(t, &providers),
CheckDestroy: testAccVPCPeeringConnectionAccepterDestroy,
Steps: []resource.TestStep{
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func TestAccVPCPeeringConnectionOptions_differentRegionSameAccount(t *testing.T)
acctest.PreCheckMultipleRegion(t, 2)
},
ErrorCheck: acctest.ErrorCheck(t, ec2.EndpointsID),
ProviderFactories: acctest.FactoriesAlternate(&providers),
ProviderFactories: acctest.FactoriesAlternate(t, &providers),
CheckDestroy: testAccCheckVPCPeeringConnectionDestroy,
Steps: []resource.TestStep{
{
Expand Down
4 changes: 2 additions & 2 deletions internal/service/efs/replication_configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestAccEFSReplicationConfiguration_disappears(t *testing.T) {
acctest.PreCheckMultipleRegion(t, 2)
},
ErrorCheck: acctest.ErrorCheck(t, efs.EndpointsID),
ProviderFactories: acctest.FactoriesAlternate(&providers),
ProviderFactories: acctest.FactoriesAlternate(t, &providers),
CheckDestroy: acctest.CheckWithProviders(testAccCheckReplicationConfigurationDestroy, &providers),
Steps: []resource.TestStep{
{
Expand Down Expand Up @@ -98,7 +98,7 @@ func TestAccEFSReplicationConfiguration_allAttributes(t *testing.T) {
acctest.PreCheckMultipleRegion(t, 2)
},
ErrorCheck: acctest.ErrorCheck(t, efs.EndpointsID),
ProviderFactories: acctest.FactoriesAlternate(&providers),
ProviderFactories: acctest.FactoriesAlternate(t, &providers),
CheckDestroy: acctest.CheckWithProviders(testAccCheckReplicationConfigurationDestroy, &providers),
Steps: []resource.TestStep{
{
Expand Down
10 changes: 8 additions & 2 deletions internal/service/fms/acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,19 @@ func testAccPreCheckAdmin(t *testing.T) {
// Since we are outside the scope of the Terraform configuration we must
// call Configure() to properly initialize the provider configuration.
testAccProviderAdminConfigure.Do(func() {
testAccProviderAdmin = provider.Provider()
ctx := context.Background()
var err error
testAccProviderAdmin, err = provider.New(ctx)

if err != nil {
t.Fatal(err)
}

config := map[string]interface{}{
"region": testAccGetAdminRegion(),
}

diags := testAccProviderAdmin.Configure(context.Background(), terraform.NewResourceConfigRaw(config))
diags := testAccProviderAdmin.Configure(ctx, terraform.NewResourceConfigRaw(config))

if diags != nil && diags.HasError() {
for _, d := range diags {
Expand Down
15 changes: 9 additions & 6 deletions internal/service/lightsail/acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ var testAccProviderLightsailDomain *schema.Provider
// testAccProviderLightsailDomainConfigure ensures the provider is only configured once
var testAccProviderLightsailDomainConfigure sync.Once

// Prevent panic with acctest.CheckResourceDisappears
func init() {
testAccProviderLightsailDomain = provider.Provider()
}

// testAccPreCheckDomain verifies AWS credentials and that Lightsail Domains is supported
func testAccPreCheckDomain(t *testing.T) {
acctest.PreCheckPartitionHasService(lightsail.EndpointsID, t)
Expand All @@ -50,11 +45,19 @@ func testAccPreCheckDomain(t *testing.T) {
// Since we are outside the scope of the Terraform configuration we must
// call Configure() to properly initialize the provider configuration.
testAccProviderLightsailDomainConfigure.Do(func() {
ctx := context.Background()
var err error
testAccProviderLightsailDomain, err = provider.New(ctx)

if err != nil {
t.Fatal(err)
}

config := map[string]interface{}{
"region": region,
}

diags := testAccProviderLightsailDomain.Configure(context.Background(), terraform.NewResourceConfigRaw(config))
diags := testAccProviderLightsailDomain.Configure(ctx, terraform.NewResourceConfigRaw(config))

if diags != nil && diags.HasError() {
for _, d := range diags {
Expand Down
Loading

0 comments on commit 8ad8580

Please sign in to comment.