Skip to content

Commit

Permalink
dependencies - swap Virtual Machine Scale Set Ids for `hashicorp/go-a…
Browse files Browse the repository at this point in the history
…zure-helpers/commonids` (hashicorp#23272)
  • Loading branch information
mbfrahry authored Sep 15, 2023
1 parent 9636cfd commit f14aaf5
Show file tree
Hide file tree
Showing 31 changed files with 226 additions and 683 deletions.
8 changes: 4 additions & 4 deletions internal/services/compute/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"log"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2021-07-01/skus"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2021-11-01/availabilitysets"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2021-11-01/dedicatedhostgroups"
Expand All @@ -29,7 +30,6 @@ import (
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2023-04-02/disks"
"github.com/hashicorp/go-azure-sdk/resource-manager/marketplaceordering/2015-06-01/agreements"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/compute/parse"
"github.com/hashicorp/terraform-provider-azurerm/utils"
"github.com/tombuildsstuff/kermit/sdk/compute/2023-03-01/compute"
)
Expand Down Expand Up @@ -251,8 +251,8 @@ func NewClient(o *common.ClientOptions) (*Client, error) {
}, nil
}

func (c *Client) CancelRollingUpgradesBeforeDeletion(ctx context.Context, id parse.VirtualMachineScaleSetId) error {
resp, err := c.VMScaleSetRollingUpgradesClient.GetLatest(ctx, id.ResourceGroup, id.Name)
func (c *Client) CancelRollingUpgradesBeforeDeletion(ctx context.Context, id commonids.VirtualMachineScaleSetId) error {
resp, err := c.VMScaleSetRollingUpgradesClient.GetLatest(ctx, id.ResourceGroupName, id.VirtualMachineScaleSetName)
if err != nil {
// No rolling upgrades are running so skipping attempt to cancel them before deletion
if utils.ResponseWasNotFound(resp.Response) {
Expand All @@ -271,7 +271,7 @@ func (c *Client) CancelRollingUpgradesBeforeDeletion(ctx context.Context, id par
return nil
}

future, err := c.VMScaleSetRollingUpgradesClient.Cancel(ctx, id.ResourceGroup, id.Name)
future, err := c.VMScaleSetRollingUpgradesClient.Cancel(ctx, id.ResourceGroupName, id.VirtualMachineScaleSetName)
if err != nil {
// If there is no rolling upgrade the API will throw a 409/No rolling upgrade to cancel
// we don't error out in this case
Expand Down
6 changes: 3 additions & 3 deletions internal/services/compute/image_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ 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-sdk/resource-manager/compute/2021-11-01/virtualmachines"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-01/images"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/ssh"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/compute/parse"
networkParse "github.com/hashicorp/terraform-provider-azurerm/internal/services/network/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
Expand Down Expand Up @@ -332,13 +332,13 @@ func (ImageResource) virtualMachineExists(ctx context.Context, client *clients.C
}

func (ImageResource) virtualMachineScaleSetExists(ctx context.Context, client *clients.Client, state *pluginsdk.InstanceState) error {
id, err := parse.VirtualMachineScaleSetID(state.ID)
id, err := commonids.ParseVirtualMachineScaleSetID(state.ID)
if err != nil {
return err
}

// Upgrading to the 2021-07-01 exposed a new expand parameter in the GET method
resp, err := client.Compute.VMScaleSetClient.Get(ctx, id.ResourceGroup, id.Name, "")
resp, err := client.Compute.VMScaleSetClient.Get(ctx, id.ResourceGroupName, id.VirtualMachineScaleSetName, "")
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("%s does not exist", *id)
Expand Down
3 changes: 2 additions & 1 deletion internal/services/compute/linux_virtual_machine_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"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-sdk/resource-manager/compute/2021-11-01/availabilitysets"
Expand Down Expand Up @@ -337,7 +338,7 @@ func resourceLinuxVirtualMachine() *pluginsdk.Resource {
ConflictsWith: []string{
"availability_set_id",
},
ValidateFunc: computeValidate.VirtualMachineScaleSetID,
ValidateFunc: commonids.ValidateVirtualMachineScaleSetID,
},

"vtpm_enabled": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log"
"time"

"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/zones"
Expand All @@ -20,7 +21,6 @@ import (
azValidate "github.com/hashicorp/terraform-provider-azurerm/helpers/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/features"
"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/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/base64"
Expand All @@ -40,7 +40,7 @@ func resourceLinuxVirtualMachineScaleSet() *pluginsdk.Resource {
Delete: resourceLinuxVirtualMachineScaleSetDelete,

Importer: pluginsdk.ImporterValidatingResourceIdThen(func(id string) error {
_, err := parse.VirtualMachineScaleSetID(id)
_, err := commonids.ParseVirtualMachineScaleSetID(id)
return err
}, importVirtualMachineScaleSet(compute.OperatingSystemTypesLinux, "azurerm_linux_virtual_machine_scale_set")),

Expand All @@ -64,10 +64,10 @@ func resourceLinuxVirtualMachineScaleSetCreate(d *pluginsdk.ResourceData, meta i
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
defer cancel()

id := parse.NewVirtualMachineScaleSetID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))
id := commonids.NewVirtualMachineScaleSetID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))

// Upgrading to the 2021-07-01 exposed a new expand parameter to the GET method
exists, err := client.Get(ctx, id.ResourceGroup, id.Name, compute.ExpandTypesForGetVMScaleSetsUserData)
exists, err := client.Get(ctx, id.ResourceGroupName, id.VirtualMachineScaleSetName, compute.ExpandTypesForGetVMScaleSetsUserData)
if err != nil {
if !utils.ResponseWasNotFound(exists.Response) {
return fmt.Errorf("checking for existing Linux %s: %+v", id, err)
Expand Down Expand Up @@ -159,7 +159,7 @@ func resourceLinuxVirtualMachineScaleSetCreate(d *pluginsdk.ResourceData, meta i
if len(errs) > 0 {
return fmt.Errorf("unable to assume default computer name prefix %s. Please adjust the %q, or specify an explicit %q", errs[0], "name", "computer_name_prefix")
}
computerNamePrefix = id.Name
computerNamePrefix = id.VirtualMachineScaleSetName
}

disablePasswordAuthentication := d.Get("disable_password_authentication").(bool)
Expand Down Expand Up @@ -445,7 +445,7 @@ func resourceLinuxVirtualMachineScaleSetCreate(d *pluginsdk.ResourceData, meta i
}

log.Printf("[DEBUG] Creating Linux %s", id)
future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.Name, props)
future, err := client.CreateOrUpdate(ctx, id.ResourceGroupName, id.VirtualMachineScaleSetName, props)
if err != nil {
return fmt.Errorf("creating Linux %s: %+v", id, err)
}
Expand All @@ -466,7 +466,7 @@ func resourceLinuxVirtualMachineScaleSetUpdate(d *pluginsdk.ResourceData, meta i
ctx, cancel := timeouts.ForUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.VirtualMachineScaleSetID(d.Id())
id, err := commonids.ParseVirtualMachineScaleSetID(d.Id())
if err != nil {
return err
}
Expand All @@ -475,18 +475,18 @@ func resourceLinuxVirtualMachineScaleSetUpdate(d *pluginsdk.ResourceData, meta i

// retrieve
// Upgrading to the 2021-07-01 exposed a new expand parameter to the GET method
existing, err := client.Get(ctx, id.ResourceGroup, id.Name, compute.ExpandTypesForGetVMScaleSetsUserData)
existing, err := client.Get(ctx, id.ResourceGroupName, id.VirtualMachineScaleSetName, compute.ExpandTypesForGetVMScaleSetsUserData)
if err != nil {
return fmt.Errorf("retrieving Linux Virtual Machine Scale Set %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
return fmt.Errorf("retrieving Linux %s: %+v", id, err)
}
if existing.VirtualMachineScaleSetProperties == nil {
return fmt.Errorf("retrieving Linux Virtual Machine Scale Set %q (Resource Group %q): `properties` was nil", id.Name, id.ResourceGroup)
return fmt.Errorf("retrieving Linux %s: `properties` was nil", id)
}
if existing.VirtualMachineScaleSetProperties.VirtualMachineProfile == nil {
return fmt.Errorf("retrieving Linux Virtual Machine Scale Set %q (Resource Group %q): `properties.virtualMachineProfile` was nil", id.Name, id.ResourceGroup)
return fmt.Errorf("retrieving Linux %s: `properties.virtualMachineProfile` was nil", id)
}
if existing.VirtualMachineScaleSetProperties.VirtualMachineProfile.StorageProfile == nil {
return fmt.Errorf("retrieving Linux Virtual Machine Scale Set %q (Resource Group %q): `properties.virtualMachineProfile,storageProfile` was nil", id.Name, id.ResourceGroup)
return fmt.Errorf("retrieving Linux %s: `properties.virtualMachineProfile,storageProfile` was nil", id)
}

updateProps := compute.VirtualMachineScaleSetUpdateProperties{
Expand Down Expand Up @@ -808,25 +808,25 @@ func resourceLinuxVirtualMachineScaleSetRead(d *pluginsdk.ResourceData, meta int
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.VirtualMachineScaleSetID(d.Id())
id, err := commonids.ParseVirtualMachineScaleSetID(d.Id())
if err != nil {
return err
}

// Upgrading to the 2021-07-01 exposed a new expand parameter to the GET method
resp, err := client.Get(ctx, id.ResourceGroup, id.Name, compute.ExpandTypesForGetVMScaleSetsUserData)
resp, err := client.Get(ctx, id.ResourceGroupName, id.VirtualMachineScaleSetName, compute.ExpandTypesForGetVMScaleSetsUserData)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[DEBUG] Linux Virtual Machine Scale Set %q was not found in Resource Group %q - removing from state!", id.Name, id.ResourceGroup)
log.Printf("[DEBUG] Linux %s - removing from state!", id)
d.SetId("")
return nil
}

return fmt.Errorf("retrieving Linux Virtual Machine Scale Set %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
return fmt.Errorf("retrieving Linux %s: %+v", id, err)
}

d.Set("name", id.Name)
d.Set("resource_group_name", id.ResourceGroup)
d.Set("name", id.VirtualMachineScaleSetName)
d.Set("resource_group_name", id.ResourceGroupName)
d.Set("location", location.NormalizeNilable(resp.Location))
d.Set("edge_zone", flattenEdgeZone(resp.ExtendedLocation))
d.Set("zones", zones.FlattenUntyped(resp.Zones))
Expand Down Expand Up @@ -855,7 +855,7 @@ func resourceLinuxVirtualMachineScaleSetRead(d *pluginsdk.ResourceData, meta int
}

if resp.VirtualMachineScaleSetProperties == nil {
return fmt.Errorf("retrieving Linux Virtual Machine Scale Set %q (Resource Group %q): `properties` was nil", id.Name, id.ResourceGroup)
return fmt.Errorf("retrieving Linux %s: `properties` was nil", id)
}
props := *resp.VirtualMachineScaleSetProperties

Expand Down Expand Up @@ -1075,19 +1075,19 @@ func resourceLinuxVirtualMachineScaleSetDelete(d *pluginsdk.ResourceData, meta i
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.VirtualMachineScaleSetID(d.Id())
id, err := commonids.ParseVirtualMachineScaleSetID(d.Id())
if err != nil {
return err
}

// Upgrading to the 2021-07-01 exposed a new expand parameter to the GET method
resp, err := client.Get(ctx, id.ResourceGroup, id.Name, compute.ExpandTypesForGetVMScaleSetsUserData)
resp, err := client.Get(ctx, id.ResourceGroupName, id.VirtualMachineScaleSetName, compute.ExpandTypesForGetVMScaleSetsUserData)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return nil
}

return fmt.Errorf("retrieving Linux Virtual Machine Scale Set %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
return fmt.Errorf("retrieving Linux %s: %+v", id, err)
}

// If rolling upgrades are configured and running we need to cancel them before trying to delete the VMSS
Expand All @@ -1109,36 +1109,36 @@ func resourceLinuxVirtualMachineScaleSetDelete(d *pluginsdk.ResourceData, meta i
update := compute.VirtualMachineScaleSetUpdate{
Sku: resp.Sku,
}
future, err := client.Update(ctx, id.ResourceGroup, id.Name, update)
future, err := client.Update(ctx, id.ResourceGroupName, id.VirtualMachineScaleSetName, update)
if err != nil {
return fmt.Errorf("updating number of instances in Linux Virtual Machine Scale Set %q (Resource Group %q) to scale to 0: %+v", id.Name, id.ResourceGroup, err)
return fmt.Errorf("updating number of instances in %s to scale to 0: %+v", id, err)
}

log.Printf("[DEBUG] Waiting for scaling of instances to 0 prior to deletion - this helps avoids networking issues within Azure")
err = future.WaitForCompletionRef(ctx, client.Client)
if err != nil {
return fmt.Errorf("waiting for number of instances in Linux Virtual Machine Scale Set %q (Resource Group %q) to scale to 0: %+v", id.Name, id.ResourceGroup, err)
return fmt.Errorf("waiting for number of instances in Linux %s to scale to 0: %+v", id, err)
}
log.Printf("[DEBUG] Scaled instances to 0 prior to deletion - this helps avoids networking issues within Azure")
} else {
log.Printf("[DEBUG] Unable to scale instances to `0` since the `sku` block is nil - trying to delete anyway")
}

log.Printf("[DEBUG] Deleting Linux Virtual Machine Scale Set %q (Resource Group %q)..", id.Name, id.ResourceGroup)
log.Printf("[DEBUG] Deleting Linux %s", id)
// @ArcturusZhang (mimicking from linux_virtual_machine_pluginsdk.go): sending `nil` here omits this value from being sent
// which matches the previous behaviour - we're only splitting this out so it's clear why
// TODO: support force deletion once it's out of Preview, if applicable
var forceDeletion *bool = nil
future, err := client.Delete(ctx, id.ResourceGroup, id.Name, forceDeletion)
future, err := client.Delete(ctx, id.ResourceGroupName, id.VirtualMachineScaleSetName, forceDeletion)
if err != nil {
return fmt.Errorf("deleting Linux Virtual Machine Scale Set %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
return fmt.Errorf("deleting Linux %s: %+v", id, err)
}

log.Printf("[DEBUG] Waiting for deletion of Linux Virtual Machine Scale Set %q (Resource Group %q)..", id.Name, id.ResourceGroup)
log.Printf("[DEBUG] Waiting for deletion of Linux %s", id)
if err := future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for deletion of Linux Virtual Machine Scale Set %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
return fmt.Errorf("waiting for deletion of Linux %s: %+v", id, err)
}
log.Printf("[DEBUG] Deleted Linux Virtual Machine Scale Set %q (Resource Group %q).", id.Name, id.ResourceGroup)
log.Printf("[DEBUG] Deleted Linux %s", id)

return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
"testing"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/compute/parse"
"github.com/tombuildsstuff/kermit/sdk/compute/2023-03-01/compute"
)

Expand Down Expand Up @@ -803,12 +803,12 @@ func TestAccLinuxVirtualMachineScaleSet_otherCancelRollingUpgrades(t *testing.T)
// and simulates the scenario where rolling upgrades are running when we try to delete a VMSS
client := clients.Compute.VMScaleSetClient

id, err := parse.VirtualMachineScaleSetID(state.Attributes["id"])
id, err := commonids.ParseVirtualMachineScaleSetID(state.Attributes["id"])
if err != nil {
return err
}

existing, err := client.Get(ctx, id.ResourceGroup, id.Name, compute.ExpandTypesForGetVMScaleSetsUserData)
existing, err := client.Get(ctx, id.ResourceGroupName, id.VirtualMachineScaleSetName, compute.ExpandTypesForGetVMScaleSetsUserData)
if err != nil {
return fmt.Errorf("retrieving %s: %+v", *id, err)
}
Expand All @@ -834,7 +834,7 @@ func TestAccLinuxVirtualMachineScaleSet_otherCancelRollingUpgrades(t *testing.T)
VirtualMachineScaleSetUpdateProperties: &updateProps,
}

if _, err := client.Update(ctx, id.ResourceGroup, id.Name, update); err != nil {
if _, err := client.Update(ctx, id.ResourceGroupName, id.VirtualMachineScaleSetName, update); err != nil {
return fmt.Errorf("updating %s: %+v", *id, err)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@ import (
"context"
"fmt"

"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/compute/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

type LinuxVirtualMachineScaleSetResource struct{}

func (r LinuxVirtualMachineScaleSetResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.VirtualMachineScaleSetID(state.ID)
id, err := commonids.ParseVirtualMachineScaleSetID(state.ID)
if err != nil {
return nil, err
}

resp, err := clients.Compute.VMScaleSetClient.Get(ctx, id.ResourceGroup, id.Name, "")
resp, err := clients.Compute.VMScaleSetClient.Get(ctx, id.ResourceGroupName, id.VirtualMachineScaleSetName, "")
if err != nil {
return nil, fmt.Errorf("retrieving Compute Linux Virtual Machine Scale Set %q: %+v", id, err)
return nil, fmt.Errorf("retrieving Linux %s: %+v", id, err)
}

return utils.Bool(resp.ID != nil), nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
"fmt"
"time"

"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/identity"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/compute/parse"
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/utils"
Expand Down Expand Up @@ -149,9 +149,9 @@ func (r OrchestratedVirtualMachineScaleSetDataSource) Read() sdk.ResourceFunc {
return err
}

id := parse.NewVirtualMachineScaleSetID(subscriptionId, orchestratedVMSS.ResourceGroup, orchestratedVMSS.Name)
id := commonids.NewVirtualMachineScaleSetID(subscriptionId, orchestratedVMSS.ResourceGroup, orchestratedVMSS.Name)

existing, err := client.Get(ctx, id.ResourceGroup, id.Name, compute.ExpandTypesForGetVMScaleSetsUserData)
existing, err := client.Get(ctx, id.ResourceGroupName, id.VirtualMachineScaleSetName, compute.ExpandTypesForGetVMScaleSetsUserData)
if err != nil {
if utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("%s not found", id)
Expand Down
Loading

0 comments on commit f14aaf5

Please sign in to comment.