More changes in support of Chef environments

- Add and update constructors for org.jclouds.chef.domain.Node
- Make Node.chefEnvironment nullable
- Add JavaDoc indicating environments apply since Chef 0.10
- Update unit tests
This commit is contained in:
Jeff Gehlbach 2012-07-25 14:10:23 -04:00 committed by Adrian Cole
parent a75d816ad7
commit ef1dc56484
9 changed files with 45 additions and 14 deletions

View File

@ -21,6 +21,8 @@ package org.jclouds.chef.domain;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;
import org.jclouds.domain.JsonBall;
import com.google.common.collect.Iterables;
@ -43,7 +45,12 @@ public class Node {
private Map<String, JsonBall> automatic = Maps.newLinkedHashMap();
@SerializedName("run_list")
private List<String> runList = Lists.newArrayList();
/**
* @since chef 0.10
*/
@SerializedName("chef_environment")
@Nullable
private String chefEnvironment;
// internal
@ -51,8 +58,18 @@ public class Node {
private String _jsonClass = "Chef::Node";
public Node(String name, Map<String, JsonBall> normal, Map<String, JsonBall> override,
Map<String, JsonBall> defaultA, Map<String, JsonBall> automatic, Iterable<String> runList) {
Map<String, JsonBall> defaultA, Map<String, JsonBall> automatic, Iterable<String> runList) {
this(name, normal, override, defaultA, automatic, runList, null);
}
/**
* @since chef 0.10
*/
public Node(String name, Map<String, JsonBall> normal, Map<String, JsonBall> override,
Map<String, JsonBall> defaultA, Map<String, JsonBall> automatic, Iterable<String> runList,
String chefEnvironment) {
this.name = name;
this.chefEnvironment = chefEnvironment;
this.normal.putAll(normal);
this.override.putAll(override);
this.defaultA.putAll(defaultA);
@ -67,7 +84,15 @@ public class Node {
}
public Node(String name, Iterable<String> runList) {
this(name, runList, "_default");
}
/**
* @since chef 0.10
*/
public Node(String name, Iterable<String> runList, String chefEnvironment) {
this.name = name;
this.chefEnvironment = chefEnvironment;
Iterables.addAll(this.runList, runList);
}
@ -100,6 +125,9 @@ public class Node {
return runList;
}
/**
* @since chef 0.10
*/
public String getChefEnvironment() {
return chefEnvironment;
}

View File

@ -71,6 +71,6 @@ public class CreateNodeAndPopulateAutomaticAttributesImpl implements CreateNodeA
@Override
public Node execute(String nodeName, Iterable<String> runList) {
return execute(new Node(nodeName, runList));
return execute(new Node(nodeName, runList, "_default"));
}
}

View File

@ -341,13 +341,13 @@ public class ChefAsyncApiTest extends BaseAsyncApiTest<ChefAsyncApi> {
public void testCreateNode() throws SecurityException, NoSuchMethodException, IOException {
Method method = ChefAsyncApi.class.getMethod("createNode", Node.class);
GeneratedHttpRequest httpRequest = processor.createRequest(method, new Node("testnode",
ImmutableSet.of("recipe[java]")));
ImmutableSet.of("recipe[java]"), "_default"));
assertRequestLineEquals(httpRequest, "POST http://localhost:4000/nodes HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\nX-Chef-Version: " + ChefAsyncApi.VERSION + "-test\n");
assertPayloadEquals(
httpRequest,
"{\"name\":\"testnode\",\"normal\":{},\"override\":{},\"default\":{},\"automatic\":{},\"run_list\":[\"recipe[java]\"],\"json_class\":\"Chef::Node\",\"chef_type\":\"node\"}",
"{\"name\":\"testnode\",\"normal\":{},\"override\":{},\"default\":{},\"automatic\":{},\"run_list\":[\"recipe[java]\"],\"chef_environment\":\"_default\",\"json_class\":\"Chef::Node\",\"chef_type\":\"node\"}",
"application/json", false);
assertResponseParserClassEquals(method, httpRequest, ReleasePayloadAndReturn.class);
@ -361,13 +361,13 @@ public class ChefAsyncApiTest extends BaseAsyncApiTest<ChefAsyncApi> {
public void testUpdateNode() throws SecurityException, NoSuchMethodException, IOException {
Method method = ChefAsyncApi.class.getMethod("updateNode", Node.class);
GeneratedHttpRequest httpRequest = processor.createRequest(method, new Node("testnode",
ImmutableSet.of("recipe[java]")));
ImmutableSet.of("recipe[java]"), "_default"));
assertRequestLineEquals(httpRequest, "PUT http://localhost:4000/nodes/testnode HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\nX-Chef-Version: " + ChefAsyncApi.VERSION + "-test\n");
assertPayloadEquals(
httpRequest,
"{\"name\":\"testnode\",\"normal\":{},\"override\":{},\"default\":{},\"automatic\":{},\"run_list\":[\"recipe[java]\"],\"json_class\":\"Chef::Node\",\"chef_type\":\"node\"}",
"{\"name\":\"testnode\",\"normal\":{},\"override\":{},\"default\":{},\"automatic\":{},\"run_list\":[\"recipe[java]\"],\"chef_environment\":\"_default\",\"json_class\":\"Chef::Node\",\"chef_type\":\"node\"}",
"application/json", false);
assertResponseParserClassEquals(method, httpRequest, ParseJson.class);

View File

@ -69,7 +69,7 @@ public class ParseNodeFromJsonTest {
Node node = new Node("adrian-jcloudstest", ImmutableMap.<String, JsonBall> of("tomcat6", new JsonBall(
"{\"ssl_port\":8433}")), ImmutableMap.<String, JsonBall> of(), ImmutableMap.<String, JsonBall> of(),
ImmutableMap.<String, JsonBall> of(), Collections.singleton("recipe[java]"));
ImmutableMap.<String, JsonBall> of(), Collections.singleton("recipe[java]"), "prod");
assertEquals(handler.apply(HttpResponse.builder()
.statusCode(200)

View File

@ -221,7 +221,7 @@ public abstract class BaseChefApiLiveTest<C extends Context> extends BaseChefCon
@Test(dependsOnMethods = "testCreateRole")
public void testCreateNode() throws Exception {
chefApi.deleteNode(PREFIX);
chefApi.createNode(new Node(PREFIX, Collections.singleton("role[" + PREFIX + "]")));
chefApi.createNode(new Node(PREFIX, Collections.singleton("role[" + PREFIX + "]"), "_default"));
node = chefApi.getNode(PREFIX);
// TODO check recipes
assertNotNull(node);

View File

@ -48,12 +48,13 @@ public class CreateNodeAndPopulateAutomaticAttributesImplTest {
Map<String, JsonBall> automatic = ImmutableMap.<String, JsonBall> of();
Node node = new Node("name", ImmutableSet.<String> of());
Node node = new Node("name", ImmutableSet.<String> of(), "_default");
Supplier<Map<String, JsonBall>> automaticSupplier = Suppliers.<Map<String, JsonBall>> ofInstance(automatic);
Node nodeWithAutomatic = new Node("name", ImmutableMap.<String, JsonBall> of(), ImmutableMap
.<String, JsonBall> of(), ImmutableMap.<String, JsonBall> of(), automatic, ImmutableSet.<String> of());
.<String, JsonBall> of(), ImmutableMap.<String, JsonBall> of(), automatic, ImmutableSet.<String> of(),
"_default");
node.getAutomatic().putAll(automaticSupplier.get());
chef.createNode(nodeWithAutomatic);

View File

@ -53,7 +53,7 @@ public class UpdateAutomaticAttributesOnNodeImplLiveTest extends BaseChefContext
public void testExecute() {
Set<String> runList = ImmutableSet.of("role[" + prefix + "]");
try {
context.getApi().createNode(new Node(prefix, runList));
context.getApi().createNode(new Node(prefix, runList, "_default"));
context.utils().injector().getInstance(UpdateAutomaticAttributesOnNodeImpl.class).execute(prefix);
Node node = context.getApi().getNode(prefix);
assertEquals(node.getName(), prefix);

View File

@ -49,12 +49,13 @@ public class UpdateAutomaticAttributesOnNodeImplTest {
Map<String, JsonBall> automatic = ImmutableMap.<String, JsonBall> of();
Node node = new Node("name", ImmutableSet.<String> of());
Node node = new Node("name", ImmutableSet.<String> of(), "_default");
Supplier<Map<String, JsonBall>> automaticSupplier = Suppliers.<Map<String, JsonBall>> ofInstance(automatic);
Node nodeWithAutomatic = new Node("name", ImmutableMap.<String, JsonBall> of(), ImmutableMap
.<String, JsonBall> of(), ImmutableMap.<String, JsonBall> of(), automatic, ImmutableSet.<String> of());
.<String, JsonBall> of(), ImmutableMap.<String, JsonBall> of(), automatic, ImmutableSet.<String> of(),
"_default");
expect(chef.getNode("name")).andReturn(node);
node.getAutomatic().putAll(automaticSupplier.get());

View File

@ -1 +1,2 @@
{"normal":{"tomcat6":{"ssl_port":8433}},"name":"adrian-jcloudstest","override":{},"default":{},"json_class":"Chef::Node","automatic":{},"run_list":["recipe[java]"],"chef_type":"node"}
{"normal":{"tomcat6":{"ssl_port":8433}},"name":"adrian-jcloudstest","override":{},"default":{},"json_class":"Chef::Node","automatic":{},"run_list":["recipe[java]"],"chef_type":"node","chef_environment": "prod"}