diff --git a/internal/services/loganalytics/custompollers/log_analytics_data_export_poller.go b/internal/services/loganalytics/custompollers/log_analytics_data_export_poller.go new file mode 100644 index 000000000000..69165c5efa4c --- /dev/null +++ b/internal/services/loganalytics/custompollers/log_analytics_data_export_poller.go @@ -0,0 +1,47 @@ +package custompollers + +import ( + "context" + "fmt" + "time" + + "github.com/hashicorp/go-azure-helpers/lang/response" + "github.com/hashicorp/go-azure-sdk/resource-manager/operationalinsights/2020-08-01/dataexport" + "github.com/hashicorp/go-azure-sdk/sdk/client/pollers" +) + +var _ pollers.PollerType = &LogAnalyticsDataExportPoller{} + +type LogAnalyticsDataExportPoller struct { + client *dataexport.DataExportClient + id dataexport.DataExportId +} + +var ( + pollingSuccess = pollers.PollResult{ + Status: pollers.PollingStatusSucceeded, + PollInterval: 10 * time.Second, + } + pollingInProgress = pollers.PollResult{ + Status: pollers.PollingStatusInProgress, + PollInterval: 10 * time.Second, + } +) + +func NewLogAnalyticsDataExportPoller(client *dataexport.DataExportClient, id dataexport.DataExportId) *LogAnalyticsDataExportPoller { + return &LogAnalyticsDataExportPoller{ + client: client, + id: id, + } +} + +func (p LogAnalyticsDataExportPoller) Poll(ctx context.Context) (*pollers.PollResult, error) { + resp, err := p.client.Get(ctx, p.id) + if err != nil { + if response.WasNotFound(resp.HttpResponse) { + return &pollingInProgress, nil + } + return nil, fmt.Errorf("retrieving %s: %+v", p.id, err) + } + return &pollingSuccess, nil +} diff --git a/internal/services/loganalytics/log_analytics_data_export_resource.go b/internal/services/loganalytics/log_analytics_data_export_resource.go index 5103faaaa81e..1018af40fb5f 100644 --- a/internal/services/loganalytics/log_analytics_data_export_resource.go +++ b/internal/services/loganalytics/log_analytics_data_export_resource.go @@ -4,7 +4,6 @@ package loganalytics import ( - "context" "fmt" "log" "strings" @@ -15,9 +14,11 @@ import ( "github.com/hashicorp/go-azure-sdk/resource-manager/eventhub/2021-11-01/eventhubs" "github.com/hashicorp/go-azure-sdk/resource-manager/eventhub/2022-01-01-preview/namespaces" "github.com/hashicorp/go-azure-sdk/resource-manager/operationalinsights/2020-08-01/dataexport" + "github.com/hashicorp/go-azure-sdk/sdk/client/pollers" "github.com/hashicorp/terraform-provider-azurerm/helpers/azure" "github.com/hashicorp/terraform-provider-azurerm/helpers/tf" "github.com/hashicorp/terraform-provider-azurerm/internal/clients" + "github.com/hashicorp/terraform-provider-azurerm/internal/services/loganalytics/custompollers" "github.com/hashicorp/terraform-provider-azurerm/internal/services/loganalytics/migration" "github.com/hashicorp/terraform-provider-azurerm/internal/services/loganalytics/validate" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" @@ -165,24 +166,12 @@ func resourceOperationalinsightsDataExportCreateUpdate(d *pluginsdk.ResourceData return fmt.Errorf("creating/updating %s: %+v", id, err) } - deadline, ok := ctx.Deadline() - if !ok { - return fmt.Errorf("internal error: could not retrieve context deadline for %s", id.ID()) - } - // Tracked on https://github.com/Azure/azure-rest-api-specs/issues/31399 log.Printf("[DEBUG] Waiting for Log Analytics Workspace Data Export Rule %q to become ready", id.ID()) - stateConf := &pluginsdk.StateChangeConf{ - Pending: []string{"NotFound"}, - Target: []string{"Exists"}, - Refresh: dataExportRefreshFunc(ctx, client, id), - MinTimeout: 5 * time.Second, - ContinuousTargetOccurence: 3, - Timeout: time.Until(deadline), - } - - if _, err = stateConf.WaitForStateContext(ctx); err != nil { - return fmt.Errorf("waiting for Log Analytics Workspace Data Export Rule %q to become ready: %+v", id.ID(), err) + pollerType := custompollers.NewLogAnalyticsDataExportPoller(client, id) + poller := pollers.NewPoller(pollerType, 10*time.Second, pollers.DefaultNumberOfDroppedConnectionsToAllow) + if err := poller.PollUntilDone(ctx); err != nil { + return err } d.SetId(id.ID()) @@ -276,17 +265,3 @@ func flattenDataExportDestination(input *dataexport.Destination) (string, error) return resourceID, nil } - -func dataExportRefreshFunc(ctx context.Context, client *dataexport.DataExportClient, id dataexport.DataExportId) pluginsdk.StateRefreshFunc { - return func() (result interface{}, state string, err error) { - res, err := client.Get(ctx, id) - if err != nil { - if response.WasNotFound(res.HttpResponse) { - return "NotFound", "NotFound", nil - } - return nil, "", fmt.Errorf("retrieving %s: %+v", id, err) - } - - return res, "Exists", nil - } -}