-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
nested_item.go
124 lines (103 loc) · 3.53 KB
/
nested_item.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package parse
import (
"fmt"
"net/url"
"strings"
"github.com/hashicorp/go-azure-helpers/resourcemanager/resourceids"
)
var _ resourceids.Id = NestedItemId{}
type NestedItemId struct {
KeyVaultBaseUrl string
NestedItemType string
Name string
Version string
}
func NewNestedItemID(keyVaultBaseUrl, nestedItemType, name, version string) (*NestedItemId, error) {
keyVaultUrl, err := url.Parse(keyVaultBaseUrl)
if err != nil || keyVaultBaseUrl == "" {
return nil, fmt.Errorf("parsing %q: %+v", keyVaultBaseUrl, err)
}
// (@jackofallops) - Log Analytics service adds the port number to the API returns, so we strip it here
if hostParts := strings.Split(keyVaultUrl.Host, ":"); len(hostParts) > 1 {
keyVaultUrl.Host = hostParts[0]
}
return &NestedItemId{
KeyVaultBaseUrl: keyVaultUrl.String(),
NestedItemType: nestedItemType,
Name: name,
Version: version,
}, nil
}
func (id NestedItemId) ID() string {
// example: https://tharvey-keyvault.vault.azure.net/type/bird/fdf067c93bbb4b22bff4d8b7a9a56217
segments := []string{
strings.TrimSuffix(id.KeyVaultBaseUrl, "/"),
id.NestedItemType,
id.Name,
}
if id.Version != "" {
segments = append(segments, id.Version)
}
return strings.TrimSuffix(strings.Join(segments, "/"), "/")
}
func (id NestedItemId) String() string {
components := []string{
fmt.Sprintf("Base Url %q", id.KeyVaultBaseUrl),
fmt.Sprintf("Nested Item Type %q", id.NestedItemType),
fmt.Sprintf("Name %q", id.Name),
fmt.Sprintf("Version %q", id.Version),
}
return fmt.Sprintf("Key Vault Nested Item %s", strings.Join(components, " / "))
}
func (id NestedItemId) VersionlessID() string {
// example: https://tharvey-keyvault.vault.azure.net/type/bird
segments := []string{
strings.TrimSuffix(id.KeyVaultBaseUrl, "/"),
id.NestedItemType,
id.Name,
}
return strings.TrimSuffix(strings.Join(segments, "/"), "/")
}
// ParseNestedItemID parses a Key Vault Nested Item ID (such as a Certificate, Key or Secret)
// containing a version into a NestedItemId object
func ParseNestedItemID(input string) (*NestedItemId, error) {
item, err := parseNestedItemId(input)
if err != nil {
return nil, err
}
if item.Version == "" {
return nil, fmt.Errorf("expected a versioned ID but no version in %q", input)
}
return item, nil
}
// ParseOptionallyVersionedNestedItemID parses a Key Vault Nested Item ID (such as a Certificate, Key or Secret)
// optionally containing a version into a NestedItemId object
func ParseOptionallyVersionedNestedItemID(input string) (*NestedItemId, error) {
return parseNestedItemId(input)
}
func parseNestedItemId(id string) (*NestedItemId, error) {
// versioned example: https://tharvey-keyvault.vault.azure.net/type/bird/fdf067c93bbb4b22bff4d8b7a9a56217
// versionless example: https://tharvey-keyvault.vault.azure.net/type/bird/
idURL, err := url.ParseRequestURI(id)
if err != nil {
return nil, fmt.Errorf("Cannot parse Azure KeyVault Child Id: %s", err)
}
path := idURL.Path
path = strings.TrimPrefix(path, "/")
path = strings.TrimSuffix(path, "/")
components := strings.Split(path, "/")
if len(components) != 2 && len(components) != 3 {
return nil, fmt.Errorf("KeyVault Nested Item should contain 2 or 3 segments, got %d from %q", len(components), path)
}
version := ""
if len(components) == 3 {
version = components[2]
}
childId := NestedItemId{
KeyVaultBaseUrl: fmt.Sprintf("%s://%s/", idURL.Scheme, idURL.Host),
NestedItemType: components[0],
Name: components[1],
Version: version,
}
return &childId, nil
}