Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a cache to store storage account keys. #634

Merged
merged 5 commits into from
Jan 5, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 45 additions & 12 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"net/http/httputil"
"os"
"sync"

"github.com/Azure/azure-sdk-for-go/arm/appinsights"
"github.com/Azure/azure-sdk-for-go/arm/authorization"
Expand Down Expand Up @@ -45,6 +46,7 @@ import (
"github.com/Azure/go-autorest/autorest/azure"
"github.com/hashicorp/terraform/terraform"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/authentication"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

// ArmClient contains the handles to all the specific Azure Resource Manager
Expand Down Expand Up @@ -838,23 +840,54 @@ func (c *ArmClient) registerResourcesClients(endpoint, subscriptionId string, au
c.managementLocksClient = locksClient
}

var (
storageKeyCacheMu sync.RWMutex
storageKeyCache = make(map[string]string)
)

func (armClient *ArmClient) getKeyForStorageAccount(resourceGroupName, storageAccountName string) (string, bool, error) {
accountKeys, err := armClient.storageServiceClient.ListKeys(resourceGroupName, storageAccountName)
if accountKeys.StatusCode == http.StatusNotFound {
return "", false, nil
}
if err != nil {
// We assume this is a transient error rather than a 404 (which is caught above), so assume the
// account still exists.
return "", true, fmt.Errorf("Error retrieving keys for storage account %q: %s", storageAccountName, err)
cacheIndex := resourceGroupName + "/" + storageAccountName
storageKeyCacheMu.RLock()
key, ok := storageKeyCache[cacheIndex]
storageKeyCacheMu.RUnlock()

if ok {
return key, true, nil
}

if accountKeys.Keys == nil {
return "", false, fmt.Errorf("Nil key returned for storage account %q", storageAccountName)
storageKeyCacheMu.Lock()
defer storageKeyCacheMu.Unlock()
key, ok = storageKeyCache[cacheIndex]
if !ok {
accountKeys, err := armClient.storageServiceClient.ListKeys(resourceGroupName, storageAccountName)
if utils.ResponseWasNotFound(accountKeys.Response) {
return "", false, nil
}
if err != nil {
// We assume this is a transient error rather than a 404 (which is caught above), so assume the
// account still exists.
return "", true, fmt.Errorf("Error retrieving keys for storage account %q: %s", storageAccountName, err)
}

if accountKeys.Keys == nil {
return "", false, fmt.Errorf("Nil key returned for storage account %q", storageAccountName)
}

keys := *accountKeys.Keys
if len(keys) <= 0 {
return "", false, fmt.Errorf("No keys returned for storage account %q", storageAccountName)
}

keyPtr := keys[0].Value
if keyPtr == nil {
return "", false, fmt.Errorf("The first key returned is nil for storage account %q", storageAccountName)
}

key = *keyPtr
storageKeyCache[cacheIndex] = key
}

keys := *accountKeys.Keys
return *keys[0].Value, true, nil
return key, true, nil
}

func (armClient *ArmClient) getBlobStorageClientForStorageAccount(resourceGroupName, storageAccountName string) (*mainStorage.BlobStorageClient, bool, error) {
Expand Down