forked from gruntwork-io/terratest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
identity.go
81 lines (68 loc) · 2.56 KB
/
identity.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
package oci
import (
"context"
"fmt"
"os"
"github.com/oracle/oci-go-sdk/common"
"github.com/oracle/oci-go-sdk/identity"
"github.com/wahlfeld/terratest/modules/logger"
"github.com/wahlfeld/terratest/modules/random"
"github.com/wahlfeld/terratest/modules/testing"
)
// GetRandomAvailabilityDomain gets a randomly chosen availability domain for given compartment.
// The returned value can be overridden by of the environment variable TF_VAR_availability_domain.
func GetRandomAvailabilityDomain(t testing.TestingT, compartmentID string) string {
ad, err := GetRandomAvailabilityDomainE(t, compartmentID)
if err != nil {
t.Fatal(err)
}
return ad
}
// GetRandomAvailabilityDomainE gets a randomly chosen availability domain for given compartment.
// The returned value can be overridden by of the environment variable TF_VAR_availability_domain.
func GetRandomAvailabilityDomainE(t testing.TestingT, compartmentID string) (string, error) {
adFromEnvVar := os.Getenv(availabilityDomainEnvVar)
if adFromEnvVar != "" {
logger.Logf(t, "Using availability domain %s from environment variable %s", adFromEnvVar, availabilityDomainEnvVar)
return adFromEnvVar, nil
}
allADs, err := GetAllAvailabilityDomainsE(t, compartmentID)
if err != nil {
return "", err
}
ad := random.RandomString(allADs)
logger.Logf(t, "Using availability domain %s", ad)
return ad, nil
}
// GetAllAvailabilityDomains gets the list of availability domains available in the given compartment.
func GetAllAvailabilityDomains(t testing.TestingT, compartmentID string) []string {
ads, err := GetAllAvailabilityDomainsE(t, compartmentID)
if err != nil {
t.Fatal(err)
}
return ads
}
// GetAllAvailabilityDomainsE gets the list of availability domains available in the given compartment.
func GetAllAvailabilityDomainsE(t testing.TestingT, compartmentID string) ([]string, error) {
configProvider := common.DefaultConfigProvider()
client, err := identity.NewIdentityClientWithConfigurationProvider(configProvider)
if err != nil {
return nil, err
}
request := identity.ListAvailabilityDomainsRequest{CompartmentId: &compartmentID}
response, err := client.ListAvailabilityDomains(context.Background(), request)
if err != nil {
return nil, err
}
if len(response.Items) == 0 {
return nil, fmt.Errorf("No availability domains found in the %s compartment", compartmentID)
}
return availabilityDomainsNames(response.Items), nil
}
func availabilityDomainsNames(ads []identity.AvailabilityDomain) []string {
names := []string{}
for _, ad := range ads {
names = append(names, *ad.Name)
}
return names
}