github.com/mponton/terratest@v0.44.0/modules/azure/networkinterface.go (about)

     1  package azure
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2019-09-01/network"
     7  	"github.com/mponton/terratest/modules/testing"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  // NetworkInterfaceExists indicates whether the specified Azure Network Interface exists.
    12  // This function would fail the test if there is an error.
    13  func NetworkInterfaceExists(t testing.TestingT, nicName string, resGroupName string, subscriptionID string) bool {
    14  	exists, err := NetworkInterfaceExistsE(nicName, resGroupName, subscriptionID)
    15  	require.NoError(t, err)
    16  	return exists
    17  }
    18  
    19  // NetworkInterfaceExistsE indicates whether the specified Azure Network Interface exists.
    20  func NetworkInterfaceExistsE(nicName string, resGroupName string, subscriptionID string) (bool, error) {
    21  	// Get the Network Interface
    22  	_, err := GetNetworkInterfaceE(nicName, 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  // GetNetworkInterfacePrivateIPs gets a list of the Private IPs of a Network Interface configs.
    33  // This function would fail the test if there is an error.
    34  func GetNetworkInterfacePrivateIPs(t testing.TestingT, nicName string, resGroupName string, subscriptionID string) []string {
    35  	IPs, err := GetNetworkInterfacePrivateIPsE(nicName, resGroupName, subscriptionID)
    36  	require.NoError(t, err)
    37  
    38  	return IPs
    39  }
    40  
    41  // GetNetworkInterfacePrivateIPsE gets a list of the Private IPs of a Network Interface configs.
    42  func GetNetworkInterfacePrivateIPsE(nicName string, resGroupName string, subscriptionID string) ([]string, error) {
    43  	var privateIPs []string
    44  
    45  	// Get the Network Interface client
    46  	nic, err := GetNetworkInterfaceE(nicName, resGroupName, subscriptionID)
    47  	if err != nil {
    48  		return privateIPs, err
    49  	}
    50  
    51  	// Get the Private IPs from each configuration
    52  	for _, IPConfiguration := range *nic.IPConfigurations {
    53  		privateIPs = append(privateIPs, *IPConfiguration.PrivateIPAddress)
    54  	}
    55  
    56  	return privateIPs, nil
    57  }
    58  
    59  // GetNetworkInterfacePublicIPs returns a list of all the Public IPs found in the Network Interface configurations.
    60  // This function would fail the test if there is an error.
    61  func GetNetworkInterfacePublicIPs(t testing.TestingT, nicName string, resGroupName string, subscriptionID string) []string {
    62  	IPs, err := GetNetworkInterfacePublicIPsE(nicName, resGroupName, subscriptionID)
    63  	require.NoError(t, err)
    64  	return IPs
    65  }
    66  
    67  // GetNetworkInterfacePublicIPsE returns a list of all the Public IPs found in the Network Interface configurations.
    68  func GetNetworkInterfacePublicIPsE(nicName string, resGroupName string, subscriptionID string) ([]string, error) {
    69  	var publicIPs []string
    70  
    71  	// Get the Network Interface client
    72  	nic, err := GetNetworkInterfaceE(nicName, resGroupName, subscriptionID)
    73  	if err != nil {
    74  		return publicIPs, err
    75  	}
    76  
    77  	// Get the Public IPs from each configuration available
    78  	for _, IPConfiguration := range *nic.IPConfigurations {
    79  		// Iterate each config, for successful configurations check for a Public Address reference.
    80  		// Not failing on errors as this is an optimistic accumulator.
    81  		nicConfig, err := GetNetworkInterfaceConfigurationE(nicName, *IPConfiguration.Name, resGroupName, subscriptionID)
    82  		if err == nil {
    83  			if nicConfig.PublicIPAddress != nil {
    84  				publicAddressID := GetNameFromResourceID(*nicConfig.PublicIPAddress.ID)
    85  				publicIP, err := GetIPOfPublicIPAddressByNameE(publicAddressID, resGroupName, subscriptionID)
    86  				if err == nil {
    87  					publicIPs = append(publicIPs, publicIP)
    88  				}
    89  			}
    90  		}
    91  	}
    92  
    93  	return publicIPs, nil
    94  }
    95  
    96  // GetNetworkInterfaceConfigurationE gets a Network Interface Configuration in the specified Azure Resource Group.
    97  func GetNetworkInterfaceConfigurationE(nicName string, nicConfigName string, resGroupName string, subscriptionID string) (*network.InterfaceIPConfiguration, error) {
    98  	// Validate Azure Resource Group
    99  	resGroupName, err := getTargetAzureResourceGroupName(resGroupName)
   100  	if err != nil {
   101  		return nil, err
   102  	}
   103  
   104  	// Get the client reference
   105  	client, err := GetNetworkInterfaceConfigurationClientE(subscriptionID)
   106  	if err != nil {
   107  		return nil, err
   108  	}
   109  
   110  	// Get the Network Interface
   111  	nicConfig, err := client.Get(context.Background(), resGroupName, nicName, nicConfigName)
   112  	if err != nil {
   113  		return nil, err
   114  	}
   115  
   116  	return &nicConfig, nil
   117  }
   118  
   119  // GetNetworkInterfaceConfigurationClientE creates a new Network Interface Configuration client in the specified Azure Subscription.
   120  func GetNetworkInterfaceConfigurationClientE(subscriptionID string) (*network.InterfaceIPConfigurationsClient, error) {
   121  	// Create a new client from client factory
   122  	client, err := CreateNewNetworkInterfaceIPConfigurationClientE(subscriptionID)
   123  	if err != nil {
   124  		return nil, err
   125  	}
   126  
   127  	// Create an authorizer
   128  	authorizer, err := NewAuthorizer()
   129  	if err != nil {
   130  		return nil, err
   131  	}
   132  	client.Authorizer = *authorizer
   133  
   134  	return client, nil
   135  }
   136  
   137  // GetNetworkInterfaceE gets a Network Interface in the specified Azure Resource Group.
   138  func GetNetworkInterfaceE(nicName string, resGroupName string, subscriptionID string) (*network.Interface, error) {
   139  	// Validate Azure Resource Group
   140  	resGroupName, err := getTargetAzureResourceGroupName(resGroupName)
   141  	if err != nil {
   142  		return nil, err
   143  	}
   144  
   145  	// Get the client reference
   146  	client, err := GetNetworkInterfaceClientE(subscriptionID)
   147  	if err != nil {
   148  		return nil, err
   149  	}
   150  
   151  	// Get the Network Interface
   152  	nic, err := client.Get(context.Background(), resGroupName, nicName, "")
   153  	if err != nil {
   154  		return nil, err
   155  	}
   156  
   157  	return &nic, nil
   158  }
   159  
   160  // GetNetworkInterfaceClientE creates a new Network Interface client in the specified Azure Subscription.
   161  func GetNetworkInterfaceClientE(subscriptionID string) (*network.InterfacesClient, error) {
   162  	// Create new NIC client from client factory
   163  	client, err := CreateNewNetworkInterfacesClientE(subscriptionID)
   164  	if err != nil {
   165  		return nil, err
   166  	}
   167  
   168  	// Create an authorizer
   169  	authorizer, err := NewAuthorizer()
   170  	if err != nil {
   171  		return nil, err
   172  	}
   173  	client.Authorizer = *authorizer
   174  
   175  	return client, nil
   176  }