/
key_vault_child.go
127 lines (98 loc) · 3.46 KB
/
key_vault_child.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
125
126
127
package azure
import (
"fmt"
"net/url"
"regexp"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
)
type KeyVaultChildID struct {
KeyVaultBaseUrl string
Name string
Version string
}
func ParseKeyVaultChildID(id string) (*KeyVaultChildID, error) {
// example: https://tharvey-keyvault.vault.azure.net/type/bird/fdf067c93bbb4b22bff4d8b7a9a56217
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) != 3 {
return nil, fmt.Errorf("Azure KeyVault Child Id should have 3 segments, got %d: '%s'", len(components), path)
}
childId := KeyVaultChildID{
KeyVaultBaseUrl: fmt.Sprintf("%s://%s/", idURL.Scheme, idURL.Host),
Name: components[1],
Version: components[2],
}
return &childId, nil
}
func ParseKeyVaultChildIDVersionOptional(id string) (*KeyVaultChildID, error) {
// example: https://tharvey-keyvault.vault.azure.net/type/bird/fdf067c93bbb4b22bff4d8b7a9a56217
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("Azure KeyVault Child Id should have 2 or 3 segments, got %d: '%s'", len(components), path)
}
version := ""
if len(components) == 3 {
version = components[2]
}
childId := KeyVaultChildID{
KeyVaultBaseUrl: fmt.Sprintf("%s://%s/", idURL.Scheme, idURL.Host),
Name: components[1],
Version: version,
}
return &childId, nil
}
func ValidateKeyVaultChildName(v interface{}, k string) (warnings []string, errors []error) {
value := v.(string)
if matched := regexp.MustCompile(`^[0-9a-zA-Z-]+$`).Match([]byte(value)); !matched {
errors = append(errors, fmt.Errorf("%q may only contain alphanumeric characters and dashes", k))
}
return warnings, errors
}
// Unfortunately this can't (easily) go in the Validate package
// since there's a circular reference on this package
func ValidateKeyVaultChildId(i interface{}, k string) (warnings []string, errors []error) {
if warnings, errors = validation.StringIsNotEmpty(i, k); len(errors) > 0 {
return warnings, errors
}
v, ok := i.(string)
if !ok {
errors = append(errors, fmt.Errorf("Expected %s to be a string!", k))
return warnings, errors
}
if _, err := ParseKeyVaultChildID(v); err != nil {
errors = append(errors, fmt.Errorf("Error parsing Key Vault Child ID: %s", err))
return warnings, errors
}
return warnings, errors
}
// Unfortunately this can't (easily) go in the Validate package
// since there's a circular reference on this package
func ValidateKeyVaultChildIdVersionOptional(i interface{}, k string) (warnings []string, errors []error) {
if warnings, errors = validation.StringIsNotEmpty(i, k); len(errors) > 0 {
return warnings, errors
}
v, ok := i.(string)
if !ok {
errors = append(errors, fmt.Errorf("Expected %s to be a string!", k))
return warnings, errors
}
if _, err := ParseKeyVaultChildIDVersionOptional(v); err != nil {
errors = append(errors, fmt.Errorf("Error parsing Key Vault Child ID: %s", err))
return warnings, errors
}
return warnings, errors
}