Skip to content

Commit

Permalink
Replace if-else chain with switch/case
Browse files Browse the repository at this point in the history
  • Loading branch information
Botje committed Feb 28, 2024
1 parent 77cb5f8 commit 6bb1a73
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand Down
31 changes: 19 additions & 12 deletions internal/services/storage/storage_account_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}{
Expand Down

0 comments on commit 6bb1a73

Please sign in to comment.