Skip to content

Commit

Permalink
azurerm_storage_sync_server_endpoint create/update polling (#26204)
Browse files Browse the repository at this point in the history
  • Loading branch information
bruceharrison1984 authored Jun 4, 2024
1 parent 64a989a commit 818118d
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package custompollers

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

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-sdk/resource-manager/storagesync/2020-03-01/serverendpointresource"
"github.com/hashicorp/go-azure-sdk/sdk/client"
"github.com/hashicorp/go-azure-sdk/sdk/client/pollers"
)

var _ pollers.PollerType = &storageSyncServerEndpointPoller{}

type storageSyncServerEndpointPoller struct {
client *serverendpointresource.ServerEndpointResourceClient
id serverendpointresource.ServerEndpointId
}

// The `ServerEndpointsCreateThenPoll` and `ServerEndpointsUpdateThenPoll` methods do not properly await, so a custom poller is required
func NewStorageSyncServerEndpointPoller(client *serverendpointresource.ServerEndpointResourceClient, id serverendpointresource.ServerEndpointId) *storageSyncServerEndpointPoller {
return &storageSyncServerEndpointPoller{
client: client,
id: id,
}
}

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

provisioningState := ""
if model := resp.Model; model != nil && model.Properties != nil {
provisioningState = pointer.From(model.Properties.ProvisioningState)
}

if strings.EqualFold(provisioningState, "Succeeded") {
return &pollers.PollResult{
HttpResponse: &client.Response{
Response: resp.HttpResponse,
},
PollInterval: 10 * time.Second,
Status: pollers.PollingStatusSucceeded,
}, nil
}

if strings.EqualFold(provisioningState, "runServerJob") {
return &pollers.PollResult{
HttpResponse: &client.Response{
Response: resp.HttpResponse,
},
PollInterval: 10 * time.Second,
Status: pollers.PollingStatusInProgress,
}, nil
}

return nil, pollers.PollingFailedError{
HttpResponse: &client.Response{
Response: resp.HttpResponse,
},
Message: fmt.Sprintf("unexpected provisioningState %q", provisioningState),
}
}
16 changes: 16 additions & 0 deletions internal/services/storage/storage_sync_server_endpoint_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import (
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-sdk/resource-manager/storagesync/2020-03-01/registeredserverresource"
"github.com/hashicorp/go-azure-sdk/resource-manager/storagesync/2020-03-01/serverendpointresource"
"github.com/hashicorp/go-azure-sdk/sdk/client/pollers"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/storage/custompollers"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
)
Expand Down Expand Up @@ -162,10 +164,17 @@ func (r SyncServerEndpointResource) Create() sdk.ResourceFunc {
payload.Properties.TierFilesOlderThanDays = pointer.To(config.TierFilesOlderThanDays)
}

pollerType := custompollers.NewStorageSyncServerEndpointPoller(client, id)
poller := pollers.NewPoller(pollerType, 20*time.Second, pollers.DefaultNumberOfDroppedConnectionsToAllow)

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

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

metadata.SetID(id)
return nil
},
Expand Down Expand Up @@ -267,10 +276,17 @@ func (r SyncServerEndpointResource) Update() sdk.ResourceFunc {
payload.Properties.TierFilesOlderThanDays = pointer.To(config.TierFilesOlderThanDays)
}

pollerType := custompollers.NewStorageSyncServerEndpointPoller(client, *id)
poller := pollers.NewPoller(pollerType, 20*time.Second, pollers.DefaultNumberOfDroppedConnectionsToAllow)

if _, err = client.ServerEndpointsUpdate(ctx, *id, payload); err != nil {
return fmt.Errorf("updating %s: %+v", *id, err)
}

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

return nil
},
}
Expand Down
13 changes: 12 additions & 1 deletion website/docs/r/storage_sync_server_endpoint.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ description: |-

Manages a Storage Sync Server Endpoint.

~> **NOTE:** The parent `azurerm_storage_sync_group` must have an `azurerm_storage_sync_cloud_endpoint` available before an `azurerm_storage_sync_server_endpoint` resource can be created.

## Example Usage

```hcl
Expand Down Expand Up @@ -50,10 +52,19 @@ resource "azurerm_storage_share" "example" {
}
}
resource "azurerm_storage_sync_cloud_endpoint" "example" {
name = "example-ss-ce"
storage_sync_group_id = azurerm_storage_sync_group.example.id
file_share_name = azurerm_storage_share.example.name
storage_account_id = azurerm_storage_account.example.id
}
resource "azurerm_storage_sync_server_endpoint" "example" {
name = "example-storage-sync-server-endpoint"
storage_sync_group_id = azurerm_storage_sync_group.example.id
registered_server_id = azurerm_storage_sync.example.registered_servers[0]
depends_on = [azurerm_storage_sync_cloud_endpoint.example]
}
```

Expand All @@ -67,7 +78,7 @@ The following arguments are supported:

* `registered_server_id` - (Required) The ID of the Registered Server that will be associate with the Storage Sync Server Endpoint. Changing this forces a new Storage Sync Server Endpoint to be created.

~> **NOTE:** For more information on registering a server see the [Microsoft documentation](https://learn.microsoft.com/azure/storage/file-sync/file-sync-server-registration)
~> **NOTE:** The target server must already be registered with the parent `azurerm_storage_sync` prior to creating this endpoint. For more information on registering a server see the [Microsoft documentation](https://learn.microsoft.com/azure/storage/file-sync/file-sync-server-registration)

* `server_local_path` - (Required) The path on the Windows Server to be synced to the Azure file share. Changing this forces a new Storage Sync Server Endpoint to be created.

Expand Down

0 comments on commit 818118d

Please sign in to comment.