JCLOUDS-1206: Replace user metadata by byte[] userData

This commit is contained in:
Fritz Elfert 2016-11-26 12:22:44 +01:00 committed by Ignasi Barrera
parent d993501309
commit 3ae2213a7d
4 changed files with 60 additions and 16 deletions

View File

@ -26,7 +26,6 @@ import static com.google.common.collect.Sets.newHashSet;
import static org.jclouds.compute.config.ComputeServiceProperties.TIMEOUT_NODE_RUNNING; import static org.jclouds.compute.config.ComputeServiceProperties.TIMEOUT_NODE_RUNNING;
import static org.jclouds.compute.config.ComputeServiceProperties.TIMEOUT_NODE_SUSPENDED; import static org.jclouds.compute.config.ComputeServiceProperties.TIMEOUT_NODE_SUSPENDED;
import static org.jclouds.compute.config.ComputeServiceProperties.TIMEOUT_NODE_TERMINATED; import static org.jclouds.compute.config.ComputeServiceProperties.TIMEOUT_NODE_TERMINATED;
import static org.jclouds.compute.util.ComputeServiceUtils.metadataAndTagsAsCommaDelimitedValue;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -83,6 +82,17 @@ public class DigitalOcean2ComputeServiceAdapter implements ComputeServiceAdapter
this.json = json; this.json = json;
} }
private void setUserDataIfSupported(Template template, CreateDropletOptions.Builder options, String userData) {
@SuppressWarnings("unchecked")
List<String> regionFeatures = (List<String>) template.getLocation().getMetadata().get("features");
if (regionFeatures.contains("metadata")) {
options.userData(userData);
} else {
logger.debug(">> region %s does not support metadata, ignoring provided user data", template.getLocation()
.getId());
}
}
@Override @Override
public NodeAndInitialCredentials<Droplet> createNodeWithGroupEncodedIntoName(String group, final String name, public NodeAndInitialCredentials<Droplet> createNodeWithGroupEncodedIntoName(String group, final String name,
Template template) { Template template) {
@ -96,16 +106,15 @@ public class DigitalOcean2ComputeServiceAdapter implements ComputeServiceAdapter
options.addSshKeyIds(templateOptions.getSshKeyIds()); options.addSshKeyIds(templateOptions.getSshKeyIds());
} }
Map<String, String> metadataAndTags = metadataAndTagsAsCommaDelimitedValue(templateOptions); // In DigitalOcean, user_data is a SINGLE string, NOT a map!
if (!metadataAndTags.isEmpty()) { // Encoding tags or anything else than user_data in here breaks their functionality.
@SuppressWarnings("unchecked") if (null != templateOptions.getUserData()) {
List<String> regionFeatures = (List<String>) template.getLocation().getMetadata().get("features"); setUserDataIfSupported(template, options, new String(templateOptions.getUserData()));
if (regionFeatures.contains("metadata")) {
options.userData(json.toJson(metadataAndTags));
} else {
logger.debug(">> region %s does not support metadata, ignoring provided user data", template.getLocation()
.getId());
} }
// Backwards compatible variant, getting userData from userMetaData map.
Map<String, String> metadataAndTags = templateOptions.getUserMetadata();
if (null != metadataAndTags.get("user_data")) {
setUserDataIfSupported(template, options, metadataAndTags.get("user_data"));
} }
DropletCreate dropletCreated = api.dropletApi().create(name, DropletCreate dropletCreated = api.dropletApi().create(name,

View File

@ -19,6 +19,7 @@ package org.jclouds.digitalocean2.compute.options;
import static com.google.common.base.Objects.equal; import static com.google.common.base.Objects.equal;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Arrays;
import java.util.Set; import java.util.Set;
import org.jclouds.compute.options.TemplateOptions; import org.jclouds.compute.options.TemplateOptions;
@ -35,6 +36,7 @@ public class DigitalOcean2TemplateOptions extends TemplateOptions implements Clo
private boolean privateNetworking = false; private boolean privateNetworking = false;
private boolean backupsEnabled = false; private boolean backupsEnabled = false;
private boolean autoCreateKeyPair = true; private boolean autoCreateKeyPair = true;
private byte[] userData;
/** /**
* Enables a private network interface if the region supports private networking. * Enables a private network interface if the region supports private networking.
@ -68,6 +70,14 @@ public class DigitalOcean2TemplateOptions extends TemplateOptions implements Clo
return this; return this;
} }
/**
* Sets the userData member.
*/
public DigitalOcean2TemplateOptions userData(byte[] userData) {
this.userData = userData;
return this;
}
public Set<Integer> getSshKeyIds() { public Set<Integer> getSshKeyIds() {
return sshKeyIds; return sshKeyIds;
} }
@ -84,6 +94,10 @@ public class DigitalOcean2TemplateOptions extends TemplateOptions implements Clo
return autoCreateKeyPair; return autoCreateKeyPair;
} }
public byte[] getUserData() {
return userData;
}
@Override @Override
public DigitalOcean2TemplateOptions clone() { public DigitalOcean2TemplateOptions clone() {
DigitalOcean2TemplateOptions options = new DigitalOcean2TemplateOptions(); DigitalOcean2TemplateOptions options = new DigitalOcean2TemplateOptions();
@ -100,12 +114,16 @@ public class DigitalOcean2TemplateOptions extends TemplateOptions implements Clo
eTo.backupsEnabled(backupsEnabled); eTo.backupsEnabled(backupsEnabled);
eTo.autoCreateKeyPair(autoCreateKeyPair); eTo.autoCreateKeyPair(autoCreateKeyPair);
eTo.sshKeyIds(sshKeyIds); eTo.sshKeyIds(sshKeyIds);
if (null != getUserData()) {
eTo.userData(getUserData());
}
} }
} }
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hashCode(super.hashCode(), backupsEnabled, privateNetworking, autoCreateKeyPair, sshKeyIds); return Objects.hashCode(super.hashCode(), backupsEnabled, privateNetworking, autoCreateKeyPair, sshKeyIds,
Arrays.hashCode(userData));
} }
@Override @Override
@ -122,7 +140,8 @@ public class DigitalOcean2TemplateOptions extends TemplateOptions implements Clo
DigitalOcean2TemplateOptions other = (DigitalOcean2TemplateOptions) obj; DigitalOcean2TemplateOptions other = (DigitalOcean2TemplateOptions) obj;
return super.equals(other) && equal(this.backupsEnabled, other.backupsEnabled) return super.equals(other) && equal(this.backupsEnabled, other.backupsEnabled)
&& equal(this.privateNetworking, other.privateNetworking) && equal(this.privateNetworking, other.privateNetworking)
&& equal(this.autoCreateKeyPair, other.autoCreateKeyPair) && equal(this.sshKeyIds, other.sshKeyIds); && equal(this.autoCreateKeyPair, other.autoCreateKeyPair) && equal(this.sshKeyIds, other.sshKeyIds)
&& Arrays.equals(this.userData, other.userData);
} }
@Override @Override
@ -134,6 +153,7 @@ public class DigitalOcean2TemplateOptions extends TemplateOptions implements Clo
toString.add("sshKeyIds", sshKeyIds); toString.add("sshKeyIds", sshKeyIds);
} }
toString.add("autoCreateKeyPair", autoCreateKeyPair); toString.add("autoCreateKeyPair", autoCreateKeyPair);
toString.add("userData", userData);
return toString; return toString;
} }
@ -170,5 +190,13 @@ public class DigitalOcean2TemplateOptions extends TemplateOptions implements Clo
DigitalOcean2TemplateOptions options = new DigitalOcean2TemplateOptions(); DigitalOcean2TemplateOptions options = new DigitalOcean2TemplateOptions();
return options.autoCreateKeyPair(autoCreateKeyPair); return options.autoCreateKeyPair(autoCreateKeyPair);
} }
/**
* @see DigitalOcean2TemplateOptions#userData
*/
public static DigitalOcean2TemplateOptions userData(byte[] userData) {
DigitalOcean2TemplateOptions options = new DigitalOcean2TemplateOptions();
return options.userData(userData);
}
} }
} }

