@@ -7,102 +7,118 @@ import (
7
7
"github.com/hashicorp/go-azure-helpers/resourcemanager/resourceids"
8
8
)
9
9
10
+ var _ resourceids.ResourceId = AccountId {}
11
+
12
+ // AccountId is a struct representing the Resource ID for a Account
10
13
type AccountId struct {
11
- SubscriptionId string
12
- ResourceGroup string
13
- Name string
14
+ SubscriptionId string
15
+ ResourceGroupName string
16
+ AccountName string
14
17
}
15
18
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 {
17
21
return AccountId {
18
- SubscriptionId : subscriptionId ,
19
- ResourceGroup : resourceGroup ,
20
- Name : name ,
22
+ SubscriptionId : subscriptionId ,
23
+ ResourceGroupName : resourceGroupName ,
24
+ AccountName : accountName ,
21
25
}
22
26
}
23
27
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
39
29
func ParseAccountID (input string ) (* AccountId , error ) {
40
- id , err := resourceids .ParseAzureResourceID (input )
30
+ parser := resourceids .NewParserFromResourceIdType (AccountId {})
31
+ parsed , err := parser .Parse (input , false )
41
32
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 )
48
34
}
49
35
50
- if resourceId .SubscriptionId == "" {
51
- return nil , fmt .Errorf ("ID was missing the 'subscriptions' element" )
52
- }
36
+ var ok bool
37
+ id := AccountId {}
53
38
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 )
56
41
}
57
42
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 )
60
45
}
61
46
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 )
64
49
}
65
50
66
- return & resourceId , nil
51
+ return & id , nil
67
52
}
68
53
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
72
56
func ParseAccountIDInsensitively (input string ) (* AccountId , error ) {
73
- id , err := resourceids .ParseAzureResourceID (input )
57
+ parser := resourceids .NewParserFromResourceIdType (AccountId {})
58
+ parsed , err := parser .Parse (input , true )
74
59
if err != nil {
75
- return nil , err
60
+ return nil , fmt . Errorf ( "parsing %q: %+v" , input , err )
76
61
}
77
62
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 )
81
68
}
82
69
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 )
85
72
}
86
73
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 )
89
76
}
90
77
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
98
87
}
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 )
101
91
}
102
92
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" ),
105
113
}
114
+ }
106
115
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 " ))
108
124
}
0 commit comments