forked from jenkinsci/spotinst-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Jenkins plugin instance type search (#27)
Added capability to search for instances by instance types
- Loading branch information
Showing
8 changed files
with
316 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
src/main/java/hudson/plugins/spotinst/cloud/monitor/AwsSpotinstCloudInstanceTypeMonitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package hudson.plugins.spotinst.cloud.monitor; | ||
|
||
import hudson.Extension; | ||
import hudson.model.AdministrativeMonitor; | ||
import hudson.plugins.spotinst.cloud.AwsSpotinstCloud; | ||
import hudson.slaves.Cloud; | ||
import jenkins.model.Jenkins; | ||
import org.apache.commons.collections.CollectionUtils; | ||
|
||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
@Extension | ||
public class AwsSpotinstCloudInstanceTypeMonitor extends AdministrativeMonitor { | ||
//region members | ||
Map<String, List<String>> invalidInstancesByGroupId; | ||
//endregion | ||
|
||
//region Overrides | ||
@Override | ||
public boolean isActivated() { | ||
boolean retVal; | ||
initInvalidInstances(); | ||
retVal = hasInvalidInstanceType(); | ||
return retVal; | ||
} | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "Aws Spotinst Cloud Instance Type Monitor"; | ||
} | ||
//endregion | ||
|
||
//region Methods | ||
public boolean hasInvalidInstanceType() { | ||
return invalidInstancesByGroupId.isEmpty() == false; | ||
} | ||
//endregion | ||
|
||
//region getters & setters | ||
public String getInvalidInstancesByGroupId() { | ||
Stream<String> invalidInstancesForOutput = | ||
invalidInstancesByGroupId.keySet().stream().map(this::generateAlertMessage); | ||
String retVal = invalidInstancesForOutput.collect(Collectors.joining(", ")); | ||
|
||
return retVal; | ||
} | ||
//endregion | ||
|
||
//region private Methods | ||
private void initInvalidInstances() { | ||
invalidInstancesByGroupId = new HashMap<>(); | ||
Jenkins jenkinsInstance = Jenkins.getInstance(); | ||
List<Cloud> clouds = jenkinsInstance != null ? jenkinsInstance.clouds : new LinkedList<>(); | ||
List<AwsSpotinstCloud> awsClouds = clouds.stream().filter(cloud -> cloud instanceof AwsSpotinstCloud) | ||
.map(awsCloud -> (AwsSpotinstCloud) awsCloud) | ||
.collect(Collectors.toList()); | ||
|
||
awsClouds.forEach(awsCloud -> { | ||
String elastigroupId = awsCloud.getGroupId(); | ||
List<String> invalidTypes = awsCloud.getInvalidInstanceTypes(); | ||
|
||
if (CollectionUtils.isEmpty(invalidTypes) == false) { | ||
invalidInstancesByGroupId.put(elastigroupId, invalidTypes); | ||
} | ||
}); | ||
} | ||
|
||
private String generateAlertMessage(String group) { | ||
StringBuilder retVal = new StringBuilder(); | ||
retVal.append('\'').append(group).append('\'').append(": ["); | ||
|
||
List<String> InvalidInstancesByGroup = invalidInstancesByGroupId.get(group); | ||
Stream<String> InvalidInstancesForAlert = | ||
InvalidInstancesByGroup.stream().map(invalidInstance -> '\'' + invalidInstance + '\''); | ||
|
||
String instances = InvalidInstancesForAlert.collect(Collectors.joining(", ")); | ||
retVal.append(instances).append(']'); | ||
return retVal.toString(); | ||
} | ||
//region | ||
} |
Oops, something went wrong.