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 @@ -184,6 +184,14 @@ public <T extends IResource> T create(String version, String kind) {
return (T) create(new ModelNode(), version, kind);
}

@Override
@SuppressWarnings("unchecked")
public <T extends IResource> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public Container(ModelNode node, Map<String, String[]> propertyKeys) {
this.node = node;
this.propertyKeys = propertyKeys;
}

@Override
public void setName(String name) {
set(node, propertyKeys, NAME, name);
Expand Down Expand Up @@ -222,6 +223,15 @@ public Set<IVolumeMount> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,21 @@ public void addVolume(IVolumeSource volumeSource) {
}
volList.add(ModelNode.fromJSONString(volumeSource.toJSONString()));
}



@Override
@SuppressWarnings("unchecked")
@Override
public <T extends IVolumeSource> 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dare to correct identation for great and consistent code formatting experience?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed. Modified my IDE to replace all tabs with spaces

public void setServiceAccountName(String serviceAccountName) {
set(SERVICEACCOUNTNAME, serviceAccountName);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {};
}
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/openshift/restclient/IResourceFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ public interface IResourceFactory extends ITypeFactory{
*/
<T extends IResource> 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 extends IResource> T create(String version, String kind, String name);

/**
* Stub out the given resource kind using a version determined by the factory
* @param kind
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/com/openshift/restclient/model/IContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -71,5 +71,13 @@ public interface IContainer {
void setVolumeMounts(Set<IVolumeMount> volumes);
Set<IVolumeMount> getVolumeMounts();

/**
* Add a volumemount with the given name
* @param name
* @return IVolumeMount
*/
IVolumeMount addVolumeMount(String name);


String toJSONString();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 extends IVolumeSource> T addVolume(String volumetype, String name);

/**
* Sets the volumes associated with the pod spec. Existing volumes will be overwritten.
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

}
Original file line number Diff line number Diff line change
@@ -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"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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);
Expand Down