github.com/darmach/terratest@v0.34.8-0.20210517103231-80931f95e3ff/modules/azure/availabilityset.go (about) 1 package azure 2 3 import ( 4 "context" 5 "strings" 6 7 "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-07-01/compute" 8 "github.com/gruntwork-io/terratest/modules/testing" 9 "github.com/stretchr/testify/require" 10 ) 11 12 // AvailabilitySetExists indicates whether the specified Azure Availability Set exists. 13 // This function would fail the test if there is an error. 14 func AvailabilitySetExists(t testing.TestingT, avsName string, resGroupName string, subscriptionID string) bool { 15 exists, err := AvailabilitySetExistsE(t, avsName, resGroupName, subscriptionID) 16 require.NoError(t, err) 17 return exists 18 } 19 20 // AvailabilitySetExistsE indicates whether the specified Azure Availability Set exists 21 func AvailabilitySetExistsE(t testing.TestingT, avsName string, resGroupName string, subscriptionID string) (bool, error) { 22 _, err := GetAvailabilitySetE(t, avsName, resGroupName, subscriptionID) 23 if err != nil { 24 if ResourceNotFoundErrorExists(err) { 25 return false, nil 26 } 27 return false, err 28 } 29 return true, nil 30 } 31 32 // CheckAvailabilitySetContainsVM checks if the Virtual Machine is contained in the Availability Set VMs. 33 // This function would fail the test if there is an error. 34 func CheckAvailabilitySetContainsVM(t testing.TestingT, vmName string, avsName string, resGroupName string, subscriptionID string) bool { 35 success, err := CheckAvailabilitySetContainsVME(t, vmName, avsName, resGroupName, subscriptionID) 36 require.NoError(t, err) 37 return success 38 } 39 40 // CheckAvailabilitySetContainsVME checks if the Virtual Machine is contained in the Availability Set VMs 41 func CheckAvailabilitySetContainsVME(t testing.TestingT, vmName string, avsName string, resGroupName string, subscriptionID string) (bool, error) { 42 client, err := CreateAvailabilitySetClientE(subscriptionID) 43 if err != nil { 44 return false, err 45 } 46 47 // Get the Availability Set 48 avs, err := client.Get(context.Background(), resGroupName, avsName) 49 if err != nil { 50 return false, err 51 } 52 53 // Check if the VM is found in the AVS VM collection and return true 54 for _, vm := range *avs.VirtualMachines { 55 // VM IDs are always ALL CAPS in this property so ignoring case 56 if strings.EqualFold(vmName, GetNameFromResourceID(*vm.ID)) { 57 return true, nil 58 } 59 } 60 61 return false, NewNotFoundError("Virtual Machine", vmName, avsName) 62 } 63 64 // GetAvailabilitySetVMNamesInCaps gets a list of VM names in the specified Azure Availability Set. 65 // This function would fail the test if there is an error. 66 func GetAvailabilitySetVMNamesInCaps(t testing.TestingT, avsName string, resGroupName string, subscriptionID string) []string { 67 vms, err := GetAvailabilitySetVMNamesInCapsE(t, avsName, resGroupName, subscriptionID) 68 require.NoError(t, err) 69 return vms 70 } 71 72 // GetAvailabilitySetVMNamesInCapsE gets a list of VM names in the specified Azure Availability Set 73 func GetAvailabilitySetVMNamesInCapsE(t testing.TestingT, avsName string, resGroupName string, subscriptionID string) ([]string, error) { 74 client, err := CreateAvailabilitySetClientE(subscriptionID) 75 if err != nil { 76 return nil, err 77 } 78 79 avs, err := client.Get(context.Background(), resGroupName, avsName) 80 if err != nil { 81 return nil, err 82 } 83 84 vms := []string{} 85 86 // Get the names for all VMs in the Availability Set 87 for _, vm := range *avs.VirtualMachines { 88 // IDs are returned in ALL CAPS for this property 89 if vmName := GetNameFromResourceID(*vm.ID); len(vmName) > 0 { 90 vms = append(vms, vmName) 91 } 92 } 93 94 return vms, nil 95 } 96 97 // GetAvailabilitySetFaultDomainCount gets the Fault Domain Count for the specified Azure Availability Set. 98 // This function would fail the test if there is an error. 99 func GetAvailabilitySetFaultDomainCount(t testing.TestingT, avsName string, resGroupName string, subscriptionID string) int32 { 100 avsFaultDomainCount, err := GetAvailabilitySetFaultDomainCountE(t, avsName, resGroupName, subscriptionID) 101 require.NoError(t, err) 102 return avsFaultDomainCount 103 } 104 105 // GetAvailabilitySetFaultDomainCountE gets the Fault Domain Count for the specified Azure Availability Set 106 func GetAvailabilitySetFaultDomainCountE(t testing.TestingT, avsName string, resGroupName string, subscriptionID string) (int32, error) { 107 avs, err := GetAvailabilitySetE(t, avsName, resGroupName, subscriptionID) 108 if err != nil { 109 return -1, err 110 } 111 return *avs.PlatformFaultDomainCount, nil 112 } 113 114 // GetAvailabilitySetE gets an Availability Set in the specified Azure Resource Group 115 func GetAvailabilitySetE(t testing.TestingT, avsName string, resGroupName string, subscriptionID string) (*compute.AvailabilitySet, error) { 116 // Validate resource group name and subscription ID 117 resGroupName, err := getTargetAzureResourceGroupName(resGroupName) 118 if err != nil { 119 return nil, err 120 } 121 122 // Get the client reference 123 client, err := CreateAvailabilitySetClientE(subscriptionID) 124 if err != nil { 125 return nil, err 126 } 127 128 // Get the Availability Set 129 avs, err := client.Get(context.Background(), resGroupName, avsName) 130 if err != nil { 131 return nil, err 132 } 133 134 return &avs, nil 135 } 136 137 // GetAvailabilitySetClientE gets a new Availability Set client in the specified Azure Subscription 138 // TODO: remove in next version 139 func GetAvailabilitySetClientE(subscriptionID string) (*compute.AvailabilitySetsClient, error) { 140 // Validate Azure subscription ID 141 subscriptionID, err := getTargetAzureSubscription(subscriptionID) 142 if err != nil { 143 return nil, err 144 } 145 146 // Get the Availability Set client 147 client := compute.NewAvailabilitySetsClient(subscriptionID) 148 149 // Create an authorizer 150 authorizer, err := NewAuthorizer() 151 if err != nil { 152 return nil, err 153 } 154 client.Authorizer = *authorizer 155 156 return &client, nil 157 }