Skip to content

Commit

Permalink
turn to poller
Browse files Browse the repository at this point in the history
  • Loading branch information
ziyeqf committed Dec 12, 2024
1 parent 9c96896 commit c93d2f1
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package loganalytics

import (
"context"
"fmt"
"log"
"strings"
Expand All @@ -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"
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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
}
}

0 comments on commit c93d2f1

Please sign in to comment.