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

network - update several resources to use hashicorp/go-azure-sdk #25905

Merged
merged 10 commits into from
May 14, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package custompollers

import (
"context"
"fmt"
"strings"
"time"

"github.com/hashicorp/go-azure-sdk/resource-manager/network/2023-09-01/localnetworkgateways"
"github.com/hashicorp/go-azure-sdk/sdk/client/pollers"
)

var _ pollers.PollerType = &localNetworkGatewayPoller{}

type localNetworkGatewayPoller struct {
client *localnetworkgateways.LocalNetworkGatewaysClient
id localnetworkgateways.LocalNetworkGatewayId
}

var (
pollingSuccess = pollers.PollResult{
Status: pollers.PollingStatusSucceeded,
PollInterval: 10 * time.Second,
}
pollingInProgress = pollers.PollResult{
Status: pollers.PollingStatusInProgress,
PollInterval: 10 * time.Second,
}
)

func NewLocalNetworkGatewayPoller(client *localnetworkgateways.LocalNetworkGatewaysClient, id localnetworkgateways.LocalNetworkGatewayId) *localNetworkGatewayPoller {
return &localNetworkGatewayPoller{
client: client,
id: id,
}
}

func (p localNetworkGatewayPoller) Poll(ctx context.Context) (*pollers.PollResult, error) {
resp, err := p.client.Get(ctx, p.id)
if err != nil {
return nil, fmt.Errorf("retrieving %s: %+v", p.id, err)
}

if resp.Model != nil {
if provisioningStatus := resp.Model.Properties.ProvisioningState; provisioningStatus != nil {
if !strings.EqualFold(string(*provisioningStatus), string(pollingSuccess.Status)) {
return &pollingInProgress, nil
}
}
}

return &pollingSuccess, nil
}
15 changes: 14 additions & 1 deletion internal/services/network/local_network_gateway_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package network

import (
"fmt"
"github.com/hashicorp/go-azure-sdk/sdk/client/pollers"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/network/custompollers"
stephybun marked this conversation as resolved.
Show resolved Hide resolved
"time"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
Expand Down Expand Up @@ -139,6 +141,10 @@ func resourceLocalNetworkGatewayCreateUpdate(d *pluginsdk.ResourceData, meta int
gateway.Properties.Fqdn = &fqdn
}

// This custompoller can be removed once https://github.com/hashicorp/go-azure-sdk/issues/989 has been fixed
pollerType := custompollers.NewLocalNetworkGatewayPoller(client, id)
poller := pollers.NewPoller(pollerType, 10*time.Second, pollers.DefaultNumberOfDroppedConnectionsToAllow)

// There is a bug in the provider where the address space ordering doesn't change as expected.
// In the UI we have to remove the current list of addresses in the address space and re-add them in the new order and we'll copy that here.
if !d.IsNewResource() && d.HasChange("address_space") {
Expand All @@ -154,13 +160,20 @@ func resourceLocalNetworkGatewayCreateUpdate(d *pluginsdk.ResourceData, meta int
if _, err := client.CreateOrUpdate(ctx, id, gateway); err != nil {
return fmt.Errorf("removing %s: %+v", id, err)
}
if err := poller.PollUntilDone(ctx); err != nil {
return err
}
}
gateway.Properties.LocalNetworkAddressSpace = expandLocalNetworkGatewayAddressSpaces(d)

if err := client.CreateOrUpdateThenPoll(ctx, id, gateway); err != nil {
if _, err := client.CreateOrUpdate(ctx, id, gateway); err != nil {
return fmt.Errorf("creating %s: %+v", id, err)
}

if err := poller.PollUntilDone(ctx); err != nil {
return err
}

d.SetId(id.ID())

return resourceLocalNetworkGatewayRead(d, meta)
Expand Down
Loading