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/sdk: Fix pagination logic #19020

Merged
merged 5 commits into from
Oct 27, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,11 @@ func (k KeysDataSource) Read() sdk.ResourceFunc {
Key: decodedKey,
Label: model.Label,
}

client, err := metadata.Client.AppConfiguration.DataPlaneClient(ctx, model.ConfigurationStoreId)
// @favoretti: API returns pagination nextLink (Link header) without complete URI, only path:
// Link: "</kv?somepath...>; rel=next;"
// 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)
}
Expand Down
104 changes: 104 additions & 0 deletions internal/services/appconfiguration/azuresdkhacks/dataplaneclient.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package azuresdkhacks

import (
"context"
"net/http"

"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/azure"
"github.com/Azure/go-autorest/tracing"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/appconfiguration/sdk/1.0/appconfiguration"
)

type DataPlaneClient struct {
client *appconfiguration.BaseClient
}

func NewDataPlaneClient(client appconfiguration.BaseClient) DataPlaneClient {
return DataPlaneClient{
client: &client,
}
}

func (c DataPlaneClient) GetKeyValuesComplete(ctx context.Context, key string, label string, after string, acceptDatetime string, selectParameter []string) (result KeyValueListResultIterator, err error) {
if tracing.IsEnabled() {
ctx = tracing.StartSpan(ctx, fqdn+"/BaseClient.GetKeyValues")
defer func() {
sc := -1
if result.Response().Response.Response != nil {
sc = result.page.Response().Response.Response.StatusCode
}
tracing.EndSpan(ctx, sc, err)
}()
}
result.page, err = c.GetKeyValues(ctx, key, label, after, acceptDatetime, selectParameter)
return
}

func (c DataPlaneClient) GetKeyValues(ctx context.Context, key string, label string, after string, acceptDatetime string, selectParameter []string) (result KeyValueListResultPage, err error) {
if tracing.IsEnabled() {
ctx = tracing.StartSpan(ctx, fqdn+"/BaseClient.GetKeyValues")
defer func() {
sc := -1
if result.kvlr.Response.Response != nil {
sc = result.kvlr.Response.Response.StatusCode
}
tracing.EndSpan(ctx, sc, err)
}()
}
result.fn = c.getKeyValuesNextResults
req, err := c.client.GetKeyValuesPreparer(ctx, key, label, after, acceptDatetime, selectParameter)
if err != nil {
err = autorest.NewErrorWithError(err, "appconfiguration.BaseClient", "GetKeyValues", nil, "Failure preparing request")
return
}

resp, err := c.client.GetKeyValuesSender(req)
if err != nil {
result.kvlr.Response = autorest.Response{Response: resp}
err = autorest.NewErrorWithError(err, "appconfiguration.BaseClient", "GetKeyValues", resp, "Failure sending request")
return
}

result.kvlr, err = c.GetKeyValuesResponder(resp)
if err != nil {
err = autorest.NewErrorWithError(err, "appconfiguration.BaseClient", "GetKeyValues", resp, "Failure responding to request")
return
}
if result.kvlr.hasNextLink() && result.kvlr.IsEmpty() {
err = result.NextWithContext(ctx)
return
}

return
}

func (c DataPlaneClient) getKeyValuesNextResults(ctx context.Context, lastResults KeyValueListResult) (result KeyValueListResult, err error) {
req, err := lastResults.keyValueListResultPreparer(ctx, c.client.Endpoint)
if err != nil {
return result, autorest.NewErrorWithError(err, "appconfiguration.BaseClient", "getKeyValuesNextResults", nil, "Failure preparing next results request")
}
if req == nil {
return
}
resp, err := c.client.GetKeyValuesSender(req)
if err != nil {
result.Response = autorest.Response{Response: resp}
return result, autorest.NewErrorWithError(err, "appconfiguration.BaseClient", "getKeyValuesNextResults", resp, "Failure sending next results request")
}
result, err = c.GetKeyValuesResponder(resp)
if err != nil {
err = autorest.NewErrorWithError(err, "appconfiguration.BaseClient", "getKeyValuesNextResults", resp, "Failure responding to next results request")
}
return
}

func (c DataPlaneClient) GetKeyValuesResponder(resp *http.Response) (result KeyValueListResult, err error) {
err = autorest.Respond(
resp,
azure.WithErrorUnlessStatusCode(http.StatusOK),
autorest.ByUnmarshallingJSON(&result),
autorest.ByClosing())
result.Response = autorest.Response{Response: resp}
return
}
231 changes: 231 additions & 0 deletions internal/services/appconfiguration/azuresdkhacks/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions internal/services/appconfiguration/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/hashicorp/go-azure-sdk/resource-manager/appconfiguration/2022-05-01/configurationstores"
"github.com/hashicorp/go-azure-sdk/resource-manager/appconfiguration/2022-05-01/deletedconfigurationstores"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/appconfiguration/azuresdkhacks"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/appconfiguration/sdk/1.0/appconfiguration"
)

Expand Down Expand Up @@ -50,6 +51,38 @@ func (c Client) DataPlaneClient(ctx context.Context, configurationStoreId string
return &client, nil
}

func (c Client) LinkWorkaroundDataPlaneClient(ctx context.Context, configurationStoreId string) (*azuresdkhacks.DataPlaneClient, error) {
appConfigId, err := configurationstores.ParseConfigurationStoreID(configurationStoreId)
if err != nil {
return nil, err
}

// TODO: caching all of this
appConfig, err := c.ConfigurationStoresClient.Get(ctx, *appConfigId)
if err != nil {
if response.WasNotFound(appConfig.HttpResponse) {
return nil, nil
}

return nil, err
}

if appConfig.Model == nil || appConfig.Model.Properties == nil || appConfig.Model.Properties.Endpoint == nil {
return nil, fmt.Errorf("endpoint was nil")
}

endpoint := *appConfig.Model.Properties.Endpoint
appConfigAuth, err := c.tokenFunc(endpoint)
if err != nil {
return nil, fmt.Errorf("obtaining auth token for %q: %+v", endpoint, err)
}

client := appconfiguration.NewWithoutDefaults("", endpoint)
c.configureClientFunc(&client.Client, appConfigAuth)
workaroundClient := azuresdkhacks.NewDataPlaneClient(client)
return &workaroundClient, nil
}

func NewClient(o *common.ClientOptions) *Client {
configurationStores := configurationstores.NewConfigurationStoresClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&configurationStores.Client, o.ResourceManagerAuthorizer)
Expand Down