Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
teowa committed Dec 30, 2024
1 parent fa76c82 commit cab84b5
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"log"
"regexp"
"time"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
Expand All @@ -22,6 +21,7 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/azurestackhci/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/azurestackhci/validate"
computeValidate "github.com/hashicorp/terraform-provider-azurerm/internal/services/compute/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
)
Expand Down Expand Up @@ -62,15 +62,15 @@ type StackHCIWindowsVirtualMachineResourceModel struct {

type StackHCIVirtualMachineHardwareProfile struct {
DynamicMemory []StackHCIVirtualMachineDynamicMemory `tfschema:"dynamic_memory"`
MemoryMb int64 `tfschema:"memory_mb"`
MemoryInMb int64 `tfschema:"memory_in_mb"`
ProcessorNumber int64 `tfschema:"processor_number"`
VmSize string `tfschema:"vm_size"`
}

type StackHCIVirtualMachineDynamicMemory struct {
MaximumMemoryMb int64 `tfschema:"maximum_memory_mb"`
MinimumMemoryMb int64 `tfschema:"minimum_memory_mb"`
TargetMemoryBuffer int64 `tfschema:"target_memory_buffer"`
MaximumMemoryInMb int64 `tfschema:"maximum_memory_in_mb"`
MinimumMemoryInMb int64 `tfschema:"minimum_memory_in_mb"`
TargetMemoryBufferPercentage int64 `tfschema:"target_memory_buffer_percentage"`
}

type StackHCIVirtualMachineHttpProxyConfiguration struct {
Expand Down Expand Up @@ -105,12 +105,9 @@ type StackHCIVirtualMachineSshPublicKey struct {
type StackHCIVirtualMachineStorageProfile struct {
DataDiskIds []string `tfschema:"data_disk_ids"`
ImageId string `tfschema:"image_id"`
OsDiskId string `tfschema:"os_disk_id"`
VmConfigStoragePathId string `tfschema:"vm_config_storage_path_id"`
}

type StackHCIVirtualMachineOsDisk struct{}

func (StackHCIWindowsVirtualMachineResource) Arguments() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"arc_machine_id": commonschema.ResourceIDReferenceRequiredForceNew(&machines.MachineId{}),
Expand Down Expand Up @@ -138,7 +135,7 @@ func (StackHCIWindowsVirtualMachineResource) Arguments() map[string]*pluginsdk.S
ValidateFunc: validation.IntAtLeast(1),
},

"memory_mb": {
"memory_in_mb": {
Type: pluginsdk.TypeInt,
Required: true,
ForceNew: true,
Expand All @@ -152,21 +149,21 @@ func (StackHCIWindowsVirtualMachineResource) Arguments() map[string]*pluginsdk.S
MaxItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"maximum_memory_mb": {
"maximum_memory_in_mb": {
Type: pluginsdk.TypeInt,
Required: true,
ForceNew: true,
ValidateFunc: validation.IntAtLeast(1),
},

"minimum_memory_mb": {
"minimum_memory_in_mb": {
Type: pluginsdk.TypeInt,
Required: true,
ForceNew: true,
ValidateFunc: validation.IntAtLeast(1),
},

"target_memory_buffer": {
"target_memory_buffer_percentage": {
Type: pluginsdk.TypeInt,
Required: true,
ForceNew: true,
Expand All @@ -189,10 +186,7 @@ func (StackHCIWindowsVirtualMachineResource) Arguments() map[string]*pluginsdk.S
Type: pluginsdk.TypeList,
Required: true,
MinItems: 1,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
ValidateFunc: networkinterfaces.ValidateNetworkInterfaceID,
},
Elem: commonschema.ResourceIDReferenceElem(&networkinterfaces.NetworkInterfaceId{}),
},
},
},
Expand All @@ -209,25 +203,22 @@ func (StackHCIWindowsVirtualMachineResource) Arguments() map[string]*pluginsdk.S
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
ValidateFunc: computeValidate.WindowsAdminUsername,
},

"admin_password": {
Type: pluginsdk.TypeString,
Optional: true,
ForceNew: true,
Sensitive: true,
ValidateFunc: validation.StringIsNotEmpty,
ValidateFunc: computeValidate.WindowsAdminPassword,
},

"computer_name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringMatch(
regexp.MustCompile(`^[\-a-zA-Z0-9]{0,15}$`),
"computer_name must begin and end with an alphanumeric character, be between 2 and 15 characters in length and can only contain alphanumeric characters and hyphens.",
),
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: computeValidate.WindowsComputerNameFull,
},

"automatic_update_enabled": {
Expand Down Expand Up @@ -315,10 +306,7 @@ func (StackHCIWindowsVirtualMachineResource) Arguments() map[string]*pluginsdk.S
Type: pluginsdk.TypeList,
Required: true,
MinItems: 1,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
ValidateFunc: virtualharddisks.ValidateVirtualHardDiskID,
},
Elem: commonschema.ResourceIDReferenceElem(&virtualharddisks.VirtualHardDiskId{}),
},

