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

     1  package azure
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2019-06-01/storage"
     9  	"github.com/Azure/go-autorest/autorest/azure"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  // StorageAccountExists indicates whether the storage account name exactly matches; otherwise false.
    14  // This function would fail the test if there is an error.
    15  func StorageAccountExists(t *testing.T, storageAccountName string, resourceGroupName string, subscriptionID string) bool {
    16  	result, err := StorageAccountExistsE(storageAccountName, resourceGroupName, subscriptionID)
    17  	require.NoError(t, err)
    18  	return result
    19  }
    20  
    21  // StorageBlobContainerExists returns true if the container name exactly matches; otherwise false
    22  // This function would fail the test if there is an error.
    23  func StorageBlobContainerExists(t *testing.T, containerName string, storageAccountName string, resourceGroupName string, subscriptionID string) bool {
    24  	result, err := StorageBlobContainerExistsE(containerName, storageAccountName, resourceGroupName, subscriptionID)
    25  	require.NoError(t, err)
    26  	return result
    27  }
    28  
    29  // GetStorageBlobContainerPublicAccess indicates whether a storage container has public access; otherwise false.
    30  // This function would fail the test if there is an error.
    31  func GetStorageBlobContainerPublicAccess(t *testing.T, containerName string, storageAccountName string, resourceGroupName string, subscriptionID string) bool {
    32  	result, err := GetStorageBlobContainerPublicAccessE(containerName, storageAccountName, resourceGroupName, subscriptionID)
    33  	require.NoError(t, err)
    34  	return result
    35  }
    36  
    37  // GetStorageAccountKind returns one of Storage, StorageV2, BlobStorage, FileStorage, or BlockBlobStorage.
    38  // This function would fail the test if there is an error.
    39  func GetStorageAccountKind(t *testing.T, storageAccountName string, resourceGroupName string, subscriptionID string) string {
    40  	result, err := GetStorageAccountKindE(storageAccountName, resourceGroupName, subscriptionID)
    41  	require.NoError(t, err)
    42  	return result
    43  }
    44  
    45  // GetStorageAccountSkuTier returns the storage account sku tier as Standard or Premium.
    46  // This function would fail the test if there is an error.
    47  func GetStorageAccountSkuTier(t *testing.T, storageAccountName string, resourceGroupName string, subscriptionID string) string {
    48  	result, err := GetStorageAccountSkuTierE(storageAccountName, resourceGroupName, subscriptionID)
    49  	require.NoError(t, err)
    50  	return result
    51  }
    52  
    53  // GetStorageDNSString builds and returns the storage account dns string if the storage account exists.
    54  // This function would fail the test if there is an error.
    55  func GetStorageDNSString(t *testing.T, storageAccountName string, resourceGroupName string, subscriptionID string) string {
    56  	result, err := GetStorageDNSStringE(storageAccountName, resourceGroupName, subscriptionID)
    57  	require.NoError(t, err)
    58  	return result
    59  }
    60  
    61  // StorageAccountExistsE indicates whether the storage account name exists; otherwise false.
    62  func StorageAccountExistsE(storageAccountName, resourceGroupName, subscriptionID string) (bool, error) {
    63  	_, err := GetStorageAccountE(storageAccountName, resourceGroupName, subscriptionID)
    64  	if err != nil {
    65  		if ResourceNotFoundErrorExists(err) {
    66  			return false, nil
    67  		}
    68  		return false, err
    69  	}
    70  	return true, nil
    71  }
    72  
    73  // GetStorageAccountE gets a storage account; otherwise error.  See https://docs.microsoft.com/rest/api/storagerp/storageaccounts/getproperties for more information.
    74  func GetStorageAccountE(storageAccountName, resourceGroupName, subscriptionID string) (*storage.Account, error) {
    75  	subscriptionID, err := getTargetAzureSubscription(subscriptionID)
    76  	if err != nil {
    77  		return nil, err
    78  	}
    79  	resourceGroupName, err2 := getTargetAzureResourceGroupName((resourceGroupName))
    80  	if err2 != nil {
    81  		return nil, err2
    82  	}
    83  	storageAccount, err3 := GetStorageAccountPropertyE(storageAccountName, resourceGroupName, subscriptionID)
    84  	if err3 != nil {
    85  		return nil, err3
    86  	}
    87  	return storageAccount, nil
    88  }
    89  
    90  // StorageBlobContainerExistsE returns true if the container name exists; otherwise false.
    91  func StorageBlobContainerExistsE(containerName, storageAccountName, resourceGroupName, subscriptionID string) (bool, error) {
    92  	_, err := GetStorageBlobContainerE(containerName, storageAccountName, resourceGroupName, subscriptionID)
    93  	if err != nil {
    94  		if ResourceNotFoundErrorExists(err) {
    95  			return false, nil
    96  		}
    97  		return false, err
    98  	}
    99  	return true, nil
   100  }
   101  
   102  // GetStorageBlobContainerPublicAccessE indicates whether a storage container has public access; otherwise false.
   103  func GetStorageBlobContainerPublicAccessE(containerName, storageAccountName, resourceGroupName, subscriptionID string) (bool, error) {
   104  	container, err := GetStorageBlobContainerE(containerName, storageAccountName, resourceGroupName, subscriptionID)
   105  	if err != nil {
   106  		if ResourceNotFoundErrorExists(err) {
   107  			return false, nil
   108  		}
   109  		return false, err
   110  	}
   111  
   112  	return (string(container.PublicAccess) != "None"), nil
   113  }
   114  
   115  // GetStorageAccountKindE returns one of Storage, StorageV2, BlobStorage, FileStorage, or BlockBlobStorage.
   116  func GetStorageAccountKindE(storageAccountName, resourceGroupName, subscriptionID string) (string, error) {
   117  
   118  	storageAccount, err := GetStorageAccountPropertyE(storageAccountName, resourceGroupName, subscriptionID)
   119  	if err != nil {
   120  		return "", err
   121  	}
   122  	return string(storageAccount.Kind), nil
   123  }
   124  
   125  // GetStorageAccountSkuTierE returns the storage account sku tier as Standard or Premium.
   126  func GetStorageAccountSkuTierE(storageAccountName, resourceGroupName, subscriptionID string) (string, error) {
   127  	storageAccount, err := GetStorageAccountPropertyE(storageAccountName, resourceGroupName, subscriptionID)
   128  	if err != nil {
   129  		return "", err
   130  	}
   131  	return string(storageAccount.Sku.Tier), nil
   132  }
   133  
   134  // GetStorageBlobContainerE returns Blob container client.
   135  func GetStorageBlobContainerE(containerName, storageAccountName, resourceGroupName, subscriptionID string) (*storage.BlobContainer, error) {
   136  	subscriptionID, err := getTargetAzureSubscription(subscriptionID)
   137  	if err != nil {
   138  		return nil, err
   139  	}
   140  	resourceGroupName, err2 := getTargetAzureResourceGroupName((resourceGroupName))
   141  	if err2 != nil {
   142  		return nil, err2
   143  	}
   144  	client, err := CreateStorageBlobContainerClientE(subscriptionID)
   145  	if err != nil {
   146  		return nil, err
   147  	}
   148  	container, err := client.Get(context.Background(), resourceGroupName, storageAccountName, containerName)
   149  	if err != nil {
   150  		return nil, err
   151  	}
   152  	return &container, nil
   153  }
   154  
   155  // GetStorageAccountPropertyE returns StorageAccount properties.
   156  func GetStorageAccountPropertyE(storageAccountName, resourceGroupName, subscriptionID string) (*storage.Account, error) {
   157  	subscriptionID, err := getTargetAzureSubscription(subscriptionID)
   158  	if err != nil {
   159  		return nil, err
   160  	}
   161  	resourceGroupName, err2 := getTargetAzureResourceGroupName((resourceGroupName))
   162  	if err2 != nil {
   163  		return nil, err2
   164  	}
   165  	client, err := CreateStorageAccountClientE(subscriptionID)
   166  	if err != nil {
   167  		return nil, err
   168  	}
   169  	account, err := client.GetProperties(context.Background(), resourceGroupName, storageAccountName, "")
   170  	if err != nil {
   171  		return nil, err
   172  	}
   173  	return &account, nil
   174  }
   175  
   176  // GetStorageAccountClientE creates a storage account client.
   177  // TODO: remove in next version
   178  func GetStorageAccountClientE(subscriptionID string) (*storage.AccountsClient, error) {
   179  	// Validate Azure subscription ID
   180  	subscriptionID, err := getTargetAzureSubscription(subscriptionID)
   181  	if err != nil {
   182  		return nil, err
   183  	}
   184  
   185  	storageAccountClient := storage.NewAccountsClient(subscriptionID)
   186  	authorizer, err := NewAuthorizer()
   187  	if err != nil {
   188  		return nil, err
   189  	}
   190  	storageAccountClient.Authorizer = *authorizer
   191  	return &storageAccountClient, nil
   192  }
   193  
   194  // GetStorageBlobContainerClientE creates a storage container client.
   195  // TODO: remove in next version
   196  func GetStorageBlobContainerClientE(subscriptionID string) (*storage.BlobContainersClient, error) {
   197  	subscriptionID, err := getTargetAzureSubscription(subscriptionID)
   198  	if err != nil {
   199  		return nil, err
   200  	}
   201  
   202  	blobContainerClient := storage.NewBlobContainersClient(subscriptionID)
   203  	authorizer, err := NewAuthorizer()
   204  
   205  	if err != nil {
   206  		return nil, err
   207  	}
   208  	blobContainerClient.Authorizer = *authorizer
   209  	return &blobContainerClient, nil
   210  }
   211  
   212  // GetStorageURISuffixE returns the proper storage URI suffix for the configured Azure environment.
   213  func GetStorageURISuffixE() (string, error) {
   214  	envName := "AzurePublicCloud"
   215  	env, err := azure.EnvironmentFromName(envName)
   216  	if err != nil {
   217  		return "", err
   218  	}
   219  	return env.StorageEndpointSuffix, nil
   220  }
   221  
   222  // GetStorageAccountPrimaryBlobEndpointE gets the storage account blob endpoint as URI string.
   223  func GetStorageAccountPrimaryBlobEndpointE(storageAccountName, resourceGroupName, subscriptionID string) (string, error) {
   224  	storageAccount, err := GetStorageAccountPropertyE(storageAccountName, resourceGroupName, subscriptionID)
   225  	if err != nil {
   226  		return "", err
   227  	}
   228  
   229  	return *storageAccount.AccountProperties.PrimaryEndpoints.Blob, nil
   230  }
   231  
   232  // GetStorageDNSStringE builds and returns the storage account dns string if the storage account exists.
   233  func GetStorageDNSStringE(storageAccountName, resourceGroupName, subscriptionID string) (string, error) {
   234  	retval, err := StorageAccountExistsE(storageAccountName, resourceGroupName, subscriptionID)
   235  	if err != nil {
   236  		return "", err
   237  	}
   238  	if retval {
   239  		storageSuffix, err2 := GetStorageURISuffixE()
   240  		if err2 != nil {
   241  			return "", err2
   242  		}
   243  		return fmt.Sprintf("https://%s.blob.%s/", storageAccountName, storageSuffix), nil
   244  	}
   245  
   246  	return "", NewNotFoundError("storage account", storageAccountName, "")
   247  }