Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

appconfiguration: surfacing an error when one exists, rather than misleadingly marking the app configuration as gone #20533

Merged
merged 1 commit into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/common/correlation_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 5 additions & 3 deletions internal/services/appconfiguration/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down