github.com/mponton/terratest@v0.44.0/modules/oci/network.go (about) 1 package oci 2 3 import ( 4 "context" 5 "fmt" 6 7 "github.com/mponton/terratest/modules/logger" 8 "github.com/mponton/terratest/modules/random" 9 "github.com/mponton/terratest/modules/testing" 10 "github.com/oracle/oci-go-sdk/common" 11 "github.com/oracle/oci-go-sdk/core" 12 ) 13 14 // GetRandomSubnetID gets a randomly chosen subnet OCID in the given availability domain. 15 // The returned value can be overridden by of the environment variable TF_VAR_subnet_ocid. 16 func GetRandomSubnetID(t testing.TestingT, compartmentID string, availabilityDomain string) string { 17 ocid, err := GetRandomSubnetIDE(t, compartmentID, availabilityDomain) 18 if err != nil { 19 t.Fatal(err) 20 } 21 return ocid 22 } 23 24 // GetRandomSubnetIDE gets a randomly chosen subnet OCID in the given availability domain. 25 // The returned value can be overridden by of the environment variable TF_VAR_subnet_ocid. 26 func GetRandomSubnetIDE(t testing.TestingT, compartmentID string, availabilityDomain string) (string, error) { 27 configProvider := common.DefaultConfigProvider() 28 client, err := core.NewVirtualNetworkClientWithConfigurationProvider(configProvider) 29 if err != nil { 30 return "", err 31 } 32 33 vcnIDs, err := GetAllVcnIDsE(t, compartmentID) 34 if err != nil { 35 return "", err 36 } 37 38 allSubnetIDs := map[string][]string{} 39 for _, vcnID := range vcnIDs { 40 request := core.ListSubnetsRequest{ 41 CompartmentId: &compartmentID, 42 VcnId: &vcnID, 43 } 44 response, err := client.ListSubnets(context.Background(), request) 45 if err != nil { 46 return "", err 47 } 48 49 mapSubnetsByAvailabilityDomain(allSubnetIDs, response.Items) 50 } 51 52 subnetID := random.RandomString(allSubnetIDs[availabilityDomain]) 53 54 logger.Logf(t, "Using subnet with OCID %s", subnetID) 55 return subnetID, nil 56 } 57 58 // GetAllVcnIDs gets the list of VCNs available in the given compartment. 59 func GetAllVcnIDs(t testing.TestingT, compartmentID string) []string { 60 vcnIDS, err := GetAllVcnIDsE(t, compartmentID) 61 if err != nil { 62 t.Fatal(err) 63 } 64 return vcnIDS 65 } 66 67 // GetAllVcnIDsE gets the list of VCNs available in the given compartment. 68 func GetAllVcnIDsE(t testing.TestingT, compartmentID string) ([]string, error) { 69 configProvider := common.DefaultConfigProvider() 70 client, err := core.NewVirtualNetworkClientWithConfigurationProvider(configProvider) 71 if err != nil { 72 return nil, err 73 } 74 75 request := core.ListVcnsRequest{CompartmentId: &compartmentID} 76 response, err := client.ListVcns(context.Background(), request) 77 if err != nil { 78 return nil, err 79 } 80 81 if len(response.Items) == 0 { 82 return nil, fmt.Errorf("No VCNs found in the %s compartment", compartmentID) 83 } 84 85 return vcnsIDs(response.Items), nil 86 } 87 88 func mapSubnetsByAvailabilityDomain(allSubnets map[string][]string, subnets []core.Subnet) map[string][]string { 89 for _, subnet := range subnets { 90 allSubnets[*subnet.AvailabilityDomain] = append(allSubnets[*subnet.AvailabilityDomain], *subnet.Id) 91 } 92 return allSubnets 93 } 94 95 func vcnsIDs(vcns []core.Vcn) []string { 96 ids := []string{} 97 for _, vcn := range vcns { 98 ids = append(ids, *vcn.Id) 99 } 100 return ids 101 }