From 0abe955c9be9d751c345e8f64b3c5c6200cec74e Mon Sep 17 00:00:00 2001 From: Antonio Busuladzich Date: Tue, 25 Apr 2023 13:38:18 +0300 Subject: [PATCH] Created method that would allow to programmatically add new slave templates to a EC2Cloud --- src/main/java/hudson/plugins/ec2/EC2Cloud.java | 13 +++++++++++-- src/test/java/hudson/plugins/ec2/EC2CloudTest.java | 12 ++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/main/java/hudson/plugins/ec2/EC2Cloud.java b/src/main/java/hudson/plugins/ec2/EC2Cloud.java index f20644f0e..95320c336 100644 --- a/src/main/java/hudson/plugins/ec2/EC2Cloud.java +++ b/src/main/java/hudson/plugins/ec2/EC2Cloud.java @@ -165,7 +165,7 @@ public abstract class EC2Cloud extends Cloud { */ private final int instanceCap; - private final List templates; + private List templates; private transient KeyPair usableKeyPair; @@ -216,7 +216,16 @@ public EC2PrivateKey resolvePrivateKey(){ public abstract URL getS3EndpointUrl() throws IOException; - private void migratePrivateSshKeyToCredential(String privateKey){ + public void addTemplate(SlaveTemplate newTemplate) throws Exception { + String newTemplateDescription = newTemplate.description; + if (getTemplate(newTemplateDescription) != null) throw new Exception( + String.format("A SlaveTemplate with description %s already exists", newTemplateDescription)); + List templatesHolder = new ArrayList<>(templates); + templatesHolder.add(newTemplate); + templates = templatesHolder; + } + + private void migratePrivateSshKeyToCredential(String privateKey) { // GET matching private key credential from Credential API if exists Optional keyCredential = SystemCredentialsProvider.getInstance().getCredentials() .stream() diff --git a/src/test/java/hudson/plugins/ec2/EC2CloudTest.java b/src/test/java/hudson/plugins/ec2/EC2CloudTest.java index c8ef46e4d..1fb8de13e 100644 --- a/src/test/java/hudson/plugins/ec2/EC2CloudTest.java +++ b/src/test/java/hudson/plugins/ec2/EC2CloudTest.java @@ -3,6 +3,7 @@ import com.amazonaws.services.ec2.AmazonEC2; import com.amazonaws.services.ec2.model.DescribeInstancesResult; import com.amazonaws.services.ec2.model.Instance; +import com.amazonaws.services.ec2.model.InstanceType; import hudson.model.Node; import jenkins.model.Jenkins; import org.junit.Test; @@ -19,12 +20,23 @@ import java.util.List; import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertNotNull; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.times; @RunWith(MockitoJUnitRunner.class) public class EC2CloudTest { + @Test + public void testSlaveTemplateAddition() throws Exception { + AmazonEC2Cloud cloud = new AmazonEC2Cloud("us-east-1", true, + "abc", "us-east-1", null, "ghi", + "3", Collections.emptyList(), "roleArn", "roleSessionName"); + SlaveTemplate orig = new SlaveTemplate("ami-123", EC2AbstractSlave.TEST_ZONE, null, "default", "foo", InstanceType.M1Large, false, "ttt", Node.Mode.NORMAL, "description", "bar", "bbb", "aaa", "10", "fff", null, "-Xmx1g", false, "subnet 456", null, null, 0, 0, null, "iamInstanceProfile", true, false, "", false, "", false, false, false, ConnectionStrategy.PUBLIC_IP, -1, null, null, Tenancy.Default, EbsEncryptRootVolume.DEFAULT); + cloud.addTemplate(orig); + assertNotNull(cloud.getTemplate(orig.description)); + } + @Test public void testReattachOrphanStoppedNodes() throws Exception { /* Mocked items */