github.com/darmach/terratest@v0.34.8-0.20210517103231-80931f95e3ff/modules/azure/publicaddress.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/gruntwork-io/terratest/modules/testing"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  // PublicAddressExists indicates whether the specified AzurePublic Address exists.
    12  // This function would fail the test if there is an error.
    13  func PublicAddressExists(t testing.TestingT, publicAddressName string, resGroupName string, subscriptionID string) bool {
    14  	exists, err := PublicAddressExistsE(publicAddressName, resGroupName, subscriptionID)
    15  	require.NoError(t, err)
    16  	return exists
    17  }
    18  
    19  // PublicAddressExistsE indicates whether the specified AzurePublic Address exists.
    20  func PublicAddressExistsE(publicAddressName string, resGroupName string, subscriptionID string) (bool, error) {
    21  	// Get the Public Address
    22  	_, err := GetPublicIPAddressE(publicAddressName, 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  // GetIPOfPublicIPAddressByName gets the Public IP of the Public IP Address specified.
    33  // This function would fail the test if there is an error.
    34  func GetIPOfPublicIPAddressByName(t testing.TestingT, publicAddressName string, resGroupName string, subscriptionID string) string {
    35  	IP, err := GetIPOfPublicIPAddressByNameE(publicAddressName, resGroupName, subscriptionID)
    36  	require.NoError(t, err)
    37  	return IP
    38  }
    39  
    40  // GetIPOfPublicIPAddressByNameE gets the Public IP of the Public IP Address specified.
    41  func GetIPOfPublicIPAddressByNameE(publicAddressName string, resGroupName string, subscriptionID string) (string, error) {
    42  	// Create a NIC client
    43  	pip, err := GetPublicIPAddressE(publicAddressName, resGroupName, subscriptionID)
    44  	if err != nil {
    45  		return "", err
    46  	}
    47  
    48  	return *pip.IPAddress, nil
    49  }
    50  
    51  // CheckPublicDNSNameAvailability checks whether a Domain Name in the cloudapp.azure.com zone
    52  // is available for use. This function would fail the test if there is an error.
    53  func CheckPublicDNSNameAvailability(t testing.TestingT, location string, domainNameLabel string, subscriptionID string) bool {
    54  	available, err := CheckPublicDNSNameAvailabilityE(location, domainNameLabel, subscriptionID)
    55  	if err != nil {
    56  		return false
    57  	}
    58  	return available
    59  }
    60  
    61  // CheckPublicDNSNameAvailabilityE checks whether a Domain Name in the cloudapp.azure.com zone is available for use.
    62  func CheckPublicDNSNameAvailabilityE(location string, domainNameLabel string, subscriptionID string) (bool, error) {
    63  	client, err := GetPublicIPAddressClientE(subscriptionID)
    64  	if err != nil {
    65  		return false, err
    66  	}
    67  
    68  	res, err := client.CheckDNSNameAvailability(context.Background(), location, domainNameLabel)
    69  	if err != nil {
    70  		return false, err
    71  	}
    72  
    73  	return *res.Available, nil
    74  }
    75  
    76  // GetPublicIPAddressE gets a Public IP Addresses in the specified Azure Resource Group.
    77  func GetPublicIPAddressE(publicIPAddressName string, resGroupName string, subscriptionID string) (*network.PublicIPAddress, error) {
    78  	// Validate resource group name and subscription ID
    79  	resGroupName, err := getTargetAzureResourceGroupName(resGroupName)
    80  	if err != nil {
    81  		return nil, err
    82  	}
    83  
    84  	// Get the client reference
    85  	client, err := GetPublicIPAddressClientE(subscriptionID)
    86  	if err != nil {
    87  		return nil, err
    88  	}
    89  
    90  	// Get the Public IP Address
    91  	pip, err := client.Get(context.Background(), resGroupName, publicIPAddressName, "")
    92  	if err != nil {
    93  		return nil, err
    94  	}
    95  	return &pip, nil
    96  }
    97  
    98  // GetPublicIPAddressClientE creates a Public IP Addresses client in the specified Azure Subscription.
    99  func GetPublicIPAddressClientE(subscriptionID string) (*network.PublicIPAddressesClient, error) {
   100  	// Get the Public IP Address client from clientfactory
   101  	client, err := CreatePublicIPAddressesClientE(subscriptionID)
   102  	if err != nil {
   103  		return nil, err
   104  	}
   105  
   106  	// Create an authorizer
   107  	authorizer, err := NewAuthorizer()
   108  	if err != nil {
   109  		return nil, err
   110  	}
   111  	client.Authorizer = *authorizer
   112  
   113  	return client, nil
   114  }