"image_id": {
Expand All @@ -331,19 +319,9 @@ func (StackHCIWindowsVirtualMachineResource) Arguments() map[string]*pluginsdk.S
),
},

"os_disk_id": {
Type: pluginsdk.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: virtualharddisks.ValidateVirtualHardDiskID,
},
"os_disk_id": commonschema.ResourceIDReferenceOptionalForceNew(&virtualharddisks.VirtualHardDiskId{}),

"vm_config_storage_path_id": {
Type: pluginsdk.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: storagecontainers.ValidateStorageContainerID,
},
"vm_config_storage_path_id": commonschema.ResourceIDReferenceOptionalForceNew(&storagecontainers.StorageContainerId{}),
},
},
},
Expand All @@ -357,17 +335,15 @@ func (StackHCIWindowsVirtualMachineResource) Arguments() map[string]*pluginsdk.S
Schema: map[string]*pluginsdk.Schema{
"http_proxy": {
Type: pluginsdk.TypeString,
Optional: true,
Required: true,
ForceNew: true,
Sensitive: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"https_proxy": {
Type: pluginsdk.TypeString,
Optional: true,
Required: true,
ForceNew: true,
Sensitive: true,
ValidateFunc: validation.StringIsNotEmpty,
},

Expand All @@ -385,6 +361,7 @@ func (StackHCIWindowsVirtualMachineResource) Arguments() map[string]*pluginsdk.S
Type: pluginsdk.TypeString,
Optional: true,
ForceNew: true,
Sensitive: true,
ValidateFunc: validation.StringIsNotEmpty,
},
},
Expand Down Expand Up @@ -592,7 +569,7 @@ func expandVirtualMachineHardwareProfile(input []StackHCIVirtualMachineHardwareP
v := input[0]
output := &virtualmachineinstances.VirtualMachineInstancePropertiesHardwareProfile{
DynamicMemoryConfig: expandVirtualMachineDynamicMemory(v.DynamicMemory),
MemoryMB: pointer.To(v.MemoryMb),
MemoryMB: pointer.To(v.MemoryInMb),
Processors: pointer.To(v.ProcessorNumber),
VMSize: pointer.To(virtualmachineinstances.VMSizeEnum(v.VmSize)),
}
Expand All @@ -608,7 +585,7 @@ func flattenVirtualMachineHardwareProfile(input *virtualmachineinstances.Virtual
return []StackHCIVirtualMachineHardwareProfile{
{
DynamicMemory: flattenVirtualMachineDynamicMemory(input.DynamicMemoryConfig),
MemoryMb: pointer.From(input.MemoryMB),
MemoryInMb: pointer.From(input.MemoryMB),
ProcessorNumber: pointer.From(input.Processors),
VmSize: string(pointer.From(input.VMSize)),
},
Expand All @@ -622,9 +599,9 @@ func expandVirtualMachineDynamicMemory(input []StackHCIVirtualMachineDynamicMemo

v := input[0]
output := &virtualmachineinstances.VirtualMachineInstancePropertiesHardwareProfileDynamicMemoryConfig{
MaximumMemoryMB: pointer.To(v.MaximumMemoryMb),
MinimumMemoryMB: pointer.To(v.MinimumMemoryMb),
TargetMemoryBuffer: pointer.To(v.TargetMemoryBuffer),
MaximumMemoryMB: pointer.To(v.MaximumMemoryInMb),
MinimumMemoryMB: pointer.To(v.MinimumMemoryInMb),
TargetMemoryBuffer: pointer.To(v.TargetMemoryBufferPercentage),
}

return output
Expand All @@ -642,9 +619,9 @@ func flattenVirtualMachineDynamicMemory(input *virtualmachineinstances.VirtualMa

return []StackHCIVirtualMachineDynamicMemory{
{
MaximumMemoryMb: pointer.From(input.MaximumMemoryMB),
MinimumMemoryMb: pointer.From(input.MinimumMemoryMB),
TargetMemoryBuffer: pointer.From(input.TargetMemoryBuffer),
MaximumMemoryInMb: pointer.From(input.MaximumMemoryMB),
MinimumMemoryInMb: pointer.From(input.MinimumMemoryMB),
TargetMemoryBufferPercentage: pointer.From(input.TargetMemoryBuffer),
},
}
}
Expand Down Expand Up @@ -688,7 +665,7 @@ func flattenVirtualMachineHttpProxyConfig(input *virtualmachineinstances.HTTPPro

func expandVirtualMachineNetworkProfileForUpdate(input []StackHCIVirtualMachineNetworkProfile) *virtualmachineinstances.NetworkProfileUpdate {
if len(input) == 0 {
return nil
return &virtualmachineinstances.NetworkProfileUpdate{}
}

networkInterfaces := make([]virtualmachineinstances.NetworkProfileUpdateNetworkInterfacesInlined, 0)
Expand Down Expand Up @@ -841,7 +818,7 @@ func expandVirtualMachineSecurityProfile(input StackHCIWindowsVirtualMachineReso

func expandVirtualMachineStorageProfileWindowsForUpdate(input []StackHCIVirtualMachineStorageProfile) *virtualmachineinstances.StorageProfileUpdate {
if len(input) == 0 {
return nil
return &virtualmachineinstances.StorageProfileUpdate{}
}

v := input[0]
Expand Down Expand Up @@ -884,10 +861,6 @@ func expandVirtualMachineStorageProfileWindows(input []StackHCIVirtualMachineSto
},
}

if v.OsDiskId != "" {
output.OsDisk.Id = pointer.To(v.OsDiskId)
}

if v.VmConfigStoragePathId != "" {
output.VMConfigStoragePathId = pointer.To(v.VmConfigStoragePathId)
}
Expand All @@ -914,16 +887,10 @@ func flattenVirtualMachineStorageProfileWindows(input *virtualmachineinstances.V
imageId = pointer.From(input.ImageReference.Id)
}

var osDiskId string
if input.OsDisk != nil {
osDiskId = pointer.From(input.OsDisk.Id)
}

return []StackHCIVirtualMachineStorageProfile{
{
DataDiskIds: dataDiskIds,
ImageId: imageId,
OsDiskId: osDiskId,
VmConfigStoragePathId: pointer.From(input.VMConfigStoragePathId),
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,14 @@ func testAccStackHCIWindowsVirtualMachine_requiresImport(t *testing.T) {
}

func (r StackHCIWindowsVirtualMachineResource) Exists(ctx context.Context, client *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
clusterClient := client.AzureStackHCI.VirtualMachineInstances
id, err := parse.StackHCIVirtualMachineID(state.ID)
if err != nil {
return nil, err
}

arcMachineId := machines.NewMachineID(id.SubscriptionId, id.ResourceGroup, id.MachineName)
scopeId := commonids.NewScopeID(arcMachineId.ID())
resp, err := clusterClient.Get(ctx, scopeId)
resp, err := client.AzureStackHCI.VirtualMachineInstances.Get(ctx, scopeId)
if err != nil {
return nil, fmt.Errorf("retrieving %s: %+v", id, err)
}
Expand All @@ -183,7 +182,7 @@ resource "azurerm_stack_hci_windows_virtual_machine" "test" {
hardware_profile {
vm_size = "Custom"
processor_number = 2
memory_mb = 8192
memory_in_mb = 8192
}
network_profile {
Expand Down Expand Up @@ -223,7 +222,7 @@ resource "azurerm_stack_hci_windows_virtual_machine" "import" {
hardware_profile {
vm_size = "Custom"
processor_number = 2
memory_mb = 8192
memory_in_mb = 8192
}
network_profile {
Expand Down Expand Up @@ -299,7 +298,7 @@ resource "azurerm_stack_hci_windows_virtual_machine" "test" {
hardware_profile {
vm_size = "Custom"
processor_number = 2
memory_mb = 8192
memory_in_mb = 8192
}
network_profile {
Expand Down Expand Up @@ -386,11 +385,11 @@ resource "azurerm_stack_hci_windows_virtual_machine" "test" {
hardware_profile {
vm_size = "Custom"
processor_number = 2
memory_mb = 8192
memory_in_mb = 8192
dynamic_memory {
maximum_memory_mb = 8192
minimum_memory_mb = 512
target_memory_buffer = 20
maximum_memory_in_mb = 8192
minimum_memory_in_mb = 512
target_memory_buffer_percentage = 20
}
}
Expand Down
Loading

0 comments on commit cab84b5

Please sign in to comment.