diff --git a/server/src/main/java/org/opensearch/accesscontrol/resources/EntityType.java b/server/src/main/java/org/opensearch/accesscontrol/resources/EntityType.java index e82f4a52c9381..070585562e4e6 100644 --- a/server/src/main/java/org/opensearch/accesscontrol/resources/EntityType.java +++ b/server/src/main/java/org/opensearch/accesscontrol/resources/EntityType.java @@ -8,6 +8,11 @@ package org.opensearch.accesscontrol.resources; +import java.util.Arrays; +import java.util.Map; +import java.util.function.Function; +import java.util.stream.Collectors; + /** * This enum contains the type of entities a resource can be shared with. * @@ -19,6 +24,9 @@ public enum EntityType { ROLES("roles"), BACKEND_ROLES("backend_roles"); + private static final Map VALUE_MAP = Arrays.stream(values()) + .collect(Collectors.toMap(EntityType::toString, Function.identity())); + private final String value; EntityType(String value) { @@ -29,4 +37,12 @@ public enum EntityType { public String toString() { return value; } + + public static EntityType fromValue(String value) { + EntityType type = VALUE_MAP.get(value); + if (type == null) { + throw new IllegalArgumentException("No enum constant with value: " + value); + } + return type; + } }