diff --git a/src/main/java/com/openshift3/client/ResourceKind.java b/src/main/java/com/openshift3/client/ResourceKind.java index 72a60536..8c81686e 100644 --- a/src/main/java/com/openshift3/client/ResourceKind.java +++ b/src/main/java/com/openshift3/client/ResourceKind.java @@ -34,5 +34,4 @@ public enum ResourceKind { public String pluralize() { return plural; } - } diff --git a/src/main/java/com/openshift3/internal/client/APIModelVersion.java b/src/main/java/com/openshift3/internal/client/APIModelVersion.java new file mode 100644 index 00000000..f5ef7502 --- /dev/null +++ b/src/main/java/com/openshift3/internal/client/APIModelVersion.java @@ -0,0 +1,27 @@ +/******************************************************************************* + * Copyright (c) 2015 Red Hat, Inc. Distributed under license by Red Hat, Inc. + * All rights reserved. This program is made available under the terms of the + * Eclipse Public License v1.0 which accompanies this distribution, and is + * available at http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: Red Hat, Inc. + ******************************************************************************/ +package com.openshift3.internal.client; + +import java.util.Comparator; + +public interface APIModelVersion { + + int getOrder(); + + static class VersionComparitor implements Comparator { + @Override + public int compare(APIModelVersion v1, APIModelVersion v2) { + if(v2 == null) return 1; + if(v1 == null) return -1; + if(v1.getOrder() < v2.getOrder()) return -1; + if(v1.getOrder() > v2.getOrder()) return 1; + return 0; + } + }; +} diff --git a/src/main/java/com/openshift3/internal/client/DefaultClient.java b/src/main/java/com/openshift3/internal/client/DefaultClient.java index a0dcfd2d..91dfdcec 100644 --- a/src/main/java/com/openshift3/internal/client/DefaultClient.java +++ b/src/main/java/com/openshift3/internal/client/DefaultClient.java @@ -8,7 +8,7 @@ ******************************************************************************/ package com.openshift3.internal.client; -import static com.openshift3.client.capability.CapabilityInitializer.initializeCapability;; +import static com.openshift3.client.capability.CapabilityInitializer.initializeCapability; import java.net.SocketTimeoutException; import java.net.URL; @@ -17,6 +17,7 @@ import java.util.List; import java.util.Map; +import org.jboss.dmr.ModelNode; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -30,6 +31,7 @@ import com.openshift3.client.capability.server.IImageRegistryHosting; import com.openshift3.client.model.IResource; import com.openshift3.internal.client.capability.server.DefaultImageRegistryHosting; +import com.openshift3.internal.client.model.ResourcePropertiesRegistry; import com.openshift3.internal.client.model.Status; public class DefaultClient implements IClient{ @@ -41,24 +43,13 @@ public class DefaultClient implements IClient{ private Map, ICapability> capabilities = new HashMap, ICapability>(); private boolean capabilitiesInitialized = false; - private static final String apiEndpoint = "api/v1beta1"; - private static final String osApiEndpoint = "osapi/v1beta1"; + private static final String apiEndpoint = "api"; + private static final String osApiEndpoint = "osapi"; - private static final Map TYPE_MAPPING = new HashMap(); + private final Map typeMappings = new HashMap(); + private OpenShiftAPIVersion openShiftVersion; + private KubernetesAPIVersion kubernetesVersion; - static { - //OpenShift endpoints - TYPE_MAPPING.put(ResourceKind.BuildConfig, osApiEndpoint); - TYPE_MAPPING.put(ResourceKind.DeploymentConfig, osApiEndpoint); - TYPE_MAPPING.put(ResourceKind.ImageRepository, osApiEndpoint); - TYPE_MAPPING.put(ResourceKind.Project, osApiEndpoint); - - //Kubernetes endpoints - TYPE_MAPPING.put(ResourceKind.Pod, apiEndpoint); - TYPE_MAPPING.put(ResourceKind.Service, apiEndpoint); - - } - public DefaultClient(URL baseUrl){ this(baseUrl, null); } @@ -94,10 +85,10 @@ public List list(ResourceKind kind, String namespace) { @SuppressWarnings("unchecked") @Override public List list(ResourceKind kind, String namespace, Map labels) { - if(!TYPE_MAPPING.containsKey(kind)) + if(!getTypeMappings().containsKey(kind)) throw new RuntimeException("No OpenShift resource endpoint for type: " + kind); try { - URLBuilder builder = new URLBuilder(this.baseUrl, TYPE_MAPPING) + URLBuilder builder = new URLBuilder(this.baseUrl, getTypeMappings()) .kind(kind) .namespace(namespace); final URL endpoint = builder.build(); @@ -127,7 +118,7 @@ private List filterItems(List items, Map T create(T resource) { try { - final URL endpoint = new URLBuilder(this.baseUrl, TYPE_MAPPING) + final URL endpoint = new URLBuilder(this.baseUrl, getTypeMappings()) .kind(resource.getKind()) .addParmeter("namespace", resource.getNamespace()) .build(); @@ -145,10 +136,11 @@ public T create(T resource) { @Override public void delete(T resource) { try { - final URL endpoint = new URLBuilder(this.baseUrl, TYPE_MAPPING) + final URL endpoint = new URLBuilder(this.baseUrl, getTypeMappings()) .resource(resource) .addParmeter("namespace", resource.getNamespace()) .build(); + LOGGER.debug(String.format("Deleting resource: %s", endpoint)); String response = client.delete(endpoint, IHttpClient.DEFAULT_READ_TIMEOUT); LOGGER.debug(response); //TODO return response object here @@ -163,7 +155,7 @@ public void delete(T resource) { @Override public T get(ResourceKind kind, String name, String namespace) { try { - final URL endpoint = new URLBuilder(this.baseUrl, TYPE_MAPPING) + final URL endpoint = new URLBuilder(this.baseUrl, getTypeMappings()) .kind(kind) .name(name) .addParmeter("namespace", namespace) @@ -212,6 +204,75 @@ public boolean supports(Class capability) { return capabilities.containsKey(capability); } + public List getKubernetesVersions() { + return getVersion(KubernetesAPIVersion.class, apiEndpoint); + } + + public List getOpenShiftVersions() { + return getVersion(OpenShiftAPIVersion.class, osApiEndpoint); + } + + public KubernetesAPIVersion getKubernetesVersion() { + if(kubernetesVersion == null){ + List versions = getKubernetesVersions(); + kubernetesVersion = ResourcePropertiesRegistry.getInstance().getMaxSupportedKubernetesVersion(); + if(!versions.contains(kubernetesVersion)){ + throw new RuntimeException(String.format("Kubernetes API version '%s' is not supported by this client")); + } + } + return kubernetesVersion; + } + + public OpenShiftAPIVersion getOpenShiftVersion() { + if(openShiftVersion == null){ + List versions = getOpenShiftVersions(); + openShiftVersion = ResourcePropertiesRegistry.getInstance().getMaxSupportedOpenShiftVersion(); + if(!versions.contains(openShiftVersion)){ + throw new RuntimeException(String.format("OpenShift API version '%s' is not supported by this client")); + } + } + return openShiftVersion; + } + + private > List getVersion(Class klass, String endpoint) { + try { + final URL url = new URL(this.baseUrl, endpoint); + LOGGER.debug(url.toString()); + String response = client.get(url, IHttpClient.DEFAULT_READ_TIMEOUT); + LOGGER.debug(response); + ModelNode json = ModelNode.fromJSONString(response); + List versionNodes = json.get("versions").asList(); + List versions = new ArrayList(versionNodes.size()); + for (ModelNode node : versionNodes) { + try{ + versions.add(Enum.valueOf(klass, node.asString())); + }catch(IllegalArgumentException e){ + LOGGER.warn(String.format("Unsupported server version '%s' for '%s'", node.asString(), klass.getSimpleName())); + } + } + return versions; + } catch (Exception e) { + LOGGER.error("Exception", e); + throw new RuntimeException(e); + } + } + + private Map getTypeMappings(){ + if(typeMappings.isEmpty()){ + //OpenShift endpoints + final String osEndpoint = String.format("%s/%s", osApiEndpoint, getOpenShiftVersion()); + typeMappings.put(ResourceKind.BuildConfig, osEndpoint); + typeMappings.put(ResourceKind.DeploymentConfig, osEndpoint); + typeMappings.put(ResourceKind.ImageRepository, osEndpoint); + typeMappings.put(ResourceKind.Project, osEndpoint); + + //Kubernetes endpoints + final String k8eEndpoint = String.format("%s/%s", apiEndpoint, getKubernetesVersion()); + typeMappings.put(ResourceKind.Pod, k8eEndpoint); + typeMappings.put(ResourceKind.Service, k8eEndpoint); + } + return typeMappings; + } } diff --git a/src/main/java/com/openshift3/internal/client/IResourceFactory.java b/src/main/java/com/openshift3/internal/client/IResourceFactory.java index 43e17260..58f43e60 100644 --- a/src/main/java/com/openshift3/internal/client/IResourceFactory.java +++ b/src/main/java/com/openshift3/internal/client/IResourceFactory.java @@ -34,4 +34,14 @@ public interface IResourceFactory { * @return */ T create(String response) ; + + /** + * Create a resource for a given version and kind + * @param version + * @param kind + * @return + */ + T create(String version, ResourceKind kind); + + } diff --git a/src/main/java/com/openshift3/internal/client/KubernetesAPIVersion.java b/src/main/java/com/openshift3/internal/client/KubernetesAPIVersion.java new file mode 100644 index 00000000..e2148cf1 --- /dev/null +++ b/src/main/java/com/openshift3/internal/client/KubernetesAPIVersion.java @@ -0,0 +1,31 @@ +/******************************************************************************* + * Copyright (c) 2015 Red Hat, Inc. Distributed under license by Red Hat, Inc. + * All rights reserved. This program is made available under the terms of the + * Eclipse Public License v1.0 which accompanies this distribution, and is + * available at http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: Red Hat, Inc. + ******************************************************************************/ +package com.openshift3.internal.client; + + + + +/** + * This list of supported Kubernetes API Models + * by this client + */ +public enum KubernetesAPIVersion implements APIModelVersion{ + v1beta1(1); + + private int order; + + KubernetesAPIVersion( int order){ + this.order = order; + } + + @Override + public int getOrder(){ + return order; + } +} diff --git a/src/main/java/com/openshift3/internal/client/OpenShiftAPIVersion.java b/src/main/java/com/openshift3/internal/client/OpenShiftAPIVersion.java new file mode 100644 index 00000000..21387fca --- /dev/null +++ b/src/main/java/com/openshift3/internal/client/OpenShiftAPIVersion.java @@ -0,0 +1,30 @@ +/******************************************************************************* + * Copyright (c) 2015 Red Hat, Inc. Distributed under license by Red Hat, Inc. + * All rights reserved. This program is made available under the terms of the + * Eclipse Public License v1.0 which accompanies this distribution, and is + * available at http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: Red Hat, Inc. + ******************************************************************************/ +package com.openshift3.internal.client; + + + + +/** + * This list of supported OpenShift API Models + * by this client + */ +public enum OpenShiftAPIVersion implements APIModelVersion{ + v1beta1(1); + + private int order; + OpenShiftAPIVersion(int order){ + this.order = order; + } + + @Override + public int getOrder(){ + return order; + } +} diff --git a/src/main/java/com/openshift3/internal/client/ResourceFactory.java b/src/main/java/com/openshift3/internal/client/ResourceFactory.java index 5106538d..f9fc3892 100644 --- a/src/main/java/com/openshift3/internal/client/ResourceFactory.java +++ b/src/main/java/com/openshift3/internal/client/ResourceFactory.java @@ -23,10 +23,10 @@ import com.openshift3.internal.client.model.BuildConfig; import com.openshift3.internal.client.model.DeploymentConfig; import com.openshift3.internal.client.model.ImageRepository; -import com.openshift3.internal.client.model.KubernetesResource; import com.openshift3.internal.client.model.Pod; import com.openshift3.internal.client.model.Project; import com.openshift3.internal.client.model.ReplicationController; +import com.openshift3.internal.client.model.ResourcePropertiesRegistry; import com.openshift3.internal.client.model.Service; import com.openshift3.internal.client.model.Status; @@ -35,6 +35,8 @@ */ public class ResourceFactory implements IResourceFactory{ + private static final String KIND = "kind"; + private static final String APIVERSION = "apiVersion"; private static final Map> IMPL_MAP = new HashMap>(); static { IMPL_MAP.put(ResourceKind.BuildConfig, BuildConfig.class); @@ -54,16 +56,17 @@ public ResourceFactory(IClient client) { public List createList(String json, ResourceKind kind){ ModelNode data = ModelNode.fromJSONString(json); - String dataKind = data.get("kind").asString(); + final String dataKind = data.get(KIND).asString(); if(ResourceKind.Project.toString().equals(dataKind)){ - return buildProjectListForSingleProject(data); + return buildProjectListForSingleProject(json); } if(!(kind.toString() + "List").equals(dataKind)){ throw new RuntimeException(String.format("Unexpected container type '%s' for desired kind: %s", dataKind, kind)); } try{ - return buildList(data.get("items").asList(), IMPL_MAP.get(kind)); + final String version = data.get(APIVERSION).asString(); + return buildList(version, data.get("items").asList(), kind); }catch(Exception e){ throw new RuntimeException(e); } @@ -73,30 +76,43 @@ public List createList(String json, ResourceKind kind){ * Project is apparently special as query for project with namespace returns a singular * project */ - private List buildProjectListForSingleProject(ModelNode data) { + private List buildProjectListForSingleProject(String data) { ArrayList projects = new ArrayList(1); - projects.add(new Project(data, client)); + projects.add(create(data)); return projects; } - private List buildList(List items, Class kind) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { - Constructor constructor = kind.getConstructor(ModelNode.class, IClient.class); + private List buildList(final String version, List items, ResourceKind kind) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { List resources = new ArrayList(items.size()); for (ModelNode item : items) { - resources.add(constructor.newInstance(item, client)); + resources.add(create(item, version, kind)); } return resources; } - @SuppressWarnings("unchecked") public T create(String response) { - KubernetesResource resource = new KubernetesResource(response); + ModelNode node = ModelNode.fromJSONString(response); + String version = node.get(APIVERSION).asString(); + ResourceKind kind = ResourceKind.valueOf(node.get(KIND).asString()); + return create(node, version, kind); + } + + public T create(String version, ResourceKind kind) { + ModelNode node = new ModelNode(); + node.get(APIVERSION).set(version); + node.get(KIND).set(kind.toString()); + return create(node, version, kind); + } + + @SuppressWarnings("unchecked") + private T create(ModelNode node, String version, ResourceKind kind) { try { - Constructor constructor = IMPL_MAP.get(resource.getKind()).getConstructor(ModelNode.class, IClient.class); - return (T) constructor.newInstance(resource.getNode(), client); + Map properyKeyMap = ResourcePropertiesRegistry.getInstance().get(version, kind); + Constructor constructor = IMPL_MAP.get(kind).getConstructor(ModelNode.class, IClient.class, Map.class); + return (T) constructor.newInstance(node, client, properyKeyMap); } catch (Exception e) { throw new RuntimeException(e); } - } + } diff --git a/src/main/java/com/openshift3/internal/client/model/BuildConfig.java b/src/main/java/com/openshift3/internal/client/model/BuildConfig.java index 226c531f..2870f1e1 100644 --- a/src/main/java/com/openshift3/internal/client/model/BuildConfig.java +++ b/src/main/java/com/openshift3/internal/client/model/BuildConfig.java @@ -1,5 +1,7 @@ package com.openshift3.internal.client.model; +import java.util.Map; + import org.jboss.dmr.ModelNode; import com.openshift3.client.IClient; @@ -10,42 +12,48 @@ public class BuildConfig extends KubernetesResource implements IBuildConfig { - public BuildConfig(ModelNode node, IClient client) { - super(node, client); + public BuildConfig(ModelNode node, IClient client, Map propertyKeys) { + super(node, client, propertyKeys); + //TODO add check to kind here set("kind",ResourceKind.BuildConfig.toString()); } + //TODO delete me...require construction through factory public BuildConfig() { - this(new ModelNode(), null); + this(new ModelNode(), null, null); } public String getSourceUri() { - return getNode().get(new String[]{"parameters","source","git","uri"}).asString(); + return asString(BUILDCONFIG_SOURCE_URI); } public void addTrigger(BuildTrigger type, String secret){ - ModelNode trigger = new ModelNode(); - trigger.get("type").set(type.toString()); - trigger.get(new String[]{type.toString(),"secret"}).set(secret); - getNode().get("triggers").add(trigger); + //FIXME +// ModelNode trigger = new ModelNode(); +// trigger.get("type").set(type.toString()); +// trigger.get(new String[]{type.toString(),"secret"}).set(secret); +// getNode().get("triggers").add(trigger); } public void setSource(String type, String uri){ - ModelNode params = getNode().get("parameters"); - params.get(new String[]{"source","type"}).set(type); - params.get(new String[]{"source","git","uri"}).set(uri); + //FIXME +// ModelNode params = getNode().get("parameters"); +// params.get(new String[]{"source","type"}).set(type); +// params.get(new String[]{"source","git","uri"}).set(uri); } public void setStrategy(String type, String baseImage){ - ModelNode strategy = getNode().get(new String []{"parameters","strategy"}); - strategy.get("type").set(type); - strategy.get(new String[]{"stiStrategy","image"}).set(baseImage); + //FIXME +// ModelNode strategy = getNode().get(new String []{"parameters","strategy"}); +// strategy.get("type").set(type); +// strategy.get(new String[]{"stiStrategy","image"}).set(baseImage); } public void setOutput(ImageUri imageUri){ - ModelNode output = getNode().get(new String []{"parameters","output"}); - output.get("imageTag").set(imageUri.getUriWithoutHost()); - output.get("registry").set(imageUri.getRepositoryHost()); + //FIXME +// ModelNode output = getNode().get(new String []{"parameters","output"}); +// output.get("imageTag").set(imageUri.getUriWithoutHost()); +// output.get("registry").set(imageUri.getRepositoryHost()); } } diff --git a/src/main/java/com/openshift3/internal/client/model/DeploymentConfig.java b/src/main/java/com/openshift3/internal/client/model/DeploymentConfig.java index 9aa91286..844525cb 100644 --- a/src/main/java/com/openshift3/internal/client/model/DeploymentConfig.java +++ b/src/main/java/com/openshift3/internal/client/model/DeploymentConfig.java @@ -2,6 +2,7 @@ import java.util.ArrayList; import java.util.List; +import java.util.Map; import org.jboss.dmr.ModelNode; @@ -12,21 +13,17 @@ public class DeploymentConfig extends KubernetesResource implements IDeploymentConfig{ - public static final String TRIGGERS = "triggers"; - public static final String [] CONTAINERS = new String[]{"template","controllerTemplate","podTemplate","desiredState","manifest","containers"}; - public static final String [] REPLICAS = new String[]{"template","controllerTemplate","replicas"}; - - public DeploymentConfig(ModelNode node, IClient client) { - super(node, client); + public DeploymentConfig(ModelNode node, IClient client, Map propertyKeys) { + super(node, client, propertyKeys); set("kind", ResourceKind.DeploymentConfig.toString()); } public DeploymentConfig(){ - this(new ModelNode(), null); + this(new ModelNode(), null, null); } public List getTriggerTypes(){ List types = new ArrayList(); - ModelNode triggers = getNode().get(TRIGGERS); + ModelNode triggers = get(DEPLOYMENTCONFIG_TRIGGERS); for (ModelNode node : triggers.asList()) { types.add(node.get("type").asString()); } @@ -34,7 +31,7 @@ public List getTriggerTypes(){ } public List getImageNames(){ List names = new ArrayList(); - List containers = getNode().get(CONTAINERS).asList(); + List containers = get(DEPLOYMENTCONFIG_CONTAINERS).asList(); for (ModelNode container : containers) { names.add(container.get("image").asString()); } @@ -42,7 +39,7 @@ public List getImageNames(){ } public int getReplicas(){ - return getNode().get(REPLICAS).asInt(); + return asInt(DEPLOYMENTCONFIG_REPLICAS); } public void addContainer(ImageUri tag, int containerPort){ @@ -50,8 +47,9 @@ public void addContainer(ImageUri tag, int containerPort){ buildTemplate(tag, containerPort); } + //FIXME private void addImageChangeTrigger(ImageUri imageTag){ - ModelNode triggers = getNode().get(TRIGGERS); + ModelNode triggers = get(DEPLOYMENTCONFIG_TRIGGERS); ModelNode imageChange = new ModelNode(); imageChange.get("type").set("ImageChange"); ModelNode params = imageChange.get("imageChangeParams"); @@ -66,6 +64,7 @@ private void addImageChangeTrigger(ImageUri imageTag){ triggers.add(configChange); } + //FIXME private void buildTemplate(ImageUri imageTag, int containerPort) { ModelNode template = getNode().get("template"); template.get(new String[]{"strategy","type"}).set( "Recreate"); diff --git a/src/main/java/com/openshift3/internal/client/model/ImageRepository.java b/src/main/java/com/openshift3/internal/client/model/ImageRepository.java index 98116db5..271425ec 100644 --- a/src/main/java/com/openshift3/internal/client/model/ImageRepository.java +++ b/src/main/java/com/openshift3/internal/client/model/ImageRepository.java @@ -1,5 +1,7 @@ package com.openshift3.internal.client.model; +import java.util.Map; + import org.jboss.dmr.ModelNode; import com.openshift3.client.IClient; @@ -9,11 +11,11 @@ public class ImageRepository extends KubernetesResource implements IImageRepository { public ImageRepository(){ - this(new ModelNode(), null); + this(new ModelNode(), null, null); } - public ImageRepository(ModelNode node, IClient client) { - super(node, client); + public ImageRepository(ModelNode node, IClient client, Map propertyKeys) { + super(node, client, propertyKeys); set("kind", ResourceKind.ImageRepository.toString()); } @@ -22,7 +24,7 @@ public ImageRepository(String json) { } public void setDockerImageRepository(ImageUri uri) { - getNode().get("dockerImageRepository").set(uri.getAbsoluteUri()); + set(IMAGEREPO_DOCKER_IMAGE_REPO, uri.getAbsoluteUri()); } } diff --git a/src/main/java/com/openshift3/internal/client/model/KubernetesResource.java b/src/main/java/com/openshift3/internal/client/model/KubernetesResource.java index a5707e23..100b0f8d 100644 --- a/src/main/java/com/openshift3/internal/client/model/KubernetesResource.java +++ b/src/main/java/com/openshift3/internal/client/model/KubernetesResource.java @@ -18,7 +18,6 @@ import com.openshift3.client.IClient; import com.openshift3.client.ResourceKind; -import com.openshift3.client.capability.CapabilityInitializer; import com.openshift3.client.capability.ICapability; import com.openshift3.client.capability.resources.IDeploymentConfigTraceability; import com.openshift3.client.capability.resources.IDeploymentTraceability; @@ -32,20 +31,15 @@ * Resource is an abstract representation of a Kubernetes resource * */ -public class KubernetesResource implements IResource{ - - private static final String [] ANNOTATIONS = {"annotations"}; - private static final String [] CREATION_TIMESTAMP = {"creationTimestamp"}; - private static final String [] LABELS = {"labels"}; - private static final String [] NAME = {"id"}; - private static final String [] NAMESPACE = {"namespace"}; +public class KubernetesResource implements IResource, ResourcePropertyKeys{ private ModelNode node; private IClient client; private Map, ICapability> capabilities = new HashMap, ICapability>(); + private Map propertyKeys; public KubernetesResource(){ - this(new ModelNode(), null); + this(new ModelNode(), null, null); } public KubernetesResource(String json){ @@ -53,11 +47,10 @@ public KubernetesResource(String json){ initializeCapabilities(); } - public KubernetesResource(ModelNode node, IClient client){ + public KubernetesResource(ModelNode node, IClient client, Map propertyKeys){ this.node = node; this.client = client; - //TODO figure out how to handle version changes - setApiVersion("v1beta1"); + this.propertyKeys = propertyKeys; initializeCapabilities(); } @@ -114,11 +107,7 @@ public ResourceKind getKind(){ @Override public String getApiVersion(){ - return node.get("apiVersion").asString(); - } - - public void setApiVersion(String version){ - node.get("apiVersion").set(version); + return asString(APIVERSION); } @Override @@ -127,22 +116,22 @@ public String getCreationTimeStamp(){ } @Override public String getName(){ - return node.get(NAME).asString(); + return asString(NAME); } @Override public void setName(String name) { - node.get(NAME).set(name); + set(NAME, name); } @Override public String getNamespace(){ - return node.get(NAMESPACE).asString(); + return asString(NAMESPACE); } @Override public void setNamespace(String namespace){ - node.get(NAMESPACE).set(namespace); + set(NAMESPACE, namespace); } @Override @@ -158,27 +147,23 @@ public Map getLabels() { } /*---------- utility methods ------*/ - protected ModelNode get(String path){ - return node.get(path); - } - - protected ModelNode get(String [] path){ - return node.get(path); + protected ModelNode get(String key){ + String [] property = propertyKeys.get(key); + return node.get(property); } - protected void set(String property, String value) { + protected void set(String key, int value) { + String [] property = propertyKeys.get(key); node.get(property).set(value); } - protected void set(String propery, int value) { - node.get(propery).set(value); - } - - protected void set(String [] property, String value){ + protected void set(String key, String value){ + String [] property = propertyKeys.get(key); node.get(property).set(value); } - protected Map asMap(String [] path){ + protected Map asMap(String property){ + String [] path = propertyKeys.get(property); ModelNode node = this.node.get(path); HashMap map = new HashMap(); if( ModelType.UNDEFINED == node.getType()) @@ -189,19 +174,14 @@ protected Map asMap(String [] path){ return map; } - protected int asInt(String property){ + protected int asInt(String key){ + String [] property = propertyKeys.get(key); return node.get(property).asInt(); } - protected int asInt(String [] path){ - return node.get(path).asInt(); - } - protected String asString(String property){ - return node.get(property).asString(); - } - protected String asString(String [] property){ - return node.get(property).asString(); + String [] path = propertyKeys.get(property); + return node.get(path).asString(); } @Override diff --git a/src/main/java/com/openshift3/internal/client/model/OpenShiftResource.java b/src/main/java/com/openshift3/internal/client/model/OpenShiftResource.java deleted file mode 100644 index eec95be0..00000000 --- a/src/main/java/com/openshift3/internal/client/model/OpenShiftResource.java +++ /dev/null @@ -1,48 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2015 Red Hat, Inc. Distributed under license by Red Hat, Inc. - * All rights reserved. This program is made available under the terms of the - * Eclipse Public License v1.0 which accompanies this distribution, and is - * available at http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: Red Hat, Inc. - ******************************************************************************/ -package com.openshift3.internal.client.model; - -import org.jboss.dmr.ModelNode; - -import com.openshift3.client.IClient; - -public class OpenShiftResource extends KubernetesResource { - - public OpenShiftResource(ModelNode node, IClient client) { - super(node, client); - setApiVersion("v1beta1"); - } - - public OpenShiftResource(String json){ - super(json); - } - - private static final String [] NAME = {"metadata", "name"}; - private static final String [] NAMESPACE = {"metadata", "namespace"}; - - @Override - public String getName() { - return asString(NAME); - } - - @Override - public String getNamespace() { - return asString(NAMESPACE); - } - - @Override - public void setName(String name) { - set(NAME, name); - } - - @Override - public void setNamespace(String namespace) { - set(NAMESPACE, namespace); - } -} diff --git a/src/main/java/com/openshift3/internal/client/model/Pod.java b/src/main/java/com/openshift3/internal/client/model/Pod.java index d186b623..6a51f76d 100644 --- a/src/main/java/com/openshift3/internal/client/model/Pod.java +++ b/src/main/java/com/openshift3/internal/client/model/Pod.java @@ -1,5 +1,7 @@ package com.openshift3.internal.client.model; +import java.util.Map; + import org.jboss.dmr.ModelNode; import com.openshift3.client.IClient; @@ -7,12 +9,8 @@ public class Pod extends KubernetesResource implements IPod { - public Pod(){ - super(); - } - - public Pod(ModelNode node, IClient client) { - super(node, client); + public Pod(ModelNode node, IClient client, Map propertyKeys) { + super(node, client, propertyKeys); } diff --git a/src/main/java/com/openshift3/internal/client/model/Project.java b/src/main/java/com/openshift3/internal/client/model/Project.java index 384a2ab1..c90d6eec 100644 --- a/src/main/java/com/openshift3/internal/client/model/Project.java +++ b/src/main/java/com/openshift3/internal/client/model/Project.java @@ -10,6 +10,7 @@ import java.util.ArrayList; import java.util.List; +import java.util.Map; import org.jboss.dmr.ModelNode; @@ -17,24 +18,18 @@ import com.openshift3.client.ResourceKind; import com.openshift3.client.model.IProject; -public class Project extends OpenShiftResource implements IProject{ +public class Project extends KubernetesResource implements IProject{ - - public Project() { - this(new ModelNode(), null); - set("kind", ResourceKind.Project.toString()); - } - - public Project(ModelNode node, IClient client) { - super(node, client); + public Project(ModelNode node, IClient client, Map propertyKeys) { + super(node, client, propertyKeys); } public String getDisplayName(){ - return asString("displayName"); + return asString(PROJECT_DISPLAY_NAME); } public void setDisplayName(String name) { - get("displayName").set(name); + set(PROJECT_DISPLAY_NAME, name); } public List getResources(ResourceKind kind){ diff --git a/src/main/java/com/openshift3/internal/client/model/ReplicationController.java b/src/main/java/com/openshift3/internal/client/model/ReplicationController.java index 91cdd4a2..0d787749 100644 --- a/src/main/java/com/openshift3/internal/client/model/ReplicationController.java +++ b/src/main/java/com/openshift3/internal/client/model/ReplicationController.java @@ -17,21 +17,18 @@ public class ReplicationController extends KubernetesResource implements IReplicationController{ - private static final String DESIRED_STATE = "desiredState"; - private static final String [] DESIRED_REPLICA_COUNT = {DESIRED_STATE, "replicas"}; - private static final String [] REPLICA_SELECTOR = {DESIRED_STATE, "replicaSelector"}; - public ReplicationController(ModelNode node, IClient client) { - super(node, client); + public ReplicationController(ModelNode node, IClient client, Map propertyKeys) { + super(node, client, propertyKeys); } @Override public int getReplicaCount() { - return asInt(DESIRED_REPLICA_COUNT); + return asInt(REPLICATION_CONTROLLER_REPLICA_COUNT); } @Override public Map getSelector() { - return asMap(REPLICA_SELECTOR); + return asMap(REPLICATION_CONTROLLER_REPLICA_SELECTOR); } } diff --git a/src/main/java/com/openshift3/internal/client/model/ResourcePropertiesRegistry.java b/src/main/java/com/openshift3/internal/client/model/ResourcePropertiesRegistry.java new file mode 100644 index 00000000..7c6484c8 --- /dev/null +++ b/src/main/java/com/openshift3/internal/client/model/ResourcePropertiesRegistry.java @@ -0,0 +1,154 @@ +/******************************************************************************* + * Copyright (c) 2015 Red Hat, Inc. Distributed under license by Red Hat, Inc. + * All rights reserved. This program is made available under the terms of the + * Eclipse Public License v1.0 which accompanies this distribution, and is + * available at http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: Red Hat, Inc. + ******************************************************************************/ +package com.openshift3.internal.client.model; + +import java.util.HashMap; +import java.util.Map; + +import com.openshift3.client.ResourceKind; +import com.openshift3.internal.client.APIModelVersion; +import com.openshift3.internal.client.KubernetesAPIVersion; +import com.openshift3.internal.client.OpenShiftAPIVersion; + +/** + * Registry of keys to property paths by version for each API resource type + */ +@SuppressWarnings("serial") +public final class ResourcePropertiesRegistry implements ResourcePropertyKeys { + + private static ResourcePropertiesRegistry instance; + + private final Map> versionPropertyMap = new HashMap>(); + private static final Map V1BETA1_KUBERNETES_MAP = new HashMap(){{ + put(ANNOTATIONS, new String [] {"annotations"}); + put(APIVERSION, new String [] {"apiVersion"}); + put(CREATION_TIMESTAMP, new String [] {"creationTimestamp"}); + put(LABELS, new String [] {"labels"}); + put(NAME , new String [] {"id"}); + put(NAMESPACE, new String [] {"namespace"}); + + put(REPLICATION_CONTROLLER_REPLICA_COUNT, new String [] {"desiredState", "replicas"}); + put(REPLICATION_CONTROLLER_REPLICA_SELECTOR, new String [] {"desiredState", "replicaSelector"}); + put(SERVICE_CONTAINER_PORT, new String [] {"containerPort"}); + put(SERVICE_PORT, new String [] {"port"}); + put(SERVICE_SELECTOR, new String [] {"selector"}); + put(SERVICE_PORTALIP, new String [] {"portalIP"}); + put(STATUS_MESSAGE, new String [] {"message"}); + }}; + + private static final Map V1BETA1_OPENSHIFT_MAP = new HashMap(){{ + put(ANNOTATIONS, new String [] {"metadata", "annotations"}); + put(CREATION_TIMESTAMP, new String [] {"metadata", "creationTimestamp"}); + put(LABELS, new String [] {"metadata", "labels"}); + put(NAME , new String [] {"metadata", "name"}); + put(NAMESPACE, new String [] {"metadata", "namespace"}); + + put(BUILDCONFIG_SOURCE_URI, new String[]{"parameters","source","git","uri"}); + put(DEPLOYMENTCONFIG_CONTAINERS, new String[]{"template","controllerTemplate","podTemplate","desiredState","manifest","containers"}); + put(DEPLOYMENTCONFIG_REPLICAS, new String[]{"template","controllerTemplate","replicas"}); + put(DEPLOYMENTCONFIG_TRIGGERS, new String[]{"triggers"}); + put(IMAGEREPO_DOCKER_IMAGE_REPO, new String[]{"dockerImageRepository"}); + put(PROJECT_DISPLAY_NAME, new String[]{"displayName"}); + }}; + + private ResourcePropertiesRegistry(){ + versionPropertyMap.put(new VersionKey(KubernetesAPIVersion.v1beta1, ResourceKind.Pod), V1BETA1_KUBERNETES_MAP); + versionPropertyMap.put(new VersionKey(KubernetesAPIVersion.v1beta1, ResourceKind.ReplicationController), V1BETA1_KUBERNETES_MAP); + versionPropertyMap.put(new VersionKey(KubernetesAPIVersion.v1beta1, ResourceKind.Service), V1BETA1_KUBERNETES_MAP); + versionPropertyMap.put(new VersionKey(KubernetesAPIVersion.v1beta1, ResourceKind.Status), V1BETA1_KUBERNETES_MAP); + + versionPropertyMap.put(new VersionKey(OpenShiftAPIVersion.v1beta1, ResourceKind.BuildConfig), V1BETA1_OPENSHIFT_MAP); + versionPropertyMap.put(new VersionKey(OpenShiftAPIVersion.v1beta1, ResourceKind.DeploymentConfig), V1BETA1_OPENSHIFT_MAP); + versionPropertyMap.put(new VersionKey(OpenShiftAPIVersion.v1beta1, ResourceKind.ImageRepository), V1BETA1_OPENSHIFT_MAP); + versionPropertyMap.put(new VersionKey(OpenShiftAPIVersion.v1beta1, ResourceKind.Project), V1BETA1_OPENSHIFT_MAP); + } + + public static final ResourcePropertiesRegistry getInstance(){ + if(instance == null){ + instance = new ResourcePropertiesRegistry(); + } + return instance; + } + + /** + * Retrieve a given resource property map for a given version + */ + public Map get(final String apiVersion, final ResourceKind kind) { + final VersionKey key = new VersionKey(apiVersion, kind); + if(!versionPropertyMap.containsKey(key)){ + throw new RuntimeException(String.format("Version '%s' not supported for kind '%s'", apiVersion, kind)); + } + return versionPropertyMap.get(key); + } + + /** + * The maximum Kubernetes API supported by this client + * @return + */ + public KubernetesAPIVersion getMaxSupportedKubernetesVersion(){ + return KubernetesAPIVersion.v1beta1; + } + + /** + * The maximum OpenShift API supported by this client + * @return + */ + public OpenShiftAPIVersion getMaxSupportedOpenShiftVersion(){ + return OpenShiftAPIVersion.v1beta1; + } + + private static class VersionKey { + private String version; + private ResourceKind kind; + + VersionKey(APIModelVersion version, ResourceKind kind){ + this(version.toString(), kind); + } + + VersionKey(String version, ResourceKind kind){ + this.version = version.toString(); + this.kind = kind; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + + ((kind == null) ? 0 : kind.hashCode()); + result = prime * result + + ((version == null) ? 0 : version.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + VersionKey other = (VersionKey) obj; + if (kind == null) { + if (other.kind != null) + return false; + } else if (!kind.equals(other.kind)) + return false; + if (version == null) { + if (other.version != null) + return false; + } else if (!version.equals(other.version)) + return false; + return true; + } + + } + +} diff --git a/src/main/java/com/openshift3/internal/client/model/ResourcePropertyKeys.java b/src/main/java/com/openshift3/internal/client/model/ResourcePropertyKeys.java new file mode 100644 index 00000000..09b2230d --- /dev/null +++ b/src/main/java/com/openshift3/internal/client/model/ResourcePropertyKeys.java @@ -0,0 +1,38 @@ +/******************************************************************************* + * Copyright (c) 2015 Red Hat, Inc. Distributed under license by Red Hat, Inc. + * All rights reserved. This program is made available under the terms of the + * Eclipse Public License v1.0 which accompanies this distribution, and is + * available at http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: Red Hat, Inc. + ******************************************************************************/ +package com.openshift3.internal.client.model; + +/** + * Keys used to determine where a given property is for a given resource + */ +public interface ResourcePropertyKeys { + static final String ANNOTATIONS = "annotations"; + static final String APIVERSION = "apiversion"; + static final String CREATION_TIMESTAMP = "creationTimestamp"; + static final String LABELS = "labels"; + static final String NAME = "name"; + static final String NAMESPACE = "namespace"; + + static final String REPLICATION_CONTROLLER_REPLICA_COUNT = "replicationcontroller.replicacount"; + static final String REPLICATION_CONTROLLER_REPLICA_SELECTOR = "replicationcontroller.selector"; + + static final String SERVICE_CONTAINER_PORT = "service.containerport"; + static final String SERVICE_PORT = "service.port"; + static final String SERVICE_SELECTOR = "service.selector"; + static final String SERVICE_PORTALIP = "service.portalIP"; + + static final String STATUS_MESSAGE = "status.message"; + + static final String BUILDCONFIG_SOURCE_URI = "buildconfig.sourceuri"; + static final String DEPLOYMENTCONFIG_CONTAINERS = "deploymentconfig.containers"; + static final String DEPLOYMENTCONFIG_REPLICAS = "deploymentconfig.replicas"; + static final String DEPLOYMENTCONFIG_TRIGGERS = "deploymentconfig.triggers"; + static final String IMAGEREPO_DOCKER_IMAGE_REPO = "imagerepo.dockerimagerepo"; + static final String PROJECT_DISPLAY_NAME = "project.displayname"; +} diff --git a/src/main/java/com/openshift3/internal/client/model/Service.java b/src/main/java/com/openshift3/internal/client/model/Service.java index 1a808d87..c965381b 100644 --- a/src/main/java/com/openshift3/internal/client/model/Service.java +++ b/src/main/java/com/openshift3/internal/client/model/Service.java @@ -9,7 +9,6 @@ package com.openshift3.internal.client.model; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import java.util.Map; @@ -23,31 +22,23 @@ public class Service extends KubernetesResource implements IService { - private static String [] SELECTOR = {"selector"}; - - public Service (IClient client) { - this(new ModelNode(), client); - setApiVersion("v1beta1"); - set("kind", ResourceKind.Service.toString()); - } - - public Service(ModelNode node, IClient client) { - super(node, client); + public Service(ModelNode node, IClient client, Map propertyKeys) { + super(node, client, propertyKeys); } @Override public void setPort(int port){ - set("port", port); + set(SERVICE_PORT, port); } @Override public int getPort(){ - return asInt("port"); + return asInt(SERVICE_PORT); } @Override public Map getSelector(){ - return asMap(SELECTOR); + return asMap(SERVICE_SELECTOR); } @Override @@ -56,30 +47,28 @@ public void setSelector(Map selector) { for (Map.Entry entry : selector.entrySet()) { node.get(entry.getKey()).set(entry.getValue()); } - getNode().get(SELECTOR).set(node); + get(SERVICE_SELECTOR).set(node); } @Override public void setSelector(String key, String value) { - String[] path = Arrays.copyOf(SELECTOR, SELECTOR.length + 1); - path[SELECTOR.length] = key; - getNode().get(path).set(value); + get(SERVICE_SELECTOR).get(key).set(value); } @Override public void setContainerPort(int port){ - set("containerPort", port); + set(SERVICE_CONTAINER_PORT, port); } @Override public int getContainerPort(){ - return asInt("containerPort"); + return asInt(SERVICE_CONTAINER_PORT); } @Override public String getPortalIP() { - return asString("portalIP"); + return asString(SERVICE_PORTALIP); } @Override diff --git a/src/main/java/com/openshift3/internal/client/model/Status.java b/src/main/java/com/openshift3/internal/client/model/Status.java index fe7d1848..11cc8c41 100644 --- a/src/main/java/com/openshift3/internal/client/model/Status.java +++ b/src/main/java/com/openshift3/internal/client/model/Status.java @@ -8,6 +8,8 @@ ******************************************************************************/ package com.openshift3.internal.client.model; +import java.util.Map; + import org.jboss.dmr.ModelNode; import com.openshift3.client.IClient; @@ -15,8 +17,8 @@ public class Status extends KubernetesResource implements IStatus{ - public Status(ModelNode node, IClient client) { - super(node, client); + public Status(ModelNode node, IClient client, Map propertyKeys) { + super(node, client, propertyKeys); } public Status(String json) { @@ -24,7 +26,7 @@ public Status(String json) { } public String getMessage(){ - return getNode().get("message").asString(); + return asString(STATUS_MESSAGE); } } diff --git a/src/test/java/com/openshift3/client/DefaultClientIntegrationTest.java b/src/test/java/com/openshift3/client/DefaultClientIntegrationTest.java index b736cbe7..a0bec60a 100644 --- a/src/test/java/com/openshift3/client/DefaultClientIntegrationTest.java +++ b/src/test/java/com/openshift3/client/DefaultClientIntegrationTest.java @@ -23,6 +23,8 @@ import com.openshift3.client.model.IResource; import com.openshift3.client.model.IService; import com.openshift3.internal.client.DefaultClient; +import com.openshift3.internal.client.IResourceFactory; +import com.openshift3.internal.client.ResourceFactory; import com.openshift3.internal.client.model.Project; import com.openshift3.internal.client.model.Service; @@ -33,16 +35,20 @@ public class DefaultClientIntegrationTest { @Test public void testResourceLifeCycle() throws MalformedURLException { - IClient client = new DefaultClient(new URL("http://localhost:8080")); - IProject project = new Project(); + DefaultClient client = new DefaultClient(new URL("http://localhost:8080")); + LOG.debug(String.format("Supported Kubernetes Versions: %s", client.getKubernetesVersions())); + LOG.debug(String.format("Supported OpenShift Versions: %s", client.getOpenShiftVersions())); + IResourceFactory factory = new ResourceFactory(client); + + IProject project = factory.create("v1beta1", ResourceKind.Project); project.setName("firstproject"); LOG.debug(String.format("Stubbing project: %s", project)); - IProject other = new Project(); + IProject other = factory.create("v1beta1", ResourceKind.Project); other.setName("other"); LOG.debug(String.format("Stubbing project: %s", project)); - IService service = new Service(client); + IService service = factory.create("v1beta1", ResourceKind.Service); service.setNamespace(project.getName()); //this will be the project's namespace service.setName("some-service"); service.setContainerPort(6767); @@ -50,7 +56,7 @@ public void testResourceLifeCycle() throws MalformedURLException { service.setSelector("name", "barpod"); LOG.debug(String.format("Stubbing service: %s", service)); - Service otherService = new Service(client); + Service otherService = factory.create("v1beta1", ResourceKind.Service); otherService.setNamespace("someothernamespace"); //this will be the project's namespace otherService.setName("some-other-service"); otherService.setContainerPort(8787); diff --git a/src/test/java/com/openshift3/internal/client/DefaultClientTest.java b/src/test/java/com/openshift3/internal/client/DefaultClientTest.java index e8ce205c..2a9741bd 100644 --- a/src/test/java/com/openshift3/internal/client/DefaultClientTest.java +++ b/src/test/java/com/openshift3/internal/client/DefaultClientTest.java @@ -25,27 +25,29 @@ public class DefaultClientTest { private ModelNode response; private Pod podFrontEnd; private Pod podBackEnd; + private IResourceFactory factory; + private URL baseUrl; private void givenAPodList(){ - - podFrontEnd = new Pod(); + podFrontEnd = factory.create("v1beta1", ResourceKind.Pod); podFrontEnd.setName("frontend"); podFrontEnd.setNamespace("aNamespace"); podFrontEnd.addLabel("name", "frontend"); podFrontEnd.addLabel("env", "production"); - podBackEnd = new Pod(); + podBackEnd = factory.create("v1beta1", ResourceKind.Pod); podBackEnd.setName("backend"); podBackEnd.setNamespace("aNamespace"); podBackEnd.addLabel("name", "backend"); podBackEnd.addLabel("env", "production"); - Pod otherPod = new Pod(); + Pod otherPod = factory.create("v1beta1", ResourceKind.Pod); otherPod.setName("other"); otherPod.setNamespace("aNamespace"); otherPod.addLabel("env", "production"); response = new ModelNode(); + response.get("apiVersion").set("v1beta1"); response.get("kind").set("PodList"); ModelNode items = response.get("items"); items.add(podFrontEnd.getNode()); @@ -55,16 +57,23 @@ private void givenAPodList(){ private void givenAClient() throws MalformedURLException{ httpClient = mock(IHttpClient.class); - client = new DefaultClient(new URL("http://myopenshift"), httpClient); + client = new DefaultClient(baseUrl, httpClient); + factory = new ResourceFactory(client); } @Before public void setUp() throws Exception{ + baseUrl = new URL("http://myopenshift"); + URL kubeApi = new URL(baseUrl, "api"); + URL osApi = new URL(baseUrl, "osapi"); givenAClient(); givenAPodList(); when(httpClient.get(any(URL.class), anyInt())) .thenReturn(response.toJSONString(false)); - + when(httpClient.get(eq(kubeApi), anyInt())) + .thenReturn("{\"versions\": [ \"v1beta1\"]}"); + when(httpClient.get(eq(osApi), anyInt())) + .thenReturn("{\"versions\": [ \"v1beta1\"]}"); } @SuppressWarnings("serial") @Test diff --git a/src/test/java/com/openshift3/internal/client/capability/server/DefaultImageRegistryHostingTest.java b/src/test/java/com/openshift3/internal/client/capability/server/DefaultImageRegistryHostingTest.java index cd5b560d..ab2bcd8e 100644 --- a/src/test/java/com/openshift3/internal/client/capability/server/DefaultImageRegistryHostingTest.java +++ b/src/test/java/com/openshift3/internal/client/capability/server/DefaultImageRegistryHostingTest.java @@ -21,7 +21,8 @@ import com.openshift3.client.ResourceKind; import com.openshift3.client.capability.server.IImageRegistryHosting; import com.openshift3.client.model.IService; -import com.openshift3.internal.client.model.Service; +import com.openshift3.internal.client.IResourceFactory; +import com.openshift3.internal.client.ResourceFactory; import com.openshift3.internal.client.model.Status; public class DefaultImageRegistryHostingTest { @@ -33,7 +34,8 @@ public class DefaultImageRegistryHostingTest { @Before public void setUp(){ client = mock(IClient.class); - service = new Service(client); + IResourceFactory factory = new ResourceFactory(client); + service = factory.create("v1beta1", ResourceKind.Service); capability = new DefaultImageRegistryHosting(client); } diff --git a/src/test/java/com/openshift3/internal/client/model/KubernetesResourceTest.java b/src/test/java/com/openshift3/internal/client/model/KubernetesResourceTest.java index d53359b7..4ffbc3bb 100644 --- a/src/test/java/com/openshift3/internal/client/model/KubernetesResourceTest.java +++ b/src/test/java/com/openshift3/internal/client/model/KubernetesResourceTest.java @@ -14,6 +14,7 @@ import org.junit.Before; import org.junit.Test; +import com.openshift3.client.ResourceKind; import com.openshift3.client.capability.resources.IDeploymentTraceability; import com.openshift3.client.capability.resources.ITemplateTraceability; @@ -30,7 +31,7 @@ public void setup(){ node = new ModelNode(); node.get("annotations").set(annotations); - resource = new KubernetesResource(node, null); + resource = new KubernetesResource(node, null, ResourcePropertiesRegistry.getInstance().get("v1beta1", ResourceKind.Service)); } @Test diff --git a/src/test/java/com/openshift3/internal/client/model/ServiceTest.java b/src/test/java/com/openshift3/internal/client/model/ServiceTest.java index d82801e5..a705c260 100644 --- a/src/test/java/com/openshift3/internal/client/model/ServiceTest.java +++ b/src/test/java/com/openshift3/internal/client/model/ServiceTest.java @@ -10,6 +10,8 @@ import com.openshift3.client.ResourceKind; import com.openshift3.client.model.IPod; import com.openshift3.client.model.IService; +import com.openshift3.internal.client.IResourceFactory; +import com.openshift3.internal.client.ResourceFactory; public class ServiceTest { @@ -20,7 +22,8 @@ public void testGetPods() { IClient client = mock(IClient.class); when(client.list(any(ResourceKind.class), anyString(), anyMap())) .thenReturn(new ArrayList()); - IService service = new Service(client); + IResourceFactory factory = new ResourceFactory(client); + IService service = factory.create("v1beta1", ResourceKind.Service); service.addLabel("bar","foo"); service.setSelector("foo", "bar"); diff --git a/src/test/java/com/openshift3/internal/client/model/V1Beta1ServiceTest.java b/src/test/java/com/openshift3/internal/client/model/V1Beta1ServiceTest.java index d4c7e8bb..8fdcd831 100644 --- a/src/test/java/com/openshift3/internal/client/model/V1Beta1ServiceTest.java +++ b/src/test/java/com/openshift3/internal/client/model/V1Beta1ServiceTest.java @@ -12,6 +12,7 @@ import com.openshift.client.utils.Samples; import com.openshift3.client.IClient; +import com.openshift3.client.ResourceKind; import com.openshift3.client.model.IService; /** @@ -27,7 +28,7 @@ public class V1Beta1ServiceTest{ public void setUp(){ IClient client = mock(IClient.class); ModelNode node = ModelNode.fromJSONString(Samples.V1BETA1_SERVICE.getContentAsString()); - service = new Service(node, client); + service = new Service(node, client, ResourcePropertiesRegistry.getInstance().get("v1beta1", ResourceKind.Service)); } @Test