From 6bb1a73dfb2071b79f214301f5058661e5233304 Mon Sep 17 00:00:00 2001 From: Dries Harnie Date: Wed, 28 Feb 2024 22:53:36 +0100 Subject: [PATCH] Replace if-else chain with switch/case --- ...e_account_customer_managed_key_resource.go | 29 ++++++++++------- .../storage/storage_account_resource.go | 31 ++++++++++++------- 2 files changed, 37 insertions(+), 23 deletions(-) diff --git a/internal/services/storage/storage_account_customer_managed_key_resource.go b/internal/services/storage/storage_account_customer_managed_key_resource.go index 7afb5fb9e946..b7c0f5ac78fe 100644 --- a/internal/services/storage/storage_account_customer_managed_key_resource.go +++ b/internal/services/storage/storage_account_customer_managed_key_resource.go @@ -283,18 +283,25 @@ func resourceStorageAccountCustomerManagedKeyRead(d *pluginsdk.ResourceData, met // we can't look up the ID when using federated identity as the key will be under different tenant if federatedIdentityClientID == "" { isHSMURI, err := managedHsmParse.IsManagedHSMURI(keyVaultURI, &env) - if err != nil { - return fmt.Errorf("parsing Base Key Vault URI %q: %+v", keyVaultURI, err) - } else if isHSMURI { - d.Set("managed_hsm_uri", keyVaultURI) - } else { - d.Set("key_vault_uri", keyVaultURI) - subscriptionResourceId := commonids.NewSubscriptionID(id.SubscriptionId) - tmpKeyVaultID, err := keyVaultsClient.KeyVaultIDFromBaseUrl(ctx, subscriptionResourceId, keyVaultURI) - if err != nil { - return fmt.Errorf("retrieving Key Vault ID from the Base URI %q: %+v", keyVaultURI, err) + switch { + case err != nil: + { + return fmt.Errorf("parsing Base Key Vault URI %q: %+v", keyVaultURI, err) + } + case isHSMURI: + { + d.Set("managed_hsm_uri", keyVaultURI) + } + case !isHSMURI: + { + d.Set("key_vault_uri", keyVaultURI) + subscriptionResourceId := commonids.NewSubscriptionID(id.SubscriptionId) + tmpKeyVaultID, err := keyVaultsClient.KeyVaultIDFromBaseUrl(ctx, subscriptionResourceId, keyVaultURI) + if err != nil { + return fmt.Errorf("retrieving Key Vault ID from the Base URI %q: %+v", keyVaultURI, err) + } + d.Set("key_vault_id", pointer.From(tmpKeyVaultID)) } - d.Set("key_vault_id", pointer.From(tmpKeyVaultID)) } } diff --git a/internal/services/storage/storage_account_resource.go b/internal/services/storage/storage_account_resource.go index 9bbc01b44d23..5cd142949ab9 100644 --- a/internal/services/storage/storage_account_resource.go +++ b/internal/services/storage/storage_account_resource.go @@ -2614,20 +2614,27 @@ func flattenStorageAccountCustomerManagedKey(storageAccountId *commonids.Storage } isHSMURI, err := managedHsmParse.IsManagedHSMURI(keyVaultURI, env) - if err != nil { - return nil, fmt.Errorf("parsing Base Key Vault URI %q: %+v", keyVaultURI, err) - } else if isHSMURI { - keyId, err := managedHsmParse.NewNestedItemID(keyVaultURI, "keys", "", keyName) - if err != nil { - return nil, err + switch { + case err != nil: + { + return nil, fmt.Errorf("parsing Base Key Vault URI %q: %+v", keyVaultURI, err) + } + case isHSMURI: + { + keyId, err := managedHsmParse.NewNestedItemID(keyVaultURI, "keys", "", keyName) + if err != nil { + return nil, err + } + ret["managed_hsm_key_id"] = keyId.ID() } - ret["managed_hsm_key_id"] = keyId.ID() - } else { - keyId, err := keyVaultParse.NewNestedItemID(keyVaultURI, keyVaultParse.NestedItemTypeKey, keyName, keyVersion) - if err != nil { - return nil, err + case !isHSMURI: + { + keyId, err := keyVaultParse.NewNestedItemID(keyVaultURI, keyVaultParse.NestedItemTypeKey, keyName, keyVersion) + if err != nil { + return nil, err + } + ret["key_vault_key_id"] = keyId.ID() } - ret["key_vault_key_id"] = keyId.ID() } return []interface{}{