Skip to content

Commit

Permalink
Merge pull request #160 from rundeck-plugins/enh/warnings
Browse files Browse the repository at this point in the history
RUN-3113: Cleanup: code and deprecation warnings
  • Loading branch information
gschueler authored Feb 13, 2025
2 parents 7710235 + 7bcf76a commit 7ded35b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 40 deletions.
12 changes: 10 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
buildscript {
repositories {
mavenCentral()
maven { url "https://oss.sonatype.org/content/groups/public"}
}
}

Expand All @@ -16,7 +15,16 @@ apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'idea'
apply plugin: 'pl.allegro.tech.build.axion-release'
sourceCompatibility = 11
java {
sourceCompatibility = JavaVersion.VERSION_11
}
allprojects {
gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
options.compilerArgs.add("-Xlint:deprecation")
}
}
}
defaultTasks 'clean', 'build'
ext.rundeckPluginVersion = '1.1'
scmVersion {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
*/
package com.dtolabs.rundeck.plugin.resources.ec2;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.regions.RegionImpl;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.ec2.AmazonEC2Client;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.AmazonEC2ClientBuilder;
import com.amazonaws.services.ec2.model.*;
import com.dtolabs.rundeck.core.common.INodeEntry;
import com.dtolabs.rundeck.core.common.NodeEntryImpl;
Expand All @@ -37,9 +37,6 @@
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
Expand All @@ -52,15 +49,14 @@
class InstanceToNodeMapper {
static final Logger logger = LoggerFactory.getLogger(InstanceToNodeMapper.class);
final AWSCredentials credentials;
private ClientConfiguration clientConfiguration;
private ExecutorService executorService = Executors.newSingleThreadExecutor();
private final ClientConfiguration clientConfiguration;
private ArrayList<String> filterParams;
private String endpoint;
private String region;
private boolean runningStateOnly = true;
private Properties mapping;
private int maxResults;
private AmazonEC2Client ec2 ;
private final int maxResults;
private AmazonEC2 ec2;
private DescribeAvailabilityZonesResult zones;

private static final String[] extraInstanceMappingAttributes= {"imageName","region"};
Expand All @@ -78,14 +74,12 @@ class InstanceToNodeMapper {
/**
* Create with the credentials and mapping definition
*/
InstanceToNodeMapper(final AmazonEC2Client ec2, final AWSCredentials credentials,final Properties mapping, final ClientConfiguration clientConfiguration, final int maxResults) {
InstanceToNodeMapper(final AmazonEC2 ec2, final AWSCredentials credentials,final Properties mapping, final ClientConfiguration clientConfiguration, final int maxResults) {
this.credentials = credentials;
this.mapping = mapping;
this.clientConfiguration = clientConfiguration;
this.maxResults = maxResults;
this.ec2 = ec2;


}

/**
Expand All @@ -95,15 +89,15 @@ class InstanceToNodeMapper {
public NodeSetImpl performQuery() {
final NodeSetImpl nodeSet = new NodeSetImpl();

Set<Instance> instances = new HashSet<Instance>();;
Set<Instance> instances = new HashSet<>();

ArrayList<String> regions = new ArrayList<>();

if(ec2 ==null) {
if (null != credentials) {
ec2 = new AmazonEC2Client(credentials, clientConfiguration);
ec2 = AmazonEC2ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(credentials)).withClientConfiguration(clientConfiguration).build();
} else {
ec2 = new AmazonEC2Client(clientConfiguration);
ec2 = AmazonEC2ClientBuilder.standard().withClientConfiguration(clientConfiguration).build();
}
}

Expand Down Expand Up @@ -174,9 +168,9 @@ else if(region != null){
return nodeSet;
}

private Set<Instance> query(final AmazonEC2Client ec2, final DescribeInstancesRequest request) {
private Set<Instance> query(final AmazonEC2 ec2, final DescribeInstancesRequest request) {
//create "running" filter
final Set<Instance> instances = new HashSet<Instance>();
final Set<Instance> instances = new HashSet<>();

String token = null;
do {
Expand All @@ -195,7 +189,7 @@ private Set<Instance> query(final AmazonEC2Client ec2, final DescribeInstancesRe

private Set<Instance> examineResult(DescribeInstancesResult describeInstancesRequest) {
final List<Reservation> reservations = describeInstancesRequest.getReservations();
final Set<Instance> instances = new HashSet<Instance>();
final Set<Instance> instances = new HashSet<>();

for (final Reservation reservation : reservations) {
instances.addAll(reservation.getInstances());
Expand All @@ -204,7 +198,7 @@ private Set<Instance> examineResult(DescribeInstancesResult describeInstancesReq
}

private ArrayList<Filter> buildFilters() {
final ArrayList<Filter> filters = new ArrayList<Filter>();
final ArrayList<Filter> filters = new ArrayList<>();
if (isRunningStateOnly()) {
final Filter filter = new Filter("instance-state-name").withValues(InstanceStateName.Running.toString());
filters.add(filter);
Expand Down Expand Up @@ -247,7 +241,7 @@ static INodeEntry instanceToNode(final Instance inst, final Properties mapping)
//iterate through instance tags and generate settings
for (final Tag tag : inst.getTags()) {
if (null == node.getAttributes()) {
node.setAttributes(new HashMap<String, String>());
node.setAttributes(new HashMap<>());
}
node.getAttributes().put(tag.getKey(), tag.getValue());
}
Expand All @@ -257,23 +251,23 @@ static INodeEntry instanceToNode(final Instance inst, final Properties mapping)
final String value = applySelector(inst, selector, mapping.getProperty("tags.default"), true);
if (null != value) {
final String[] values = value.split(",");
final HashSet<String> tagset = new HashSet<String>();
final HashSet<String> tagset = new HashSet<>();
for (final String s : values) {
tagset.add(s.trim());
}
if (null == node.getTags()) {
node.setTags(tagset);
} else {
final HashSet orig = new HashSet(node.getTags());
final Set<String> orig = new HashSet<String>(node.getTags());
orig.addAll(tagset);
node.setTags(orig);
}
}
}
if (null == node.getTags()) {
node.setTags(new HashSet());
node.setTags(new HashSet<String>());
}
final HashSet orig = new HashSet(node.getTags());
final Set<String> orig = new HashSet<String>(node.getTags());
//apply specific tag selectors
final Pattern tagPat = Pattern.compile("^tag\\.(.+?)\\.selector$");
//evaluate tag selectors
Expand All @@ -286,7 +280,7 @@ static INodeEntry instanceToNode(final Instance inst, final Properties mapping)
if (m.matches()) {
final String tagName = m.group(1);
if (null == node.getAttributes()) {
node.setAttributes(new HashMap<String, String>());
node.setAttributes(new HashMap<>());
}
final String value = applySelector(inst, selparts[0], null);
if (null != value) {
Expand All @@ -311,7 +305,7 @@ static INodeEntry instanceToNode(final Instance inst, final Properties mapping)
key + ".selector")))) {
final String attrName = m.group(1);
if (null == node.getAttributes()) {
node.setAttributes(new HashMap<String, String>());
node.setAttributes(new HashMap<>());
}
if (null != value) {
node.getAttributes().put(attrName, value);
Expand All @@ -332,7 +326,7 @@ static INodeEntry instanceToNode(final Instance inst, final Properties mapping)
continue;
}
if (null == node.getAttributes()) {
node.setAttributes(new HashMap<String, String>());
node.setAttributes(new HashMap<>());
}
final String value = applySelector(inst, selector, mapping.getProperty(attrName + ".default"));
if (null != value) {
Expand All @@ -348,17 +342,17 @@ static INodeEntry instanceToNode(final Instance inst, final Properties mapping)
// return null;
// }
String name = node.getNodename();
if (null == name || "".equals(name)) {
if (null == name || name.isEmpty()) {
name = node.getHostname();
}
if (null == name || "".equals(name)) {
if (null == name || name.isEmpty()) {
name = inst.getInstanceId();
}
node.setNodename(name);

// Set ssh port on hostname if not 22
String sshport = node.getAttributes().get("sshport");
if (sshport != null && !sshport.equals("") && !sshport.equals("22")) {
if (sshport != null && !sshport.isEmpty() && !sshport.equals("22")) {
node.setHostname(node.getHostname() + ":" + sshport);
}

Expand Down Expand Up @@ -441,7 +435,7 @@ static String applyMultiSelector(final Instance inst, final String... selectors)
sb.append(matcher.group(2));
} else {
String val = applySingleSelector(inst, selector);
if (null != val && !"".equals(val)) {
if (null != val && !val.isEmpty()) {
hasVal = true;
sb.append(val);
}
Expand All @@ -452,15 +446,15 @@ static String applyMultiSelector(final Instance inst, final String... selectors)
}
static String applySingleSelector(final Instance inst, final String selector) throws
GeneratorException {
if (null != selector && !"".equals(selector) && selector.startsWith("tags/")) {
if (null != selector && selector.startsWith("tags/")) {
final String tag = selector.substring("tags/".length());
final List<Tag> tags = inst.getTags();
for (final Tag tag1 : tags) {
if (tag.equals(tag1.getKey())) {
return tag1.getValue();
}
}
} else if (null != selector && !"".equals(selector)) {
} else if (null != selector && !selector.isEmpty()) {
try {
final String value = BeanUtils.getProperty(inst, selector);
if (null != value) {
Expand Down Expand Up @@ -546,7 +540,7 @@ public GeneratorException(final Throwable cause) {
}

public Set<Instance> addExtraMappingAttribute(Set<Instance> instances){
for(String extraAttribute: Arrays.asList(extraInstanceMappingAttributes)){
for(String extraAttribute: extraInstanceMappingAttributes){
if(mappingHasExtraAttribute(extraAttribute)){
if(extraAttribute.equals("imageName")){
instances = addingImageName(instances);
Expand Down Expand Up @@ -576,7 +570,7 @@ public Set<Instance> addingImageName(Set<Instance> originalInstances){
Set<Instance> instances = new HashSet<>();
Map<String,Image> ec2Images = new HashMap<>();
List<String> imagesList = originalInstances.stream().map(Instance::getImageId).collect(Collectors.toList());
logger.debug("Image list: " + imagesList.toString());
logger.debug("Image list: {}", imagesList);
try{
DescribeImagesRequest describeImagesRequest = new DescribeImagesRequest();
describeImagesRequest.setImageIds(imagesList);
Expand All @@ -587,7 +581,7 @@ public Set<Instance> addingImageName(Set<Instance> originalInstances){
ec2Images.put(image.getImageId(),image);
}
}catch(Exception e){
logger.error("error getting image info" + e.getMessage());
logger.error("error getting image info{}", e.getMessage());
}

for (final Instance inst : originalInstances) {
Expand Down

0 comments on commit 7ded35b

Please sign in to comment.