Skip to content

Commit

Permalink
Merge pull request #1011 from basil/spotless
Browse files Browse the repository at this point in the history
Format repository with Spotless
  • Loading branch information
res0nance authored Nov 27, 2024
2 parents 47818f8 + 98fc9a9 commit d284528
Show file tree
Hide file tree
Showing 108 changed files with 7,984 additions and 3,142 deletions.
402 changes: 201 additions & 201 deletions pom.xml

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions src/main/java/hudson/plugins/ec2/AMITypeData.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package hudson.plugins.ec2;

import hudson.model.AbstractDescribableImpl;

import java.util.concurrent.TimeUnit;

public abstract class AMITypeData extends AbstractDescribableImpl<AMITypeData> {
Expand All @@ -14,13 +13,13 @@ public abstract class AMITypeData extends AbstractDescribableImpl<AMITypeData> {
public abstract String getBootDelay();

public int getBootDelayInMillis() {
if (getBootDelay() == null)
if (getBootDelay() == null) {

Check warning on line 16 in src/main/java/hudson/plugins/ec2/AMITypeData.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 16 is not covered by tests
return 0;
}
try {
return (int) TimeUnit.SECONDS.toMillis(Integer.parseInt(getBootDelay()));
} catch (NumberFormatException nfe) {
return 0;
}
}

}
105 changes: 77 additions & 28 deletions src/main/java/hudson/plugins/ec2/AmazonEC2Cloud.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,32 @@
package hudson.plugins.ec2;

import com.amazonaws.SdkClientException;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.model.DescribeRegionsResult;
import com.amazonaws.services.ec2.model.Region;
import edu.umd.cs.findbugs.annotations.Nullable;
import hudson.Extension;
import hudson.Util;
import hudson.model.Failure;
import hudson.model.ItemGroup;
import hudson.plugins.ec2.util.AmazonEC2Factory;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import edu.umd.cs.findbugs.annotations.Nullable;
import javax.servlet.ServletException;

import jenkins.model.Jenkins;

import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.interceptor.RequirePOST;

import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.model.DescribeRegionsResult;
import com.amazonaws.services.ec2.model.Region;
import org.kohsuke.stapler.verb.POST;

/**
Expand All @@ -62,8 +58,8 @@
* @author Kohsuke Kawaguchi
*/
public class AmazonEC2Cloud extends EC2Cloud {
private final static Logger LOGGER = Logger.getLogger(AmazonEC2Cloud.class.getName());
private static final Logger LOGGER = Logger.getLogger(AmazonEC2Cloud.class.getName());

/**
* Represents the region. Can be null for backward compatibility reasons.
*/
Expand All @@ -74,14 +70,50 @@ public class AmazonEC2Cloud extends EC2Cloud {
private boolean noDelayProvisioning;

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

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

Expand All @@ -94,12 +126,14 @@ public String getCloudName() {
}

public String getRegion() {
if (region == null)
if (region == null) {
region = DEFAULT_EC2_HOST; // Backward compatibility
}
// Handles pre 1.14 region names that used the old AwsRegion enum, note we don't change
// the region here to keep the meta-data compatible in the case of a downgrade (is that right?)
if (region.indexOf('_') > 0)
if (region.indexOf('_') > 0) {

Check warning on line 134 in src/main/java/hudson/plugins/ec2/AmazonEC2Cloud.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 134 is only partially covered, one branch is missing
return region.replace('_', '-').toLowerCase(Locale.ENGLISH);
}
return region;
}

Expand Down Expand Up @@ -145,7 +179,12 @@ public void setAltEC2Endpoint(String altEC2Endpoint) {

@Override
protected AWSCredentialsProvider createCredentialsProvider() {
return createCredentialsProvider(isUseInstanceProfileForCredentials(), getCredentialsId(), getRoleArn(), getRoleSessionName(), getRegion());
return createCredentialsProvider(
isUseInstanceProfileForCredentials(),
getCredentialsId(),
getRoleArn(),
getRoleSessionName(),
getRegion());
}

@Extension
Expand Down Expand Up @@ -180,20 +219,20 @@ public FormValidation doCheckAltEC2Endpoint(@QueryParameter String value) {
}
return FormValidation.ok();
}

@RequirePOST
public ListBoxModel doFillRegionItems(
@QueryParameter String altEC2Endpoint,
@QueryParameter boolean useInstanceProfileForCredentials,
@QueryParameter String credentialsId)

throws IOException, ServletException {
ListBoxModel model = new ListBoxModel();
if (Jenkins.get().hasPermission(Jenkins.ADMINISTER)) {
try {
AWSCredentialsProvider credentialsProvider = createCredentialsProvider(useInstanceProfileForCredentials,
credentialsId);
AmazonEC2 client = AmazonEC2Factory.getInstance().connect(credentialsProvider, determineEC2EndpointURL(altEC2Endpoint));
AWSCredentialsProvider credentialsProvider =
createCredentialsProvider(useInstanceProfileForCredentials, credentialsId);
AmazonEC2 client = AmazonEC2Factory.getInstance()
.connect(credentialsProvider, determineEC2EndpointURL(altEC2Endpoint));
DescribeRegionsResult regions = client.describeRegions();
List<Region> regionList = regions.getRegions();
for (Region r : regionList) {
Expand All @@ -209,15 +248,18 @@ public ListBoxModel doFillRegionItems(

// Will use the alternate EC2 endpoint if provided by the UI (via a @QueryParameter field), or use the default
// value if not specified.
//VisibleForTesting
// VisibleForTesting
URL determineEC2EndpointURL(@Nullable String altEC2Endpoint) throws MalformedURLException {
if (Util.fixEmpty(altEC2Endpoint) == null) {
return new URL(DEFAULT_EC2_ENDPOINT);
}
try {
return new URL(altEC2Endpoint);
return new URL(altEC2Endpoint);
} catch (MalformedURLException e) {
LOGGER.log(Level.WARNING, "The alternate EC2 endpoint is malformed ({0}). Using the default endpoint ({1})", new Object[]{altEC2Endpoint, DEFAULT_EC2_ENDPOINT});
LOGGER.log(
Level.WARNING,
"The alternate EC2 endpoint is malformed ({0}). Using the default endpoint ({1})",
new Object[] {altEC2Endpoint, DEFAULT_EC2_ENDPOINT});
return new URL(DEFAULT_EC2_ENDPOINT);
}
}
Expand All @@ -231,14 +273,21 @@ public FormValidation doTestConnection(
@QueryParameter String sshKeysCredentialsId,
@QueryParameter String roleArn,
@QueryParameter String roleSessionName)

throws IOException, ServletException {

if (Util.fixEmpty(region) == null) {
region = DEFAULT_EC2_HOST;
}

return super.doTestConnection(context, getEc2EndpointUrl(region), useInstanceProfileForCredentials, credentialsId, sshKeysCredentialsId, roleArn, roleSessionName, region);
return super.doTestConnection(
context,
getEc2EndpointUrl(region),

Check warning on line 284 in src/main/java/hudson/plugins/ec2/AmazonEC2Cloud.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 259-284 are not covered by tests
useInstanceProfileForCredentials,
credentialsId,
sshKeysCredentialsId,
roleArn,
roleSessionName,
region);
}
}
}
47 changes: 26 additions & 21 deletions src/main/java/hudson/plugins/ec2/CloudHelper.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package hudson.plugins.ec2;

import static hudson.plugins.ec2.EC2Cloud.EC2_REQUEST_EXPIRED_ERROR_CODE;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.services.ec2.AmazonEC2;
Expand All @@ -10,28 +12,27 @@
import com.amazonaws.services.ec2.model.Image;
import com.amazonaws.services.ec2.model.Instance;
import com.amazonaws.services.ec2.model.Reservation;
import java.util.ArrayList;
import org.apache.commons.lang.StringUtils;

import edu.umd.cs.findbugs.annotations.CheckForNull;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Logger;

import static hudson.plugins.ec2.EC2Cloud.EC2_REQUEST_EXPIRED_ERROR_CODE;
import org.apache.commons.lang.StringUtils;

final class CloudHelper {
private static final Logger LOGGER = Logger.getLogger(CloudHelper.class.getName());

static Instance getInstanceWithRetry(String instanceId, EC2Cloud cloud) throws AmazonClientException, InterruptedException {
static Instance getInstanceWithRetry(String instanceId, EC2Cloud cloud)
throws AmazonClientException, InterruptedException {
// Sometimes even after a successful RunInstances, DescribeInstances
// returns an error for a few seconds. We do a few retries instead of
// failing instantly. See [JENKINS-15319].
for (int i = 0; i < 5; i++) {
try {
return getInstance(instanceId, cloud);
} catch (AmazonServiceException e) {
if (e.getErrorCode().equals("InvalidInstanceID.NotFound") || EC2_REQUEST_EXPIRED_ERROR_CODE.equals(e.getErrorCode())) {
if (e.getErrorCode().equals("InvalidInstanceID.NotFound")
|| EC2_REQUEST_EXPIRED_ERROR_CODE.equals(e.getErrorCode())) {

Check warning on line 35 in src/main/java/hudson/plugins/ec2/CloudHelper.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 35 is only partially covered, one branch is missing
// retry in 5 seconds.
Thread.sleep(5000);
continue;
Expand All @@ -45,31 +46,35 @@ static Instance getInstanceWithRetry(String instanceId, EC2Cloud cloud) throws A

@CheckForNull
static Instance getInstance(String instanceId, EC2Cloud cloud) throws AmazonClientException {
if (StringUtils.isEmpty(instanceId) || cloud == null)
if (StringUtils.isEmpty(instanceId) || cloud == null) {

Check warning on line 49 in src/main/java/hudson/plugins/ec2/CloudHelper.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 49 is only partially covered, one branch is missing
return null;
}

DescribeInstancesRequest request = new DescribeInstancesRequest();
request.setInstanceIds(Collections.singletonList(instanceId));

List<Reservation> reservations = cloud.connect().describeInstances(request).getReservations();
List<Reservation> reservations =
cloud.connect().describeInstances(request).getReservations();
if (reservations.size() != 1) {
String message = "Unexpected number of reservations reported by EC2 for instance id '" + instanceId + "', expected 1 result, found " + reservations + ".";
if (reservations.size() == 0) {
message += " Instance seems to be dead.";
}
LOGGER.info(message);
throw new AmazonClientException(message);
String message = "Unexpected number of reservations reported by EC2 for instance id '" + instanceId
+ "', expected 1 result, found " + reservations + ".";
if (reservations.size() == 0) {
message += " Instance seems to be dead.";
}
LOGGER.info(message);
throw new AmazonClientException(message);
}
Reservation reservation = reservations.get(0);

List<Instance> instances = reservation.getInstances();
if (instances.size() != 1) {
String message = "Unexpected number of instances reported by EC2 for instance id '" + instanceId + "', expected 1 result, found " + instances + ".";
if (instances.size() == 0) {
message += " Instance seems to be dead.";
}
LOGGER.info(message);
throw new AmazonClientException(message);
String message = "Unexpected number of instances reported by EC2 for instance id '" + instanceId

Check warning on line 71 in src/main/java/hudson/plugins/ec2/CloudHelper.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 59-71 are not covered by tests
+ "', expected 1 result, found " + instances + ".";
if (instances.size() == 0) {
message += " Instance seems to be dead.";
}
LOGGER.info(message);
throw new AmazonClientException(message);
}
return instances.get(0);
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/hudson/plugins/ec2/ConnectionStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public String getDisplayText() {
* @param associatePublicIp whether or not to associate to a public ip.
* @return an {@link ConnectionStrategy} based on provided parameters.
*/
public static ConnectionStrategy backwardsCompatible(boolean usePrivateDnsName, boolean connectUsingPublicIp, boolean associatePublicIp) {
public static ConnectionStrategy backwardsCompatible(
boolean usePrivateDnsName, boolean connectUsingPublicIp, boolean associatePublicIp) {
if (usePrivateDnsName && !connectUsingPublicIp) {
return PRIVATE_DNS;
} else if (connectUsingPublicIp || associatePublicIp) {
Expand Down
Loading

0 comments on commit d284528

Please sign in to comment.