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

     1  package azure
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/Azure/azure-sdk-for-go/profiles/latest/frontdoor/mgmt/frontdoor"
     7  	"github.com/mponton/terratest/modules/testing"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  // FrontDoorExists indicates whether the Front Door exists for the subscription.
    12  // This function would fail the test if there is an error.
    13  func FrontDoorExists(t testing.TestingT, frontDoorName string, resourceGroupName string, subscriptionID string) bool {
    14  	exists, err := FrontDoorExistsE(frontDoorName, resourceGroupName, subscriptionID)
    15  	require.NoError(t, err)
    16  	return exists
    17  }
    18  
    19  // GetFrontDoor gets a Front Door by name if it exists for the subscription.
    20  // This function would fail the test if there is an error.
    21  func GetFrontDoor(t testing.TestingT, frontDoorName string, resourceGroupName string, subscriptionID string) *frontdoor.FrontDoor {
    22  	fd, err := GetFrontDoorE(frontDoorName, resourceGroupName, subscriptionID)
    23  	require.NoError(t, err)
    24  	return fd
    25  }
    26  
    27  // FrontDoorFrontendEndpointExists indicates whether the frontend endpoint exists for the provided Front Door.
    28  // This function would fail the test if there is an error.
    29  func FrontDoorFrontendEndpointExists(t testing.TestingT, endpointName string, frontDoorName string, resourceGroupName string, subscriptionID string) bool {
    30  	exists, err := FrontDoorFrontendEndpointExistsE(endpointName, frontDoorName, resourceGroupName, subscriptionID)
    31  	require.NoError(t, err)
    32  	return exists
    33  }
    34  
    35  // GetFrontDoorFrontendEndpoint gets a frontend endpoint by name for the provided Front Door if it exists for the subscription.
    36  // This function would fail the test if there is an error.
    37  func GetFrontDoorFrontendEndpoint(t testing.TestingT, endpointName string, frontDoorName string, resourceGroupName string, subscriptionID string) *frontdoor.FrontendEndpoint {
    38  	ep, err := GetFrontDoorFrontendEndpointE(endpointName, frontDoorName, resourceGroupName, subscriptionID)
    39  	require.NoError(t, err)
    40  	return ep
    41  }
    42  
    43  // FrontDoorExistsE indicates whether the specified Front Door exists and may return an error.
    44  func FrontDoorExistsE(frontDoorName string, resourceGroupName string, subscriptionID string) (bool, error) {
    45  	_, err := GetFrontDoorE(frontDoorName, resourceGroupName, subscriptionID)
    46  	if err != nil {
    47  		if ResourceNotFoundErrorExists(err) {
    48  			return false, nil
    49  		}
    50  		return false, err
    51  	}
    52  	return true, nil
    53  }
    54  
    55  // FrontDoorFrontendEndpointExistsE indicates whether the specified endpoint exists for the provided Front Door and may return an error.
    56  func FrontDoorFrontendEndpointExistsE(endpointName string, frontDoorName string, resourceGroupName string, subscriptionID string) (bool, error) {
    57  	_, err := GetFrontDoorFrontendEndpointE(endpointName, frontDoorName, resourceGroupName, subscriptionID)
    58  	if err != nil {
    59  		if ResourceNotFoundErrorExists(err) {
    60  			return false, nil
    61  		}
    62  		return false, err
    63  	}
    64  	return true, nil
    65  }
    66  
    67  // GetFrontDoorE gets the specified Front Door if it exists and may return an error.
    68  func GetFrontDoorE(frontDoorName, resoureGroupName, subscriptionID string) (*frontdoor.FrontDoor, error) {
    69  	client, err := GetFrontDoorClientE(subscriptionID)
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  
    74  	fd, err := client.Get(context.Background(), resoureGroupName, frontDoorName)
    75  	if err != nil {
    76  		return nil, err
    77  	}
    78  
    79  	return &fd, nil
    80  }
    81  
    82  // GetFrontDoorFrontendEndpointE gets the specified Frontend Endpoint for the provided Front Door if it exists and may return an error.
    83  func GetFrontDoorFrontendEndpointE(endpointName, frontDoorName, resourceGroupName, subscriptionID string) (*frontdoor.FrontendEndpoint, error) {
    84  	client, err := GetFrontDoorFrontendEndpointClientE(subscriptionID)
    85  	if err != nil {
    86  		return nil, err
    87  	}
    88  
    89  	endpoint, err := client.Get(context.Background(), resourceGroupName, frontDoorName, endpointName)
    90  	if err != nil {
    91  		return nil, err
    92  	}
    93  
    94  	return &endpoint, nil
    95  }
    96  
    97  // GetFrontDoorClientE return a front door client; otherwise error.
    98  func GetFrontDoorClientE(subscriptionID string) (*frontdoor.FrontDoorsClient, error) {
    99  	client, err := CreateFrontDoorClientE(subscriptionID)
   100  	if err != nil {
   101  		return nil, err
   102  	}
   103  
   104  	authorizer, err := NewAuthorizer()
   105  	if err != nil {
   106  		return nil, err
   107  	}
   108  
   109  	client.Authorizer = *authorizer
   110  	return client, nil
   111  }
   112  
   113  // GetFrontDoorFrontendEndpointClientE returns a front door frontend endpoints client; otherwise error.
   114  func GetFrontDoorFrontendEndpointClientE(subscriptionID string) (*frontdoor.FrontendEndpointsClient, error) {
   115  	client, err := CreateFrontDoorFrontendEndpointClientE(subscriptionID)
   116  	if err != nil {
   117  		return nil, err
   118  	}
   119  
   120  	authorizer, err := NewAuthorizer()
   121  	if err != nil {
   122  		return nil, err
   123  	}
   124  
   125  	client.Authorizer = *authorizer
   126  	return client, nil
   127  }