Skip to content

Commit

Permalink
Created method that would allow to programmatically add new slave tem…
Browse files Browse the repository at this point in the history
…plates to a EC2Cloud
  • Loading branch information
drullar committed Apr 25, 2023
1 parent 05cc875 commit 0abe955
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/main/java/hudson/plugins/ec2/EC2Cloud.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public abstract class EC2Cloud extends Cloud {
*/
private final int instanceCap;

private final List<? extends SlaveTemplate> templates;
private List<? extends SlaveTemplate> templates;

private transient KeyPair usableKeyPair;

Expand Down Expand Up @@ -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<SlaveTemplate> 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<SSHUserPrivateKey> keyCredential = SystemCredentialsProvider.getInstance().getCredentials()
.stream()
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/hudson/plugins/ec2/EC2CloudTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 */
Expand Down

0 comments on commit 0abe955

Please sign in to comment.