From bbfb210b6549776ebf1618b0dc9401c26c68495a Mon Sep 17 00:00:00 2001 From: xuwu1 Date: Wed, 20 Mar 2024 11:15:15 +0800 Subject: [PATCH 1/3] add mhsm key ids --- .../managed_hsm_nested_key_versionless.go | 78 ++++++++++ ...managed_hsm_nested_key_versionless_test.go | 131 ++++++++++++++++ .../managed_hsm_nested_key_with_version.go | 84 ++++++++++ ...anaged_hsm_nested_key_with_version_test.go | 147 ++++++++++++++++++ internal/services/managedhsm/resourceids.go | 7 + .../managed_hsm_nested_key_versionless_id.go | 26 ++++ ...aged_hsm_nested_key_versionless_id_test.go | 91 +++++++++++ .../managed_hsm_nested_key_with_version_id.go | 26 ++++ ...ged_hsm_nested_key_with_version_id_test.go | 103 ++++++++++++ 9 files changed, 693 insertions(+) create mode 100644 internal/services/managedhsm/parse/managed_hsm_nested_key_versionless.go create mode 100644 internal/services/managedhsm/parse/managed_hsm_nested_key_versionless_test.go create mode 100644 internal/services/managedhsm/parse/managed_hsm_nested_key_with_version.go create mode 100644 internal/services/managedhsm/parse/managed_hsm_nested_key_with_version_test.go create mode 100644 internal/services/managedhsm/resourceids.go create mode 100644 internal/services/managedhsm/validate/managed_hsm_nested_key_versionless_id.go create mode 100644 internal/services/managedhsm/validate/managed_hsm_nested_key_versionless_id_test.go create mode 100644 internal/services/managedhsm/validate/managed_hsm_nested_key_with_version_id.go create mode 100644 internal/services/managedhsm/validate/managed_hsm_nested_key_with_version_id_test.go diff --git a/internal/services/managedhsm/parse/managed_hsm_nested_key_versionless.go b/internal/services/managedhsm/parse/managed_hsm_nested_key_versionless.go new file mode 100644 index 000000000000..bd8a2c155bd7 --- /dev/null +++ b/internal/services/managedhsm/parse/managed_hsm_nested_key_versionless.go @@ -0,0 +1,78 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.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 ManagedHSMNestedKeyVersionlessId struct { + SubscriptionId string + ResourceGroup string + ManagedHSMName string + KeyName string +} + +func NewManagedHSMNestedKeyVersionlessID(subscriptionId, resourceGroup, managedHSMName, keyName string) ManagedHSMNestedKeyVersionlessId { + return ManagedHSMNestedKeyVersionlessId{ + SubscriptionId: subscriptionId, + ResourceGroup: resourceGroup, + ManagedHSMName: managedHSMName, + KeyName: keyName, + } +} + +func (id ManagedHSMNestedKeyVersionlessId) String() string { + segments := []string{ + fmt.Sprintf("Key Name %q", id.KeyName), + fmt.Sprintf("Managed H S M Name %q", id.ManagedHSMName), + fmt.Sprintf("Resource Group %q", id.ResourceGroup), + } + segmentsStr := strings.Join(segments, " / ") + return fmt.Sprintf("%s: (%s)", "Managed H S M Nested Key Versionless", segmentsStr) +} + +func (id ManagedHSMNestedKeyVersionlessId) ID() string { + fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.KeyVault/managedHSMs/%s/keys/%s" + return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroup, id.ManagedHSMName, id.KeyName) +} + +// ManagedHSMNestedKeyVersionlessID parses a ManagedHSMNestedKeyVersionless ID into an ManagedHSMNestedKeyVersionlessId struct +func ManagedHSMNestedKeyVersionlessID(input string) (*ManagedHSMNestedKeyVersionlessId, error) { + id, err := resourceids.ParseAzureResourceID(input) + if err != nil { + return nil, fmt.Errorf("parsing %q as an ManagedHSMNestedKeyVersionless ID: %+v", input, err) + } + + resourceId := ManagedHSMNestedKeyVersionlessId{ + 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.ManagedHSMName, err = id.PopSegment("managedHSMs"); err != nil { + return nil, err + } + if resourceId.KeyName, err = id.PopSegment("keys"); err != nil { + return nil, err + } + + if err := id.ValidateNoEmptySegments(input); err != nil { + return nil, err + } + + return &resourceId, nil +} diff --git a/internal/services/managedhsm/parse/managed_hsm_nested_key_versionless_test.go b/internal/services/managedhsm/parse/managed_hsm_nested_key_versionless_test.go new file mode 100644 index 000000000000..321ffdd3aaed --- /dev/null +++ b/internal/services/managedhsm/parse/managed_hsm_nested_key_versionless_test.go @@ -0,0 +1,131 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.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 = ManagedHSMNestedKeyVersionlessId{} + +func TestManagedHSMNestedKeyVersionlessIDFormatter(t *testing.T) { + actual := NewManagedHSMNestedKeyVersionlessID("12345678-1234-9876-4563-123456789012", "resGroup1", "mhsm1", "key1").ID() + expected := "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/keys/key1" + if actual != expected { + t.Fatalf("Expected %q but got %q", expected, actual) + } +} + +func TestManagedHSMNestedKeyVersionlessID(t *testing.T) { + testData := []struct { + Input string + Error bool + Expected *ManagedHSMNestedKeyVersionlessId + }{ + + { + // 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 ManagedHSMName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/", + Error: true, + }, + + { + // missing value for ManagedHSMName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/", + Error: true, + }, + + { + // missing KeyName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/", + Error: true, + }, + + { + // missing value for KeyName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/keys/", + Error: true, + }, + + { + // valid + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/keys/key1", + Expected: &ManagedHSMNestedKeyVersionlessId{ + SubscriptionId: "12345678-1234-9876-4563-123456789012", + ResourceGroup: "resGroup1", + ManagedHSMName: "mhsm1", + KeyName: "key1", + }, + }, + + { + // upper-cased + Input: "/SUBSCRIPTIONS/12345678-1234-9876-4563-123456789012/RESOURCEGROUPS/RESGROUP1/PROVIDERS/MICROSOFT.KEYVAULT/MANAGEDHSMS/MHSM1/KEYS/KEY1", + Error: true, + }, + } + + for _, v := range testData { + t.Logf("[DEBUG] Testing %q", v.Input) + + actual, err := ManagedHSMNestedKeyVersionlessID(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.ManagedHSMName != v.Expected.ManagedHSMName { + t.Fatalf("Expected %q but got %q for ManagedHSMName", v.Expected.ManagedHSMName, actual.ManagedHSMName) + } + if actual.KeyName != v.Expected.KeyName { + t.Fatalf("Expected %q but got %q for KeyName", v.Expected.KeyName, actual.KeyName) + } + } +} diff --git a/internal/services/managedhsm/parse/managed_hsm_nested_key_with_version.go b/internal/services/managedhsm/parse/managed_hsm_nested_key_with_version.go new file mode 100644 index 000000000000..31a9f154fa32 --- /dev/null +++ b/internal/services/managedhsm/parse/managed_hsm_nested_key_with_version.go @@ -0,0 +1,84 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.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 ManagedHSMNestedKeyWithVersionId struct { + SubscriptionId string + ResourceGroup string + ManagedHSMName string + KeyName string + VersionName string +} + +func NewManagedHSMNestedKeyWithVersionID(subscriptionId, resourceGroup, managedHSMName, keyName, versionName string) ManagedHSMNestedKeyWithVersionId { + return ManagedHSMNestedKeyWithVersionId{ + SubscriptionId: subscriptionId, + ResourceGroup: resourceGroup, + ManagedHSMName: managedHSMName, + KeyName: keyName, + VersionName: versionName, + } +} + +func (id ManagedHSMNestedKeyWithVersionId) String() string { + segments := []string{ + fmt.Sprintf("Version Name %q", id.VersionName), + fmt.Sprintf("Key Name %q", id.KeyName), + fmt.Sprintf("Managed H S M Name %q", id.ManagedHSMName), + fmt.Sprintf("Resource Group %q", id.ResourceGroup), + } + segmentsStr := strings.Join(segments, " / ") + return fmt.Sprintf("%s: (%s)", "Managed H S M Nested Key With Version", segmentsStr) +} + +func (id ManagedHSMNestedKeyWithVersionId) ID() string { + fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.KeyVault/managedHSMs/%s/keys/%s/versions/%s" + return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroup, id.ManagedHSMName, id.KeyName, id.VersionName) +} + +// ManagedHSMNestedKeyWithVersionID parses a ManagedHSMNestedKeyWithVersion ID into an ManagedHSMNestedKeyWithVersionId struct +func ManagedHSMNestedKeyWithVersionID(input string) (*ManagedHSMNestedKeyWithVersionId, error) { + id, err := resourceids.ParseAzureResourceID(input) + if err != nil { + return nil, fmt.Errorf("parsing %q as an ManagedHSMNestedKeyWithVersion ID: %+v", input, err) + } + + resourceId := ManagedHSMNestedKeyWithVersionId{ + 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.ManagedHSMName, err = id.PopSegment("managedHSMs"); err != nil { + return nil, err + } + if resourceId.KeyName, err = id.PopSegment("keys"); err != nil { + return nil, err + } + if resourceId.VersionName, err = id.PopSegment("versions"); err != nil { + return nil, err + } + + if err := id.ValidateNoEmptySegments(input); err != nil { + return nil, err + } + + return &resourceId, nil +} diff --git a/internal/services/managedhsm/parse/managed_hsm_nested_key_with_version_test.go b/internal/services/managedhsm/parse/managed_hsm_nested_key_with_version_test.go new file mode 100644 index 000000000000..5e1db4e0841c --- /dev/null +++ b/internal/services/managedhsm/parse/managed_hsm_nested_key_with_version_test.go @@ -0,0 +1,147 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.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 = ManagedHSMNestedKeyWithVersionId{} + +func TestManagedHSMNestedKeyWithVersionIDFormatter(t *testing.T) { + actual := NewManagedHSMNestedKeyWithVersionID("12345678-1234-9876-4563-123456789012", "resGroup1", "mhsm1", "key1", "version1").ID() + expected := "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/keys/key1/versions/version1" + if actual != expected { + t.Fatalf("Expected %q but got %q", expected, actual) + } +} + +func TestManagedHSMNestedKeyWithVersionID(t *testing.T) { + testData := []struct { + Input string + Error bool + Expected *ManagedHSMNestedKeyWithVersionId + }{ + + { + // 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 ManagedHSMName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/", + Error: true, + }, + + { + // missing value for ManagedHSMName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/", + Error: true, + }, + + { + // missing KeyName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/", + Error: true, + }, + + { + // missing value for KeyName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/keys/", + Error: true, + }, + + { + // missing VersionName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/keys/key1/", + Error: true, + }, + + { + // missing value for VersionName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/keys/key1/versions/", + Error: true, + }, + + { + // valid + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/keys/key1/versions/version1", + Expected: &ManagedHSMNestedKeyWithVersionId{ + SubscriptionId: "12345678-1234-9876-4563-123456789012", + ResourceGroup: "resGroup1", + ManagedHSMName: "mhsm1", + KeyName: "key1", + VersionName: "version1", + }, + }, + + { + // upper-cased + Input: "/SUBSCRIPTIONS/12345678-1234-9876-4563-123456789012/RESOURCEGROUPS/RESGROUP1/PROVIDERS/MICROSOFT.KEYVAULT/MANAGEDHSMS/MHSM1/KEYS/KEY1/VERSIONS/VERSION1", + Error: true, + }, + } + + for _, v := range testData { + t.Logf("[DEBUG] Testing %q", v.Input) + + actual, err := ManagedHSMNestedKeyWithVersionID(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.ManagedHSMName != v.Expected.ManagedHSMName { + t.Fatalf("Expected %q but got %q for ManagedHSMName", v.Expected.ManagedHSMName, actual.ManagedHSMName) + } + if actual.KeyName != v.Expected.KeyName { + t.Fatalf("Expected %q but got %q for KeyName", v.Expected.KeyName, actual.KeyName) + } + if actual.VersionName != v.Expected.VersionName { + t.Fatalf("Expected %q but got %q for VersionName", v.Expected.VersionName, actual.VersionName) + } + } +} diff --git a/internal/services/managedhsm/resourceids.go b/internal/services/managedhsm/resourceids.go new file mode 100644 index 000000000000..09dbb9f012b9 --- /dev/null +++ b/internal/services/managedhsm/resourceids.go @@ -0,0 +1,7 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package managedhsm + +//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=ManagedHSMNestedKeyWithVersion -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/keys/key1/versions/version1 +//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=ManagedHSMNestedKeyVersionless -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/keys/key1 diff --git a/internal/services/managedhsm/validate/managed_hsm_nested_key_versionless_id.go b/internal/services/managedhsm/validate/managed_hsm_nested_key_versionless_id.go new file mode 100644 index 000000000000..a2925df99097 --- /dev/null +++ b/internal/services/managedhsm/validate/managed_hsm_nested_key_versionless_id.go @@ -0,0 +1,26 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.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/managedhsm/parse" +) + +func ManagedHSMNestedKeyVersionlessID(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.ManagedHSMNestedKeyVersionlessID(v); err != nil { + errors = append(errors, err) + } + + return +} diff --git a/internal/services/managedhsm/validate/managed_hsm_nested_key_versionless_id_test.go b/internal/services/managedhsm/validate/managed_hsm_nested_key_versionless_id_test.go new file mode 100644 index 000000000000..e12960a66540 --- /dev/null +++ b/internal/services/managedhsm/validate/managed_hsm_nested_key_versionless_id_test.go @@ -0,0 +1,91 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package validate + +// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten + +import "testing" + +func TestManagedHSMNestedKeyVersionlessID(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 ManagedHSMName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/", + Valid: false, + }, + + { + // missing value for ManagedHSMName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/", + Valid: false, + }, + + { + // missing KeyName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/", + Valid: false, + }, + + { + // missing value for KeyName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/keys/", + Valid: false, + }, + + { + // valid + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/keys/key1", + Valid: true, + }, + + { + // upper-cased + Input: "/SUBSCRIPTIONS/12345678-1234-9876-4563-123456789012/RESOURCEGROUPS/RESGROUP1/PROVIDERS/MICROSOFT.KEYVAULT/MANAGEDHSMS/MHSM1/KEYS/KEY1", + Valid: false, + }, + } + for _, tc := range cases { + t.Logf("[DEBUG] Testing Value %s", tc.Input) + _, errors := ManagedHSMNestedKeyVersionlessID(tc.Input, "test") + valid := len(errors) == 0 + + if tc.Valid != valid { + t.Fatalf("Expected %t but got %t", tc.Valid, valid) + } + } +} diff --git a/internal/services/managedhsm/validate/managed_hsm_nested_key_with_version_id.go b/internal/services/managedhsm/validate/managed_hsm_nested_key_with_version_id.go new file mode 100644 index 000000000000..ffe2569f8b27 --- /dev/null +++ b/internal/services/managedhsm/validate/managed_hsm_nested_key_with_version_id.go @@ -0,0 +1,26 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.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/managedhsm/parse" +) + +func ManagedHSMNestedKeyWithVersionID(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.ManagedHSMNestedKeyWithVersionID(v); err != nil { + errors = append(errors, err) + } + + return +} diff --git a/internal/services/managedhsm/validate/managed_hsm_nested_key_with_version_id_test.go b/internal/services/managedhsm/validate/managed_hsm_nested_key_with_version_id_test.go new file mode 100644 index 000000000000..f94040ceb671 --- /dev/null +++ b/internal/services/managedhsm/validate/managed_hsm_nested_key_with_version_id_test.go @@ -0,0 +1,103 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package validate + +// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten + +import "testing" + +func TestManagedHSMNestedKeyWithVersionID(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 ManagedHSMName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/", + Valid: false, + }, + + { + // missing value for ManagedHSMName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/", + Valid: false, + }, + + { + // missing KeyName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/", + Valid: false, + }, + + { + // missing value for KeyName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/keys/", + Valid: false, + }, + + { + // missing VersionName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/keys/key1/", + Valid: false, + }, + + { + // missing value for VersionName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/keys/key1/versions/", + Valid: false, + }, + + { + // valid + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/keys/key1/versions/version1", + Valid: true, + }, + + { + // upper-cased + Input: "/SUBSCRIPTIONS/12345678-1234-9876-4563-123456789012/RESOURCEGROUPS/RESGROUP1/PROVIDERS/MICROSOFT.KEYVAULT/MANAGEDHSMS/MHSM1/KEYS/KEY1/VERSIONS/VERSION1", + Valid: false, + }, + } + for _, tc := range cases { + t.Logf("[DEBUG] Testing Value %s", tc.Input) + _, errors := ManagedHSMNestedKeyWithVersionID(tc.Input, "test") + valid := len(errors) == 0 + + if tc.Valid != valid { + t.Fatalf("Expected %t but got %t", tc.Valid, valid) + } + } +} From 67b49f6b9ef6f9eef8993520d3dcfd9c6d515db2 Mon Sep 17 00:00:00 2001 From: xuwu1 Date: Thu, 21 Mar 2024 11:29:29 +0800 Subject: [PATCH 2/3] add nest id --- .../parse/managed_hsm_key_versionless.go | 127 ++++++++++ .../parse/managed_hsm_key_versionless_test.go | 151 ++++++++++++ .../parse/managed_hsm_key_with_version.go | 137 +++++++++++ .../managed_hsm_key_with_version_test.go | 166 +++++++++++++ .../managed_hsm_nested_key_versionless.go | 108 ++++---- ...managed_hsm_nested_key_versionless_test.go | 205 +++++++++------- .../managed_hsm_nested_key_with_version.go | 114 +++++---- ...anaged_hsm_nested_key_with_version_test.go | 231 ++++++++++-------- .../managed_hsm_nested_key_versionless_id.go | 26 -- ...aged_hsm_nested_key_versionless_id_test.go | 91 ------- .../managed_hsm_nested_key_with_version_id.go | 26 -- ...ged_hsm_nested_key_with_version_id_test.go | 103 -------- 12 files changed, 967 insertions(+), 518 deletions(-) create mode 100644 internal/services/managedhsm/parse/managed_hsm_key_versionless.go create mode 100644 internal/services/managedhsm/parse/managed_hsm_key_versionless_test.go create mode 100644 internal/services/managedhsm/parse/managed_hsm_key_with_version.go create mode 100644 internal/services/managedhsm/parse/managed_hsm_key_with_version_test.go delete mode 100644 internal/services/managedhsm/validate/managed_hsm_nested_key_versionless_id.go delete mode 100644 internal/services/managedhsm/validate/managed_hsm_nested_key_versionless_id_test.go delete mode 100644 internal/services/managedhsm/validate/managed_hsm_nested_key_with_version_id.go delete mode 100644 internal/services/managedhsm/validate/managed_hsm_nested_key_with_version_id_test.go diff --git a/internal/services/managedhsm/parse/managed_hsm_key_versionless.go b/internal/services/managedhsm/parse/managed_hsm_key_versionless.go new file mode 100644 index 000000000000..873d77fdec6b --- /dev/null +++ b/internal/services/managedhsm/parse/managed_hsm_key_versionless.go @@ -0,0 +1,127 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package parse + +import ( + "fmt" + "strings" + + "github.com/hashicorp/go-azure-helpers/resourcemanager/resourceids" +) + +var _ resourceids.ResourceId = &ManagedHSMKeyVersionlessId{} + +type ManagedHSMKeyVersionlessId struct { + SubscriptionId string + ResourceGroup string + ManagedHSMName string + KeyName string +} + +func NewManagedHSMKeyVersionlessId(subscriptionId, resourceGroup, managedHSMName, keyName string) ManagedHSMKeyVersionlessId { + return ManagedHSMKeyVersionlessId{ + SubscriptionId: subscriptionId, + ResourceGroup: resourceGroup, + ManagedHSMName: managedHSMName, + KeyName: keyName, + } +} + +// FromParseResult implements resourceids.ResourceId. +func (id *ManagedHSMKeyVersionlessId) FromParseResult(input resourceids.ParseResult) error { + var ok bool + + if id.SubscriptionId, ok = input.Parsed["subscriptionId"]; !ok { + return resourceids.NewSegmentNotSpecifiedError(id, "subscriptionId", input) + } + + if id.ResourceGroup, ok = input.Parsed["resourceGroupName"]; !ok { + return resourceids.NewSegmentNotSpecifiedError(id, "resourceGroupName", input) + } + + if id.ManagedHSMName, ok = input.Parsed["managedHSMName"]; !ok { + return resourceids.NewSegmentNotSpecifiedError(id, "managedHSMName", input) + } + + if id.KeyName, ok = input.Parsed["keyName"]; !ok { + return resourceids.NewSegmentNotSpecifiedError(id, "keyName", input) + } + + return nil +} + +// Segments implements resourceids.ResourceId. +func (id *ManagedHSMKeyVersionlessId) Segments() []resourceids.Segment { + return []resourceids.Segment{ + resourceids.StaticSegment("staticSubscriptions", "subscriptions", "subscriptions"), + resourceids.SubscriptionIdSegment("subscriptionId", "12345678-1234-9876-4563-123456789012"), + resourceids.StaticSegment("staticResourceGroups", "resourceGroups", "resourceGroups"), + resourceids.ResourceGroupSegment("resourceGroupName", "example-resource-group"), + resourceids.StaticSegment("staticProviders", "providers", "providers"), + resourceids.ResourceProviderSegment("staticMicrosoftKeyVault", "Microsoft.KeyVault", "Microsoft.KeyVault"), + resourceids.StaticSegment("staticManagedHSMs", "managedHSMs", "managedHSMs"), + resourceids.UserSpecifiedSegment("managedHSMName", "managedHSMValue"), + resourceids.StaticSegment("staticKeys", "keys", "keys"), + resourceids.UserSpecifiedSegment("keyName", "keyValue"), + } +} + +func (id ManagedHSMKeyVersionlessId) String() string { + segments := []string{ + fmt.Sprintf("Key Name %q", id.KeyName), + fmt.Sprintf("Managed H S M Name %q", id.ManagedHSMName), + fmt.Sprintf("Resource Group %q", id.ResourceGroup), + } + segmentsStr := strings.Join(segments, " / ") + return fmt.Sprintf("%s: (%s)", "Managed H S M Key With Version", segmentsStr) +} + +func (id ManagedHSMKeyVersionlessId) ID() string { + fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.KeyVault/managedHSMs/%s/keys/%s" + return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroup, id.ManagedHSMName, id.KeyName) +} + +// ManagedHSMKeyVersionlessId parses a ManagedHSMKeyWithVersion ID into an ManagedHSMKeyVersionlessId struct +func ParseManagedHSMKeyVersionlessId(input string) (*ManagedHSMKeyVersionlessId, error) { + parser := resourceids.NewParserFromResourceIdType(&ManagedHSMKeyVersionlessId{}) + parsed, err := parser.Parse(input, false) + if err != nil { + return nil, fmt.Errorf("parsing %q: %+v", input, err) + } + + id := ManagedHSMKeyVersionlessId{} + if err := id.FromParseResult(*parsed); err != nil { + return nil, err + } + + return &id, nil +} + +func ParseManagedHSMKeyVersionlessIdInsensitively(input string) (*ManagedHSMKeyVersionlessId, error) { + parser := resourceids.NewParserFromResourceIdType(&ManagedHSMKeyVersionlessId{}) + parsed, err := parser.Parse(input, true) + if err != nil { + return nil, fmt.Errorf("parsing %q: %+v", input, err) + } + + id := ManagedHSMKeyVersionlessId{} + if err := id.FromParseResult(*parsed); err != nil { + return nil, err + } + + return &id, nil +} +func ValidateManagedHSMKeyVersionlessID(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 := ParseManagedHSMKeyVersionlessId(v); err != nil { + errors = append(errors, err) + } + + return +} diff --git a/internal/services/managedhsm/parse/managed_hsm_key_versionless_test.go b/internal/services/managedhsm/parse/managed_hsm_key_versionless_test.go new file mode 100644 index 000000000000..52a82d6ad28b --- /dev/null +++ b/internal/services/managedhsm/parse/managed_hsm_key_versionless_test.go @@ -0,0 +1,151 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package parse + +// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten + +import ( + "testing" +) + +func TestManagedHSMKeyVersionlessIdFormatter(t *testing.T) { + actual := NewManagedHSMKeyVersionlessId("12345678-1234-9876-4563-123456789012", "resGroup1", "mhsm1", "key1").ID() + expected := "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/keys/key1" + if actual != expected { + t.Fatalf("Expected %q but got %q", expected, actual) + } +} + +func TestManagedHSMKeyVersionlessIdD(t *testing.T) { + testData := []struct { + Input string + Insenstively bool + Error bool + Expected *ManagedHSMKeyVersionlessId + }{ + + { + // 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 ManagedHSMName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/", + Error: true, + }, + + { + // missing value for ManagedHSMName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/", + Error: true, + }, + + { + // missing KeyName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/", + Error: true, + }, + + { + // missing value for KeyName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/keys/", + Error: true, + }, + + { + // valid + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/keys/key1", + Expected: &ManagedHSMKeyVersionlessId{ + SubscriptionId: "12345678-1234-9876-4563-123456789012", + ResourceGroup: "resGroup1", + ManagedHSMName: "mhsm1", + KeyName: "key1", + }, + }, + { + // invalid case + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourcegroups/resGroup1/providers/Microsoft.KeyVault/managedhsms/mhsm1/keys/key1", + Error: true, + }, + { + // valid case insensitive + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/keys/key1", + Insenstively: true, + Expected: &ManagedHSMKeyVersionlessId{ + SubscriptionId: "12345678-1234-9876-4563-123456789012", + ResourceGroup: "resGroup1", + ManagedHSMName: "mhsm1", + KeyName: "key1", + }, + }, + + { + // upper-cased + Input: "/SUBSCRIPTIONS/12345678-1234-9876-4563-123456789012/RESOURCEGROUPS/RESGROUP1/PROVIDERS/MICROSOFT.KEYVAULT/MANAGEDHSMS/MHSM1/KEYS/KEY1", + Error: true, + }, + } + + for _, v := range testData { + t.Logf("[DEBUG] Testing %q", v.Input) + + var actual *ManagedHSMKeyVersionlessId + var err error + + if v.Insenstively { + actual, err = ParseManagedHSMKeyVersionlessIdInsensitively(v.Input) + } else { + actual, err = ParseManagedHSMKeyVersionlessId(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.ManagedHSMName != v.Expected.ManagedHSMName { + t.Fatalf("Expected %q but got %q for ManagedHSMName", v.Expected.ManagedHSMName, actual.ManagedHSMName) + } + if actual.KeyName != v.Expected.KeyName { + t.Fatalf("Expected %q but got %q for KeyName", v.Expected.KeyName, actual.KeyName) + } + } +} diff --git a/internal/services/managedhsm/parse/managed_hsm_key_with_version.go b/internal/services/managedhsm/parse/managed_hsm_key_with_version.go new file mode 100644 index 000000000000..9602a984c3da --- /dev/null +++ b/internal/services/managedhsm/parse/managed_hsm_key_with_version.go @@ -0,0 +1,137 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package parse + +import ( + "fmt" + "strings" + + "github.com/hashicorp/go-azure-helpers/resourcemanager/resourceids" +) + +var _ resourceids.ResourceId = &ManagedHSMKeyWithVersionId{} + +type ManagedHSMKeyWithVersionId struct { + SubscriptionId string + ResourceGroup string + ManagedHSMName string + KeyName string + VersionName string +} + +func NewManagedHSMKeyWithVersionID(subscriptionId, resourceGroup, managedHSMName, keyName, versionName string) ManagedHSMKeyWithVersionId { + return ManagedHSMKeyWithVersionId{ + SubscriptionId: subscriptionId, + ResourceGroup: resourceGroup, + ManagedHSMName: managedHSMName, + KeyName: keyName, + VersionName: versionName, + } +} + +// FromParseResult implements resourceids.ResourceId. +func (id *ManagedHSMKeyWithVersionId) FromParseResult(input resourceids.ParseResult) error { + var ok bool + + if id.SubscriptionId, ok = input.Parsed["subscriptionId"]; !ok { + return resourceids.NewSegmentNotSpecifiedError(id, "subscriptionId", input) + } + + if id.ResourceGroup, ok = input.Parsed["resourceGroupName"]; !ok { + return resourceids.NewSegmentNotSpecifiedError(id, "resourceGroupName", input) + } + + if id.ManagedHSMName, ok = input.Parsed["managedHSMName"]; !ok { + return resourceids.NewSegmentNotSpecifiedError(id, "managedHSMName", input) + } + + if id.KeyName, ok = input.Parsed["keyName"]; !ok { + return resourceids.NewSegmentNotSpecifiedError(id, "keyName", input) + } + + if id.VersionName, ok = input.Parsed["versionName"]; !ok { + return resourceids.NewSegmentNotSpecifiedError(id, "versionName", input) + } + + return nil +} + +// Segments implements resourceids.ResourceId. +func (id *ManagedHSMKeyWithVersionId) Segments() []resourceids.Segment { + return []resourceids.Segment{ + resourceids.StaticSegment("staticSubscriptions", "subscriptions", "subscriptions"), + resourceids.SubscriptionIdSegment("subscriptionId", "12345678-1234-9876-4563-123456789012"), + resourceids.StaticSegment("staticResourceGroups", "resourceGroups", "resourceGroups"), + resourceids.ResourceGroupSegment("resourceGroupName", "example-resource-group"), + resourceids.StaticSegment("staticProviders", "providers", "providers"), + resourceids.ResourceProviderSegment("staticMicrosoftKeyVault", "Microsoft.KeyVault", "Microsoft.KeyVault"), + resourceids.StaticSegment("staticManagedHSMs", "managedHSMs", "managedHSMs"), + resourceids.UserSpecifiedSegment("managedHSMName", "managedHSMValue"), + resourceids.StaticSegment("staticKeys", "keys", "keys"), + resourceids.UserSpecifiedSegment("keyName", "keyValue"), + resourceids.StaticSegment("staticVersion", "versions", "versions"), + resourceids.UserSpecifiedSegment("versionName", "versionValue"), + } +} + +func (id ManagedHSMKeyWithVersionId) String() string { + segments := []string{ + fmt.Sprintf("Version Name %q", id.VersionName), + fmt.Sprintf("Key Name %q", id.KeyName), + fmt.Sprintf("Managed H S M Name %q", id.ManagedHSMName), + fmt.Sprintf("Resource Group %q", id.ResourceGroup), + } + segmentsStr := strings.Join(segments, " / ") + return fmt.Sprintf("%s: (%s)", "Managed H S M Key With Version", segmentsStr) +} + +func (id ManagedHSMKeyWithVersionId) ID() string { + fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.KeyVault/managedHSMs/%s/keys/%s/versions/%s" + return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroup, id.ManagedHSMName, id.KeyName, id.VersionName) +} + +// ManagedHSMKeyWithVersionID parses a ManagedHSMKeyWithVersion ID into an ManagedHSMKeyWithVersionId struct +func ParseManagedHSMKeyWithVersionID(input string) (*ManagedHSMKeyWithVersionId, error) { + parser := resourceids.NewParserFromResourceIdType(&ManagedHSMKeyWithVersionId{}) + parsed, err := parser.Parse(input, false) + if err != nil { + return nil, fmt.Errorf("parsing %q: %+v", input, err) + } + + id := ManagedHSMKeyWithVersionId{} + if err := id.FromParseResult(*parsed); err != nil { + return nil, err + } + + return &id, nil +} + +func ParseManagedHSMKeyWithVersionIDInsensitively(input string) (*ManagedHSMKeyWithVersionId, error) { + parser := resourceids.NewParserFromResourceIdType(&ManagedHSMKeyWithVersionId{}) + parsed, err := parser.Parse(input, true) + if err != nil { + return nil, fmt.Errorf("parsing %q: %+v", input, err) + } + + id := ManagedHSMKeyWithVersionId{} + if err := id.FromParseResult(*parsed); err != nil { + return nil, err + } + + return &id, nil +} + +func ValidateManagedHSMKeyWithVersionID(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 := ParseManagedHSMKeyWithVersionID(v); err != nil { + errors = append(errors, err) + } + + return +} diff --git a/internal/services/managedhsm/parse/managed_hsm_key_with_version_test.go b/internal/services/managedhsm/parse/managed_hsm_key_with_version_test.go new file mode 100644 index 000000000000..70836fa86983 --- /dev/null +++ b/internal/services/managedhsm/parse/managed_hsm_key_with_version_test.go @@ -0,0 +1,166 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package parse + +// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten + +import ( + "testing" +) + +func TestManagedHSMKeyWithVersionIDFormatter(t *testing.T) { + actual := NewManagedHSMKeyWithVersionID("12345678-1234-9876-4563-123456789012", "resGroup1", "mhsm1", "key1", "version1").ID() + expected := "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/keys/key1/versions/version1" + if actual != expected { + t.Fatalf("Expected %q but got %q", expected, actual) + } +} + +func TestManagedHSMKeyWithVersionID(t *testing.T) { + testData := []struct { + Input string + insensitively bool + Error bool + Expected *ManagedHSMKeyWithVersionId + }{ + + { + // 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 ManagedHSMName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/", + Error: true, + }, + + { + // missing value for ManagedHSMName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/", + Error: true, + }, + + { + // missing KeyName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/", + Error: true, + }, + + { + // missing value for KeyName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/keys/", + Error: true, + }, + + { + // missing VersionName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/keys/key1/", + Error: true, + }, + + { + // missing value for VersionName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/keys/key1/versions/", + Error: true, + }, + { + // valid + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/keys/key1/versions/version1", + Expected: &ManagedHSMKeyWithVersionId{ + SubscriptionId: "12345678-1234-9876-4563-123456789012", + ResourceGroup: "resGroup1", + ManagedHSMName: "mhsm1", + KeyName: "key1", + VersionName: "version1", + }, + }, + { + // invalid insenseitively + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourcegroups/resGroup1/providers/Microsoft.KeyVault/managedhsms/mhsm1/keys/key1/versions/version1", + Error: true, + }, + { + // valid insenseitively + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourcegroups/resGroup1/providers/Microsoft.KeyVault/managedhsms/mhsm1/keys/key1/versions/version1", + insensitively: true, + Expected: &ManagedHSMKeyWithVersionId{ + SubscriptionId: "12345678-1234-9876-4563-123456789012", + ResourceGroup: "resGroup1", + ManagedHSMName: "mhsm1", + KeyName: "key1", + VersionName: "version1", + }, + }, + + { + // upper-cased + Input: "/SUBSCRIPTIONS/12345678-1234-9876-4563-123456789012/RESOURCEGROUPS/RESGROUP1/PROVIDERS/MICROSOFT.KEYVAULT/MANAGEDHSMS/MHSM1/KEYS/KEY1/VERSIONS/VERSION1", + Error: true, + }, + } + + for _, v := range testData { + t.Logf("[DEBUG] Testing %q", v.Input) + var actual *ManagedHSMKeyWithVersionId + var err error + + if v.insensitively { + actual, err = ParseManagedHSMKeyWithVersionIDInsensitively(v.Input) + } else { + actual, err = ParseManagedHSMKeyWithVersionID(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.ManagedHSMName != v.Expected.ManagedHSMName { + t.Fatalf("Expected %q but got %q for ManagedHSMName", v.Expected.ManagedHSMName, actual.ManagedHSMName) + } + if actual.KeyName != v.Expected.KeyName { + t.Fatalf("Expected %q but got %q for KeyName", v.Expected.KeyName, actual.KeyName) + } + if actual.VersionName != v.Expected.VersionName { + t.Fatalf("Expected %q but got %q for VersionName", v.Expected.VersionName, actual.VersionName) + } + } +} diff --git a/internal/services/managedhsm/parse/managed_hsm_nested_key_versionless.go b/internal/services/managedhsm/parse/managed_hsm_nested_key_versionless.go index bd8a2c155bd7..7b3f20e42fcb 100644 --- a/internal/services/managedhsm/parse/managed_hsm_nested_key_versionless.go +++ b/internal/services/managedhsm/parse/managed_hsm_nested_key_versionless.go @@ -3,76 +3,100 @@ 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 ManagedHSMNestedKeyVersionlessId struct { - SubscriptionId string - ResourceGroup string - ManagedHSMName string - KeyName string +// example: https://hsm-name.managedhsm.azure.net/keys/key-name +var _ resourceids.ResourceId = &ManagedHSMNestedkeyVersionless{} + +type ManagedHSMNestedkeyVersionless struct { + BaseURI string + KeyName string +} + +func NewManagedHSMNestedkeyVersionless(baseURI, keyName string) ManagedHSMNestedkeyVersionless { + return ManagedHSMNestedkeyVersionless{ + BaseURI: baseURI, + KeyName: keyName, + } } -func NewManagedHSMNestedKeyVersionlessID(subscriptionId, resourceGroup, managedHSMName, keyName string) ManagedHSMNestedKeyVersionlessId { - return ManagedHSMNestedKeyVersionlessId{ - SubscriptionId: subscriptionId, - ResourceGroup: resourceGroup, - ManagedHSMName: managedHSMName, - KeyName: keyName, +// FromParseResult implements resourceids.ResourceId. +func (id *ManagedHSMNestedkeyVersionless) FromParseResult(input resourceids.ParseResult) error { + var ok bool + + if id.BaseURI, ok = input.Parsed["baseURI"]; !ok { + return resourceids.NewSegmentNotSpecifiedError(id, "baseURI", input) + } + + if id.KeyName, ok = input.Parsed["keyName"]; !ok { + return resourceids.NewSegmentNotSpecifiedError(id, "keyName", input) } + + return nil } -func (id ManagedHSMNestedKeyVersionlessId) String() string { - segments := []string{ - fmt.Sprintf("Key Name %q", id.KeyName), - fmt.Sprintf("Managed H S M Name %q", id.ManagedHSMName), - fmt.Sprintf("Resource Group %q", id.ResourceGroup), +// Segments implements resourceids.ResourceId. +func (id *ManagedHSMNestedkeyVersionless) Segments() []resourceids.Segment { + return []resourceids.Segment{ + resourceids.DataPlaneHostSegment("baseURI", "https://hsm-name.managedhsm.azure.net"), + resourceids.StaticSegment("staticKeys", "keys", "keys"), + resourceids.UserSpecifiedSegment("keyName", "keyValue"), } - segmentsStr := strings.Join(segments, " / ") - return fmt.Sprintf("%s: (%s)", "Managed H S M Nested Key Versionless", segmentsStr) } -func (id ManagedHSMNestedKeyVersionlessId) ID() string { - fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.KeyVault/managedHSMs/%s/keys/%s" - return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroup, id.ManagedHSMName, id.KeyName) +func (id ManagedHSMNestedkeyVersionless) String() string { + return fmt.Sprintf("%s: (%s)", "Managed H S M Nested Key Versionless", id.ID()) +} + +func (id ManagedHSMNestedkeyVersionless) ID() string { + return fmt.Sprintf("%s/keys/%s", id.BaseURI, id.KeyName) } -// ManagedHSMNestedKeyVersionlessID parses a ManagedHSMNestedKeyVersionless ID into an ManagedHSMNestedKeyVersionlessId struct -func ManagedHSMNestedKeyVersionlessID(input string) (*ManagedHSMNestedKeyVersionlessId, error) { - id, err := resourceids.ParseAzureResourceID(input) +// ManagedHSMNestedkeyVersionless parses a ManagedHSMNestedKeyWithVersion ID into an ManagedHSMNestedkeyVersionless struct +func ParseManagedHSMNestedkeyVersionless(input string) (*ManagedHSMNestedkeyVersionless, error) { + parser := resourceids.NewParserFromResourceIdType(&ManagedHSMNestedkeyVersionless{}) + parsed, err := parser.Parse(input, false) if err != nil { - return nil, fmt.Errorf("parsing %q as an ManagedHSMNestedKeyVersionless ID: %+v", input, err) + return nil, fmt.Errorf("parsing %q: %+v", input, err) } - resourceId := ManagedHSMNestedKeyVersionlessId{ - SubscriptionId: id.SubscriptionID, - ResourceGroup: id.ResourceGroup, + id := ManagedHSMNestedkeyVersionless{} + if err := id.FromParseResult(*parsed); err != nil { + return nil, err } - if resourceId.SubscriptionId == "" { - return nil, fmt.Errorf("ID was missing the 'subscriptions' element") - } + return &id, nil +} - if resourceId.ResourceGroup == "" { - return nil, fmt.Errorf("ID was missing the 'resourceGroups' element") +func ParseManagedHSMNestedkeyVersionlessInsensitively(input string) (*ManagedHSMNestedkeyVersionless, error) { + parser := resourceids.NewParserFromResourceIdType(&ManagedHSMNestedkeyVersionless{}) + parsed, err := parser.Parse(input, true) + if err != nil { + return nil, fmt.Errorf("parsing %q: %+v", input, err) } - if resourceId.ManagedHSMName, err = id.PopSegment("managedHSMs"); err != nil { + id := ManagedHSMNestedkeyVersionless{} + if err := id.FromParseResult(*parsed); err != nil { return nil, err } - if resourceId.KeyName, err = id.PopSegment("keys"); err != nil { - return nil, err + + return &id, nil +} + +func ValidateManagedHSMNestedKeyVersionlessID(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 := id.ValidateNoEmptySegments(input); err != nil { - return nil, err + if _, err := ParseManagedHSMNestedkeyVersionless(v); err != nil { + errors = append(errors, err) } - return &resourceId, nil + return } diff --git a/internal/services/managedhsm/parse/managed_hsm_nested_key_versionless_test.go b/internal/services/managedhsm/parse/managed_hsm_nested_key_versionless_test.go index 321ffdd3aaed..2bffe05321df 100644 --- a/internal/services/managedhsm/parse/managed_hsm_nested_key_versionless_test.go +++ b/internal/services/managedhsm/parse/managed_hsm_nested_key_versionless_test.go @@ -3,129 +3,158 @@ package parse -// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten - import ( + "strings" "testing" - - "github.com/hashicorp/go-azure-helpers/resourcemanager/resourceids" ) -var _ resourceids.Id = ManagedHSMNestedKeyVersionlessId{} - -func TestManagedHSMNestedKeyVersionlessIDFormatter(t *testing.T) { - actual := NewManagedHSMNestedKeyVersionlessID("12345678-1234-9876-4563-123456789012", "resGroup1", "mhsm1", "key1").ID() - expected := "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/keys/key1" - if actual != expected { - t.Fatalf("Expected %q but got %q", expected, actual) - } -} - -func TestManagedHSMNestedKeyVersionlessID(t *testing.T) { - testData := []struct { - Input string - Error bool - Expected *ManagedHSMNestedKeyVersionlessId +func TestParseManagedHSMNestedKeyVersionlessId(t *testing.T) { + cases := []struct { + Input string + Insenstively bool + Expected ManagedHSMNestedKeyWithVersionId + ExpectError bool }{ - { - // empty - Input: "", - Error: true, + Input: "", + ExpectError: true, }, - { - // missing SubscriptionId - Input: "/", - Error: true, + Input: "https://my-hsm.managedhsm.azure.net/keys", + ExpectError: true, }, - { - // missing value for SubscriptionId - Input: "/subscriptions/", - Error: true, + Input: "https://my-hsm.managedhsm.azure.net/invalidNestedItemObjectType/hello/world", + ExpectError: true, }, - { - // missing ResourceGroup - Input: "/subscriptions/12345678-1234-9876-4563-123456789012/", - Error: true, + Input: "https://my-hsm.managedhsm.azure.net/Keys/hello/world", + ExpectError: true, }, - { - // missing value for ResourceGroup - Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/", - Error: true, - }, - - { - // missing ManagedHSMName - Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/", - Error: true, - }, - - { - // missing value for ManagedHSMName - Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/", - Error: true, - }, - - { - // missing KeyName - Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/", - Error: true, + Input: "https://my-hsm.managedhsm.azure.net/Keys/hello", + Insenstively: true, + Expected: ManagedHSMNestedKeyWithVersionId{ + KeyName: "hello", + BaseURI: "https://my-hsm.managedhsm.azure.net", + }, }, - { - // missing value for KeyName - Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/keys/", - Error: true, + Input: "https://my-hsm.managedhsm.azure.net/keys/bird/version", + ExpectError: true, }, - { - // valid - Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/keys/key1", - Expected: &ManagedHSMNestedKeyVersionlessId{ - SubscriptionId: "12345678-1234-9876-4563-123456789012", - ResourceGroup: "resGroup1", - ManagedHSMName: "mhsm1", - KeyName: "key1", + Input: "https://my-hsm.managedhsm.azure.net/keys/bird", + Expected: ManagedHSMNestedKeyWithVersionId{ + KeyName: "bird", + BaseURI: "https://my-hsm.managedhsm.azure.net", }, }, - { - // upper-cased - Input: "/SUBSCRIPTIONS/12345678-1234-9876-4563-123456789012/RESOURCEGROUPS/RESGROUP1/PROVIDERS/MICROSOFT.KEYVAULT/MANAGEDHSMS/MHSM1/KEYS/KEY1", - Error: true, + Input: "https://my-hsm.managedhsm.azure.net/Keys/castle", + Insenstively: true, + Expected: ManagedHSMNestedKeyWithVersionId{ + KeyName: "castle", + BaseURI: "https://my-hsm.managedhsm.azure.net", + }, }, } - for _, v := range testData { - t.Logf("[DEBUG] Testing %q", v.Input) + for _, tc := range cases { + t.Logf("[DEBUG] Testing %q", tc.Input) - actual, err := ManagedHSMNestedKeyVersionlessID(v.Input) + var keyId *ManagedHSMNestedkeyVersionless + var err error + if tc.Insenstively { + keyId, err = ParseManagedHSMNestedkeyVersionlessInsensitively(tc.Input) + } else { + keyId, err = ParseManagedHSMNestedkeyVersionless(tc.Input) + } if err != nil { - if v.Error { + if tc.ExpectError { + t.Logf("[DEBUG] --> [Received Expected Error]: %+v", err) continue } - t.Fatalf("Expect a value but got an error: %s", err) + t.Fatalf("Got error for ID '%s': %+v", tc.Input, err) } - if v.Error { - t.Fatal("Expect an error but didn't get one") + + if keyId == nil { + t.Fatalf("Expected a SecretID to be parsed for ID '%s', got nil.", tc.Input) } - if actual.SubscriptionId != v.Expected.SubscriptionId { - t.Fatalf("Expected %q but got %q for SubscriptionId", v.Expected.SubscriptionId, actual.SubscriptionId) + if tc.Expected.BaseURI != keyId.BaseURI { + t.Fatalf("Expected 'BaseURI' to be '%s', got '%s' for ID '%s'", tc.Expected.BaseURI, keyId.BaseURI, tc.Input) } - if actual.ResourceGroup != v.Expected.ResourceGroup { - t.Fatalf("Expected %q but got %q for ResourceGroup", v.Expected.ResourceGroup, actual.ResourceGroup) + + if tc.Expected.KeyName != keyId.KeyName { + t.Fatalf("Expected 'Version' to be '%s', got '%s' for ID '%s'", tc.Expected.KeyName, keyId.KeyName, tc.Input) } - if actual.ManagedHSMName != v.Expected.ManagedHSMName { - t.Fatalf("Expected %q but got %q for ManagedHSMName", v.Expected.ManagedHSMName, actual.ManagedHSMName) + + if !(tc.Input == keyId.ID() || (tc.Insenstively && strings.EqualFold(tc.Input, keyId.ID()))) { + t.Fatalf("Expected 'ID()' to be '%s', got '%s'", tc.Input, keyId.ID()) } - if actual.KeyName != v.Expected.KeyName { - t.Fatalf("Expected %q but got %q for KeyName", v.Expected.KeyName, actual.KeyName) + t.Logf("[DEBUG] --> [Valid Value]: %+v", tc.Input) + } +} + +func TestValidateManagedHSMNestedVersionlessId(t *testing.T) { + cases := []struct { + Input string + ExpectError bool + }{ + { + Input: "", + ExpectError: true, + }, + { + Input: "https", + ExpectError: true, + }, + { + Input: "https://", + ExpectError: true, + }, + { + Input: "https://my-hsm.managedhsm.azure.net", + ExpectError: true, + }, + { + Input: "https://my-hsm.managedhsm.azure.net/", + ExpectError: true, + }, + { + Input: "https://my-hsm.managedhsm.azure.net/keys", + ExpectError: true, + }, + { + Input: "https://my-hsm.managedhsm.azure.net/Keys/bird", + ExpectError: true, + }, + { + Input: "https://my-hsm.managedhsm.azure.net/invalidNestedItemObjectType/bird/fdf067c93bbb4b22bff4d8b7a9a56217", + ExpectError: true, + }, + { + Input: "https://my-hsm.managedhsm.azure.net/keys/bird/fdf067c93bbb4b22bff4d8b7a9a56217", + ExpectError: true, + }, + { + Input: "https://my-hsm.managedhsm.azure.net/keys/bird/fdf067c93bbb4b22bff4d8b7a9a56217/XXX", + ExpectError: true, + }, + { + Input: "https://my-hsm.managedhsm.azure.net/keys/bird", + ExpectError: false, + }, + } + + for idx, tc := range cases { + t.Logf("[DEBUG] Testing %q", tc.Input) + + _, errors := ValidateManagedHSMNestedKeyVersionlessID(tc.Input, "test") + if (len(errors) > 0) != tc.ExpectError { + t.Fatalf("case %d: expect error %t, but got %t", idx, tc.ExpectError, len(errors) > 0) } } } diff --git a/internal/services/managedhsm/parse/managed_hsm_nested_key_with_version.go b/internal/services/managedhsm/parse/managed_hsm_nested_key_with_version.go index 31a9f154fa32..16f9629fa590 100644 --- a/internal/services/managedhsm/parse/managed_hsm_nested_key_with_version.go +++ b/internal/services/managedhsm/parse/managed_hsm_nested_key_with_version.go @@ -3,82 +3,108 @@ 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" ) +// example: https://hsm-name.managedhsm.azure.net/keys/key-name/version-name +var _ resourceids.ResourceId = &ManagedHSMNestedKeyWithVersionId{} + type ManagedHSMNestedKeyWithVersionId struct { - SubscriptionId string - ResourceGroup string - ManagedHSMName string - KeyName string - VersionName string + BaseURI string + KeyName string + Version string } -func NewManagedHSMNestedKeyWithVersionID(subscriptionId, resourceGroup, managedHSMName, keyName, versionName string) ManagedHSMNestedKeyWithVersionId { +func NewManagedHSMNestedKeyWithVersionID(baseURI, keyName, versionName string) ManagedHSMNestedKeyWithVersionId { return ManagedHSMNestedKeyWithVersionId{ - SubscriptionId: subscriptionId, - ResourceGroup: resourceGroup, - ManagedHSMName: managedHSMName, - KeyName: keyName, - VersionName: versionName, + BaseURI: baseURI, + KeyName: keyName, + Version: versionName, } } -func (id ManagedHSMNestedKeyWithVersionId) String() string { - segments := []string{ - fmt.Sprintf("Version Name %q", id.VersionName), - fmt.Sprintf("Key Name %q", id.KeyName), - fmt.Sprintf("Managed H S M Name %q", id.ManagedHSMName), - fmt.Sprintf("Resource Group %q", id.ResourceGroup), +// FromParseResult implements resourceids.ResourceId. +func (id *ManagedHSMNestedKeyWithVersionId) FromParseResult(input resourceids.ParseResult) error { + var ok bool + + if id.BaseURI, ok = input.Parsed["baseURI"]; !ok { + return resourceids.NewSegmentNotSpecifiedError(id, "baseURI", input) } - segmentsStr := strings.Join(segments, " / ") - return fmt.Sprintf("%s: (%s)", "Managed H S M Nested Key With Version", segmentsStr) + + if id.KeyName, ok = input.Parsed["keyName"]; !ok { + return resourceids.NewSegmentNotSpecifiedError(id, "keyName", input) + } + + if id.Version, ok = input.Parsed["versionName"]; !ok { + return resourceids.NewSegmentNotSpecifiedError(id, "versionName", input) + } + + return nil +} + +// Segments implements resourceids.ResourceId. +func (id *ManagedHSMNestedKeyWithVersionId) Segments() []resourceids.Segment { + return []resourceids.Segment{ + resourceids.DataPlaneHostSegment("baseURI", "https://hsm-name.managedhsm.azure.net"), + resourceids.StaticSegment("staticKeys", "keys", "keys"), + resourceids.UserSpecifiedSegment("keyName", "keyValue"), + resourceids.UserSpecifiedSegment("versionName", "versionValue"), + } +} + +func (id ManagedHSMNestedKeyWithVersionId) String() string { + return fmt.Sprintf("%s: (%s)", "Managed H S M Nested Key With Version", id.ID()) } func (id ManagedHSMNestedKeyWithVersionId) ID() string { - fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.KeyVault/managedHSMs/%s/keys/%s/versions/%s" - return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroup, id.ManagedHSMName, id.KeyName, id.VersionName) + fmtString := "%s/keys/%s/%s" + return fmt.Sprintf(fmtString, id.BaseURI, id.KeyName, id.Version) } // ManagedHSMNestedKeyWithVersionID parses a ManagedHSMNestedKeyWithVersion ID into an ManagedHSMNestedKeyWithVersionId struct -func ManagedHSMNestedKeyWithVersionID(input string) (*ManagedHSMNestedKeyWithVersionId, error) { - id, err := resourceids.ParseAzureResourceID(input) +func ParseManagedHSMNestedKeyWithVersionID(input string) (*ManagedHSMNestedKeyWithVersionId, error) { + parser := resourceids.NewParserFromResourceIdType(&ManagedHSMNestedKeyWithVersionId{}) + parsed, err := parser.Parse(input, false) if err != nil { - return nil, fmt.Errorf("parsing %q as an ManagedHSMNestedKeyWithVersion ID: %+v", input, err) + return nil, fmt.Errorf("parsing %q: %+v", input, err) } - resourceId := ManagedHSMNestedKeyWithVersionId{ - SubscriptionId: id.SubscriptionID, - ResourceGroup: id.ResourceGroup, + id := ManagedHSMNestedKeyWithVersionId{} + if err := id.FromParseResult(*parsed); err != nil { + return nil, err } - if resourceId.SubscriptionId == "" { - return nil, fmt.Errorf("ID was missing the 'subscriptions' element") - } + return &id, nil +} - if resourceId.ResourceGroup == "" { - return nil, fmt.Errorf("ID was missing the 'resourceGroups' element") +func ParseManagedHSMNestedKeyWithVersionIDInsensitively(input string) (*ManagedHSMNestedKeyWithVersionId, error) { + parser := resourceids.NewParserFromResourceIdType(&ManagedHSMNestedKeyWithVersionId{}) + parsed, err := parser.Parse(input, true) + if err != nil { + return nil, fmt.Errorf("parsing %q: %+v", input, err) } - if resourceId.ManagedHSMName, err = id.PopSegment("managedHSMs"); err != nil { + id := ManagedHSMNestedKeyWithVersionId{} + if err := id.FromParseResult(*parsed); err != nil { return nil, err } - if resourceId.KeyName, err = id.PopSegment("keys"); err != nil { - return nil, err - } - if resourceId.VersionName, err = id.PopSegment("versions"); err != nil { - return nil, err + + return &id, nil +} + +func ValidateManagedHSMNestedKeyWithVersionID(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 := id.ValidateNoEmptySegments(input); err != nil { - return nil, err + if _, err := ParseManagedHSMNestedKeyWithVersionID(v); err != nil { + errors = append(errors, err) } - return &resourceId, nil + return } diff --git a/internal/services/managedhsm/parse/managed_hsm_nested_key_with_version_test.go b/internal/services/managedhsm/parse/managed_hsm_nested_key_with_version_test.go index 5e1db4e0841c..9a072cc885c0 100644 --- a/internal/services/managedhsm/parse/managed_hsm_nested_key_with_version_test.go +++ b/internal/services/managedhsm/parse/managed_hsm_nested_key_with_version_test.go @@ -3,145 +3,180 @@ package parse -// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten +import "testing" -import ( - "testing" - - "github.com/hashicorp/go-azure-helpers/resourcemanager/resourceids" -) - -var _ resourceids.Id = ManagedHSMNestedKeyWithVersionId{} - -func TestManagedHSMNestedKeyWithVersionIDFormatter(t *testing.T) { - actual := NewManagedHSMNestedKeyWithVersionID("12345678-1234-9876-4563-123456789012", "resGroup1", "mhsm1", "key1", "version1").ID() - expected := "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/keys/key1/versions/version1" - if actual != expected { - t.Fatalf("Expected %q but got %q", expected, actual) - } -} - -func TestManagedHSMNestedKeyWithVersionID(t *testing.T) { - testData := []struct { - Input string - Error bool - Expected *ManagedHSMNestedKeyWithVersionId +func TestParseManagedHSMNestedKeyWithVersionId(t *testing.T) { + cases := []struct { + Input string + Expected ManagedHSMNestedKeyWithVersionId + ExpectError bool }{ - { - // empty - Input: "", - Error: true, + Input: "", + ExpectError: true, }, - { - // missing SubscriptionId - Input: "/", - Error: true, + Input: "https", + ExpectError: true, }, - { - // missing value for SubscriptionId - Input: "/subscriptions/", - Error: true, + Input: "https://", + ExpectError: true, }, - { - // missing ResourceGroup - Input: "/subscriptions/12345678-1234-9876-4563-123456789012/", - Error: true, + Input: "https://my-hsm.managedhsm.azure.net", + ExpectError: true, }, - { - // missing value for ResourceGroup - Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/", - Error: true, + Input: "https://my-hsm.managedhsm.azure.net/", + ExpectError: true, }, - - { - // missing ManagedHSMName - Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/", - Error: true, - }, - { - // missing value for ManagedHSMName - Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/", - Error: true, + Input: "https://my-hsm.managedhsm.azure.net/keys", + ExpectError: true, }, - { - // missing KeyName - Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/", - Error: true, + Input: "https://my-hsm.managedhsm.azure.net/keys/bird", + ExpectError: true, }, - { - // missing value for KeyName - Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/keys/", - Error: true, + Input: "https://my-hsm.managedhsm.azure.net/invalidNestedItemObjectType/bird/fdf067c93bbb4b22bff4d8b7a9a56217", + ExpectError: true, }, - { - // missing VersionName - Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/keys/key1/", - Error: true, + Input: "https://my-hsm.managedhsm.azure.net/keys/bird/fdf067c93bbb4b22bff4d8b7a9a56217", + ExpectError: false, + Expected: ManagedHSMNestedKeyWithVersionId{ + KeyName: "bird", + BaseURI: "https://my-hsm.managedhsm.azure.net", + Version: "fdf067c93bbb4b22bff4d8b7a9a56217", + }, }, - { - // missing value for VersionName - Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/keys/key1/versions/", - Error: true, + Input: "https://my-hsm.managedhsm.azure.net/keys/hello/world", + ExpectError: false, + Expected: ManagedHSMNestedKeyWithVersionId{ + KeyName: "hello", + BaseURI: "https://my-hsm.managedhsm.azure.net", + Version: "world", + }, }, - { - // valid - Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/keys/key1/versions/version1", - Expected: &ManagedHSMNestedKeyWithVersionId{ - SubscriptionId: "12345678-1234-9876-4563-123456789012", - ResourceGroup: "resGroup1", - ManagedHSMName: "mhsm1", - KeyName: "key1", - VersionName: "version1", + Input: "https://my-hsm.managedhsm.azure.net/keys/castle/1492", + ExpectError: false, + Expected: ManagedHSMNestedKeyWithVersionId{ + KeyName: "castle", + BaseURI: "https://my-hsm.managedhsm.azure.net", + Version: "1492", }, }, - { - // upper-cased - Input: "/SUBSCRIPTIONS/12345678-1234-9876-4563-123456789012/RESOURCEGROUPS/RESGROUP1/PROVIDERS/MICROSOFT.KEYVAULT/MANAGEDHSMS/MHSM1/KEYS/KEY1/VERSIONS/VERSION1", - Error: true, + Input: "https://my-hsm.managedhsm.azure.net/keys/bird/fdf067c93bbb4b22bff4d8b7a9a56217/XXX", + ExpectError: true, }, } - for _, v := range testData { - t.Logf("[DEBUG] Testing %q", v.Input) + for _, tc := range cases { + t.Logf("[DEBUG] Testing %q", tc.Input) - actual, err := ManagedHSMNestedKeyWithVersionID(v.Input) + keyID, err := ParseManagedHSMNestedKeyWithVersionID(tc.Input) if err != nil { - if v.Error { + if tc.ExpectError { + t.Logf("[DEBUG] --> [Received Expected Error]: %+v", err) continue } - t.Fatalf("Expect a value but got an error: %s", err) + t.Fatalf("Got error for ID '%s': %+v", tc.Input, err) } - if v.Error { - t.Fatal("Expect an error but didn't get one") + + if keyID == nil { + t.Fatalf("Expected a SecretID to be parsed for ID '%s', got nil.", tc.Input) } - if actual.SubscriptionId != v.Expected.SubscriptionId { - t.Fatalf("Expected %q but got %q for SubscriptionId", v.Expected.SubscriptionId, actual.SubscriptionId) + if tc.Expected.BaseURI != keyID.BaseURI { + t.Fatalf("Expected 'BaseURI' to be '%s', got '%s' for ID '%s'", tc.Expected.BaseURI, keyID.BaseURI, tc.Input) } - if actual.ResourceGroup != v.Expected.ResourceGroup { - t.Fatalf("Expected %q but got %q for ResourceGroup", v.Expected.ResourceGroup, actual.ResourceGroup) + + if tc.Expected.KeyName != keyID.KeyName { + t.Fatalf("Expected 'Version' to be '%s', got '%s' for ID '%s'", tc.Expected.KeyName, keyID.KeyName, tc.Input) } - if actual.ManagedHSMName != v.Expected.ManagedHSMName { - t.Fatalf("Expected %q but got %q for ManagedHSMName", v.Expected.ManagedHSMName, actual.ManagedHSMName) + + if tc.Expected.Version != keyID.Version { + t.Fatalf("Expected 'Version' to be '%s', got '%s' for ID '%s'", tc.Expected.Version, keyID.Version, tc.Input) } - if actual.KeyName != v.Expected.KeyName { - t.Fatalf("Expected %q but got %q for KeyName", v.Expected.KeyName, actual.KeyName) + + if tc.Input != keyID.ID() { + t.Fatalf("Expected 'ID()' to be '%s', got '%s'", tc.Input, keyID.ID()) } - if actual.VersionName != v.Expected.VersionName { - t.Fatalf("Expected %q but got %q for VersionName", v.Expected.VersionName, actual.VersionName) + t.Logf("[DEBUG] --> [Valid Value]: %+v", tc.Input) + } +} + +func TestValidateManagedHSMNestedWithVersionId(t *testing.T) { + cases := []struct { + Input string + ExpectError bool + }{ + { + Input: "", + ExpectError: true, + }, + { + Input: "https", + ExpectError: true, + }, + { + Input: "https://", + ExpectError: true, + }, + { + Input: "https://my-hsm.managedhsm.azure.net", + ExpectError: true, + }, + { + Input: "https://my-hsm.managedhsm.azure.net/", + ExpectError: true, + }, + { + Input: "https://my-hsm.managedhsm.azure.net/keys", + ExpectError: true, + }, + { + Input: "https://my-hsm.managedhsm.azure.net/keys/bird", + ExpectError: true, + }, + { + Input: "https://my-hsm.managedhsm.azure.net/Keys/bird", + ExpectError: true, + }, + { + Input: "https://my-hsm.managedhsm.azure.net/invalidNestedItemObjectType/bird/fdf067c93bbb4b22bff4d8b7a9a56217", + ExpectError: true, + }, + { + Input: "https://my-hsm.managedhsm.azure.net/keys/bird/fdf067c93bbb4b22bff4d8b7a9a56217", + ExpectError: false, + }, + { + Input: "https://my-hsm.managedhsm.azure.net/keys/hello/world", + ExpectError: false, + }, + { + Input: "https://my-hsm.managedhsm.azure.net/keys/castle/1492", + ExpectError: false, + }, + { + Input: "https://my-hsm.managedhsm.azure.net/keys/bird/fdf067c93bbb4b22bff4d8b7a9a56217/XXX", + ExpectError: true, + }, + } + + for _, tc := range cases { + t.Logf("[DEBUG] Testing %q", tc.Input) + + _, errors := ValidateManagedHSMNestedKeyWithVersionID(tc.Input, "test") + if (len(errors) > 0) != tc.ExpectError { + t.Fatalf("expect error %T, but got %T", tc.ExpectError, len(errors) > 0) } } } diff --git a/internal/services/managedhsm/validate/managed_hsm_nested_key_versionless_id.go b/internal/services/managedhsm/validate/managed_hsm_nested_key_versionless_id.go deleted file mode 100644 index a2925df99097..000000000000 --- a/internal/services/managedhsm/validate/managed_hsm_nested_key_versionless_id.go +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright (c) HashiCorp, Inc. -// SPDX-License-Identifier: MPL-2.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/managedhsm/parse" -) - -func ManagedHSMNestedKeyVersionlessID(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.ManagedHSMNestedKeyVersionlessID(v); err != nil { - errors = append(errors, err) - } - - return -} diff --git a/internal/services/managedhsm/validate/managed_hsm_nested_key_versionless_id_test.go b/internal/services/managedhsm/validate/managed_hsm_nested_key_versionless_id_test.go deleted file mode 100644 index e12960a66540..000000000000 --- a/internal/services/managedhsm/validate/managed_hsm_nested_key_versionless_id_test.go +++ /dev/null @@ -1,91 +0,0 @@ -// Copyright (c) HashiCorp, Inc. -// SPDX-License-Identifier: MPL-2.0 - -package validate - -// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten - -import "testing" - -func TestManagedHSMNestedKeyVersionlessID(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 ManagedHSMName - Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/", - Valid: false, - }, - - { - // missing value for ManagedHSMName - Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/", - Valid: false, - }, - - { - // missing KeyName - Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/", - Valid: false, - }, - - { - // missing value for KeyName - Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/keys/", - Valid: false, - }, - - { - // valid - Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/keys/key1", - Valid: true, - }, - - { - // upper-cased - Input: "/SUBSCRIPTIONS/12345678-1234-9876-4563-123456789012/RESOURCEGROUPS/RESGROUP1/PROVIDERS/MICROSOFT.KEYVAULT/MANAGEDHSMS/MHSM1/KEYS/KEY1", - Valid: false, - }, - } - for _, tc := range cases { - t.Logf("[DEBUG] Testing Value %s", tc.Input) - _, errors := ManagedHSMNestedKeyVersionlessID(tc.Input, "test") - valid := len(errors) == 0 - - if tc.Valid != valid { - t.Fatalf("Expected %t but got %t", tc.Valid, valid) - } - } -} diff --git a/internal/services/managedhsm/validate/managed_hsm_nested_key_with_version_id.go b/internal/services/managedhsm/validate/managed_hsm_nested_key_with_version_id.go deleted file mode 100644 index ffe2569f8b27..000000000000 --- a/internal/services/managedhsm/validate/managed_hsm_nested_key_with_version_id.go +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright (c) HashiCorp, Inc. -// SPDX-License-Identifier: MPL-2.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/managedhsm/parse" -) - -func ManagedHSMNestedKeyWithVersionID(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.ManagedHSMNestedKeyWithVersionID(v); err != nil { - errors = append(errors, err) - } - - return -} diff --git a/internal/services/managedhsm/validate/managed_hsm_nested_key_with_version_id_test.go b/internal/services/managedhsm/validate/managed_hsm_nested_key_with_version_id_test.go deleted file mode 100644 index f94040ceb671..000000000000 --- a/internal/services/managedhsm/validate/managed_hsm_nested_key_with_version_id_test.go +++ /dev/null @@ -1,103 +0,0 @@ -// Copyright (c) HashiCorp, Inc. -// SPDX-License-Identifier: MPL-2.0 - -package validate - -// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten - -import "testing" - -func TestManagedHSMNestedKeyWithVersionID(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 ManagedHSMName - Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/", - Valid: false, - }, - - { - // missing value for ManagedHSMName - Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/", - Valid: false, - }, - - { - // missing KeyName - Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/", - Valid: false, - }, - - { - // missing value for KeyName - Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/keys/", - Valid: false, - }, - - { - // missing VersionName - Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/keys/key1/", - Valid: false, - }, - - { - // missing value for VersionName - Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/keys/key1/versions/", - Valid: false, - }, - - { - // valid - Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/keys/key1/versions/version1", - Valid: true, - }, - - { - // upper-cased - Input: "/SUBSCRIPTIONS/12345678-1234-9876-4563-123456789012/RESOURCEGROUPS/RESGROUP1/PROVIDERS/MICROSOFT.KEYVAULT/MANAGEDHSMS/MHSM1/KEYS/KEY1/VERSIONS/VERSION1", - Valid: false, - }, - } - for _, tc := range cases { - t.Logf("[DEBUG] Testing Value %s", tc.Input) - _, errors := ManagedHSMNestedKeyWithVersionID(tc.Input, "test") - valid := len(errors) == 0 - - if tc.Valid != valid { - t.Fatalf("Expected %t but got %t", tc.Valid, valid) - } - } -} From 7d32c68996dc9ae26451696f4c78e8129897e0da Mon Sep 17 00:00:00 2001 From: xuwu1 Date: Tue, 26 Mar 2024 13:14:20 +0800 Subject: [PATCH 3/3] remove id generate file --- internal/services/managedhsm/resourceids.go | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 internal/services/managedhsm/resourceids.go diff --git a/internal/services/managedhsm/resourceids.go b/internal/services/managedhsm/resourceids.go deleted file mode 100644 index 09dbb9f012b9..000000000000 --- a/internal/services/managedhsm/resourceids.go +++ /dev/null @@ -1,7 +0,0 @@ -// Copyright (c) HashiCorp, Inc. -// SPDX-License-Identifier: MPL-2.0 - -package managedhsm - -//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=ManagedHSMNestedKeyWithVersion -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/keys/key1/versions/version1 -//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=ManagedHSMNestedKeyVersionless -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.KeyVault/managedHSMs/mhsm1/keys/key1