Skip to content

Commit

Permalink
Merge pull request #25203 from hashicorp/f/resource-group-assertion-d…
Browse files Browse the repository at this point in the history
…eletion-use-go-azure-sdk

r/resource_group: updating the Exists and Delete checks to use `hashicorp/go-azure-sdk`
  • Loading branch information
tombuildsstuff authored Mar 11, 2024
2 parents 9c25731 + e6ee4fd commit 39b7d34
Showing 1 changed file with 30 additions and 12 deletions.
42 changes: 30 additions & 12 deletions internal/services/resource/resource_group_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
"regexp"
"testing"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/go-azure-sdk/resource-manager/resources/2023-07-01/resourcegroups"
"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"
Expand Down Expand Up @@ -120,31 +123,46 @@ func TestAccResourceGroup_withNestedItemsAndFeatureFlag(t *testing.T) {
}

func (t ResourceGroupResource) Destroy(ctx context.Context, client *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
resourceGroup := state.Attributes["name"]

groupsClient := client.Resource.GroupsClient
deleteFuture, err := groupsClient.Delete(ctx, resourceGroup, "Microsoft.Compute/virtualMachines,Microsoft.Compute/virtualMachineScaleSets")
// NOTE: Due to the Resource Group resource still using the old Azure SDK and sourcing the Resource Group ID
// from the Azure API, we need to support both `resourceGroups` and the legacy `resourcegroups` value here
// thus we parse this case-insensitively. This behaviour will be fixed in the future once the Resource is
// updated and a state migration is added to account for it, but this required additional coordination.
//
// If you're using this as a reference when building resources, please use the case-sensitive Resource ID
// parsing method instead.
id, err := commonids.ParseResourceGroupIDInsensitively(state.ID)
if err != nil {
return nil, fmt.Errorf("deleting Resource Group %q: %+v", resourceGroup, err)
return nil, err
}

err = deleteFuture.WaitForCompletionRef(ctx, groupsClient.Client)
if err != nil {
return nil, fmt.Errorf("waiting for deletion of Resource Group %q: %+v", resourceGroup, err)
opts := resourcegroups.DefaultDeleteOperationOptions()
opts.ForceDeletionTypes = pointer.To("Microsoft.Compute/virtualMachines,Microsoft.Compute/virtualMachineScaleSets")
if err := client.Resource.ResourceGroupsClient.DeleteThenPoll(ctx, *id, opts); err != nil {
return nil, fmt.Errorf("deleting %s: %+v", *id, err)
}

return utils.Bool(true), nil
}

func (t ResourceGroupResource) Exists(ctx context.Context, client *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
name := state.Attributes["name"]
// NOTE: Due to the Resource Group resource still using the old Azure SDK and sourcing the Resource Group ID
// from the Azure API, we need to support both `resourceGroups` and the legacy `resourcegroups` value here
// thus we parse this case-insensitively. This behaviour will be fixed in the future once the Resource is
// updated and a state migration is added to account for it, but this required additional coordination.
//
// If you're using this as a reference when building resources, please use the case-sensitive Resource ID
// parsing method instead.
id, err := commonids.ParseResourceGroupIDInsensitively(state.ID)
if err != nil {
return nil, err
}

resp, err := client.Resource.GroupsClient.Get(ctx, name)
resp, err := client.Resource.ResourceGroupsClient.Get(ctx, *id)
if err != nil {
return nil, fmt.Errorf("retrieving Resource Group %q: %+v", name, err)
return nil, fmt.Errorf("retrieving %s: %+v", *id, err)
}

return utils.Bool(resp.Properties != nil), nil
return utils.Bool(resp.Model != nil), nil
}

func (t ResourceGroupResource) createNetworkOutsideTerraform(name string) func(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) error {
Expand Down

0 comments on commit 39b7d34

Please sign in to comment.