diff --git a/internal/services/systemcentervirtualmachinemanager/system_center_virtual_machine_manager_server_resource.go b/internal/services/systemcentervirtualmachinemanager/system_center_virtual_machine_manager_server_resource.go index 10afeec993a2..49e57de62cc6 100644 --- a/internal/services/systemcentervirtualmachinemanager/system_center_virtual_machine_manager_server_resource.go +++ b/internal/services/systemcentervirtualmachinemanager/system_center_virtual_machine_manager_server_resource.go @@ -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" @@ -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 @@ -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 }, @@ -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 @@ -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 @@ -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 + } +} diff --git a/website/docs/r/system_center_virtual_machine_manager_server.html.markdown b/website/docs/r/system_center_virtual_machine_manager_server.html.markdown index 109cc0f4e6d8..5cdb9c2bc57a 100644 --- a/website/docs/r/system_center_virtual_machine_manager_server.html.markdown +++ b/website/docs/r/system_center_virtual_machine_manager_server.html.markdown @@ -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