From b9225fd923cd51b06784ff9c2b235282831dc6b4 Mon Sep 17 00:00:00 2001 From: Heng Lu Date: Tue, 17 Jan 2023 14:19:04 +0800 Subject: [PATCH 01/11] cache resource providers to reduce expensive api call --- internal/provider/provider.go | 3 +-- internal/resourceproviders/azure.go | 18 +++--------------- internal/resourceproviders/cache.go | 13 ++++++++----- internal/resourceproviders/validation.go | 10 ++++++++-- internal/resourceproviders/validation_test.go | 4 +++- 5 files changed, 23 insertions(+), 25 deletions(-) diff --git a/internal/provider/provider.go b/internal/provider/provider.go index cf135673f96f..d11d480cd07d 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -416,14 +416,13 @@ func providerConfigure(p *schema.Provider) schema.ConfigureContextFunc { if !skipProviderRegistration { // List all the available providers and their registration state to avoid unnecessary // requests. This also lets us check if the provider credentials are correct. - providerList, err := client.Resource.ProvidersClient.List(ctx, nil, "") + availableResourceProviders, err := resourceproviders.CacheSupportedProviders(ctx, client.Resource.ProvidersClient) if err != nil { return nil, diag.Errorf("Unable to list provider registration status, it is possible that this is due to invalid "+ "credentials or the service principal does not have permission to use the Resource Manager API, Azure "+ "error: %s", err) } - availableResourceProviders := providerList.Values() requiredResourceProviders := resourceproviders.Required() if err := resourceproviders.EnsureRegistered(ctx, *client.Resource.ProvidersClient, availableResourceProviders, requiredResourceProviders); err != nil { diff --git a/internal/resourceproviders/azure.go b/internal/resourceproviders/azure.go index 9f4584901001..c44548fff8cc 100644 --- a/internal/resourceproviders/azure.go +++ b/internal/resourceproviders/azure.go @@ -7,22 +7,10 @@ import ( "github.com/Azure/azure-sdk-for-go/profiles/2017-03-09/resources/mgmt/resources" ) -func availableResourceProviders(ctx context.Context, client *resources.ProvidersClient) (*[]string, error) { - providerNames := make([]string, 0) - providers, err := client.ListComplete(ctx, nil, "") +func availableResourceProviders(ctx context.Context, client *resources.ProvidersClient) ([]resources.Provider, error) { + providers, err := client.List(ctx, nil, "") if err != nil { return nil, fmt.Errorf("listing Resource Providers: %+v", err) } - for providers.NotDone() { - provider := providers.Value() - if provider.Namespace != nil { - providerNames = append(providerNames, *provider.Namespace) - } - - if err := providers.NextWithContext(ctx); err != nil { - return nil, err - } - } - - return &providerNames, nil + return providers.Values(), nil } diff --git a/internal/resourceproviders/cache.go b/internal/resourceproviders/cache.go index 37088100d1a8..03d6ce87219f 100644 --- a/internal/resourceproviders/cache.go +++ b/internal/resourceproviders/cache.go @@ -8,16 +8,19 @@ import ( ) // cachedResourceProviders can be (validly) nil - as such this shouldn't be relied on -var cachedResourceProviders *[]string +var cachedResourceProviders *[]resources.Provider // CacheSupportedProviders attempts to retrieve the supported Resource Providers from the Resource Manager API // and caches them, for used in enhanced validation -func CacheSupportedProviders(ctx context.Context, client *resources.ProvidersClient) { +func CacheSupportedProviders(ctx context.Context, client *resources.ProvidersClient) ([]resources.Provider, error) { + if cachedResourceProviders != nil { + return *cachedResourceProviders, nil + } providers, err := availableResourceProviders(ctx, client) if err != nil { log.Printf("[DEBUG] error retrieving providers: %s. Enhanced validation will be unavailable", err) - return + return nil, err } - - cachedResourceProviders = providers + cachedResourceProviders = &providers + return providers, nil } diff --git a/internal/resourceproviders/validation.go b/internal/resourceproviders/validation.go index 78b509efde46..58e79bf8d139 100644 --- a/internal/resourceproviders/validation.go +++ b/internal/resourceproviders/validation.go @@ -41,13 +41,19 @@ func enhancedValidation(i interface{}, k string) ([]string, []error) { found := false for _, provider := range *cachedResourceProviders { - if provider == v { + if provider.Namespace != nil && *provider.Namespace == v { found = true } } if !found { - providersJoined := strings.Join(*cachedResourceProviders, ", ") + providerNames := make([]string, 0) + for _, provider := range *cachedResourceProviders { + if provider.Namespace != nil { + providerNames = append(providerNames, *provider.Namespace) + } + } + providersJoined := strings.Join(providerNames, ", ") return nil, []error{ fmt.Errorf("%q was not found in the list of supported Resource Providers: %q", v, providersJoined), } diff --git a/internal/resourceproviders/validation_test.go b/internal/resourceproviders/validation_test.go index 1cc359a394d8..c4c562b56040 100644 --- a/internal/resourceproviders/validation_test.go +++ b/internal/resourceproviders/validation_test.go @@ -3,7 +3,9 @@ package resourceproviders import ( "testing" + "github.com/Azure/azure-sdk-for-go/profiles/2017-03-09/resources/mgmt/resources" "github.com/hashicorp/terraform-provider-azurerm/internal/features" + "github.com/hashicorp/terraform-provider-azurerm/utils" ) func TestEnhancedValidationDisabled(t *testing.T) { @@ -68,7 +70,7 @@ func TestEnhancedValidationEnabled(t *testing.T) { }, } enhancedEnabled = true - cachedResourceProviders = &[]string{"Microsoft.Compute"} + cachedResourceProviders = &[]resources.Provider{{Namespace: utils.String("Microsoft.Compute")}} defer func() { enhancedEnabled = features.EnhancedValidationEnabled() cachedResourceProviders = nil From 1ac1bef89b6c60be7a72229ce97bc0abb8235ddb Mon Sep 17 00:00:00 2001 From: Heng Lu Date: Tue, 17 Jan 2023 15:34:38 +0800 Subject: [PATCH 02/11] fix golint --- internal/clients/builder.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/internal/clients/builder.go b/internal/clients/builder.go index 190c1dbc79b4..b2c16e457d8f 100644 --- a/internal/clients/builder.go +++ b/internal/clients/builder.go @@ -152,7 +152,10 @@ func Build(ctx context.Context, builder ClientBuilder) (*Client, error) { if features.EnhancedValidationEnabled() { location.CacheSupportedLocations(ctx, env.ResourceManagerEndpoint) - resourceproviders.CacheSupportedProviders(ctx, client.Resource.ProvidersClient) + _, err := resourceproviders.CacheSupportedProviders(ctx, client.Resource.ProvidersClient) + if err != nil { + log.Printf("[DEBUG] error caching providers: %s", err) + } } return &client, nil From f3b7fa61c43b34477677eee265c7fec6edae1776 Mon Sep 17 00:00:00 2001 From: Heng Lu Date: Wed, 18 Jan 2023 10:40:18 +0800 Subject: [PATCH 03/11] provide cache func to allow passing a customized cache method --- internal/clients/builder.go | 4 +- internal/provider/provider.go | 8 +++- internal/resourceproviders/cache.go | 38 ++++++++++++++----- internal/resourceproviders/validation.go | 10 +---- internal/resourceproviders/validation_test.go | 4 +- 5 files changed, 40 insertions(+), 24 deletions(-) diff --git a/internal/clients/builder.go b/internal/clients/builder.go index b2c16e457d8f..1d44757a5fec 100644 --- a/internal/clients/builder.go +++ b/internal/clients/builder.go @@ -152,9 +152,9 @@ func Build(ctx context.Context, builder ClientBuilder) (*Client, error) { if features.EnhancedValidationEnabled() { location.CacheSupportedLocations(ctx, env.ResourceManagerEndpoint) - _, err := resourceproviders.CacheSupportedProviders(ctx, client.Resource.ProvidersClient) + err := resourceproviders.CacheSupportedProviders(resourceproviders.DefaultCacheFunc(ctx, client.Resource.ProvidersClient)) if err != nil { - log.Printf("[DEBUG] error caching providers: %s", err) + log.Printf("[DEBUG] error caching resource providers: %+v", err) } } diff --git a/internal/provider/provider.go b/internal/provider/provider.go index d11d480cd07d..79993cdf9a88 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -416,13 +416,17 @@ func providerConfigure(p *schema.Provider) schema.ConfigureContextFunc { if !skipProviderRegistration { // List all the available providers and their registration state to avoid unnecessary // requests. This also lets us check if the provider credentials are correct. - availableResourceProviders, err := resourceproviders.CacheSupportedProviders(ctx, client.Resource.ProvidersClient) + providerList, err := client.Resource.ProvidersClient.List(ctx, nil, "") if err != nil { return nil, diag.Errorf("Unable to list provider registration status, it is possible that this is due to invalid "+ "credentials or the service principal does not have permission to use the Resource Manager API, Azure "+ "error: %s", err) } - + err = resourceproviders.CacheSupportedProviders(resourceproviders.SavedResourceProvidersCacheFunc(providerList.Values())) + if err != nil { + log.Printf("[DEBUG] error caching resource providers: %+v", err) + } + availableResourceProviders := providerList.Values() requiredResourceProviders := resourceproviders.Required() if err := resourceproviders.EnsureRegistered(ctx, *client.Resource.ProvidersClient, availableResourceProviders, requiredResourceProviders); err != nil { diff --git a/internal/resourceproviders/cache.go b/internal/resourceproviders/cache.go index 03d6ce87219f..4ebae5e8d498 100644 --- a/internal/resourceproviders/cache.go +++ b/internal/resourceproviders/cache.go @@ -8,19 +8,39 @@ import ( ) // cachedResourceProviders can be (validly) nil - as such this shouldn't be relied on -var cachedResourceProviders *[]resources.Provider +var cachedResourceProviders *[]string // CacheSupportedProviders attempts to retrieve the supported Resource Providers from the Resource Manager API // and caches them, for used in enhanced validation -func CacheSupportedProviders(ctx context.Context, client *resources.ProvidersClient) ([]resources.Provider, error) { - if cachedResourceProviders != nil { - return *cachedResourceProviders, nil - } - providers, err := availableResourceProviders(ctx, client) +func CacheSupportedProviders(cacheFunc CacheFunc) error { + providers, err := cacheFunc() if err != nil { log.Printf("[DEBUG] error retrieving providers: %s. Enhanced validation will be unavailable", err) - return nil, err + return err + } + providerNames := make([]string, 0) + for _, provider := range providers { + if provider.Namespace != nil { + providerNames = append(providerNames, *provider.Namespace) + } + } + cachedResourceProviders = &providerNames + return nil +} + +// CacheFunc provides an interface to cache resource providers +type CacheFunc func() ([]resources.Provider, error) + +// DefaultCacheFunc lists all the available resource providers +func DefaultCacheFunc(ctx context.Context, client *resources.ProvidersClient) CacheFunc { + return func() ([]resources.Provider, error) { + return availableResourceProviders(ctx, client) + } +} + +// SavedResourceProvidersCacheFunc returns already saved resource providers +func SavedResourceProvidersCacheFunc(resourceProviders []resources.Provider) CacheFunc { + return func() ([]resources.Provider, error) { + return resourceProviders, nil } - cachedResourceProviders = &providers - return providers, nil } diff --git a/internal/resourceproviders/validation.go b/internal/resourceproviders/validation.go index 58e79bf8d139..78b509efde46 100644 --- a/internal/resourceproviders/validation.go +++ b/internal/resourceproviders/validation.go @@ -41,19 +41,13 @@ func enhancedValidation(i interface{}, k string) ([]string, []error) { found := false for _, provider := range *cachedResourceProviders { - if provider.Namespace != nil && *provider.Namespace == v { + if provider == v { found = true } } if !found { - providerNames := make([]string, 0) - for _, provider := range *cachedResourceProviders { - if provider.Namespace != nil { - providerNames = append(providerNames, *provider.Namespace) - } - } - providersJoined := strings.Join(providerNames, ", ") + providersJoined := strings.Join(*cachedResourceProviders, ", ") return nil, []error{ fmt.Errorf("%q was not found in the list of supported Resource Providers: %q", v, providersJoined), } diff --git a/internal/resourceproviders/validation_test.go b/internal/resourceproviders/validation_test.go index c4c562b56040..1cc359a394d8 100644 --- a/internal/resourceproviders/validation_test.go +++ b/internal/resourceproviders/validation_test.go @@ -3,9 +3,7 @@ package resourceproviders import ( "testing" - "github.com/Azure/azure-sdk-for-go/profiles/2017-03-09/resources/mgmt/resources" "github.com/hashicorp/terraform-provider-azurerm/internal/features" - "github.com/hashicorp/terraform-provider-azurerm/utils" ) func TestEnhancedValidationDisabled(t *testing.T) { @@ -70,7 +68,7 @@ func TestEnhancedValidationEnabled(t *testing.T) { }, } enhancedEnabled = true - cachedResourceProviders = &[]resources.Provider{{Namespace: utils.String("Microsoft.Compute")}} + cachedResourceProviders = &[]string{"Microsoft.Compute"} defer func() { enhancedEnabled = features.EnhancedValidationEnabled() cachedResourceProviders = nil From 558ec294000a4b020e7b7d0df9014ad74adf2b73 Mon Sep 17 00:00:00 2001 From: Heng Lu Date: Wed, 18 Jan 2023 10:44:32 +0800 Subject: [PATCH 04/11] remove unnecessary error log --- internal/clients/builder.go | 5 +---- internal/provider/provider.go | 5 +---- internal/resourceproviders/cache.go | 4 +--- 3 files changed, 3 insertions(+), 11 deletions(-) diff --git a/internal/clients/builder.go b/internal/clients/builder.go index 1d44757a5fec..a247a670d43f 100644 --- a/internal/clients/builder.go +++ b/internal/clients/builder.go @@ -152,10 +152,7 @@ func Build(ctx context.Context, builder ClientBuilder) (*Client, error) { if features.EnhancedValidationEnabled() { location.CacheSupportedLocations(ctx, env.ResourceManagerEndpoint) - err := resourceproviders.CacheSupportedProviders(resourceproviders.DefaultCacheFunc(ctx, client.Resource.ProvidersClient)) - if err != nil { - log.Printf("[DEBUG] error caching resource providers: %+v", err) - } + resourceproviders.CacheSupportedProviders(resourceproviders.DefaultCacheFunc(ctx, client.Resource.ProvidersClient)) } return &client, nil diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 79993cdf9a88..d3274b922c57 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -422,10 +422,7 @@ func providerConfigure(p *schema.Provider) schema.ConfigureContextFunc { "credentials or the service principal does not have permission to use the Resource Manager API, Azure "+ "error: %s", err) } - err = resourceproviders.CacheSupportedProviders(resourceproviders.SavedResourceProvidersCacheFunc(providerList.Values())) - if err != nil { - log.Printf("[DEBUG] error caching resource providers: %+v", err) - } + resourceproviders.CacheSupportedProviders(resourceproviders.SavedResourceProvidersCacheFunc(providerList.Values())) availableResourceProviders := providerList.Values() requiredResourceProviders := resourceproviders.Required() diff --git a/internal/resourceproviders/cache.go b/internal/resourceproviders/cache.go index 4ebae5e8d498..0ece95a668e2 100644 --- a/internal/resourceproviders/cache.go +++ b/internal/resourceproviders/cache.go @@ -12,11 +12,10 @@ var cachedResourceProviders *[]string // CacheSupportedProviders attempts to retrieve the supported Resource Providers from the Resource Manager API // and caches them, for used in enhanced validation -func CacheSupportedProviders(cacheFunc CacheFunc) error { +func CacheSupportedProviders(cacheFunc CacheFunc) { providers, err := cacheFunc() if err != nil { log.Printf("[DEBUG] error retrieving providers: %s. Enhanced validation will be unavailable", err) - return err } providerNames := make([]string, 0) for _, provider := range providers { @@ -25,7 +24,6 @@ func CacheSupportedProviders(cacheFunc CacheFunc) error { } } cachedResourceProviders = &providerNames - return nil } // CacheFunc provides an interface to cache resource providers From 8db8b9659f35e62cb01cb52fa3035125c0c8664a Mon Sep 17 00:00:00 2001 From: Heng Lu Date: Sat, 28 Jan 2023 13:22:49 +0800 Subject: [PATCH 05/11] update --- internal/acceptance/testcase.go | 7 +--- internal/clients/builder.go | 9 +++-- internal/provider/provider.go | 5 ++- internal/resourceproviders/cache.go | 44 ++++++++---------------- internal/resourceproviders/validation.go | 10 ++++-- 5 files changed, 32 insertions(+), 43 deletions(-) diff --git a/internal/acceptance/testcase.go b/internal/acceptance/testcase.go index d973915979c5..d5112d0fd361 100644 --- a/internal/acceptance/testcase.go +++ b/internal/acceptance/testcase.go @@ -131,10 +131,5 @@ func (td TestData) providers() map[string]func() (*schema.Provider, error) { } func (td TestData) externalProviders() map[string]resource.ExternalProvider { - return map[string]resource.ExternalProvider{ - "azuread": { - VersionConstraint: "=2.8.0", - Source: "registry.terraform.io/hashicorp/azuread", - }, - } + return map[string]resource.ExternalProvider{} } diff --git a/internal/clients/builder.go b/internal/clients/builder.go index a247a670d43f..6676724170a7 100644 --- a/internal/clients/builder.go +++ b/internal/clients/builder.go @@ -3,16 +3,16 @@ package clients import ( "context" "fmt" + "github.com/hashicorp/go-azure-helpers/resourcemanager/location" + "github.com/hashicorp/terraform-provider-azurerm/internal/resourceproviders" "log" "strings" "github.com/Azure/go-autorest/autorest" "github.com/hashicorp/go-azure-helpers/authentication" - "github.com/hashicorp/go-azure-helpers/resourcemanager/location" "github.com/hashicorp/go-azure-helpers/sender" "github.com/hashicorp/terraform-provider-azurerm/internal/common" "github.com/hashicorp/terraform-provider-azurerm/internal/features" - "github.com/hashicorp/terraform-provider-azurerm/internal/resourceproviders" "github.com/manicminer/hamilton/environments" ) @@ -152,7 +152,10 @@ func Build(ctx context.Context, builder ClientBuilder) (*Client, error) { if features.EnhancedValidationEnabled() { location.CacheSupportedLocations(ctx, env.ResourceManagerEndpoint) - resourceproviders.CacheSupportedProviders(resourceproviders.DefaultCacheFunc(ctx, client.Resource.ProvidersClient)) + _, err := resourceproviders.CachedSupportedProviders(ctx, client.Resource.ProvidersClient) + if err != nil { + log.Printf("[DEBUG] error retrieving providers: %+v. Enhanced validation will be unavailable", err) + } } return &client, nil diff --git a/internal/provider/provider.go b/internal/provider/provider.go index d3274b922c57..9f1b47a99bc5 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -416,14 +416,13 @@ func providerConfigure(p *schema.Provider) schema.ConfigureContextFunc { if !skipProviderRegistration { // List all the available providers and their registration state to avoid unnecessary // requests. This also lets us check if the provider credentials are correct. - providerList, err := client.Resource.ProvidersClient.List(ctx, nil, "") + providerList, err := resourceproviders.CachedSupportedProviders(ctx, client.Resource.ProvidersClient) if err != nil { return nil, diag.Errorf("Unable to list provider registration status, it is possible that this is due to invalid "+ "credentials or the service principal does not have permission to use the Resource Manager API, Azure "+ "error: %s", err) } - resourceproviders.CacheSupportedProviders(resourceproviders.SavedResourceProvidersCacheFunc(providerList.Values())) - availableResourceProviders := providerList.Values() + availableResourceProviders := *providerList requiredResourceProviders := resourceproviders.Required() if err := resourceproviders.EnsureRegistered(ctx, *client.Resource.ProvidersClient, availableResourceProviders, requiredResourceProviders); err != nil { diff --git a/internal/resourceproviders/cache.go b/internal/resourceproviders/cache.go index 0ece95a668e2..a9cbc9062435 100644 --- a/internal/resourceproviders/cache.go +++ b/internal/resourceproviders/cache.go @@ -2,43 +2,29 @@ package resourceproviders import ( "context" - "log" - "github.com/Azure/azure-sdk-for-go/profiles/2017-03-09/resources/mgmt/resources" ) // cachedResourceProviders can be (validly) nil - as such this shouldn't be relied on -var cachedResourceProviders *[]string +var cachedResourceProviders *[]resources.Provider -// CacheSupportedProviders attempts to retrieve the supported Resource Providers from the Resource Manager API +// CachedSupportedProviders returns cached the supported Resource Providers, if not cached it attempts to retrieve from the Resource Manager API // and caches them, for used in enhanced validation -func CacheSupportedProviders(cacheFunc CacheFunc) { - providers, err := cacheFunc() +func CachedSupportedProviders(ctx context.Context, client *resources.ProvidersClient) (*[]resources.Provider, error) { + if cachedResourceProviders != nil { + return cachedResourceProviders, nil + } + providers, err := availableResourceProviders(ctx, client) if err != nil { - log.Printf("[DEBUG] error retrieving providers: %s. Enhanced validation will be unavailable", err) + return nil, err } - providerNames := make([]string, 0) + cached := make([]resources.Provider, 0) for _, provider := range providers { - if provider.Namespace != nil { - providerNames = append(providerNames, *provider.Namespace) - } - } - cachedResourceProviders = &providerNames -} - -// CacheFunc provides an interface to cache resource providers -type CacheFunc func() ([]resources.Provider, error) - -// DefaultCacheFunc lists all the available resource providers -func DefaultCacheFunc(ctx context.Context, client *resources.ProvidersClient) CacheFunc { - return func() ([]resources.Provider, error) { - return availableResourceProviders(ctx, client) - } -} - -// SavedResourceProvidersCacheFunc returns already saved resource providers -func SavedResourceProvidersCacheFunc(resourceProviders []resources.Provider) CacheFunc { - return func() ([]resources.Provider, error) { - return resourceProviders, nil + cached = append(cached, resources.Provider{ + Namespace: provider.Namespace, + RegistrationState: provider.RegistrationState, + }) } + cachedResourceProviders = &cached + return cachedResourceProviders, nil } diff --git a/internal/resourceproviders/validation.go b/internal/resourceproviders/validation.go index 78b509efde46..424e06435ab2 100644 --- a/internal/resourceproviders/validation.go +++ b/internal/resourceproviders/validation.go @@ -41,13 +41,19 @@ func enhancedValidation(i interface{}, k string) ([]string, []error) { found := false for _, provider := range *cachedResourceProviders { - if provider == v { + if provider.Namespace != nil && *provider.Namespace == v { found = true } } if !found { - providersJoined := strings.Join(*cachedResourceProviders, ", ") + cachedResourceProvidersNames := make([]string, 0) + for _, provider := range *cachedResourceProviders { + if provider.Namespace != nil { + cachedResourceProvidersNames = append(cachedResourceProvidersNames, *provider.Namespace) + } + } + providersJoined := strings.Join(cachedResourceProvidersNames, ", ") return nil, []error{ fmt.Errorf("%q was not found in the list of supported Resource Providers: %q", v, providersJoined), } From 994fe05c1b2e4c3ccf63ebc5e4ba1d9c9071c4e6 Mon Sep 17 00:00:00 2001 From: Heng Lu Date: Mon, 30 Jan 2023 13:24:58 +0800 Subject: [PATCH 06/11] update --- internal/acceptance/testcase.go | 7 ++++++- internal/clients/builder.go | 4 ++-- internal/provider/provider.go | 1 + 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/internal/acceptance/testcase.go b/internal/acceptance/testcase.go index d5112d0fd361..d973915979c5 100644 --- a/internal/acceptance/testcase.go +++ b/internal/acceptance/testcase.go @@ -131,5 +131,10 @@ func (td TestData) providers() map[string]func() (*schema.Provider, error) { } func (td TestData) externalProviders() map[string]resource.ExternalProvider { - return map[string]resource.ExternalProvider{} + return map[string]resource.ExternalProvider{ + "azuread": { + VersionConstraint: "=2.8.0", + Source: "registry.terraform.io/hashicorp/azuread", + }, + } } diff --git a/internal/clients/builder.go b/internal/clients/builder.go index 6676724170a7..82f907ba7c83 100644 --- a/internal/clients/builder.go +++ b/internal/clients/builder.go @@ -3,16 +3,16 @@ package clients import ( "context" "fmt" - "github.com/hashicorp/go-azure-helpers/resourcemanager/location" - "github.com/hashicorp/terraform-provider-azurerm/internal/resourceproviders" "log" "strings" "github.com/Azure/go-autorest/autorest" "github.com/hashicorp/go-azure-helpers/authentication" + "github.com/hashicorp/go-azure-helpers/resourcemanager/location" "github.com/hashicorp/go-azure-helpers/sender" "github.com/hashicorp/terraform-provider-azurerm/internal/common" "github.com/hashicorp/terraform-provider-azurerm/internal/features" + "github.com/hashicorp/terraform-provider-azurerm/internal/resourceproviders" "github.com/manicminer/hamilton/environments" ) diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 9f1b47a99bc5..71a830ca7beb 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -422,6 +422,7 @@ func providerConfigure(p *schema.Provider) schema.ConfigureContextFunc { "credentials or the service principal does not have permission to use the Resource Manager API, Azure "+ "error: %s", err) } + availableResourceProviders := *providerList requiredResourceProviders := resourceproviders.Required() From 6bae46c7cd2e369c838698a06bb4766c36e56966 Mon Sep 17 00:00:00 2001 From: Heng Lu Date: Thu, 2 Mar 2023 13:07:05 +0800 Subject: [PATCH 07/11] update --- internal/clients/builder.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/internal/clients/builder.go b/internal/clients/builder.go index bea15c537b65..ea3a891e31b6 100644 --- a/internal/clients/builder.go +++ b/internal/clients/builder.go @@ -145,7 +145,7 @@ func Build(ctx context.Context, builder ClientBuilder) (*Client, error) { DisableCorrelationRequestID: builder.DisableCorrelationRequestID, DisableTerraformPartnerID: builder.DisableTerraformPartnerID, SkipProviderReg: builder.SkipProviderRegistration, - StorageUseAzureAD: builder.StorageUseAzureAD + StorageUseAzureAD: builder.StorageUseAzureAD, // TODO: remove when `Azure/go-autorest` is no longer used AzureEnvironment: *azureEnvironment, @@ -158,8 +158,6 @@ func Build(ctx context.Context, builder ClientBuilder) (*Client, error) { if features.EnhancedValidationEnabled() { location.CacheSupportedLocations(ctx, *resourceManagerEndpoint) - resourceproviders.CacheSupportedProviders(ctx, client.Resource.ProvidersClient) - location.CacheSupportedLocations(ctx, env.ResourceManagerEndpoint) _, err := resourceproviders.CachedSupportedProviders(ctx, client.Resource.ProvidersClient) if err != nil { log.Printf("[DEBUG] error retrieving providers: %+v. Enhanced validation will be unavailable", err) From e1e091482f9f089f627fc97bda1edde7b142807b Mon Sep 17 00:00:00 2001 From: Heng Lu Date: Thu, 2 Mar 2023 13:07:50 +0800 Subject: [PATCH 08/11] gofmt --- internal/provider/provider.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 32bd26203477..f2d7f3a5e32c 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -450,18 +450,18 @@ func buildClient(ctx context.Context, p *schema.Provider, d *schema.ResourceData client.StopContext = stopCtx - if !skipProviderRegistration { - // List all the available providers and their registration state to avoid unnecessary - // requests. This also lets us check if the provider credentials are correct. - providerList, err := resourceproviders.CachedSupportedProviders(ctx, client.Resource.ProvidersClient) - if err != nil { - return nil, diag.Errorf("Unable to list provider registration status, it is possible that this is due to invalid "+ - "credentials or the service principal does not have permission to use the Resource Manager API, Azure "+ - "error: %s", err) - } + if !skipProviderRegistration { + // List all the available providers and their registration state to avoid unnecessary + // requests. This also lets us check if the provider credentials are correct. + providerList, err := resourceproviders.CachedSupportedProviders(ctx, client.Resource.ProvidersClient) + if err != nil { + return nil, diag.Errorf("Unable to list provider registration status, it is possible that this is due to invalid "+ + "credentials or the service principal does not have permission to use the Resource Manager API, Azure "+ + "error: %s", err) + } - availableResourceProviders := *providerList - requiredResourceProviders := resourceproviders.Required() + availableResourceProviders := *providerList + requiredResourceProviders := resourceproviders.Required() if err := resourceproviders.EnsureRegistered(ctx, *client.Resource.ProvidersClient, availableResourceProviders, requiredResourceProviders); err != nil { return nil, diag.Errorf(resourceProviderRegistrationErrorFmt, err) From 01517a46af4a0da7cca8dc70e0d04eadcc7b6420 Mon Sep 17 00:00:00 2001 From: Heng Lu Date: Thu, 2 Mar 2023 13:22:08 +0800 Subject: [PATCH 09/11] fix unit test --- internal/resourceproviders/validation_test.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/internal/resourceproviders/validation_test.go b/internal/resourceproviders/validation_test.go index 1cc359a394d8..a2ec696ee908 100644 --- a/internal/resourceproviders/validation_test.go +++ b/internal/resourceproviders/validation_test.go @@ -1,6 +1,8 @@ package resourceproviders import ( + "github.com/Azure/azure-sdk-for-go/profiles/2017-03-09/resources/mgmt/resources" + "github.com/hashicorp/terraform-provider-azurerm/utils" "testing" "github.com/hashicorp/terraform-provider-azurerm/internal/features" @@ -68,7 +70,7 @@ func TestEnhancedValidationEnabled(t *testing.T) { }, } enhancedEnabled = true - cachedResourceProviders = &[]string{"Microsoft.Compute"} + cachedResourceProviders = &[]resources.Provider{{Namespace: utils.String("Microsoft.Compute")}} defer func() { enhancedEnabled = features.EnhancedValidationEnabled() cachedResourceProviders = nil From a85c0e49db15d1b019f06dcf8cc80446fe705ef2 Mon Sep 17 00:00:00 2001 From: Heng Lu Date: Thu, 2 Mar 2023 13:23:37 +0800 Subject: [PATCH 10/11] sort goimports --- internal/resourceproviders/validation_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/resourceproviders/validation_test.go b/internal/resourceproviders/validation_test.go index a2ec696ee908..c4c562b56040 100644 --- a/internal/resourceproviders/validation_test.go +++ b/internal/resourceproviders/validation_test.go @@ -1,11 +1,11 @@ package resourceproviders import ( - "github.com/Azure/azure-sdk-for-go/profiles/2017-03-09/resources/mgmt/resources" - "github.com/hashicorp/terraform-provider-azurerm/utils" "testing" + "github.com/Azure/azure-sdk-for-go/profiles/2017-03-09/resources/mgmt/resources" "github.com/hashicorp/terraform-provider-azurerm/internal/features" + "github.com/hashicorp/terraform-provider-azurerm/utils" ) func TestEnhancedValidationDisabled(t *testing.T) { From b38b26c319612c131142d76520f9dc876a92ce79 Mon Sep 17 00:00:00 2001 From: Heng Lu Date: Thu, 2 Mar 2023 13:30:56 +0800 Subject: [PATCH 11/11] sort goimports --- internal/resourceproviders/cache.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/resourceproviders/cache.go b/internal/resourceproviders/cache.go index a9cbc9062435..3745cd05c9e0 100644 --- a/internal/resourceproviders/cache.go +++ b/internal/resourceproviders/cache.go @@ -2,6 +2,7 @@ package resourceproviders import ( "context" + "github.com/Azure/azure-sdk-for-go/profiles/2017-03-09/resources/mgmt/resources" )