Skip to content

Commit 96a1ded

Browse files
Merge branch 'hashicorp:main' into fix-dv-hp-regtoken
2 parents 11fa2ff + 70a4cb3 commit 96a1ded

21 files changed

+2068
-1284
lines changed

internal/services/cognitive/sdk/2021-04-30/cognitiveservicesaccounts/constants.go

+315
Large diffs are not rendered by default.

internal/services/cognitive/sdk/2021-04-30/cognitiveservicesaccounts/id_account.go

+79-63
Original file line numberDiff line numberDiff line change
@@ -7,102 +7,118 @@ import (
77
"github.com/hashicorp/go-azure-helpers/resourcemanager/resourceids"
88
)
99

10+
var _ resourceids.ResourceId = AccountId{}
11+
12+
// AccountId is a struct representing the Resource ID for a Account
1013
type AccountId struct {
11-
SubscriptionId string
12-
ResourceGroup string
13-
Name string
14+
SubscriptionId string
15+
ResourceGroupName string
16+
AccountName string
1417
}
1518

16-
func NewAccountID(subscriptionId, resourceGroup, name string) AccountId {
19+
// NewAccountID returns a new AccountId struct
20+
func NewAccountID(subscriptionId string, resourceGroupName string, accountName string) AccountId {
1721
return AccountId{
18-
SubscriptionId: subscriptionId,
19-
ResourceGroup: resourceGroup,
20-
Name: name,
22+
SubscriptionId: subscriptionId,
23+
ResourceGroupName: resourceGroupName,
24+
AccountName: accountName,
2125
}
2226
}
2327

24-
func (id AccountId) String() string {
25-
segments := []string{
26-
fmt.Sprintf("Name %q", id.Name),
27-
fmt.Sprintf("Resource Group %q", id.ResourceGroup),
28-
}
29-
segmentsStr := strings.Join(segments, " / ")
30-
return fmt.Sprintf("%s: (%s)", "Account", segmentsStr)
31-
}
32-
33-
func (id AccountId) ID() string {
34-
fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.CognitiveServices/accounts/%s"
35-
return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroup, id.Name)
36-
}
37-
38-
// ParseAccountID parses a Account ID into an AccountId struct
28+
// ParseAccountID parses 'input' into a AccountId
3929
func ParseAccountID(input string) (*AccountId, error) {
40-
id, err := resourceids.ParseAzureResourceID(input)
30+
parser := resourceids.NewParserFromResourceIdType(AccountId{})
31+
parsed, err := parser.Parse(input, false)
4132
if err != nil {
42-
return nil, err
43-
}
44-
45-
resourceId := AccountId{
46-
SubscriptionId: id.SubscriptionID,
47-
ResourceGroup: id.ResourceGroup,
33+
return nil, fmt.Errorf("parsing %q: %+v", input, err)
4834
}
4935

50-
if resourceId.SubscriptionId == "" {
51-
return nil, fmt.Errorf("ID was missing the 'subscriptions' element")
52-
}
36+
var ok bool
37+
id := AccountId{}
5338

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

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

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

66-
return &resourceId, nil
51+
return &id, nil
6752
}
6853

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

78-
resourceId := AccountId{
79-
SubscriptionId: id.SubscriptionID,
80-
ResourceGroup: id.ResourceGroup,
63+
var ok bool
64+
id := AccountId{}
65+
66+
if id.SubscriptionId, ok = parsed.Parsed["subscriptionId"]; !ok {
67+
return nil, fmt.Errorf("the segment 'subscriptionId' was not found in the resource id %q", input)
8168
}
8269

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

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

91-
// find the correct casing for the 'accounts' segment
92-
accountsKey := "accounts"
93-
for key := range id.Path {
94-
if strings.EqualFold(key, accountsKey) {
95-
accountsKey = key
96-
break
97-
}
78+
return &id, nil
79+
}
80+
81+
// ValidateAccountID checks that 'input' can be parsed as a Account ID
82+
func ValidateAccountID(input interface{}, key string) (warnings []string, errors []error) {
83+
v, ok := input.(string)
84+
if !ok {
85+
errors = append(errors, fmt.Errorf("expected %q to be a string", key))
86+
return
9887
}
99-
if resourceId.Name, err = id.PopSegment(accountsKey); err != nil {
100-
return nil, err
88+
89+
if _, err := ParseAccountID(v); err != nil {
90+
errors = append(errors, err)
10191
}
10292

103-
if err := id.ValidateNoEmptySegments(input); err != nil {
104-
return nil, err
93+
return
94+
}
95+
96+
// ID returns the formatted Account ID
97+
func (id AccountId) ID() string {
98+
fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.CognitiveServices/accounts/%s"
99+
return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroupName, id.AccountName)
100+
}
101+
102+
// Segments returns a slice of Resource ID Segments which comprise this Account ID
103+
func (id AccountId) Segments() []resourceids.Segment {
104+
return []resourceids.Segment{
105+
resourceids.StaticSegment("subscriptions", "subscriptions", "subscriptions"),
106+
resourceids.SubscriptionIdSegment("subscriptionId", "12345678-1234-9876-4563-123456789012"),
107+
resourceids.StaticSegment("resourceGroups", "resourceGroups", "resourceGroups"),
108+
resourceids.ResourceGroupSegment("resourceGroupName", "example-resource-group"),
109+
resourceids.StaticSegment("providers", "providers", "providers"),
110+
resourceids.ResourceProviderSegment("microsoftCognitiveServices", "Microsoft.CognitiveServices", "Microsoft.CognitiveServices"),
111+
resourceids.StaticSegment("accounts", "accounts", "accounts"),
112+
resourceids.UserSpecifiedSegment("accountName", "accountValue"),
105113
}
114+
}
106115

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

0 commit comments

Comments
 (0)