Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove cloudId and use the name entered by the user directly. #827

Merged
merged 1 commit into from
Apr 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 7 additions & 26 deletions src/main/java/hudson/plugins/ec2/AmazonEC2Cloud.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import hudson.model.Failure;
import hudson.model.ItemGroup;
import hudson.plugins.ec2.util.AmazonEC2Factory;
import hudson.slaves.Cloud;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;

Expand Down Expand Up @@ -72,33 +71,26 @@ public class AmazonEC2Cloud extends EC2Cloud {

private String altEC2Endpoint;

public static final String CLOUD_ID_PREFIX = "ec2-";

private boolean noDelayProvisioning;

@DataBoundConstructor
public AmazonEC2Cloud(String cloudName, boolean useInstanceProfileForCredentials, String credentialsId, String region, String privateKey, String sshKeysCredentialsId, String instanceCapStr, List<? extends SlaveTemplate> templates, String roleArn, String roleSessionName) {
super(createCloudId(cloudName), useInstanceProfileForCredentials, credentialsId, privateKey, sshKeysCredentialsId, instanceCapStr, templates, roleArn, roleSessionName);
super(cloudName, useInstanceProfileForCredentials, credentialsId, privateKey, sshKeysCredentialsId, instanceCapStr, templates, roleArn, roleSessionName);
this.region = region;
}

@Deprecated
public AmazonEC2Cloud(String cloudName, boolean useInstanceProfileForCredentials, String credentialsId, String region, String privateKey, String instanceCapStr, List<? extends SlaveTemplate> templates, String roleArn, String roleSessionName) {
super(createCloudId(cloudName), useInstanceProfileForCredentials, credentialsId, privateKey, instanceCapStr, templates, roleArn, roleSessionName);
super(cloudName, useInstanceProfileForCredentials, credentialsId, privateKey, instanceCapStr, templates, roleArn, roleSessionName);
this.region = region;
}

/**
* @deprecated Use public field "name" instead.
*/
@Deprecated
public String getCloudName() {
return this.name.substring(CLOUD_ID_PREFIX.length());
}

@Override
public String getDisplayName() {
return getCloudName();
}

private static String createCloudId(String cloudName) {
return CLOUD_ID_PREFIX + cloudName.trim();
return name;
}

public String getRegion() {
Expand Down Expand Up @@ -172,17 +164,6 @@ public FormValidation doCheckCloudName(@QueryParameter String value) {
} catch (Failure e) {
return FormValidation.error(e.getMessage());
}

String cloudId = createCloudId(value);
int found = 0;
for (Cloud c : Jenkins.get().clouds) {
if (c.name.equals(cloudId)) {
found++;
}
}
if (found > 1) {
return FormValidation.error(Messages.AmazonEC2Cloud_NonUniqName());
}
return FormValidation.ok();
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/hudson/plugins/ec2/EC2Cloud.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,9 @@ public abstract class EC2Cloud extends Cloud {

private transient volatile AmazonEC2 connection;

protected EC2Cloud(String id, boolean useInstanceProfileForCredentials, String credentialsId, String privateKey, String sshKeysCredentialsId,
protected EC2Cloud(String name, boolean useInstanceProfileForCredentials, String credentialsId, String privateKey, String sshKeysCredentialsId,
String instanceCapStr, List<? extends SlaveTemplate> templates, String roleArn, String roleSessionName) {
super(id);
super(name);
this.useInstanceProfileForCredentials = useInstanceProfileForCredentials;
this.roleArn = roleArn;
this.roleSessionName = roleSessionName;
Expand Down
18 changes: 9 additions & 9 deletions src/test/java/hudson/plugins/ec2/ConfigurationAsCodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ public class ConfigurationAsCodeTest {
@Test
@ConfiguredWithCode("EC2CloudEmpty.yml")
public void testEmptyConfig() throws Exception {
final AmazonEC2Cloud ec2Cloud = (AmazonEC2Cloud) Jenkins.get().getCloud("ec2-");
final AmazonEC2Cloud ec2Cloud = (AmazonEC2Cloud) Jenkins.get().getCloud("");
assertNotNull(ec2Cloud);
assertEquals(0, ec2Cloud.getTemplates().size());
}

@Test
@ConfiguredWithCode("UnixData.yml")
public void testUnixData() throws Exception {
final AmazonEC2Cloud ec2Cloud = (AmazonEC2Cloud) Jenkins.get().getCloud("ec2-production");
final AmazonEC2Cloud ec2Cloud = (AmazonEC2Cloud) Jenkins.get().getCloud("production");
assertNotNull(ec2Cloud);
assertTrue(ec2Cloud.isUseInstanceProfileForCredentials());

Expand Down Expand Up @@ -83,7 +83,7 @@ public void testUnixData() throws Exception {
@Test
@ConfiguredWithCode("Unix.yml")
public void testUnix() throws Exception {
final AmazonEC2Cloud ec2Cloud = (AmazonEC2Cloud) Jenkins.get().getCloud("ec2-staging");
final AmazonEC2Cloud ec2Cloud = (AmazonEC2Cloud) Jenkins.get().getCloud("staging");
assertNotNull(ec2Cloud);
assertTrue(ec2Cloud.isUseInstanceProfileForCredentials());

Expand All @@ -105,7 +105,7 @@ public void testUnix() throws Exception {
@Test
@ConfiguredWithCode("WindowsData.yml")
public void testWindowsData() throws Exception {
final AmazonEC2Cloud ec2Cloud = (AmazonEC2Cloud) Jenkins.get().getCloud("ec2-development");
final AmazonEC2Cloud ec2Cloud = (AmazonEC2Cloud) Jenkins.get().getCloud("development");
assertNotNull(ec2Cloud);
assertTrue(ec2Cloud.isUseInstanceProfileForCredentials());

Expand Down Expand Up @@ -134,7 +134,7 @@ public void testWindowsData() throws Exception {
@Test
@ConfiguredWithCode("BackwardsCompatibleConnectionStrategy.yml")
public void testBackwardsCompatibleConnectionStrategy() throws Exception {
final AmazonEC2Cloud ec2Cloud = (AmazonEC2Cloud) Jenkins.get().getCloud("ec2-us-east-1");
final AmazonEC2Cloud ec2Cloud = (AmazonEC2Cloud) Jenkins.get().getCloud("us-east-1");
assertNotNull(ec2Cloud);

final List<SlaveTemplate> templates = ec2Cloud.getTemplates();
Expand Down Expand Up @@ -168,7 +168,7 @@ public void testConfigAsCodeWithAltEndpointAndJavaPathExport() throws Exception
@Test
@ConfiguredWithCode("Unix-withMinimumInstancesTimeRange.yml")
public void testConfigAsCodeWithMinimumInstancesTimeRange() throws Exception {
final AmazonEC2Cloud ec2Cloud = (AmazonEC2Cloud) Jenkins.get().getCloud("ec2-timed");
final AmazonEC2Cloud ec2Cloud = (AmazonEC2Cloud) Jenkins.get().getCloud("timed");
assertNotNull(ec2Cloud);
assertTrue(ec2Cloud.isUseInstanceProfileForCredentials());

Expand Down Expand Up @@ -197,7 +197,7 @@ public void testConfigAsCodeWithMinimumInstancesTimeRange() throws Exception {
@Test
@ConfiguredWithCode("Ami.yml")
public void testAmi() throws Exception {
final AmazonEC2Cloud ec2Cloud = (AmazonEC2Cloud) Jenkins.get().getCloud("ec2-test");
final AmazonEC2Cloud ec2Cloud = (AmazonEC2Cloud) Jenkins.get().getCloud("test");
assertNotNull(ec2Cloud);

final List<SlaveTemplate> templates = ec2Cloud.getTemplates();
Expand Down Expand Up @@ -231,7 +231,7 @@ public void testAmi() throws Exception {
@Test
@ConfiguredWithCode("MacData.yml")
public void testMacData() throws Exception {
final AmazonEC2Cloud ec2Cloud = (AmazonEC2Cloud) Jenkins.get().getCloud("ec2-production");
final AmazonEC2Cloud ec2Cloud = (AmazonEC2Cloud) Jenkins.get().getCloud("production");
assertNotNull(ec2Cloud);
assertTrue(ec2Cloud.isUseInstanceProfileForCredentials());

Expand Down Expand Up @@ -261,7 +261,7 @@ public void testMacData() throws Exception {
@Test
@ConfiguredWithCode("Mac.yml")
public void testMac() throws Exception {
final AmazonEC2Cloud ec2Cloud = (AmazonEC2Cloud) Jenkins.get().getCloud("ec2-staging");
final AmazonEC2Cloud ec2Cloud = (AmazonEC2Cloud) Jenkins.get().getCloud("staging");
assertNotNull(ec2Cloud);
assertTrue(ec2Cloud.isUseInstanceProfileForCredentials());

Expand Down
5 changes: 3 additions & 2 deletions src/test/java/hudson/plugins/ec2/EC2AbstractSlaveTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ public void testMaxUsesBackwardCompat() throws Exception {
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, "java", "-Xmx1g", false, "subnet 456", null, null, 1, 1, "", "profile", false, false, "", false, "", false, false, false, ConnectionStrategy.PUBLIC_IP, -1, null, HostKeyVerificationStrategyEnum.CHECK_NEW_HARD, Tenancy.Default, EbsEncryptRootVolume.DEFAULT, DEFAULT_METADATA_ENDPOINT_ENABLED, DEFAULT_METADATA_TOKENS_REQUIRED, DEFAULT_METADATA_HOPS_LIMIT);
List<SlaveTemplate> templates = new ArrayList<>();
templates.add(orig);
AmazonEC2Cloud ac = new AmazonEC2Cloud("us-east-1", false, "abc", "us-east-1", "ghi", "3", templates, null, null);
String cloudName = "us-east-1";
AmazonEC2Cloud ac = new AmazonEC2Cloud(cloudName, false, "abc", "us-east-1", "ghi", "3", templates, null, null);
r.jenkins.clouds.add(ac);
EC2AbstractSlave slave = new EC2AbstractSlave("name", "", description, "fs", 1, null, "label", null, null, "init", "tmpDir", new ArrayList<NodeProperty<?>>(), "root", "jvm", false, "idle", null, "ec2-us-east-1", false, Integer.MAX_VALUE, new UnixData("remote", null, null, "22", null), ConnectionStrategy.PRIVATE_IP, 0) {
EC2AbstractSlave slave = new EC2AbstractSlave("name", "", description, "fs", 1, null, "label", null, null, "init", "tmpDir", new ArrayList<NodeProperty<?>>(), "root", "jvm", false, "idle", null, cloudName, false, Integer.MAX_VALUE, new UnixData("remote", null, null, "22", null), ConnectionStrategy.PRIVATE_IP, 0) {
@Override
public void terminate() {
}
Expand Down