From f791bdee818c63530e84d3afb993e522a943035a Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Fri, 17 Feb 2023 13:35:27 +0100 Subject: [PATCH] appconfiguration: surfacing an error when one exists, rather than misleadingly marking the app configuration as gone --- internal/common/correlation_id.go | 2 +- .../app_configuration_feature_resource.go | 22 +++++++------- ...app_configuration_feature_resource_test.go | 6 ++-- .../app_configuration_key_data_source.go | 10 +++---- .../app_configuration_key_resource.go | 29 ++++++++++--------- .../app_configuration_key_resource_test.go | 6 ++-- .../app_configuration_keys_data_source.go | 10 +++---- .../appconfiguration/client/client.go | 8 +++-- 8 files changed, 48 insertions(+), 45 deletions(-) diff --git a/internal/common/correlation_id.go b/internal/common/correlation_id.go index 8db29bf90948..85ab15e2d008 100644 --- a/internal/common/correlation_id.go +++ b/internal/common/correlation_id.go @@ -32,7 +32,7 @@ func correlationRequestID() string { if err != nil { log.Printf("[WARN] Failed to generate uuid for msCorrelationRequestID: %+v", err) } - log.Printf("[DEBUG] Genereated Provider Correlation Request Id: %s", msCorrelationRequestID) + log.Printf("[DEBUG] Generated Provider Correlation Request Id: %s", msCorrelationRequestID) }) return msCorrelationRequestID diff --git a/internal/services/appconfiguration/app_configuration_feature_resource.go b/internal/services/appconfiguration/app_configuration_feature_resource.go index 975b2f778bda..056d9ab57102 100644 --- a/internal/services/appconfiguration/app_configuration_feature_resource.go +++ b/internal/services/appconfiguration/app_configuration_feature_resource.go @@ -7,8 +7,8 @@ import ( "time" "github.com/Azure/go-autorest/autorest" + "github.com/hashicorp/go-azure-sdk/resource-manager/appconfiguration/2022-05-01/configurationstores" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" - "github.com/hashicorp/terraform-provider-azurerm/helpers/azure" "github.com/hashicorp/terraform-provider-azurerm/helpers/tf" "github.com/hashicorp/terraform-provider-azurerm/internal/sdk" "github.com/hashicorp/terraform-provider-azurerm/internal/services/appconfiguration/parse" @@ -48,7 +48,7 @@ func (k FeatureResource) Arguments() map[string]*pluginsdk.Schema { Type: pluginsdk.TypeString, Required: true, ForceNew: true, - ValidateFunc: azure.ValidateResourceID, + ValidateFunc: configurationstores.ValidateConfigurationStoreID, }, "description": { Type: pluginsdk.TypeString, @@ -166,12 +166,12 @@ func (k FeatureResource) Create() sdk.ResourceFunc { } client, err := metadata.Client.AppConfiguration.DataPlaneClient(ctx, model.ConfigurationStoreId) - if client == nil { - return fmt.Errorf("app configuration %q was not found", model.ConfigurationStoreId) - } if err != nil { return err } + if client == nil { + return fmt.Errorf("app configuration %q was not found", model.ConfigurationStoreId) + } appCfgFeatureResourceID := parse.AppConfigurationFeatureId{ ConfigurationStoreId: model.ConfigurationStoreId, @@ -225,13 +225,13 @@ func (k FeatureResource) Read() sdk.ResourceFunc { } client, err := metadata.Client.AppConfiguration.DataPlaneClient(ctx, resourceID.ConfigurationStoreId) + if err != nil { + return err + } if client == nil { // if the AppConfiguration is gone then all the data inside it is too return metadata.MarkAsGone(resourceID) } - if err != nil { - return err - } kv, err := client.GetKeyValue(ctx, featureKey, resourceID.Label, "", "", "", []string{}) if err != nil { @@ -297,12 +297,12 @@ func (k FeatureResource) Update() sdk.ResourceFunc { featureKey := fmt.Sprintf("%s/%s", FeatureKeyPrefix, resourceID.Name) client, err := metadata.Client.AppConfiguration.DataPlaneClient(ctx, resourceID.ConfigurationStoreId) - if client == nil { - return fmt.Errorf("app configuration %q was not found", resourceID.ConfigurationStoreId) - } if err != nil { return err } + if client == nil { + return fmt.Errorf("app configuration %q was not found", resourceID.ConfigurationStoreId) + } var model FeatureResourceModel if err := metadata.Decode(&model); err != nil { diff --git a/internal/services/appconfiguration/app_configuration_feature_resource_test.go b/internal/services/appconfiguration/app_configuration_feature_resource_test.go index 501e2f5f4076..7bfe56f780c1 100644 --- a/internal/services/appconfiguration/app_configuration_feature_resource_test.go +++ b/internal/services/appconfiguration/app_configuration_feature_resource_test.go @@ -172,13 +172,13 @@ func (t AppConfigurationFeatureResource) Exists(ctx context.Context, clients *cl } client, err := clients.AppConfiguration.DataPlaneClient(ctx, resourceID.ConfigurationStoreId) + if err != nil { + return nil, err + } if client == nil { // if the AppConfiguration is gone all the data is too return utils.Bool(false), nil } - if err != nil { - return nil, err - } res, err := client.GetKeyValues(ctx, resourceID.Name, resourceID.Label, "", "", []string{}) if err != nil { diff --git a/internal/services/appconfiguration/app_configuration_key_data_source.go b/internal/services/appconfiguration/app_configuration_key_data_source.go index 4c35cd4cf366..0d7850b9d138 100644 --- a/internal/services/appconfiguration/app_configuration_key_data_source.go +++ b/internal/services/appconfiguration/app_configuration_key_data_source.go @@ -8,7 +8,7 @@ import ( "time" "github.com/Azure/go-autorest/autorest" - "github.com/hashicorp/terraform-provider-azurerm/helpers/azure" + "github.com/hashicorp/go-azure-sdk/resource-manager/appconfiguration/2022-05-01/configurationstores" "github.com/hashicorp/terraform-provider-azurerm/internal/sdk" "github.com/hashicorp/terraform-provider-azurerm/internal/services/appconfiguration/parse" "github.com/hashicorp/terraform-provider-azurerm/internal/tags" @@ -26,7 +26,7 @@ func (k KeyDataSource) Arguments() map[string]*pluginsdk.Schema { "configuration_store_id": { Type: pluginsdk.TypeString, Required: true, - ValidateFunc: azure.ValidateResourceID, + ValidateFunc: configurationstores.ValidateConfigurationStoreID, }, "key": { Type: pluginsdk.TypeString, @@ -100,12 +100,12 @@ func (k KeyDataSource) Read() sdk.ResourceFunc { } client, err := metadata.Client.AppConfiguration.DataPlaneClient(ctx, model.ConfigurationStoreId) - if client == nil { - return fmt.Errorf("building data plane client: app configuration %q was not found", model.ConfigurationStoreId) - } if err != nil { return err } + if client == nil { + return fmt.Errorf("building data plane client: app configuration %q was not found", model.ConfigurationStoreId) + } kv, err := client.GetKeyValue(ctx, decodedKey, model.Label, "", "", "", []string{}) if err != nil { diff --git a/internal/services/appconfiguration/app_configuration_key_resource.go b/internal/services/appconfiguration/app_configuration_key_resource.go index a5786cbf49a1..605dc6ca099d 100644 --- a/internal/services/appconfiguration/app_configuration_key_resource.go +++ b/internal/services/appconfiguration/app_configuration_key_resource.go @@ -8,7 +8,7 @@ import ( "time" "github.com/Azure/go-autorest/autorest" - "github.com/hashicorp/terraform-provider-azurerm/helpers/azure" + "github.com/hashicorp/go-azure-sdk/resource-manager/appconfiguration/2022-05-01/configurationstores" "github.com/hashicorp/terraform-provider-azurerm/helpers/tf" "github.com/hashicorp/terraform-provider-azurerm/internal/sdk" "github.com/hashicorp/terraform-provider-azurerm/internal/services/appconfiguration/migration" @@ -56,7 +56,7 @@ func (k KeyResource) Arguments() map[string]*pluginsdk.Schema { Type: pluginsdk.TypeString, Required: true, ForceNew: true, - ValidateFunc: azure.ValidateResourceID, + ValidateFunc: configurationstores.ValidateConfigurationStoreID, }, "key": { Type: pluginsdk.TypeString, @@ -125,12 +125,12 @@ func (k KeyResource) Create() sdk.ResourceFunc { } client, err := metadata.Client.AppConfiguration.DataPlaneClient(ctx, model.ConfigurationStoreId) - if client == nil { - return fmt.Errorf("app configuration %q was not found", model.ConfigurationStoreId) - } if err != nil { return err } + if client == nil { + return fmt.Errorf("app configuration %q was not found", model.ConfigurationStoreId) + } appCfgKeyResourceID := parse.AppConfigurationKeyId{ ConfigurationStoreId: model.ConfigurationStoreId, @@ -208,13 +208,13 @@ func (k KeyResource) Read() sdk.ResourceFunc { } client, err := metadata.Client.AppConfiguration.DataPlaneClient(ctx, resourceID.ConfigurationStoreId) + if err != nil { + return err + } if client == nil { // if the parent AppConfiguration is gone, all the data will be too return metadata.MarkAsGone(resourceID) } - if err != nil { - return err - } kv, err := client.GetKeyValue(ctx, resourceID.Key, resourceID.Label, "", "", "", []string{}) if err != nil { @@ -272,12 +272,12 @@ func (k KeyResource) Update() sdk.ResourceFunc { } client, err := metadata.Client.AppConfiguration.DataPlaneClient(ctx, resourceID.ConfigurationStoreId) - if client == nil { - return fmt.Errorf("app configuration %q was not found", resourceID.ConfigurationStoreId) - } if err != nil { return err } + if client == nil { + return fmt.Errorf("app configuration %q was not found", resourceID.ConfigurationStoreId) + } var model KeyResourceModel if err := metadata.Decode(&model); err != nil { @@ -334,11 +334,12 @@ func (k KeyResource) Delete() sdk.ResourceFunc { } client, err := metadata.Client.AppConfiguration.DataPlaneClient(ctx, resourceID.ConfigurationStoreId) - if client == nil { - return fmt.Errorf("app configuration %q was not found", resourceID.ConfigurationStoreId) - } if err != nil { return err + + } + if client == nil { + return fmt.Errorf("app configuration %q was not found", resourceID.ConfigurationStoreId) } decodedKey, err := url.QueryUnescape(resourceID.Key) diff --git a/internal/services/appconfiguration/app_configuration_key_resource_test.go b/internal/services/appconfiguration/app_configuration_key_resource_test.go index c8a1dff7452e..25814963a9fb 100644 --- a/internal/services/appconfiguration/app_configuration_key_resource_test.go +++ b/internal/services/appconfiguration/app_configuration_key_resource_test.go @@ -150,13 +150,13 @@ func (t AppConfigurationKeyResource) Exists(ctx context.Context, clients *client } client, err := clients.AppConfiguration.DataPlaneClient(ctx, resourceID.ConfigurationStoreId) + if err != nil { + return nil, err + } if client == nil { // if the AppConfiguration is gone all the data will be too return utils.Bool(false), nil } - if err != nil { - return nil, err - } res, err := client.GetKeyValues(ctx, resourceID.Key, resourceID.Label, "", "", []string{}) if err != nil { diff --git a/internal/services/appconfiguration/app_configuration_keys_data_source.go b/internal/services/appconfiguration/app_configuration_keys_data_source.go index 7f78625aa36b..5d44d66fab35 100644 --- a/internal/services/appconfiguration/app_configuration_keys_data_source.go +++ b/internal/services/appconfiguration/app_configuration_keys_data_source.go @@ -8,7 +8,7 @@ import ( "time" "github.com/Azure/go-autorest/autorest" - "github.com/hashicorp/terraform-provider-azurerm/helpers/azure" + "github.com/hashicorp/go-azure-sdk/resource-manager/appconfiguration/2022-05-01/configurationstores" "github.com/hashicorp/terraform-provider-azurerm/internal/sdk" "github.com/hashicorp/terraform-provider-azurerm/internal/services/appconfiguration/parse" "github.com/hashicorp/terraform-provider-azurerm/internal/tags" @@ -44,7 +44,7 @@ func (k KeysDataSource) Arguments() map[string]*pluginsdk.Schema { "configuration_store_id": { Type: pluginsdk.TypeString, Required: true, - ValidateFunc: azure.ValidateResourceID, + ValidateFunc: configurationstores.ValidateConfigurationStoreID, }, "key": { Type: pluginsdk.TypeString, @@ -137,12 +137,12 @@ func (k KeysDataSource) Read() sdk.ResourceFunc { // whereas the client expects a complete URI to be present and therefore fails to fetch all results if // store contains more than 100 entries client, err := metadata.Client.AppConfiguration.LinkWorkaroundDataPlaneClient(ctx, model.ConfigurationStoreId) - if client == nil { - return fmt.Errorf("building data plane client: app configuration %q was not found", model.ConfigurationStoreId) - } if err != nil { return err } + if client == nil { + return fmt.Errorf("building data plane client: app configuration %q was not found", model.ConfigurationStoreId) + } iter, err := client.GetKeyValuesComplete(ctx, decodedKey, model.Label, "", "", []string{}) if err != nil { diff --git a/internal/services/appconfiguration/client/client.go b/internal/services/appconfiguration/client/client.go index 0d3c0ae7cfd1..97d2b54869fb 100644 --- a/internal/services/appconfiguration/client/client.go +++ b/internal/services/appconfiguration/client/client.go @@ -42,13 +42,15 @@ func (c Client) DataPlaneClient(ctx context.Context, configurationStoreId string return nil, fmt.Errorf("endpoint was nil") } - api := environments.NewApiEndpoint("AppConfiguration", *appConfig.Model.Properties.Endpoint, nil) + endpoint := *appConfig.Model.Properties.Endpoint + + api := environments.NewApiEndpoint("AppConfiguration", endpoint, nil) appConfigAuth, err := c.authorizerFunc(api) if err != nil { - return nil, fmt.Errorf("obtaining auth token for %q: %+v", *appConfig.Model.Properties.Endpoint, err) + return nil, fmt.Errorf("obtaining auth token for %q: %+v", endpoint, err) } - client := appconfiguration.NewWithoutDefaults("", *appConfig.Model.Properties.Endpoint) + client := appconfiguration.NewWithoutDefaults("", endpoint) c.configureClientFunc(&client.Client, authWrapper.AutorestAuthorizer(appConfigAuth)) return &client, nil