Skip to content

Commit

Permalink
Merge branch 'hashicorp:main' into fix-dv-hp-regtoken
Browse files Browse the repository at this point in the history
  • Loading branch information
amccullough84 authored Nov 10, 2021
2 parents 11fa2ff + 70a4cb3 commit 96a1ded
Show file tree
Hide file tree
Showing 21 changed files with 2,068 additions and 1,284 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -7,102 +7,118 @@ import (
"github.com/hashicorp/go-azure-helpers/resourcemanager/resourceids"
)

var _ resourceids.ResourceId = AccountId{}

// AccountId is a struct representing the Resource ID for a Account
type AccountId struct {
SubscriptionId string
ResourceGroup string
Name string
SubscriptionId string
ResourceGroupName string
AccountName string
}

func NewAccountID(subscriptionId, resourceGroup, name string) AccountId {
// NewAccountID returns a new AccountId struct
func NewAccountID(subscriptionId string, resourceGroupName string, accountName string) AccountId {
return AccountId{
SubscriptionId: subscriptionId,
ResourceGroup: resourceGroup,
Name: name,
SubscriptionId: subscriptionId,
ResourceGroupName: resourceGroupName,
AccountName: accountName,
}
}

func (id AccountId) String() string {
segments := []string{
fmt.Sprintf("Name %q", id.Name),
fmt.Sprintf("Resource Group %q", id.ResourceGroup),
}
segmentsStr := strings.Join(segments, " / ")
return fmt.Sprintf("%s: (%s)", "Account", segmentsStr)
}

func (id AccountId) ID() string {
fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.CognitiveServices/accounts/%s"
return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroup, id.Name)
}

// ParseAccountID parses a Account ID into an AccountId struct
// ParseAccountID parses 'input' into a AccountId
func ParseAccountID(input string) (*AccountId, error) {
id, err := resourceids.ParseAzureResourceID(input)
parser := resourceids.NewParserFromResourceIdType(AccountId{})
parsed, err := parser.Parse(input, false)
if err != nil {
return nil, err
}

resourceId := AccountId{
SubscriptionId: id.SubscriptionID,
ResourceGroup: id.ResourceGroup,
return nil, fmt.Errorf("parsing %q: %+v", input, err)
}

if resourceId.SubscriptionId == "" {
return nil, fmt.Errorf("ID was missing the 'subscriptions' element")
}
var ok bool
id := AccountId{}

if resourceId.ResourceGroup == "" {
return nil, fmt.Errorf("ID was missing the 'resourceGroups' element")
if id.SubscriptionId, ok = parsed.Parsed["subscriptionId"]; !ok {
return nil, fmt.Errorf("the segment 'subscriptionId' was not found in the resource id %q", input)
}

if resourceId.Name, err = id.PopSegment("accounts"); err != nil {
return nil, err
if id.ResourceGroupName, ok = parsed.Parsed["resourceGroupName"]; !ok {
return nil, fmt.Errorf("the segment 'resourceGroupName' was not found in the resource id %q", input)
}

if err := id.ValidateNoEmptySegments(input); err != nil {
return nil, err
if id.AccountName, ok = parsed.Parsed["accountName"]; !ok {
return nil, fmt.Errorf("the segment 'accountName' was not found in the resource id %q", input)
}

return &resourceId, nil
return &id, nil
}

// ParseAccountIDInsensitively parses an Account ID into an AccountId struct, insensitively
// This should only be used to parse an ID for rewriting to a consistent casing,
// the ParseAccountID method should be used instead for validation etc.
// ParseAccountIDInsensitively parses 'input' case-insensitively into a AccountId
// note: this method should only be used for API response data and not user input
func ParseAccountIDInsensitively(input string) (*AccountId, error) {
id, err := resourceids.ParseAzureResourceID(input)
parser := resourceids.NewParserFromResourceIdType(AccountId{})
parsed, err := parser.Parse(input, true)
if err != nil {
return nil, err
return nil, fmt.Errorf("parsing %q: %+v", input, err)
}

resourceId := AccountId{
SubscriptionId: id.SubscriptionID,
ResourceGroup: id.ResourceGroup,
var ok bool
id := AccountId{}

if id.SubscriptionId, ok = parsed.Parsed["subscriptionId"]; !ok {
return nil, fmt.Errorf("the segment 'subscriptionId' was not found in the resource id %q", input)
}

if resourceId.SubscriptionId == "" {
return nil, fmt.Errorf("ID was missing the 'subscriptions' element")
if id.ResourceGroupName, ok = parsed.Parsed["resourceGroupName"]; !ok {
return nil, fmt.Errorf("the segment 'resourceGroupName' was not found in the resource id %q", input)
}

if resourceId.ResourceGroup == "" {
return nil, fmt.Errorf("ID was missing the 'resourceGroups' element")
if id.AccountName, ok = parsed.Parsed["accountName"]; !ok {
return nil, fmt.Errorf("the segment 'accountName' was not found in the resource id %q", input)
}

// find the correct casing for the 'accounts' segment
accountsKey := "accounts"
for key := range id.Path {
if strings.EqualFold(key, accountsKey) {
accountsKey = key
break
}
return &id, nil
}

// ValidateAccountID checks that 'input' can be parsed as a Account ID
func ValidateAccountID(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 resourceId.Name, err = id.PopSegment(accountsKey); err != nil {
return nil, err

if _, err := ParseAccountID(v); err != nil {
errors = append(errors, err)
}

if err := id.ValidateNoEmptySegments(input); err != nil {
return nil, err
return
}

// ID returns the formatted Account ID
func (id AccountId) ID() string {
fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.CognitiveServices/accounts/%s"
return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroupName, id.AccountName)
}

// Segments returns a slice of Resource ID Segments which comprise this Account ID
func (id AccountId) Segments() []resourceids.Segment {
return []resourceids.Segment{
resourceids.StaticSegment("subscriptions", "subscriptions", "subscriptions"),
resourceids.SubscriptionIdSegment("subscriptionId", "12345678-1234-9876-4563-123456789012"),
resourceids.StaticSegment("resourceGroups", "resourceGroups", "resourceGroups"),
resourceids.ResourceGroupSegment("resourceGroupName", "example-resource-group"),
resourceids.StaticSegment("providers", "providers", "providers"),
resourceids.ResourceProviderSegment("microsoftCognitiveServices", "Microsoft.CognitiveServices", "Microsoft.CognitiveServices"),
resourceids.StaticSegment("accounts", "accounts", "accounts"),
resourceids.UserSpecifiedSegment("accountName", "accountValue"),
}
}

return &resourceId, nil
// String returns a human-readable description of this Account ID
func (id AccountId) String() string {
components := []string{
fmt.Sprintf("Subscription: %q", id.SubscriptionId),
fmt.Sprintf("Resource Group Name: %q", id.ResourceGroupName),
fmt.Sprintf("Account Name: %q", id.AccountName),
}
return fmt.Sprintf("Account (%s)", strings.Join(components, "\n"))
}
Loading

0 comments on commit 96a1ded

Please sign in to comment.