cleanup: refactor deeply nested if statement in vault_tokens.go

Refactored deeply nested if statement in vault_tokens.go to
reduce cognitive complexity by adding fetchTenantConfig function.

Signed-off-by: Rakshith R <rar@redhat.com>
This commit is contained in:
Rakshith R 2021-04-05 10:01:47 +05:30 committed by mergify[bot]
parent 2d1a572d11
commit d4cfd7bef9

View File

@ -216,20 +216,12 @@ func initVaultTokensKMS(args KMSInitializerArgs) (EncryptionKMS, error) {
// fetch the configuration for the tenant
if args.Tenant != "" {
kms.Tenant = args.Tenant
tenantsMap, ok := config["tenants"]
if ok {
// tenants is a map per tenant, containing key/values
tenants, ok := tenantsMap.(map[string]map[string]interface{})
if ok {
// get the map for the tenant of the current operation
tenantConfig, ok := tenants[args.Tenant]
if ok {
// override connection details from the tenant
err = kms.parseConfig(tenantConfig)
if err != nil {
return nil, err
}
}
tenantConfig, found := fetchTenantConfig(config, args.Tenant)
if found {
// override connection details from the tenant
err = kms.parseConfig(tenantConfig)
if err != nil {
return nil, err
}
}
@ -506,3 +498,22 @@ func (kms *VaultTokensKMS) parseTenantConfig() error {
return nil
}
// fetchTenantConfig fetches the configuration for the tenant if it exists.
func fetchTenantConfig(config map[string]interface{}, tenant string) (map[string]interface{}, bool) {
tenantsMap, ok := config["tenants"]
if !ok {
return nil, false
}
// tenants is a map per tenant, containing key/values
tenants, ok := tenantsMap.(map[string]map[string]interface{})
if !ok {
return nil, false
}
// get the map for the tenant of the current operation
tenantConfig, ok := tenants[tenant]
if !ok {
return nil, false
}
return tenantConfig, true
}