44package storage
55
66import (
7+ "context"
78 "fmt"
89 "log"
910 "time"
@@ -25,6 +26,8 @@ import (
2526
2627//go:generate go run ../../tools/generator-tests resourceidentity -resource-name storage_account_customer_managed_key -service-package-name storage -compare-values "subscription_id:storage_account_id,resource_group_name:storage_account_id,storage_account_name:storage_account_id"
2728
29+ var storageAccountCustomerManagedKeyResourceName = "azurerm_storage_account_customer_managed_key"
30+
2831func resourceStorageAccountCustomerManagedKey () * pluginsdk.Resource {
2932 resource := & pluginsdk.Resource {
3033 Create : resourceStorageAccountCustomerManagedKeyCreateUpdate ,
@@ -161,7 +164,7 @@ func resourceStorageAccountCustomerManagedKeyCreateUpdate(d *pluginsdk.ResourceD
161164
162165 if d .IsNewResource () {
163166 if existing .Model .Properties .Encryption != nil && pointer .From (existing .Model .Properties .Encryption .KeySource ) == storageaccounts .KeySourceMicrosoftPointKeyvault {
164- return tf .ImportAsExistsError ("azurerm_storage_account_customer_managed_key" , id .ID ())
167+ return tf .ImportAsExistsError (storageAccountCustomerManagedKeyResourceName , id .ID ())
165168 }
166169 }
167170
@@ -310,7 +313,6 @@ func resourceStorageAccountCustomerManagedKeyCreateUpdate(d *pluginsdk.ResourceD
310313
311314func resourceStorageAccountCustomerManagedKeyRead (d * pluginsdk.ResourceData , meta interface {}) error {
312315 storageClient := meta .(* clients.Client ).Storage .ResourceManager .StorageAccounts
313- keyVaultsClient := meta .(* clients.Client ).KeyVault
314316
315317 ctx , cancel := timeouts .ForRead (meta .(* clients.Client ).StopContext , d )
316318 defer cancel ()
@@ -330,11 +332,15 @@ func resourceStorageAccountCustomerManagedKeyRead(d *pluginsdk.ResourceData, met
330332 return fmt .Errorf ("retrieving %s: %+v" , id , err )
331333 }
332334
335+ return resourceStorageAccountCustomerManagedKeyFlatten (ctx , meta .(* clients.Client ), d , id , resp .Model , true )
336+ }
337+
338+ func resourceStorageAccountCustomerManagedKeyFlatten (ctx context.Context , metaClient * clients.Client , d * pluginsdk.ResourceData , id * commonids.StorageAccountId , storageAccount * storageaccounts.StorageAccount , fetchCompleteData bool ) error {
333339 d .Set ("storage_account_id" , id .ID ())
334340
335341 enabled := false
336- if model := resp . Model ; model != nil {
337- if props := model .Properties ; props != nil {
342+ if storageAccount != nil {
343+ if props := storageAccount .Properties ; props != nil {
338344 if encryption := props .Encryption ; encryption != nil && pointer .From (encryption .KeySource ) == storageaccounts .KeySourceMicrosoftPointKeyvault {
339345 enabled = true
340346
@@ -371,26 +377,30 @@ func resourceStorageAccountCustomerManagedKeyRead(d *pluginsdk.ResourceData, met
371377 keyID = keyId
372378 }
373379
374- federatedIdentityClientID := ""
375- userAssignedIdentity := ""
376- if identityProps := encryption .Identity ; identityProps != nil {
377- federatedIdentityClientID = pointer .From (identityProps .FederatedIdentityClientId )
378- userAssignedIdentity = pointer .From (identityProps .UserAssignedIdentity )
379- }
380- // now we have the key vault uri we can look up the ID
381- // we can't look up the ID when using federated identity as the key will be under different tenant
382- keyVaultID := ""
383- if federatedIdentityClientID == "" && keyID != nil && ! keyID .IsManagedHSM () {
384- subscriptionResourceId := commonids .NewSubscriptionID (id .SubscriptionId )
385- tmpKeyVaultID , err := keyVaultsClient .KeyVaultIDFromBaseUrl (ctx , subscriptionResourceId , keyID .KeyVaultBaseURL )
386- if err != nil {
387- return fmt .Errorf ("retrieving Key Vault ID from the Base URI %q: %+v" , keyID .KeyVaultBaseURL , err )
380+ if fetchCompleteData {
381+ keyVaultsClient := metaClient .KeyVault
382+
383+ federatedIdentityClientID := ""
384+ userAssignedIdentity := ""
385+ if identityProps := encryption .Identity ; identityProps != nil {
386+ federatedIdentityClientID = pointer .From (identityProps .FederatedIdentityClientId )
387+ userAssignedIdentity = pointer .From (identityProps .UserAssignedIdentity )
388+ }
389+ // now we have the key vault uri we can look up the ID
390+ // we can't look up the ID when using federated identity as the key will be under different tenant
391+ keyVaultID := ""
392+ if federatedIdentityClientID == "" && keyID != nil && ! keyID .IsManagedHSM () {
393+ subscriptionResourceId := commonids .NewSubscriptionID (id .SubscriptionId )
394+ tmpKeyVaultID , err := keyVaultsClient .KeyVaultIDFromBaseUrl (ctx , subscriptionResourceId , keyID .KeyVaultBaseURL )
395+ if err != nil {
396+ return fmt .Errorf ("retrieving Key Vault ID from the Base URI %q: %+v" , keyID .KeyVaultBaseURL , err )
397+ }
398+ keyVaultID = pointer .From (tmpKeyVaultID )
388399 }
389- keyVaultID = pointer .From (tmpKeyVaultID )
400+ d .Set ("key_vault_id" , keyVaultID )
401+ d .Set ("user_assigned_identity_id" , userAssignedIdentity )
402+ d .Set ("federated_identity_client_id" , federatedIdentityClientID )
390403 }
391- d .Set ("key_vault_id" , keyVaultID )
392- d .Set ("user_assigned_identity_id" , userAssignedIdentity )
393- d .Set ("federated_identity_client_id" , federatedIdentityClientID )
394404 }
395405 }
396406 }
0 commit comments