diff --git a/src/main/java/com/openshift/internal/restclient/ResourceFactory.java b/src/main/java/com/openshift/internal/restclient/ResourceFactory.java index 1f93edcd..0da70123 100644 --- a/src/main/java/com/openshift/internal/restclient/ResourceFactory.java +++ b/src/main/java/com/openshift/internal/restclient/ResourceFactory.java @@ -184,6 +184,14 @@ public T create(String version, String kind) { return (T) create(new ModelNode(), version, kind); } + @Override + @SuppressWarnings("unchecked") + public T create(String version, String kind, String name) { + T resource = (T) create(new ModelNode(), version, kind); + ((KubernetesResource)resource).setName(name); + return resource; + } + private IResource create(ModelNode node, String version, String kind) { try { node.get(APIVERSION).set(version); diff --git a/src/main/java/com/openshift/internal/restclient/model/Container.java b/src/main/java/com/openshift/internal/restclient/model/Container.java index d0435bfe..1f171526 100644 --- a/src/main/java/com/openshift/internal/restclient/model/Container.java +++ b/src/main/java/com/openshift/internal/restclient/model/Container.java @@ -59,6 +59,7 @@ public Container(ModelNode node, Map propertyKeys) { this.node = node; this.propertyKeys = propertyKeys; } + @Override public void setName(String name) { set(node, propertyKeys, NAME, name); @@ -222,6 +223,15 @@ public Set getVolumeMounts() { return volumes; } + + @Override + public IVolumeMount addVolumeMount(String name) { + ModelNode mounts = get(node, propertyKeys, VOLUMEMOUNTS); + VolumeMount volume = new VolumeMount(mounts.add()); + volume.setName(name); + return volume; + } + @Override public String toJSONString() { return super.toJson(false); diff --git a/src/main/java/com/openshift/internal/restclient/model/ReplicationController.java b/src/main/java/com/openshift/internal/restclient/model/ReplicationController.java index 4e014a86..7c449319 100644 --- a/src/main/java/com/openshift/internal/restclient/model/ReplicationController.java +++ b/src/main/java/com/openshift/internal/restclient/model/ReplicationController.java @@ -365,8 +365,21 @@ public void addVolume(IVolumeSource volumeSource) { } volList.add(ModelNode.fromJSONString(volumeSource.toJSONString())); } + + - @Override + @SuppressWarnings("unchecked") + @Override + public T addVolume(String volumetype, String name) { + ModelNode volList = get(VOLUMES); + ModelNode node = volList.add(); + node.get(volumetype).set(new ModelNode()); + IVolumeSource source = VolumeSource.create(node); + source.setName(name); + return (T) source; + } + + @Override public void setServiceAccountName(String serviceAccountName) { set(SERVICEACCOUNTNAME, serviceAccountName); } diff --git a/src/main/java/com/openshift/internal/restclient/model/volume/HostPathVolumeSource.java b/src/main/java/com/openshift/internal/restclient/model/volume/HostPathVolumeSource.java new file mode 100644 index 00000000..fc7870fc --- /dev/null +++ b/src/main/java/com/openshift/internal/restclient/model/volume/HostPathVolumeSource.java @@ -0,0 +1,42 @@ +/******************************************************************************* + * Copyright (c) 2016 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. - initial API and implementation + ******************************************************************************/ +package com.openshift.internal.restclient.model.volume; + +import static com.openshift.internal.util.JBossDmrExtentions.*; + +import org.jboss.dmr.ModelNode; + +import com.openshift.restclient.model.volume.IHostPathVolumeSource; + +/** + * Implementation of a hostpath volume source + * @author jeff.cantrill + * + */ +public class HostPathVolumeSource extends VolumeSource implements IHostPathVolumeSource{ + + private static final String PATH = "hostPath.path"; + + public HostPathVolumeSource(ModelNode node) { + super(node); + } + + @Override + public String getPath() { + return asString(getNode(), getPropertyKeys(), PATH); + } + + @Override + public void setPath(String path) { + set(getNode(), getPropertyKeys(), PATH, path); + } + +} diff --git a/src/main/java/com/openshift/internal/restclient/model/volume/VolumeSource.java b/src/main/java/com/openshift/internal/restclient/model/volume/VolumeSource.java index 80233cbc..a708b156 100644 --- a/src/main/java/com/openshift/internal/restclient/model/volume/VolumeSource.java +++ b/src/main/java/com/openshift/internal/restclient/model/volume/VolumeSource.java @@ -60,6 +60,8 @@ public static IVolumeSource create(ModelNode node) { return new SecretVolumeSource(node); } else if (node.has(VolumeType.PERSISTENT_VOLUME_CLAIM)) { return new PersistentVolumeClaimVolumeSource(node); + } else if (node.has(VolumeType.HOST_PATH)) { + return new HostPathVolumeSource(node); } else { return new VolumeSource(node) {}; } diff --git a/src/main/java/com/openshift/restclient/IResourceFactory.java b/src/main/java/com/openshift/restclient/IResourceFactory.java index 36d034d2..1e2b2a6c 100644 --- a/src/main/java/com/openshift/restclient/IResourceFactory.java +++ b/src/main/java/com/openshift/restclient/IResourceFactory.java @@ -57,6 +57,16 @@ public interface IResourceFactory extends ITypeFactory{ */ T create(String version, String kind); + /** + * Create(or stub) a resource for a given version and kind and name + * @param version + * @param kind + * @param name + * + * @return + */ + T create(String version, String kind, String name); + /** * Stub out the given resource kind using a version determined by the factory * @param kind diff --git a/src/main/java/com/openshift/restclient/model/IContainer.java b/src/main/java/com/openshift/restclient/model/IContainer.java index b3f03559..a9b9f194 100644 --- a/src/main/java/com/openshift/restclient/model/IContainer.java +++ b/src/main/java/com/openshift/restclient/model/IContainer.java @@ -14,13 +14,13 @@ import java.util.Map; import java.util.Set; +import com.openshift.restclient.api.models.INameSetable; import com.openshift.restclient.images.DockerImageURI; import com.openshift.restclient.model.volume.IVolume; import com.openshift.restclient.model.volume.IVolumeMount; -public interface IContainer { +public interface IContainer extends INameSetable{ - void setName(String name); String getName(); void setImage(DockerImageURI tag); @@ -71,5 +71,13 @@ public interface IContainer { void setVolumeMounts(Set volumes); Set getVolumeMounts(); + /** + * Add a volumemount with the given name + * @param name + * @return IVolumeMount + */ + IVolumeMount addVolumeMount(String name); + + String toJSONString(); } diff --git a/src/main/java/com/openshift/restclient/model/IReplicationController.java b/src/main/java/com/openshift/restclient/model/IReplicationController.java index beab1158..ecc93a9c 100644 --- a/src/main/java/com/openshift/restclient/model/IReplicationController.java +++ b/src/main/java/com/openshift/restclient/model/IReplicationController.java @@ -186,6 +186,16 @@ public interface IReplicationController extends IResource{ * @param volumeSource The volume to add to the pod spec */ void addVolume(IVolumeSource volumeSource); + + /** + * Add a volume source of the given type with the given name. Unimplemented types + * will return a generic volumesource impl + * + * @param volumetype + * @param name + * @return + */ + T addVolume(String volumetype, String name); /** * Sets the volumes associated with the pod spec. Existing volumes will be overwritten. diff --git a/src/main/java/com/openshift/restclient/model/volume/IHostPathVolumeSource.java b/src/main/java/com/openshift/restclient/model/volume/IHostPathVolumeSource.java new file mode 100644 index 00000000..de363915 --- /dev/null +++ b/src/main/java/com/openshift/restclient/model/volume/IHostPathVolumeSource.java @@ -0,0 +1,27 @@ +/******************************************************************************* + * Copyright (c) 2016 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. - initial API and implementation + ******************************************************************************/ +package com.openshift.restclient.model.volume; + +/** + * VolumeSource for hostpath volumes of a pod + * @author jeff.cantrill + * + */ +public interface IHostPathVolumeSource extends IVolumeSource { + + /** + * Host path mapped into a pod + * @return + */ + String getPath(); + + void setPath(String path); +} diff --git a/src/test/java/com/openshift/internal/restclient/DefaultClientIntegrationTest.java b/src/test/java/com/openshift/internal/restclient/DefaultClientIntegrationTest.java index c7d49d99..6c3ea1e6 100644 --- a/src/test/java/com/openshift/internal/restclient/DefaultClientIntegrationTest.java +++ b/src/test/java/com/openshift/internal/restclient/DefaultClientIntegrationTest.java @@ -29,11 +29,18 @@ import com.openshift.restclient.OpenShiftException; import com.openshift.restclient.ResourceKind; import com.openshift.restclient.authorization.UnauthorizedException; +import com.openshift.restclient.images.DockerImageURI; import com.openshift.restclient.model.IBuildConfig; +import com.openshift.restclient.model.IContainer; +import com.openshift.restclient.model.IDeploymentConfig; import com.openshift.restclient.model.IProject; import com.openshift.restclient.model.build.IBuildConfigBuilder; +import com.openshift.restclient.model.deploy.DeploymentTriggerType; import com.openshift.restclient.model.project.IProjectRequest; import com.openshift.restclient.model.template.ITemplate; +import com.openshift.restclient.model.volume.IHostPathVolumeSource; +import com.openshift.restclient.model.volume.IVolumeMount; +import com.openshift.restclient.model.volume.VolumeType; /** * @author Jeff Cantrill diff --git a/src/test/java/com/openshift/internal/restclient/ResourceFactoryTest.java b/src/test/java/com/openshift/internal/restclient/ResourceFactoryTest.java index 6cbca45d..064307a8 100644 --- a/src/test/java/com/openshift/internal/restclient/ResourceFactoryTest.java +++ b/src/test/java/com/openshift/internal/restclient/ResourceFactoryTest.java @@ -61,4 +61,10 @@ public void testStubWithNamespace() { assertEquals("bar", service.getNamespace()); } + @Test + public void testCreateWithKindAndName() { + IService service = factory.create("v1", ResourceKind.SERVICE, "foo"); + assertEquals("foo", service.getName()); + } + } diff --git a/src/test/java/com/openshift/internal/restclient/model/v1/HostPathVolumeSourceTest.java b/src/test/java/com/openshift/internal/restclient/model/v1/HostPathVolumeSourceTest.java new file mode 100644 index 00000000..ae432ef6 --- /dev/null +++ b/src/test/java/com/openshift/internal/restclient/model/v1/HostPathVolumeSourceTest.java @@ -0,0 +1,51 @@ +/******************************************************************************* + * Copyright (c) 2016 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. - initial API and implementation + ******************************************************************************/ +package com.openshift.internal.restclient.model.v1; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import org.jboss.dmr.ModelNode; +import org.junit.Before; +import org.junit.Test; + +import com.openshift.internal.restclient.model.ModelNodeBuilder; +import com.openshift.internal.restclient.model.volume.VolumeSource; +import com.openshift.restclient.model.volume.IHostPathVolumeSource; + +public class HostPathVolumeSourceTest { + + private IHostPathVolumeSource source; + + @Before + public void setUp() throws Exception { + ModelNode node = new ModelNodeBuilder() + .set("name", "somevolumesourcename") + .set("hostPath", new ModelNodeBuilder() + .set("path", "/foo").build()).build(); + source = (IHostPathVolumeSource) VolumeSource.create(node); + } + + @Test + public void testName() { + assertThat(source.getName(), is("somevolumesourcename")); + source.setName("thenewname"); + assertThat(source.getName(), is("thenewname")); + } + + @Test + public void testPath() { + assertThat(source.getPath(), is("/foo")); + source.setPath("thenewpath"); + assertThat(source.getPath(), is("thenewpath")); + } + +} diff --git a/src/test/java/com/openshift/internal/restclient/model/v1/ReplicationControllerTest.java b/src/test/java/com/openshift/internal/restclient/model/v1/ReplicationControllerTest.java index 2fb7d2ff..1ff2a40a 100644 --- a/src/test/java/com/openshift/internal/restclient/model/v1/ReplicationControllerTest.java +++ b/src/test/java/com/openshift/internal/restclient/model/v1/ReplicationControllerTest.java @@ -294,6 +294,9 @@ public void testAddContainerAllowsContainerToBeFurtherManipulated() throws JSON container.setPorts(ports); container.setVolumeMounts(mounts); + IVolumeMount fooVolumeMount = container.addVolumeMount("foobar"); + fooVolumeMount.setMountPath("/tmp2"); + ModelNode exp = new ModelNodeBuilder() .set("name", uri.getName()) .set("image",uri.toString()) @@ -306,6 +309,10 @@ public void testAddContainerAllowsContainerToBeFurtherManipulated() throws JSON .set("mountPath", "/tmp") .set("readOnly", false) ) + .add("volumeMounts", new ModelNodeBuilder() + .set("name", "foobar") + .set("mountPath", "/tmp2") + ) .build(); JSONAssert.assertEquals(exp.toJSONString(false), container.toJSONString(), true);