Skip to content

Commit

Permalink
azurerm_system_center_virtual_machine_manager_server - add `StateRe…
Browse files Browse the repository at this point in the history
…freshFunc` for checking the sync status (hashicorp#26004)

* azurerm_system_center_virtual_machine_manager_server - add StateRefreshFunc for checking the sync status

* update description

* update timeout to 120 mins

* update md

* update md

* merge latest version
  • Loading branch information
neil-yechenwei authored Jun 19, 2024
1 parent ca16e3c commit 0ba163c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/go-azure-sdk/resource-manager/extendedlocation/2021-08-15/customlocations"
"github.com/hashicorp/go-azure-sdk/resource-manager/systemcentervirtualmachinemanager/2023-10-07/inventoryitems"
"github.com/hashicorp/go-azure-sdk/resource-manager/systemcentervirtualmachinemanager/2023-10-07/vmmservers"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/systemcentervirtualmachinemanager/validate"
Expand Down Expand Up @@ -103,7 +104,7 @@ func (r SystemCenterVirtualMachineManagerServerResource) Attributes() map[string

func (r SystemCenterVirtualMachineManagerServerResource) Create() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Timeout: 180 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
subscriptionId := metadata.Client.Account.SubscriptionId
client := metadata.Client.SystemCenterVirtualMachineManager.VMmServers
Expand Down Expand Up @@ -149,6 +150,20 @@ func (r SystemCenterVirtualMachineManagerServerResource) Create() sdk.ResourceFu
return fmt.Errorf("creating %s: %+v", id, err)
}

// After System Center Virtual Machine Manager Server is created, it needs some time to sync the Inventory Items. And service team confirmed that the sync would definitely be completed within 10 minutes. In case, so we need to set a timeout of 120 minutes and check the inventory quantity continuously every minute for 10 times. If the quantity doesn't change, then we consider the sync to be complete.
stateConf := &pluginsdk.StateChangeConf{
Delay: 5 * time.Second,
Pending: []string{"SyncNotCompleted"},
Target: []string{"SyncCompleted"},
Refresh: systemCenterVirtualMachineManagerServerStateRefreshFunc(ctx, metadata, id),
PollInterval: 1 * time.Minute,
Timeout: 120 * time.Minute,
}

if _, err = stateConf.WaitForStateContext(ctx); err != nil {
return fmt.Errorf("waiting for %s to become available: %s", id, err)
}

metadata.SetID(id)
return nil
},
Expand Down Expand Up @@ -198,7 +213,7 @@ func (r SystemCenterVirtualMachineManagerServerResource) Read() sdk.ResourceFunc

func (r SystemCenterVirtualMachineManagerServerResource) Update() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Timeout: 180 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.SystemCenterVirtualMachineManager.VMmServers

Expand Down Expand Up @@ -227,7 +242,7 @@ func (r SystemCenterVirtualMachineManagerServerResource) Update() sdk.ResourceFu

func (r SystemCenterVirtualMachineManagerServerResource) Delete() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Timeout: 180 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.SystemCenterVirtualMachineManager.VMmServers

Expand All @@ -246,3 +261,36 @@ func (r SystemCenterVirtualMachineManagerServerResource) Delete() sdk.ResourceFu
},
}
}

func systemCenterVirtualMachineManagerServerStateRefreshFunc(ctx context.Context, metadata sdk.ResourceMetaData, id vmmservers.VMmServerId) pluginsdk.StateRefreshFunc {
return func() (interface{}, string, error) {
client := metadata.Client.SystemCenterVirtualMachineManager.InventoryItems
scvmmServerId := inventoryitems.NewVMmServerID(id.SubscriptionId, id.ResourceGroupName, id.VmmServerName)
checkTimes := 10
lastInventoryItemCount := 0

for i := 0; i < checkTimes; i++ {
resp, err := client.ListByVMmServer(ctx, scvmmServerId)
if err != nil {
return nil, "", fmt.Errorf("polling for %s: %+v", id, err)
}

if model := resp.Model; model != nil {
currentInventoryItemCount := len(pointer.From(model))

if i == 0 {
lastInventoryItemCount = currentInventoryItemCount
continue
}

if currentInventoryItemCount != lastInventoryItemCount {
return "SyncNotCompleted", "SyncNotCompleted", nil
}

time.Sleep(1 * time.Second) // avoid checking too quickly
}
}

return "SyncCompleted", "SyncCompleted", nil
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ In addition to the Arguments listed above - the following Attributes are exporte

The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/docs/configuration/resources.html#timeouts) for certain actions:

* `create` - (Defaults to 30 minutes) Used when creating this System Center Virtual Machine Manager Server.
* `create` - (Defaults to 180 minutes) Used when creating this System Center Virtual Machine Manager Server.
* `read` - (Defaults to 5 minutes) Used when retrieving this System Center Virtual Machine Manager Server.
* `update` - (Defaults to 30 minutes) Used when updating this System Center Virtual Machine Manager Server.
* `delete` - (Defaults to 30 minutes) Used when deleting this System Center Virtual Machine Manager Server.
* `update` - (Defaults to 180 minutes) Used when updating this System Center Virtual Machine Manager Server.
* `delete` - (Defaults to 180 minutes) Used when deleting this System Center Virtual Machine Manager Server.

## Import

Expand Down

0 comments on commit 0ba163c

Please sign in to comment.