Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public class ServiceTypeTest {

private Map<ServiceType, Collection<String>> types;

private Map<ServiceType, Collection<String>> unknownTypes;

@BeforeSuite
public void setup() {
types = new HashMap<>();
Expand All @@ -37,6 +39,10 @@ public void setup() {
types.put(ServiceType.MAGNUM, Arrays.asList("container","ContainerV3","containerv1"));
types.put(ServiceType.DNS, Arrays.asList("dns","dnsv2","dnsV3"));
types.put(ServiceType.WORKFLOW, Arrays.asList("workflow","workflowv3","workflowv2"));

unknownTypes = new HashMap();
unknownTypes.put(ServiceType.ORCHESTRATION, Arrays.asList("heat-cfg","heatother","heatvm","heat-cfg4"));

}

@Test
Expand All @@ -47,4 +53,13 @@ public void testNameResolveByType() {
}
}
}

@Test
public void testNameNotResolved() {
for (Map.Entry<ServiceType, Collection<String>> entry : unknownTypes.entrySet()) {
for(String type : entry.getValue()){
assertEquals(ServiceType.UNKNOWN, ServiceType.forName(type));
}
}
}
}
39 changes: 23 additions & 16 deletions core/src/main/java/org/openstack4j/api/types/ServiceType.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.openstack4j.api.types;

import java.util.regex.Pattern;
public enum ServiceType {

IDENTITY("keystone", "identity"),
Expand Down Expand Up @@ -29,11 +30,17 @@ public enum ServiceType {

private final String serviceName;
private final String type;
private final Pattern servicePattern;
private static final String SERVICE_PATTERN_SUFFIX = "[v|\\d|\\.]*";

ServiceType(String serviceName, String type) {
this.serviceName = serviceName;
this.type = type;
}
this.servicePattern = Pattern.compile(Pattern.quote(serviceName) + SERVICE_PATTERN_SUFFIX +
"|" + Pattern.quote(type) + SERVICE_PATTERN_SUFFIX +
"|" + Pattern.quote(this.name()) + SERVICE_PATTERN_SUFFIX
, Pattern.CASE_INSENSITIVE);
}

public String getServiceName() {
return this.serviceName;
Expand All @@ -43,22 +50,22 @@ public String getType() {
return this.type;
}

public static ServiceType forName(String name) {
if (name == null || name.isEmpty()) {
return ServiceType.UNKNOWN;
}
private Pattern getServicePattern()
{
return this.servicePattern;
}

for (ServiceType s : ServiceType.values()) {
if (name.toLowerCase().startsWith(s.getServiceName().toLowerCase())) {
return s;
}
if (name.toLowerCase().startsWith(s.name().toLowerCase())) {
return s;
}
if (name.toLowerCase().startsWith(s.type.toLowerCase())) {
return s;
}
}
public static ServiceType forName(String name)
{
if (name == null || name.isEmpty())
{
return ServiceType.UNKNOWN;
}
for (ServiceType s : ServiceType.values())
{
if(s.getServicePattern().matcher(name).matches())
return s;
}
return ServiceType.UNKNOWN;
}
}