Skip to content

Commit

Permalink
add managed hsm nested key ids
Browse files Browse the repository at this point in the history
  • Loading branch information
wuxu92 committed Mar 20, 2024
1 parent 810ce18 commit 2f80ee4
Show file tree
Hide file tree
Showing 9 changed files with 693 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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 ManagedHSMNestedKeyIdVersionlessId struct {
SubscriptionId string
ResourceGroup string
ManagedHSMName string
KeyName string
}

func NewManagedHSMNestedKeyIdVersionlessID(subscriptionId, resourceGroup, managedHSMName, keyName string) ManagedHSMNestedKeyIdVersionlessId {
return ManagedHSMNestedKeyIdVersionlessId{
SubscriptionId: subscriptionId,
ResourceGroup: resourceGroup,
ManagedHSMName: managedHSMName,
KeyName: keyName,
}
}

func (id ManagedHSMNestedKeyIdVersionlessId) 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 Id Versionless", segmentsStr)
}

func (id ManagedHSMNestedKeyIdVersionlessId) 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)
}

// ManagedHSMNestedKeyIdVersionlessID parses a ManagedHSMNestedKeyIdVersionless ID into an ManagedHSMNestedKeyIdVersionlessId struct
func ManagedHSMNestedKeyIdVersionlessID(input string) (*ManagedHSMNestedKeyIdVersionlessId, error) {
id, err := resourceids.ParseAzureResourceID(input)
if err != nil {
return nil, fmt.Errorf("parsing %q as an ManagedHSMNestedKeyIdVersionless ID: %+v", input, err)
}

resourceId := ManagedHSMNestedKeyIdVersionlessId{
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
}
Original file line number Diff line number Diff line change
@@ -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 = ManagedHSMNestedKeyIdVersionlessId{}

func TestManagedHSMNestedKeyIdVersionlessIDFormatter(t *testing.T) {
actual := NewManagedHSMNestedKeyIdVersionlessID("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 TestManagedHSMNestedKeyIdVersionlessID(t *testing.T) {
testData := []struct {
Input string
Error bool
Expected *ManagedHSMNestedKeyIdVersionlessId
}{

{
// 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: &ManagedHSMNestedKeyIdVersionlessId{
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 := ManagedHSMNestedKeyIdVersionlessID(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)
}
}
}
Original file line number Diff line number Diff line change
@@ -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 ManagedHSMNestedKeyIdWithVersionId struct {
SubscriptionId string
ResourceGroup string
ManagedHSMName string
KeyName string
VersionName string
}

func NewManagedHSMNestedKeyIdWithVersionID(subscriptionId, resourceGroup, managedHSMName, keyName, versionName string) ManagedHSMNestedKeyIdWithVersionId {
return ManagedHSMNestedKeyIdWithVersionId{
SubscriptionId: subscriptionId,
ResourceGroup: resourceGroup,
ManagedHSMName: managedHSMName,
KeyName: keyName,
VersionName: versionName,
}
}

func (id ManagedHSMNestedKeyIdWithVersionId) 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 Id With Version", segmentsStr)
}

func (id ManagedHSMNestedKeyIdWithVersionId) 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)
}

// ManagedHSMNestedKeyIdWithVersionID parses a ManagedHSMNestedKeyIdWithVersion ID into an ManagedHSMNestedKeyIdWithVersionId struct
func ManagedHSMNestedKeyIdWithVersionID(input string) (*ManagedHSMNestedKeyIdWithVersionId, error) {
id, err := resourceids.ParseAzureResourceID(input)
if err != nil {
return nil, fmt.Errorf("parsing %q as an ManagedHSMNestedKeyIdWithVersion ID: %+v", input, err)
}

resourceId := ManagedHSMNestedKeyIdWithVersionId{
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
}
Loading

0 comments on commit 2f80ee4

Please sign in to comment.