Skip to content

Commit

Permalink
added support for comma separated metdata
Browse files Browse the repository at this point in the history
  • Loading branch information
papa committed Sep 24, 2024
1 parent 762e6c0 commit 114fb39
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ protected Object readResolve() {
final Map<String, String> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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<List<String>> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,13 @@
*/
public class SlaveOptions implements Describable<SlaveOptions>, 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;
Expand Down Expand Up @@ -106,6 +107,10 @@ public class SlaveOptions implements Describable<SlaveOptions>, Serializable {
return networkId;
}

public @CheckForNull String getCustomMetaData() {
return customMetaData;
}

public @CheckForNull String getUserDataId() {
return userDataId;
}
Expand Down Expand Up @@ -165,6 +170,7 @@ public SlaveOptions(Builder b) {
b.bootSource,
b.hardwareId,
b.networkId,
b.customMetaData,
b.userDataId,
b.instanceCap,
b.instancesMin,
Expand All @@ -188,6 +194,7 @@ public SlaveOptions(
@CheckForNull BootSource bootSource,
String hardwareId,
String networkId,
String customMetaData,
String userDataId,
Integer instanceCap,
Integer instancesMin,
Expand All @@ -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;
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -384,6 +397,7 @@ public Builder getBuilder() {
.bootSource(bootSource)
.hardwareId(hardwareId)
.networkId(networkId)
.customMetaData(customMetaData)
.userDataId(userDataId)
.instanceCap(instanceCap)
.instancesMin(instancesMin)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
<f:entry title="User Data" field="userDataId">
<f:select checkMethod="post"/>
</f:entry>
<f:entry title="Custom Metadata" field="customMetaData">
<f:textbox checkMethod="post"/>
</f:entry>
<f:entry title="Max. No. of Instances" field="instanceCap">
<f:number checkMethod="post"/>
</f:entry>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;


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

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

0 comments on commit 114fb39

Please sign in to comment.