View File

@ -55,12 +55,12 @@ public class DigitalOcean2ComputeServiceLiveTest extends BaseComputeServiceLiveT
@Override @Override
protected void checkTagsInNodeEquals(NodeMetadata node, ImmutableSet<String> tags) { protected void checkTagsInNodeEquals(NodeMetadata node, ImmutableSet<String> tags) {
// We encode the tags in the user data but the DigitalOcean API does not return it // We do not support tags yet.
} }
@Override @Override
protected void checkUserMetadataContains(NodeMetadata node, ImmutableMap<String, String> userMetadata) { protected void checkUserMetadataContains(NodeMetadata node, ImmutableMap<String, String> userMetadata) {
// The DigitalOcean API does not return the user data // The DigitalOcean API does not support user metadata.
} }
} }

View File

@ -49,4 +49,11 @@ public class DigitalOcean2TemplateOptionsTest {
TemplateOptions options = new DigitalOcean2TemplateOptions().autoCreateKeyPair(false); TemplateOptions options = new DigitalOcean2TemplateOptions().autoCreateKeyPair(false);
assertEquals(options.as(DigitalOcean2TemplateOptions.class).getAutoCreateKeyPair(), false); assertEquals(options.as(DigitalOcean2TemplateOptions.class).getAutoCreateKeyPair(), false);
} }
@Test
public void testUserData() {
byte[] userData = "Lorem ipsum".getBytes();
TemplateOptions options = new DigitalOcean2TemplateOptions().userData(userData);
assertEquals(options.as(DigitalOcean2TemplateOptions.class).getUserData(), userData);
}
} }