Skip to content

Commit

Permalink
dependencies - swap Virtual Machine Ids for `hashicorp/go-azure-helpe…
Browse files Browse the repository at this point in the history
…rs/commonids` (hashicorp#23306)
  • Loading branch information
mbfrahry authored Sep 19, 2023
1 parent 919d00d commit 2e393b5
Show file tree
Hide file tree
Showing 27 changed files with 284 additions and 587 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
validate4 "github.com/hashicorp/terraform-provider-azurerm/helpers/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/features"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
computeValidate "github.com/hashicorp/terraform-provider-azurerm/internal/services/compute/validate"
validate2 "github.com/hashicorp/terraform-provider-azurerm/internal/services/resource/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/suppress"
Expand Down Expand Up @@ -396,7 +395,7 @@ func (m SoftwareUpdateConfigurationResource) Arguments() map[string]*pluginsdk.S
Optional: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
ValidateFunc: computeValidate.VirtualMachineID,
ValidateFunc: commonids.ValidateVirtualMachineID,
},
},

Expand Down
4 changes: 2 additions & 2 deletions internal/services/compute/image_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/go-azure-helpers/resourcemanager/tags"
Expand All @@ -17,7 +18,6 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/compute/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/suppress"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
Expand Down Expand Up @@ -75,7 +75,7 @@ func resourceImage() *pluginsdk.Resource {
"source_virtual_machine_id": {
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: computeValidate.VirtualMachineID,
ValidateFunc: commonids.ValidateVirtualMachineID,
},

"os_disk": {
Expand Down
163 changes: 81 additions & 82 deletions internal/services/compute/linux_virtual_machine_resource.go

Large diffs are not rendered by default.

45 changes: 22 additions & 23 deletions internal/services/compute/managed_disk_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/internal/features"
"github.com/hashicorp/terraform-provider-azurerm/internal/locks"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/compute/migration"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/compute/parse"
"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/suppress"
Expand Down Expand Up @@ -827,7 +826,7 @@ func resourceManagedDiskUpdate(d *pluginsdk.ResourceData, meta interface{}) erro

// if we are attached to a VM we bring down the VM as necessary for the operations which are not allowed while it's online
if shouldShutDown {
virtualMachine, err := parse.VirtualMachineID(*disk.Model.ManagedBy)
virtualMachine, err := commonids.ParseVirtualMachineID(*disk.Model.ManagedBy)
if err != nil {
return fmt.Errorf("parsing VMID %q for disk attachment: %+v", *disk.Model.ManagedBy, err)
}
Expand All @@ -837,14 +836,14 @@ func resourceManagedDiskUpdate(d *pluginsdk.ResourceData, meta interface{}) erro
locks.ByName(name, VirtualMachineResourceName)
defer locks.UnlockByName(name, VirtualMachineResourceName)

vm, err := vmClient.Get(ctx, virtualMachine.ResourceGroup, virtualMachine.Name, "")
vm, err := vmClient.Get(ctx, virtualMachine.ResourceGroupName, virtualMachine.VirtualMachineName, "")
if err != nil {
return fmt.Errorf("retrieving Virtual Machine %q (Resource Group %q): %+v", virtualMachine.Name, virtualMachine.ResourceGroup, err)
return fmt.Errorf("retrieving %s: %+v", virtualMachine, err)
}

instanceView, err := vmClient.InstanceView(ctx, virtualMachine.ResourceGroup, virtualMachine.Name)
instanceView, err := vmClient.InstanceView(ctx, virtualMachine.ResourceGroupName, virtualMachine.VirtualMachineName)
if err != nil {
return fmt.Errorf("retrieving InstanceView for Virtual Machine %q (Resource Group %q): %+v", virtualMachine.Name, virtualMachine.ResourceGroup, err)
return fmt.Errorf("retrieving InstanceView for V%s: %+v", virtualMachine, err)
}

shouldTurnBackOn := virtualMachineShouldBeStarted(instanceView)
Expand Down Expand Up @@ -902,7 +901,7 @@ func resourceManagedDiskUpdate(d *pluginsdk.ResourceData, meta interface{}) erro
// fixes #1600
vm.Resources = nil

future, err := vmClient.CreateOrUpdate(ctx, virtualMachine.ResourceGroup, virtualMachine.Name, vm)
future, err := vmClient.CreateOrUpdate(ctx, virtualMachine.ResourceGroupName, virtualMachine.VirtualMachineName, vm)
if err != nil {
return fmt.Errorf("removing Disk %q from Virtual Machine %q : %+v", id.DiskName, virtualMachine.String(), err)
}
Expand All @@ -915,34 +914,34 @@ func resourceManagedDiskUpdate(d *pluginsdk.ResourceData, meta interface{}) erro

// Shutdown
if shouldShutDown {
log.Printf("[DEBUG] Shutting Down Virtual Machine %q (Resource Group %q)..", virtualMachine.Name, virtualMachine.ResourceGroup)
log.Printf("[DEBUG] Shutting Down %s", virtualMachine)
forceShutdown := false
future, err := vmClient.PowerOff(ctx, virtualMachine.ResourceGroup, virtualMachine.Name, utils.Bool(forceShutdown))
future, err := vmClient.PowerOff(ctx, virtualMachine.ResourceGroupName, virtualMachine.VirtualMachineName, utils.Bool(forceShutdown))
if err != nil {
return fmt.Errorf("sending Power Off to Virtual Machine %q (Resource Group %q): %+v", virtualMachine.Name, virtualMachine.ResourceGroup, err)
return fmt.Errorf("sending Power Off to %s: %+v", virtualMachine, err)
}

if err := future.WaitForCompletionRef(ctx, vmClient.Client); err != nil {
return fmt.Errorf("waiting for Power Off of Virtual Machine %q (Resource Group %q): %+v", virtualMachine.Name, virtualMachine.ResourceGroup, err)
return fmt.Errorf("waiting for Power Off of %s: %+v", virtualMachine, err)
}

log.Printf("[DEBUG] Shut Down Virtual Machine %q (Resource Group %q)..", virtualMachine.Name, virtualMachine.ResourceGroup)
log.Printf("[DEBUG] Shut Down %s", virtualMachine)
}

// De-allocate
if shouldDeallocate {
log.Printf("[DEBUG] Deallocating Virtual Machine %q (Resource Group %q)..", virtualMachine.Name, virtualMachine.ResourceGroup)
log.Printf("[DEBUG] Deallocating %s.", virtualMachine)
// Upgrading to 2021-07-01 exposed a new hibernate paramater to the Deallocate method
deAllocFuture, err := vmClient.Deallocate(ctx, virtualMachine.ResourceGroup, virtualMachine.Name, utils.Bool(false))
deAllocFuture, err := vmClient.Deallocate(ctx, virtualMachine.ResourceGroupName, virtualMachine.VirtualMachineName, utils.Bool(false))
if err != nil {
return fmt.Errorf("Deallocating to Virtual Machine %q (Resource Group %q): %+v", virtualMachine.Name, virtualMachine.ResourceGroup, err)
return fmt.Errorf("Deallocating to %s: %+v", virtualMachine, err)
}

if err := deAllocFuture.WaitForCompletionRef(ctx, vmClient.Client); err != nil {
return fmt.Errorf("waiting for Deallocation of Virtual Machine %q (Resource Group %q): %+v", virtualMachine.Name, virtualMachine.ResourceGroup, err)
return fmt.Errorf("waiting for Deallocation of %s: %+v", virtualMachine, err)
}

log.Printf("[DEBUG] Deallocated Virtual Machine %q (Resource Group %q)..", virtualMachine.Name, virtualMachine.ResourceGroup)
log.Printf("[DEBUG] Deallocated %s", virtualMachine)
}

// Update Disk
Expand All @@ -968,7 +967,7 @@ func resourceManagedDiskUpdate(d *pluginsdk.ResourceData, meta interface{}) erro
// if there's too many disks we get a 409 back with:
// `The maximum number of data disks allowed to be attached to a VM of this size is 1.`
// which we're intentionally not wrapping, since the errors good.
future, err := vmClient.CreateOrUpdate(ctx, virtualMachine.ResourceGroup, virtualMachine.Name, vm)
future, err := vmClient.CreateOrUpdate(ctx, virtualMachine.ResourceGroupName, virtualMachine.VirtualMachineName, vm)
if err != nil {
return fmt.Errorf("updating Virtual Machine %q to reattach Disk %q: %+v", virtualMachine.String(), name, err)
}
Expand All @@ -979,17 +978,17 @@ func resourceManagedDiskUpdate(d *pluginsdk.ResourceData, meta interface{}) erro
}

if shouldTurnBackOn && (shouldShutDown || shouldDeallocate) {
log.Printf("[DEBUG] Starting Linux Virtual Machine %q (Resource Group %q)..", virtualMachine.Name, virtualMachine.ResourceGroup)
future, err := vmClient.Start(ctx, virtualMachine.ResourceGroup, virtualMachine.Name)
log.Printf("[DEBUG] Starting %s", virtualMachine)
future, err := vmClient.Start(ctx, virtualMachine.ResourceGroupName, virtualMachine.VirtualMachineName)
if err != nil {
return fmt.Errorf("starting Virtual Machine %q (Resource Group %q): %+v", virtualMachine.Name, virtualMachine.ResourceGroup, err)
return fmt.Errorf("starting %s: %+v", virtualMachine, err)
}

if err := future.WaitForCompletionRef(ctx, vmClient.Client); err != nil {
return fmt.Errorf("waiting for start of Virtual Machine %q (Resource Group %q): %+v", virtualMachine.Name, virtualMachine.ResourceGroup, err)
return fmt.Errorf("waiting for start of %s %+v", virtualMachine, err)
}

log.Printf("[DEBUG] Started Virtual Machine %q (Resource Group %q)..", virtualMachine.Name, virtualMachine.ResourceGroup)
log.Printf("[DEBUG] Started %s", virtualMachine)
}
} else { // otherwise, just update it
err := client.UpdateThenPoll(ctx, *id, diskUpdate)
Expand Down
72 changes: 0 additions & 72 deletions internal/services/compute/parse/virtual_machine.go

This file was deleted.

115 changes: 0 additions & 115 deletions internal/services/compute/parse/virtual_machine_test.go

This file was deleted.

1 change: 0 additions & 1 deletion internal/services/compute/resourceids.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package compute
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=DiskEncryptionSet -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Compute/diskEncryptionSets/set1
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=SharedImage -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Compute/galleries/gallery1/images/image1
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=SharedImageVersion -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Compute/galleries/gallery1/images/image1/versions/version1
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=VirtualMachine -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Compute/virtualMachines/machine1
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=VirtualMachineExtension -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Compute/virtualMachines/machine1/extensions/extension1
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=VirtualMachineScaleSetExtension -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Compute/virtualMachineScaleSets/scaleSet1/extensions/extension1
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=SSHPublicKey -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Compute/sshPublicKeys/sshpublickey1
Expand Down
2 changes: 1 addition & 1 deletion internal/services/compute/shared_image_version_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func resourceSharedImageVersion() *pluginsdk.Resource {
ForceNew: true,
ValidateFunc: validation.Any(
images.ValidateImageID,
validate.VirtualMachineID,
commonids.ValidateVirtualMachineID,
),
ExactlyOneOf: []string{"blob_uri", "os_disk_snapshot_id", "managed_image_id"},
},
Expand Down
26 changes: 0 additions & 26 deletions internal/services/compute/validate/virtual_machine_id.go

This file was deleted.

Loading

0 comments on commit 2e393b5

Please sign in to comment.