-
Notifications
You must be signed in to change notification settings - Fork 1k
/
azure_cloud_environment.go
57 lines (43 loc) · 1.84 KB
/
azure_cloud_environment.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
package azure
import (
"fmt"
"strings"
az "github.com/Azure/go-autorest/autorest/azure"
)
const (
DefaultCloud = "azurePublicCloud"
// PrivateCloud cloud type
PrivateCloud string = "Private"
// DefaultEndpointSuffixKey is the default endpoint key in trigger metadata
DefaultEndpointSuffixKey string = "endpointSuffix"
// DefaultStorageSuffixKey is the default storage endpoint key in trigger metadata
DefaultStorageSuffixKey string = "storageEndpointSuffix"
// DefaultActiveDirectoryEndpointKey is the default active directory endpoint key in trigger metadata
DefaultActiveDirectoryEndpointKey string = "activeDirectoryEndpoint"
)
// EnvironmentPropertyProvider for different types of Azure scalers
type EnvironmentPropertyProvider func(env az.Environment) (string, error)
var activeDirectoryEndpointProvider = func(env az.Environment) (string, error) {
return env.ActiveDirectoryEndpoint, nil
}
// ParseEnvironmentProperty parses cloud metadata and returns the resolved property
func ParseEnvironmentProperty(metadata map[string]string, propertyKey string, envPropertyProvider EnvironmentPropertyProvider) (string, error) {
if val, ok := metadata["cloud"]; ok && val != "" {
if strings.EqualFold(val, PrivateCloud) {
if val, ok := metadata[propertyKey]; ok && val != "" {
return val, nil
}
return "", fmt.Errorf("%s must be provided for %s cloud type", propertyKey, PrivateCloud)
}
env, err := az.EnvironmentFromName(val)
if err != nil {
return "", fmt.Errorf("invalid cloud environment %s", val)
}
return envPropertyProvider(env)
}
// Use public cloud suffix if `cloud` isn't specified
return envPropertyProvider(az.PublicCloud)
}
func ParseActiveDirectoryEndpoint(metadata map[string]string) (string, error) {
return ParseEnvironmentProperty(metadata, DefaultActiveDirectoryEndpointKey, activeDirectoryEndpointProvider)
}