diff --git a/internal/services/keyvault/client/helpers.go b/internal/services/keyvault/client/helpers.go index 353781500c13..a8f67b1e3db0 100644 --- a/internal/services/keyvault/client/helpers.go +++ b/internal/services/keyvault/client/helpers.go @@ -39,6 +39,47 @@ func (c *Client) AddToCache(keyVaultId commonids.KeyVaultId, dataPlaneUri string keysmith.Unlock() } +// TryCacheWithKeyVaultID try to cache KeyVault information as early as possible to speed up KeyVaultIDFromBaseUrl +func (c *Client) TryCacheWithKeyVaultID(ctx context.Context, idStr string) error { + if idStr == "" { + return nil + } + + keyVaultId, err := commonids.ParseKeyVaultID(idStr) + if err != nil { + return err + } + + cacheKey := c.cacheKeyForKeyVault(keyVaultId.VaultName) + keysmith.Lock() + if lock[cacheKey] == nil { + lock[cacheKey] = &sync.RWMutex{} + } + keysmith.Unlock() + lock[cacheKey].Lock() + defer lock[cacheKey].Unlock() + + if _, ok := keyVaultsCache[cacheKey]; ok { + return nil + } + + resp, err := c.VaultsClient.Get(ctx, *keyVaultId) + if err != nil { + if response.WasNotFound(resp.HttpResponse) { + return nil + } + return fmt.Errorf("retrieving %s: %+v", keyVaultId, err) + } + + if model := resp.Model; model != nil { + if model.Properties.VaultUri != nil { + vaultUri := *model.Properties.VaultUri + c.AddToCache(*keyVaultId, vaultUri) + } + } + return nil +} + func (c *Client) BaseUriForKeyVault(ctx context.Context, keyVaultId commonids.KeyVaultId) (*string, error) { cacheKey := c.cacheKeyForKeyVault(keyVaultId.VaultName) keysmith.Lock() diff --git a/internal/services/keyvault/key_vault_certificate_contacts_resource.go b/internal/services/keyvault/key_vault_certificate_contacts_resource.go index 8055553f4fe6..a4893fefb990 100644 --- a/internal/services/keyvault/key_vault_certificate_contacts_resource.go +++ b/internal/services/keyvault/key_vault_certificate_contacts_resource.go @@ -155,6 +155,8 @@ func (r KeyVaultCertificateContactsResource) Read() sdk.ResourceFunc { return err } + _ = vaultClient.TryCacheWithKeyVaultID(ctx, metadata.ResourceData.Get("key_vault_id").(string)) + keyVaultIdRaw, err := vaultClient.KeyVaultIDFromBaseUrl(ctx, resourcesClient, id.KeyVaultBaseUrl) if err != nil { return fmt.Errorf("retrieving resource ID of the Key Vault at URL %s: %+v", id.KeyVaultBaseUrl, err) diff --git a/internal/services/keyvault/key_vault_certificate_resource.go b/internal/services/keyvault/key_vault_certificate_resource.go index 840abaa824de..cd3be7426169 100644 --- a/internal/services/keyvault/key_vault_certificate_resource.go +++ b/internal/services/keyvault/key_vault_certificate_resource.go @@ -685,6 +685,8 @@ func resourceKeyVaultCertificateRead(d *pluginsdk.ResourceData, meta interface{} return err } + _ = keyVaultsClient.TryCacheWithKeyVaultID(ctx, d.Get("key_vault_id").(string)) + keyVaultIdRaw, err := keyVaultsClient.KeyVaultIDFromBaseUrl(ctx, resourcesClient, id.KeyVaultBaseUrl) if err != nil { return fmt.Errorf("retrieving the Resource ID the Key Vault at URL %q: %s", id.KeyVaultBaseUrl, err) diff --git a/internal/services/keyvault/key_vault_key_resource.go b/internal/services/keyvault/key_vault_key_resource.go index 610ef37eccee..e69719c45005 100644 --- a/internal/services/keyvault/key_vault_key_resource.go +++ b/internal/services/keyvault/key_vault_key_resource.go @@ -17,6 +17,8 @@ import ( "strings" "time" + "golang.org/x/crypto/ssh" + "github.com/Azure/go-autorest/autorest" "github.com/Azure/go-autorest/autorest/date" "github.com/hashicorp/go-azure-helpers/lang/pointer" @@ -34,7 +36,6 @@ import ( "github.com/hashicorp/terraform-provider-azurerm/internal/timeouts" "github.com/hashicorp/terraform-provider-azurerm/utils" "github.com/tombuildsstuff/kermit/sdk/keyvault/7.4/keyvault" - "golang.org/x/crypto/ssh" ) func resourceKeyVaultKey() *pluginsdk.Resource { @@ -469,6 +470,8 @@ func resourceKeyVaultKeyRead(d *pluginsdk.ResourceData, meta interface{}) error return err } + _ = keyVaultsClient.TryCacheWithKeyVaultID(ctx, d.Get("key_vault_id").(string)) + keyVaultIdRaw, err := keyVaultsClient.KeyVaultIDFromBaseUrl(ctx, resourcesClient, id.KeyVaultBaseUrl) if err != nil { return fmt.Errorf("retrieving the Resource ID the Key Vault at URL %q: %s", id.KeyVaultBaseUrl, err) diff --git a/internal/services/keyvault/key_vault_managed_storage_account.go b/internal/services/keyvault/key_vault_managed_storage_account.go index cb63025dbdea..79dfd93b5c0b 100644 --- a/internal/services/keyvault/key_vault_managed_storage_account.go +++ b/internal/services/keyvault/key_vault_managed_storage_account.go @@ -191,6 +191,8 @@ func resourceKeyVaultManagedStorageAccountRead(d *pluginsdk.ResourceData, meta i return err } + _ = keyVaultsClient.TryCacheWithKeyVaultID(ctx, d.Get("key_vault_id").(string)) + keyVaultIdRaw, err := keyVaultsClient.KeyVaultIDFromBaseUrl(ctx, resourcesClient, id.KeyVaultBaseUrl) if err != nil { return fmt.Errorf("retrieving the Resource ID of the Key Vault at URL %q: %s", id.KeyVaultBaseUrl, err) diff --git a/internal/services/keyvault/key_vault_secret_resource.go b/internal/services/keyvault/key_vault_secret_resource.go index ce3bd264fe40..edb4e5c90d83 100644 --- a/internal/services/keyvault/key_vault_secret_resource.go +++ b/internal/services/keyvault/key_vault_secret_resource.go @@ -313,6 +313,8 @@ func resourceKeyVaultSecretRead(d *pluginsdk.ResourceData, meta interface{}) err return err } + _ = keyVaultsClient.TryCacheWithKeyVaultID(ctx, d.Get("key_vault_id").(string)) + keyVaultIdRaw, err := keyVaultsClient.KeyVaultIDFromBaseUrl(ctx, resourcesClient, id.KeyVaultBaseUrl) if err != nil { return fmt.Errorf("retrieving the Resource ID the Key Vault at URL %q: %s", id.KeyVaultBaseUrl, err)