Search for resource type efficiently (#6242)

Co-authored-by: Rakesh Venkatesh <rakeshv@apache.org>
This commit is contained in:
Rakesh 2023-01-12 13:29:38 +01:00 committed by GitHub
parent 4418574478
commit aad03530af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 7 deletions

View File

@ -20,6 +20,10 @@ import org.apache.cloudstack.acl.ControlledEntity;
import org.apache.cloudstack.api.Identity;
import org.apache.cloudstack.api.InternalIdentity;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
public interface ResourceTag extends ControlledEntity, Identity, InternalIdentity {
// FIXME - extract enum to another interface as its used both by resourceTags and resourceMetaData code
@ -80,6 +84,7 @@ public interface ResourceTag extends ControlledEntity, Identity, InternalIdentit
private final boolean resourceTagsSupport;
private final boolean metadataSupport;
private boolean resourceIconSupport;
private static final Map<String, ResourceObjectType> resourceObjectTypeMap = new HashMap<>();
public boolean resourceTagsSupport() {
return resourceTagsSupport;
@ -92,6 +97,16 @@ public interface ResourceTag extends ControlledEntity, Identity, InternalIdentit
public boolean resourceIconSupport() {
return resourceIconSupport;
}
static {
for (var value : ResourceObjectType.values()) {
resourceObjectTypeMap.put(value.toString().toLowerCase(Locale.ROOT), value);
}
}
public static ResourceObjectType getResourceObjectType(String type) {
return resourceObjectTypeMap.getOrDefault(type.toLowerCase(Locale.ROOT), null);
}
}
/**

View File

@ -72,6 +72,7 @@ import com.cloud.utils.exception.CloudRuntimeException;
import com.cloud.vm.NicVO;
import com.cloud.vm.UserVmVO;
import com.cloud.vm.snapshot.VMSnapshotVO;
import java.util.Optional;
public class ResourceManagerUtilImpl implements ResourceManagerUtil {
public static final Map<ResourceTag.ResourceObjectType, Class<?>> s_typeMap = new HashMap<>();
@ -159,13 +160,8 @@ public class ResourceManagerUtilImpl implements ResourceManagerUtil {
@Override
public ResourceTag.ResourceObjectType getResourceType(String resourceTypeStr) {
for (ResourceTag.ResourceObjectType type : ResourceTag.ResourceObjectType.values()) {
if (type.toString().equalsIgnoreCase(resourceTypeStr)) {
return type;
}
}
throw new InvalidParameterValueException("Invalid resource type: " + resourceTypeStr);
return Optional.ofNullable(ResourceTag.ResourceObjectType.getResourceObjectType(resourceTypeStr))
.orElseThrow(() -> new InvalidParameterValueException("Invalid resource type " + resourceTypeStr));
}
public void checkResourceAccessible(Long accountId, Long domainId, String exceptionMessage) {