diff --git a/internal/services/compute/managed_disk_resource.go b/internal/services/compute/managed_disk_resource.go index 13e30a28660e9..79a597215defa 100644 --- a/internal/services/compute/managed_disk_resource.go +++ b/internal/services/compute/managed_disk_resource.go @@ -41,7 +41,7 @@ func resourceManagedDisk() *pluginsdk.Resource { }), Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { - _, err := parse.ManagedDiskID(id) + _, err := disks.ParseDiskID(id) return err }), diff --git a/internal/services/compute/migration/managed_disk_v0_to_v1.go b/internal/services/compute/migration/managed_disk_v0_to_v1.go index 922eaa7c0b558..57e7160be091e 100644 --- a/internal/services/compute/migration/managed_disk_v0_to_v1.go +++ b/internal/services/compute/migration/managed_disk_v0_to_v1.go @@ -2,6 +2,7 @@ package migration import ( "context" + "log" "github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-02/disks" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" @@ -15,12 +16,15 @@ type ManagedDiskV0ToV1 struct{} func (ManagedDiskV0ToV1) UpgradeFunc() pluginsdk.StateUpgraderFunc { return func(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) { - oldId, err := disks.ParseDiskIDInsensitively(rawState["id"].(string)) + oldIdRaw := rawState["id"].(string) + oldId, err := disks.ParseDiskIDInsensitively(oldIdRaw) if err != nil { return rawState, err } - rawState["id"] = oldId.ID() + newId := oldId.ID() + log.Printf("[DEBUG] Updating the ID from %q to %q", oldIdRaw, newId) + rawState["id"] = newId return rawState, nil } } @@ -129,7 +133,7 @@ func (ManagedDiskV0ToV1) Schema() map[string]*pluginsdk.Schema { Optional: true, }, - "encryption_settings": encryptionSettingsSchema(), + "encryption_settings": managedDiskEncryptionSettingsSchemaV0(), "network_access_policy": { Type: pluginsdk.TypeString, @@ -197,7 +201,7 @@ func (ManagedDiskV0ToV1) Schema() map[string]*pluginsdk.Schema { } } -func encryptionSettingsSchema() *pluginsdk.Schema { +func managedDiskEncryptionSettingsSchemaV0() *pluginsdk.Schema { if !features.FourPointOhBeta() { return &pluginsdk.Schema{ Type: pluginsdk.TypeList, diff --git a/internal/services/compute/parse/managed_disk.go b/internal/services/compute/parse/managed_disk.go deleted file mode 100644 index 006142550e961..0000000000000 --- a/internal/services/compute/parse/managed_disk.go +++ /dev/null @@ -1,69 +0,0 @@ -package parse - -// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten - -import ( - "fmt" - "strings" - - "github.com/hashicorp/go-azure-helpers/resourcemanager/resourceids" -) - -type ManagedDiskId struct { - SubscriptionId string - ResourceGroup string - DiskName string -} - -func NewManagedDiskID(subscriptionId, resourceGroup, diskName string) ManagedDiskId { - return ManagedDiskId{ - SubscriptionId: subscriptionId, - ResourceGroup: resourceGroup, - DiskName: diskName, - } -} - -func (id ManagedDiskId) String() string { - segments := []string{ - fmt.Sprintf("Disk Name %q", id.DiskName), - fmt.Sprintf("Resource Group %q", id.ResourceGroup), - } - segmentsStr := strings.Join(segments, " / ") - return fmt.Sprintf("%s: (%s)", "Managed Disk", segmentsStr) -} - -func (id ManagedDiskId) ID() string { - fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/disks/%s" - return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroup, id.DiskName) -} - -// ManagedDiskID parses a ManagedDisk ID into an ManagedDiskId struct -func ManagedDiskID(input string) (*ManagedDiskId, error) { - id, err := resourceids.ParseAzureResourceID(input) - if err != nil { - return nil, err - } - - resourceId := ManagedDiskId{ - SubscriptionId: id.SubscriptionID, - ResourceGroup: id.ResourceGroup, - } - - if resourceId.SubscriptionId == "" { - return nil, fmt.Errorf("ID was missing the 'subscriptions' element") - } - - if resourceId.ResourceGroup == "" { - return nil, fmt.Errorf("ID was missing the 'resourceGroups' element") - } - - if resourceId.DiskName, err = id.PopSegment("disks"); err != nil { - return nil, err - } - - if err := id.ValidateNoEmptySegments(input); err != nil { - return nil, err - } - - return &resourceId, nil -} diff --git a/internal/services/compute/parse/managed_disk_test.go b/internal/services/compute/parse/managed_disk_test.go deleted file mode 100644 index 76c228d7a1785..0000000000000 --- a/internal/services/compute/parse/managed_disk_test.go +++ /dev/null @@ -1,112 +0,0 @@ -package parse - -// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten - -import ( - "testing" - - "github.com/hashicorp/go-azure-helpers/resourcemanager/resourceids" -) - -var _ resourceids.Id = ManagedDiskId{} - -func TestManagedDiskIDFormatter(t *testing.T) { - actual := NewManagedDiskID("12345678-1234-9876-4563-123456789012", "resGroup1", "disk1").ID() - expected := "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Compute/disks/disk1" - if actual != expected { - t.Fatalf("Expected %q but got %q", expected, actual) - } -} - -func TestManagedDiskID(t *testing.T) { - testData := []struct { - Input string - Error bool - Expected *ManagedDiskId - }{ - - { - // empty - Input: "", - Error: true, - }, - - { - // missing SubscriptionId - Input: "/", - Error: true, - }, - - { - // missing value for SubscriptionId - Input: "/subscriptions/", - Error: true, - }, - - { - // missing ResourceGroup - Input: "/subscriptions/12345678-1234-9876-4563-123456789012/", - Error: true, - }, - - { - // missing value for ResourceGroup - Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/", - Error: true, - }, - - { - // missing DiskName - Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Compute/", - Error: true, - }, - - { - // missing value for DiskName - Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Compute/disks/", - Error: true, - }, - - { - // valid - Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Compute/disks/disk1", - Expected: &ManagedDiskId{ - SubscriptionId: "12345678-1234-9876-4563-123456789012", - ResourceGroup: "resGroup1", - DiskName: "disk1", - }, - }, - - { - // upper-cased - Input: "/SUBSCRIPTIONS/12345678-1234-9876-4563-123456789012/RESOURCEGROUPS/RESGROUP1/PROVIDERS/MICROSOFT.COMPUTE/DISKS/DISK1", - Error: true, - }, - } - - for _, v := range testData { - t.Logf("[DEBUG] Testing %q", v.Input) - - actual, err := ManagedDiskID(v.Input) - if err != nil { - if v.Error { - continue - } - - t.Fatalf("Expect a value but got an error: %s", err) - } - if v.Error { - t.Fatal("Expect an error but didn't get one") - } - - if actual.SubscriptionId != v.Expected.SubscriptionId { - t.Fatalf("Expected %q but got %q for SubscriptionId", v.Expected.SubscriptionId, actual.SubscriptionId) - } - if actual.ResourceGroup != v.Expected.ResourceGroup { - t.Fatalf("Expected %q but got %q for ResourceGroup", v.Expected.ResourceGroup, actual.ResourceGroup) - } - if actual.DiskName != v.Expected.DiskName { - t.Fatalf("Expected %q but got %q for DiskName", v.Expected.DiskName, actual.DiskName) - } - } -} diff --git a/internal/services/compute/resourceids.go b/internal/services/compute/resourceids.go index 1ef1ea274ea7a..3f670c5ec0421 100644 --- a/internal/services/compute/resourceids.go +++ b/internal/services/compute/resourceids.go @@ -6,7 +6,6 @@ package compute //go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=GalleryApplication -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Compute/galleries/gallery1/applications/galleryApplication1 //go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=GalleryApplicationVersion -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Compute/galleries/gallery1/applications/galleryApplication1/versions/galleryApplicationVersion1 //go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=Image -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Compute/images/image1 -//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=ManagedDisk -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Compute/disks/disk1 //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=SharedImageGallery -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Compute/galleries/gallery1 //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 diff --git a/internal/services/compute/validate/managed_disk_id.go b/internal/services/compute/validate/managed_disk_id.go deleted file mode 100644 index cb559b9e8e56a..0000000000000 --- a/internal/services/compute/validate/managed_disk_id.go +++ /dev/null @@ -1,23 +0,0 @@ -package validate - -// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten - -import ( - "fmt" - - "github.com/hashicorp/terraform-provider-azurerm/internal/services/compute/parse" -) - -func ManagedDiskID(input interface{}, key string) (warnings []string, errors []error) { - v, ok := input.(string) - if !ok { - errors = append(errors, fmt.Errorf("expected %q to be a string", key)) - return - } - - if _, err := parse.ManagedDiskID(v); err != nil { - errors = append(errors, err) - } - - return -} diff --git a/internal/services/compute/validate/managed_disk_id_test.go b/internal/services/compute/validate/managed_disk_id_test.go deleted file mode 100644 index d78f640062d91..0000000000000 --- a/internal/services/compute/validate/managed_disk_id_test.go +++ /dev/null @@ -1,76 +0,0 @@ -package validate - -// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten - -import "testing" - -func TestManagedDiskID(t *testing.T) { - cases := []struct { - Input string - Valid bool - }{ - - { - // empty - Input: "", - Valid: false, - }, - - { - // missing SubscriptionId - Input: "/", - Valid: false, - }, - - { - // missing value for SubscriptionId - Input: "/subscriptions/", - Valid: false, - }, - - { - // missing ResourceGroup - Input: "/subscriptions/12345678-1234-9876-4563-123456789012/", - Valid: false, - }, - - { - // missing value for ResourceGroup - Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/", - Valid: false, - }, - - { - // missing DiskName - Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Compute/", - Valid: false, - }, - - { - // missing value for DiskName - Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Compute/disks/", - Valid: false, - }, - - { - // valid - Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Compute/disks/disk1", - Valid: true, - }, - - { - // upper-cased - Input: "/SUBSCRIPTIONS/12345678-1234-9876-4563-123456789012/RESOURCEGROUPS/RESGROUP1/PROVIDERS/MICROSOFT.COMPUTE/DISKS/DISK1", - Valid: false, - }, - } - for _, tc := range cases { - t.Logf("[DEBUG] Testing Value %s", tc.Input) - _, errors := ManagedDiskID(tc.Input, "test") - valid := len(errors) == 0 - - if tc.Valid != valid { - t.Fatalf("Expected %t but got %t", tc.Valid, valid) - } - } -}