github.com/mponton/terratest@v0.44.0/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 // StorageFileShareExists returns true if the file share name exactly matches; otherwise false 30 // This function would fail the test if there is an error. 31 func StorageFileShareExists(t *testing.T, fileSahreName string, storageAccountName string, resourceGroupName string, subscriptionID string) bool { 32 result, err := StorageFileShareExistsE(t, fileSahreName, storageAccountName, resourceGroupName, subscriptionID) 33 require.NoError(t, err) 34 35 return result 36 } 37 38 // StorageFileShareExists returns true if the file share name exactly matches; otherwise false 39 func StorageFileShareExistsE(t *testing.T, fileSahreName string, storageAccountName string, resourceGroupName string, subscriptionID string) (bool, error) { 40 _, err := GetStorageFileShareE(fileSahreName, storageAccountName, resourceGroupName, subscriptionID) 41 if err != nil { 42 if ResourceNotFoundErrorExists(err) { 43 return false, nil 44 } 45 return false, err 46 } 47 return true, nil 48 } 49 50 // GetStorageBlobContainerPublicAccess indicates whether a storage container has public access; otherwise false. 51 // This function would fail the test if there is an error. 52 func GetStorageBlobContainerPublicAccess(t *testing.T, containerName string, storageAccountName string, resourceGroupName string, subscriptionID string) bool { 53 result, err := GetStorageBlobContainerPublicAccessE(containerName, storageAccountName, resourceGroupName, subscriptionID) 54 require.NoError(t, err) 55 return result 56 } 57 58 // GetStorageAccountKind returns one of Storage, StorageV2, BlobStorage, FileStorage, or BlockBlobStorage. 59 // This function would fail the test if there is an error. 60 func GetStorageAccountKind(t *testing.T, storageAccountName string, resourceGroupName string, subscriptionID string) string { 61 result, err := GetStorageAccountKindE(storageAccountName, resourceGroupName, subscriptionID) 62 require.NoError(t, err) 63 return result 64 } 65 66 // GetStorageAccountSkuTier returns the storage account sku tier as Standard or Premium. 67 // This function would fail the test if there is an error. 68 func GetStorageAccountSkuTier(t *testing.T, storageAccountName string, resourceGroupName string, subscriptionID string) string { 69 result, err := GetStorageAccountSkuTierE(storageAccountName, resourceGroupName, subscriptionID) 70 require.NoError(t, err) 71 return result 72 } 73 74 // GetStorageDNSString builds and returns the storage account dns string if the storage account exists. 75 // This function would fail the test if there is an error. 76 func GetStorageDNSString(t *testing.T, storageAccountName string, resourceGroupName string, subscriptionID string) string { 77 result, err := GetStorageDNSStringE(storageAccountName, resourceGroupName, subscriptionID) 78 require.NoError(t, err) 79 return result 80 } 81 82 // StorageAccountExistsE indicates whether the storage account name exists; otherwise false. 83 func StorageAccountExistsE(storageAccountName, resourceGroupName, subscriptionID string) (bool, error) { 84 _, err := GetStorageAccountE(storageAccountName, resourceGroupName, subscriptionID) 85 if err != nil { 86 if ResourceNotFoundErrorExists(err) { 87 return false, nil 88 } 89 return false, err 90 } 91 return true, nil 92 } 93 94 // GetStorageAccountE gets a storage account; otherwise error. See https://docs.microsoft.com/rest/api/storagerp/storageaccounts/getproperties for more information. 95 func GetStorageAccountE(storageAccountName, resourceGroupName, subscriptionID string) (*storage.Account, error) { 96 subscriptionID, err := getTargetAzureSubscription(subscriptionID) 97 if err != nil { 98 return nil, err 99 } 100 resourceGroupName, err2 := getTargetAzureResourceGroupName((resourceGroupName)) 101 if err2 != nil { 102 return nil, err2 103 } 104 storageAccount, err3 := GetStorageAccountPropertyE(storageAccountName, resourceGroupName, subscriptionID) 105 if err3 != nil { 106 return nil, err3 107 } 108 return storageAccount, nil 109 } 110 111 // StorageBlobContainerExistsE returns true if the container name exists; otherwise false. 112 func StorageBlobContainerExistsE(containerName, storageAccountName, resourceGroupName, subscriptionID string) (bool, error) { 113 _, err := GetStorageBlobContainerE(containerName, storageAccountName, resourceGroupName, subscriptionID) 114 if err != nil { 115 if ResourceNotFoundErrorExists(err) { 116 return false, nil 117 } 118 return false, err 119 } 120 return true, nil 121 } 122 123 // GetStorageBlobContainerPublicAccessE indicates whether a storage container has public access; otherwise false. 124 func GetStorageBlobContainerPublicAccessE(containerName, storageAccountName, resourceGroupName, subscriptionID string) (bool, error) { 125 container, err := GetStorageBlobContainerE(containerName, storageAccountName, resourceGroupName, subscriptionID) 126 if err != nil { 127 if ResourceNotFoundErrorExists(err) { 128 return false, nil 129 } 130 return false, err 131 } 132 133 return (string(container.PublicAccess) != "None"), nil 134 } 135 136 // GetStorageAccountKindE returns one of Storage, StorageV2, BlobStorage, FileStorage, or BlockBlobStorage. 137 func GetStorageAccountKindE(storageAccountName, resourceGroupName, subscriptionID string) (string, error) { 138 139 storageAccount, err := GetStorageAccountPropertyE(storageAccountName, resourceGroupName, subscriptionID) 140 if err != nil { 141 return "", err 142 } 143 return string(storageAccount.Kind), nil 144 } 145 146 // GetStorageAccountSkuTierE returns the storage account sku tier as Standard or Premium. 147 func GetStorageAccountSkuTierE(storageAccountName, resourceGroupName, subscriptionID string) (string, error) { 148 storageAccount, err := GetStorageAccountPropertyE(storageAccountName, resourceGroupName, subscriptionID) 149 if err != nil { 150 return "", err 151 } 152 return string(storageAccount.Sku.Tier), nil 153 } 154 155 // GetStorageBlobContainerE returns Blob container client. 156 func GetStorageBlobContainerE(containerName, storageAccountName, resourceGroupName, subscriptionID string) (*storage.BlobContainer, 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 := CreateStorageBlobContainerClientE(subscriptionID) 166 if err != nil { 167 return nil, err 168 } 169 container, err := client.Get(context.Background(), resourceGroupName, storageAccountName, containerName) 170 if err != nil { 171 return nil, err 172 } 173 return &container, nil 174 } 175 176 // GetStorageAccountPropertyE returns StorageAccount properties. 177 func GetStorageAccountPropertyE(storageAccountName, resourceGroupName, subscriptionID string) (*storage.Account, error) { 178 subscriptionID, err := getTargetAzureSubscription(subscriptionID) 179 if err != nil { 180 return nil, err 181 } 182 resourceGroupName, err2 := getTargetAzureResourceGroupName((resourceGroupName)) 183 if err2 != nil { 184 return nil, err2 185 } 186 client, err := CreateStorageAccountClientE(subscriptionID) 187 if err != nil { 188 return nil, err 189 } 190 account, err := client.GetProperties(context.Background(), resourceGroupName, storageAccountName, "") 191 if err != nil { 192 return nil, err 193 } 194 return &account, nil 195 } 196 197 // GetStorageFileShare returns specified file share. This function would fail the test if there is an error. 198 func GetStorageFileShare(t *testing.T, fileShareName, storageAccountName, resourceGroupName, subscriptionID string) *storage.FileShare { 199 fileSahre, err := GetStorageFileShareE(fileShareName, storageAccountName, resourceGroupName, subscriptionID) 200 require.NoError(t, err) 201 202 return fileSahre 203 } 204 205 // GetStorageFileSharesE returns specified file share. 206 func GetStorageFileShareE(fileShareName, storageAccountName, resourceGroupName, subscriptionID string) (*storage.FileShare, error) { 207 resourceGroupName, err2 := getTargetAzureResourceGroupName(resourceGroupName) 208 if err2 != nil { 209 return nil, err2 210 } 211 client, err := CreateStorageFileSharesClientE(subscriptionID) 212 if err != nil { 213 return nil, err 214 } 215 fileShare, err := client.Get(context.Background(), resourceGroupName, storageAccountName, fileShareName, "stats") 216 if err != nil { 217 return nil, err 218 } 219 return &fileShare, nil 220 } 221 222 // GetStorageAccountClientE creates a storage account client. 223 // TODO: remove in next version 224 func GetStorageAccountClientE(subscriptionID string) (*storage.AccountsClient, error) { 225 // Validate Azure subscription ID 226 subscriptionID, err := getTargetAzureSubscription(subscriptionID) 227 if err != nil { 228 return nil, err 229 } 230 231 storageAccountClient := storage.NewAccountsClient(subscriptionID) 232 authorizer, err := NewAuthorizer() 233 if err != nil { 234 return nil, err 235 } 236 storageAccountClient.Authorizer = *authorizer 237 return &storageAccountClient, nil 238 } 239 240 // GetStorageBlobContainerClientE creates a storage container client. 241 // TODO: remove in next version 242 func GetStorageBlobContainerClientE(subscriptionID string) (*storage.BlobContainersClient, error) { 243 subscriptionID, err := getTargetAzureSubscription(subscriptionID) 244 if err != nil { 245 return nil, err 246 } 247 248 blobContainerClient := storage.NewBlobContainersClient(subscriptionID) 249 authorizer, err := NewAuthorizer() 250 251 if err != nil { 252 return nil, err 253 } 254 blobContainerClient.Authorizer = *authorizer 255 return &blobContainerClient, nil 256 } 257 258 // GetStorageURISuffixE returns the proper storage URI suffix for the configured Azure environment. 259 func GetStorageURISuffixE() (string, error) { 260 envName := "AzurePublicCloud" 261 env, err := azure.EnvironmentFromName(envName) 262 if err != nil { 263 return "", err 264 } 265 return env.StorageEndpointSuffix, nil 266 } 267 268 // GetStorageAccountPrimaryBlobEndpointE gets the storage account blob endpoint as URI string. 269 func GetStorageAccountPrimaryBlobEndpointE(storageAccountName, resourceGroupName, subscriptionID string) (string, error) { 270 storageAccount, err := GetStorageAccountPropertyE(storageAccountName, resourceGroupName, subscriptionID) 271 if err != nil { 272 return "", err 273 } 274 275 return *storageAccount.AccountProperties.PrimaryEndpoints.Blob, nil 276 } 277 278 // GetStorageDNSStringE builds and returns the storage account dns string if the storage account exists. 279 func GetStorageDNSStringE(storageAccountName, resourceGroupName, subscriptionID string) (string, error) { 280 retval, err := StorageAccountExistsE(storageAccountName, resourceGroupName, subscriptionID) 281 if err != nil { 282 return "", err 283 } 284 if retval { 285 storageSuffix, err2 := GetStorageURISuffixE() 286 if err2 != nil { 287 return "", err2 288 } 289 return fmt.Sprintf("https://%s.blob.%s/", storageAccountName, storageSuffix), nil 290 } 291 292 return "", NewNotFoundError("storage account", storageAccountName, "") 293 }