Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

managed_hsm_*: add managed hsm nested key ids #25324

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 127 additions & 0 deletions internal/services/managedhsm/parse/managed_hsm_key_versionless.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// Copyright (c) HashiCorp, Inc.

Check failure on line 1 in internal/services/managedhsm/parse/managed_hsm_key_versionless.go

View workflow job for this annotation

GitHub Actions / golint

: # github.com/hashicorp/terraform-provider-azurerm/internal/services/managedhsm/parse [github.com/hashicorp/terraform-provider-azurerm/internal/services/managedhsm/parse.test]
// 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
}
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
Loading
Loading