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

     1  package azure
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-07-01/compute"
     7  	"github.com/gruntwork-io/terratest/modules/testing"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  // DiskExists indicates whether the specified Azure Managed Disk exists
    12  // This function would fail the test if there is an error.
    13  func DiskExists(t testing.TestingT, diskName string, resGroupName string, subscriptionID string) bool {
    14  	exists, err := DiskExistsE(diskName, resGroupName, subscriptionID)
    15  	require.NoError(t, err)
    16  	return exists
    17  }
    18  
    19  // DiskExistsE indicates whether the specified Azure Managed Disk exists in the specified Azure Resource Group
    20  func DiskExistsE(diskName string, resGroupName string, subscriptionID string) (bool, error) {
    21  	// Get the Disk object
    22  	_, err := GetDiskE(diskName, 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  // GetDisk returns a Disk in the specified Azure Resource Group
    33  // This function would fail the test if there is an error.
    34  func GetDisk(t testing.TestingT, diskName string, resGroupName string, subscriptionID string) *compute.Disk {
    35  	disk, err := GetDiskE(diskName, resGroupName, subscriptionID)
    36  	require.NoError(t, err)
    37  	return disk
    38  }
    39  
    40  // GetDiskE returns a Disk in the specified Azure Resource Group
    41  func GetDiskE(diskName string, resGroupName string, subscriptionID string) (*compute.Disk, error) {
    42  	// Validate resource group name and subscription ID
    43  	resGroupName, err := getTargetAzureResourceGroupName(resGroupName)
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  
    48  	// Get the client reference
    49  	client, err := CreateDisksClientE(subscriptionID)
    50  	if err != nil {
    51  		return nil, err
    52  	}
    53  
    54  	// Get the Disk
    55  	disk, err := client.Get(context.Background(), resGroupName, diskName)
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  
    60  	return &disk, nil
    61  }
    62  
    63  // GetDiskClientE returns a new Disk client in the specified Azure Subscription
    64  // TODO: remove in next major/minor version
    65  func GetDiskClientE(subscriptionID string) (*compute.DisksClient, error) {
    66  	// Validate Azure subscription ID
    67  	subscriptionID, err := getTargetAzureSubscription(subscriptionID)
    68  	if err != nil {
    69  		return nil, err
    70  	}
    71  
    72  	// Get the Disk client
    73  	client := compute.NewDisksClient(subscriptionID)
    74  
    75  	// Create an authorizer
    76  	authorizer, err := NewAuthorizer()
    77  	if err != nil {
    78  		return nil, err
    79  	}
    80  	client.Authorizer = *authorizer
    81  
    82  	return &client, nil
    83  }