diff --git a/plugin/src/main/java/jenkins/plugins/openstack/compute/JCloudsSlave.java b/plugin/src/main/java/jenkins/plugins/openstack/compute/JCloudsSlave.java index 7feb13f2..072b94b5 100644 --- a/plugin/src/main/java/jenkins/plugins/openstack/compute/JCloudsSlave.java +++ b/plugin/src/main/java/jenkins/plugins/openstack/compute/JCloudsSlave.java @@ -202,6 +202,7 @@ protected Object readResolve() { final Map result = new LinkedHashMap<>(); final SlaveOptions slaveOptions = getSlaveOptions(); putIfNotNullOrEmpty(result, "Network(s)", slaveOptions.getNetworkId()); + putIfNotNullOrEmpty(result, "Custom Metadata", slaveOptions.getCustomMetaData()); putIfNotNullOrEmpty(result, "Floating Ip Pool", slaveOptions.getFloatingIpPool()); putIfNotNullOrEmpty(result, "Security Groups", slaveOptions.getSecurityGroups()); putIfNotNullOrEmpty(result, "Start Timeout (ms)", slaveOptions.getStartTimeout()); diff --git a/plugin/src/main/java/jenkins/plugins/openstack/compute/JCloudsSlaveTemplate.java b/plugin/src/main/java/jenkins/plugins/openstack/compute/JCloudsSlaveTemplate.java index d61f09d5..d8cce11f 100644 --- a/plugin/src/main/java/jenkins/plugins/openstack/compute/JCloudsSlaveTemplate.java +++ b/plugin/src/main/java/jenkins/plugins/openstack/compute/JCloudsSlaveTemplate.java @@ -269,6 +269,7 @@ public boolean canProvision(final Label label) { ) throws Openstack.ActionFailed { final String serverName = getServerName(); final SlaveOptions opts = getEffectiveSlaveOptions(); + final String customMetaData = opts.getCustomMetaData(); final ServerCreateBuilder builder = Builders.server(); builder.addMetadataItem(OPENSTACK_TEMPLATE_NAME_KEY, getName()); @@ -307,6 +308,15 @@ public boolean canProvision(final Label label) { builder.networks(networks); } + String metaData = opts.getCustomMetaData(); + if (Util.fixEmpty(metaData) != null) { + if (metaData != null) { + List> metadat = TokenGroup.from(metaData, ',', '='); + LOGGER.fine("Setting metadata to " + customMetaData); + builder.addMetadataItem(metadat.get(0).get(0), metadat.get(0).get(1)); + } + } + String securityGroups = opts.getSecurityGroups(); if (Util.fixEmpty(securityGroups) != null) { LOGGER.fine("Setting security groups to " + securityGroups); diff --git a/plugin/src/main/java/jenkins/plugins/openstack/compute/SlaveOptions.java b/plugin/src/main/java/jenkins/plugins/openstack/compute/SlaveOptions.java index 0416f854..b97b3e87 100644 --- a/plugin/src/main/java/jenkins/plugins/openstack/compute/SlaveOptions.java +++ b/plugin/src/main/java/jenkins/plugins/openstack/compute/SlaveOptions.java @@ -54,12 +54,13 @@ */ public class SlaveOptions implements Describable, Serializable { private static final long serialVersionUID = -1L; - private static final SlaveOptions EMPTY = new SlaveOptions(null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null); + private static final SlaveOptions EMPTY = new SlaveOptions(null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null); // Provisioning attributes private /*final*/ @CheckForNull BootSource bootSource; private final @CheckForNull String hardwareId; private final @CheckForNull String networkId; // csv list of networkIds, in fact + private final @CheckForNull String customMetaData; // csv list of keyvalue, in fact private final @CheckForNull String userDataId; private final Integer instanceCap; private final Integer instancesMin; @@ -106,6 +107,10 @@ public class SlaveOptions implements Describable, Serializable { return networkId; } + public @CheckForNull String getCustomMetaData() { + return customMetaData; + } + public @CheckForNull String getUserDataId() { return userDataId; } @@ -165,6 +170,7 @@ public SlaveOptions(Builder b) { b.bootSource, b.hardwareId, b.networkId, + b.customMetaData, b.userDataId, b.instanceCap, b.instancesMin, @@ -188,6 +194,7 @@ public SlaveOptions( @CheckForNull BootSource bootSource, String hardwareId, String networkId, + String customMetaData, String userDataId, Integer instanceCap, Integer instancesMin, @@ -207,6 +214,7 @@ public SlaveOptions( this.bootSource = bootSource; this.hardwareId = Util.fixEmpty(hardwareId); this.networkId = Util.fixEmpty(networkId); + this.customMetaData = Util.fixEmpty(customMetaData); this.userDataId = Util.fixEmpty(userDataId); this.instanceCap = instanceCap; this.instancesMin = instancesMin; @@ -244,6 +252,7 @@ private Object readResolve() { .bootSource(_override(this.bootSource, o.bootSource)) .hardwareId(_override(this.hardwareId, o.hardwareId)) .networkId(_override(this.networkId, o.networkId)) + .customMetaData(_override(this.customMetaData, o.customMetaData)) .userDataId(_override(this.userDataId, o.userDataId)) .instanceCap(_override(this.instanceCap, o.instanceCap)) .instancesMin(_override(this.instancesMin, o.instancesMin)) @@ -275,6 +284,7 @@ private Object readResolve() { .bootSource(_erase(this.bootSource, defaults.bootSource)) .hardwareId(_erase(this.hardwareId, defaults.hardwareId)) .networkId(_erase(this.networkId, defaults.networkId)) + .customMetaData(_erase(this.customMetaData, defaults.customMetaData)) .userDataId(_erase(this.userDataId, defaults.userDataId)) .instanceCap(_erase(this.instanceCap, defaults.instanceCap)) .instancesMin(_erase(this.instancesMin, defaults.instancesMin)) @@ -307,6 +317,7 @@ public String toString() { .append("bootSource", bootSource) .append("hardwareId", hardwareId) .append("networkId", networkId) + .append("customMetaData", customMetaData) .append("userDataId", userDataId) .append("instanceCap", instanceCap) .append("instancesMin", instancesMin) @@ -336,6 +347,7 @@ public boolean equals(Object o) { if (!Objects.equals(bootSource, that.bootSource)) return false; if (!Objects.equals(hardwareId, that.hardwareId)) return false; if (!Objects.equals(networkId, that.networkId)) return false; + if (!Objects.equals(customMetaData, that.customMetaData)) return false; if (!Objects.equals(userDataId, that.userDataId)) return false; if (!Objects.equals(instanceCap, that.instanceCap)) return false; if (!Objects.equals(instancesMin, that.instancesMin)) return false; @@ -358,6 +370,7 @@ public int hashCode() { int result = bootSource != null ? bootSource.hashCode() : 0; result = 31 * result + (hardwareId != null ? hardwareId.hashCode() : 0); result = 31 * result + (networkId != null ? networkId.hashCode() : 0); + result = 31 * result + (customMetaData != null ? customMetaData.hashCode() : 0); result = 31 * result + (userDataId != null ? userDataId.hashCode() : 0); result = 31 * result + (instanceCap != null ? instanceCap.hashCode() : 0); result = 31 * result + (instancesMin != null ? instancesMin.hashCode() : 0); @@ -384,6 +397,7 @@ public Builder getBuilder() { .bootSource(bootSource) .hardwareId(hardwareId) .networkId(networkId) + .customMetaData(customMetaData) .userDataId(userDataId) .instanceCap(instanceCap) .instancesMin(instancesMin) @@ -417,6 +431,7 @@ public static final class Builder { private @CheckForNull BootSource bootSource; private @CheckForNull String hardwareId; private @CheckForNull String networkId; + private @CheckForNull String customMetaData; private @CheckForNull String userDataId; private @CheckForNull Integer instanceCap; private @CheckForNull Integer instancesMin; @@ -456,6 +471,11 @@ public Builder() {} return this; } + public @Nonnull Builder customMetaData(String customMetaData) { + this.customMetaData = customMetaData; + return this; + } + public @Nonnull Builder userDataId(String userDataId) { this.userDataId = userDataId; return this; diff --git a/plugin/src/main/resources/jenkins/plugins/openstack/compute/SlaveOptions/config.jelly b/plugin/src/main/resources/jenkins/plugins/openstack/compute/SlaveOptions/config.jelly index 3ea4f467..7d35f922 100644 --- a/plugin/src/main/resources/jenkins/plugins/openstack/compute/SlaveOptions/config.jelly +++ b/plugin/src/main/resources/jenkins/plugins/openstack/compute/SlaveOptions/config.jelly @@ -27,6 +27,9 @@ + + + diff --git a/plugin/src/test/java/jenkins/plugins/openstack/PluginTestRule.java b/plugin/src/test/java/jenkins/plugins/openstack/PluginTestRule.java index 5bd8ec80..96fb2d01 100644 --- a/plugin/src/test/java/jenkins/plugins/openstack/PluginTestRule.java +++ b/plugin/src/test/java/jenkins/plugins/openstack/PluginTestRule.java @@ -88,6 +88,7 @@ import java.util.stream.Collectors; import static org.hamcrest.MatcherAssert.assertThat; +import static org.mockito.Matchers.any; import static org.mockito.Mockito.*; @@ -119,7 +120,7 @@ public static SlaveOptions dummySlaveOptions() { dummyUserData("dummyUserDataId"); } return new SlaveOptions( - new BootSource.VolumeSnapshot("id"), "hw", "nw1,mw2", "dummyUserDataId", 1, 2, "pool", "sg", "az", 1, null, 10, + new BootSource.VolumeSnapshot("id"), "hw", "nw1,mw2", "customdat=true", "dummyUserDataId", 1, 2, "pool", "sg", "az", 1, null, 10, "jvmo", "fsRoot", LauncherFactory.JNLP.JNLP, mkListOfNodeProperties(1, 2), 1, null ); } diff --git a/plugin/src/test/java/jenkins/plugins/openstack/compute/JCloudsCloudTest.java b/plugin/src/test/java/jenkins/plugins/openstack/compute/JCloudsCloudTest.java index 0bf8a6c4..4be49d70 100644 --- a/plugin/src/test/java/jenkins/plugins/openstack/compute/JCloudsCloudTest.java +++ b/plugin/src/test/java/jenkins/plugins/openstack/compute/JCloudsCloudTest.java @@ -133,10 +133,10 @@ public void presentUIDefaults() throws Exception { String openstackAuth = j.dummyCredentials(); JCloudsSlaveTemplate template = new JCloudsSlaveTemplate("template", "label", new SlaveOptions( - new BootSource.Image("iid"), "hw", "nw", "ud", 1, 0, "public", "sg", "az", 2, "kp", 3, "jvmo", "fsRoot", LauncherFactory.JNLP.JNLP, null, 4, false + new BootSource.Image("iid"), "hw", "nw", "cm","ud", 1, 0, "public", "sg", "az", 2, "kp", 3, "jvmo", "fsRoot", LauncherFactory.JNLP.JNLP, null, 4, false )); JCloudsCloud cloud = new JCloudsCloud("openstack", "endPointUrl", false,"zone", new SlaveOptions( - new BootSource.VolumeSnapshot("vsid"), "HW", "NW", "UD", 6, 4, null, "SG", "AZ", 7, "KP", 8, "JVMO", "FSrOOT", new LauncherFactory.SSH("cid"), null, 9, false + new BootSource.VolumeSnapshot("vsid"), "HW", "NW", "CM", "UD", 6, 4, null, "SG", "AZ", 7, "KP", 8, "JVMO", "FSrOOT", new LauncherFactory.SSH("cid"), null, 9, false ), Collections.singletonList(template),openstackAuth); j.jenkins.clouds.add(cloud); diff --git a/plugin/src/test/java/jenkins/plugins/openstack/compute/SlaveOptionsTest.java b/plugin/src/test/java/jenkins/plugins/openstack/compute/SlaveOptionsTest.java index 877bdc4f..18108d37 100644 --- a/plugin/src/test/java/jenkins/plugins/openstack/compute/SlaveOptionsTest.java +++ b/plugin/src/test/java/jenkins/plugins/openstack/compute/SlaveOptionsTest.java @@ -99,7 +99,7 @@ public void eraseDefaults() { public void emptyStrings() { SlaveOptions nulls = SlaveOptions.empty(); SlaveOptions emptyStrings = new SlaveOptions( - null, "", "", "", null, null, "", "", "", null, "", null, "", "", null, null, null, null + null, "", "", "", "", null, null, "", "", "", null, "", null, "", "", null, null, null, null ); SlaveOptions emptyBuilt = SlaveOptions.builder() .hardwareId("")