/
common.go
66 lines (53 loc) · 1.9 KB
/
common.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
package azure
import (
"os"
)
const (
// AzureSubscriptionID is an optional env variable supported by the `azurerm` Terraform provider to
// designate a target Azure subscription ID
AzureSubscriptionID = "ARM_SUBSCRIPTION_ID"
// AzureResGroupName is an optional env variable custom to Terratest to designate a target Azure resource group
AzureResGroupName = "AZURE_RES_GROUP_NAME"
)
// GetTargetAzureSubscription is a helper function to find the correct target Azure Subscription ID,
// with provided arguments taking precedence over environment variables
func GetTargetAzureSubscription(subscriptionID string) (string, error) {
return getTargetAzureSubscription(subscriptionID)
}
func getTargetAzureSubscription(subscriptionID string) (string, error) {
if subscriptionID == "" {
if id, exists := os.LookupEnv(AzureSubscriptionID); exists {
return id, nil
}
return "", SubscriptionIDNotFound{}
}
return subscriptionID, nil
}
// GetTargetAzureResourceGroupName is a helper function to find the correct target Azure Resource Group name,
// with provided arguments taking precedence over environment variables
func GetTargetAzureResourceGroupName(resourceGroupName string) (string, error) {
return getTargetAzureResourceGroupName(resourceGroupName)
}
func getTargetAzureResourceGroupName(resourceGroupName string) (string, error) {
if resourceGroupName == "" {
if name, exists := os.LookupEnv(AzureResGroupName); exists {
return name, nil
}
return "", ResourceGroupNameNotFound{}
}
return resourceGroupName, nil
}
// safePtrToString converts a string pointer to a non-pointer string value, or to "" if the pointer is nil.
func safePtrToString(raw *string) string {
if raw == nil {
return ""
}
return *raw
}
// safePtrToInt32 converts a int32 pointer to a non-pointer int32 value, or to 0 if the pointer is nil.
func safePtrToInt32(raw *int32) int32 {
if raw == nil {
return 0
}
return *raw
}