github.com/darmach/terratest@v0.34.8-0.20210517103231-80931f95e3ff/modules/azure/actiongroup.go (about)

     1  package azure
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/Azure/azure-sdk-for-go/profiles/preview/preview/monitor/mgmt/insights"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  // GetActionGroupResource gets the ActionGroupResource.
    12  // ruleName - required to find the ActionGroupResource.
    13  // resGroupName - use an empty string if you have the AZURE_RES_GROUP_NAME environment variable set
    14  // subscriptionId - use an empty string if you have the ARM_SUBSCRIPTION_ID environment variable set
    15  func GetActionGroupResource(t *testing.T, ruleName string, resGroupName string, subscriptionID string) *insights.ActionGroupResource {
    16  	actionGroupResource, err := GetActionGroupResourceE(ruleName, resGroupName, subscriptionID)
    17  	require.NoError(t, err)
    18  
    19  	return actionGroupResource
    20  }
    21  
    22  // GetActionGroupResourceE gets the ActionGroupResource with Error details on error.
    23  // ruleName - required to find the ActionGroupResource.
    24  // resGroupName - use an empty string if you have the AZURE_RES_GROUP_NAME environment variable set
    25  // subscriptionId - use an empty string if you have the ARM_SUBSCRIPTION_ID environment variable set
    26  func GetActionGroupResourceE(ruleName string, resGroupName string, subscriptionID string) (*insights.ActionGroupResource, error) {
    27  	rgName, err := getTargetAzureResourceGroupName(resGroupName)
    28  	if err != nil {
    29  		return nil, err
    30  	}
    31  
    32  	client, err := CreateActionGroupClient(subscriptionID)
    33  	if err != nil {
    34  		return nil, err
    35  	}
    36  
    37  	actionGroup, err := client.Get(context.Background(), rgName, ruleName)
    38  	if err != nil {
    39  		return nil, err
    40  	}
    41  
    42  	return &actionGroup, nil
    43  }
    44  
    45  // TODO: remove in next version
    46  func getActionGroupClient(subscriptionID string) (*insights.ActionGroupsClient, error) {
    47  	subID, err := getTargetAzureSubscription(subscriptionID)
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  
    52  	metricAlertsClient := insights.NewActionGroupsClient(subID)
    53  
    54  	authorizer, err := NewAuthorizer()
    55  	if err != nil {
    56  		return nil, err
    57  	}
    58  
    59  	metricAlertsClient.Authorizer = *authorizer
    60  
    61  	return &metricAlertsClient, nil
    62  }