Modifying filter support in 3 EC2 Describe* API's(DescribeInstances, DescribeVolumes and DescribeSnapshots) to include tags.

Component: AWSAPI.
Reviewed-by: Prachi
This commit is contained in:
Likitha Shetty 2012-07-13 12:17:18 +05:30
parent 2bf8026ea8
commit 5489a0b9fa
3 changed files with 159 additions and 26 deletions

View File

@ -47,20 +47,23 @@ public class EC2InstanceFilterSet {
filterTypes.put( "root-device-name", "string" );
filterTypes.put( "private-ip-address", "string" );
filterTypes.put( "group-id", "string" );
filterTypes.put( "tag-key", "string" );
filterTypes.put( "tag-value", "string" );
}
public void addFilter( EC2Filter param )
{
String filterName = param.getName();
String value = (String) filterTypes.get( filterName );
if (null == value)
throw new EC2ServiceException( "Unsupported filter [" + filterName + "]", 501 );
if (null != value && value.equalsIgnoreCase( "null" ))
throw new EC2ServiceException( "Unsupported filter [" + filterName + "]", 501 );
if (!filterName.startsWith("tag:")) {
String value = (String) filterTypes.get( filterName );
if (null == value)
throw new EC2ServiceException( "Unsupported filter [" + filterName + "]", 501 );
if (null != value && value.equalsIgnoreCase( "null" ))
throw new EC2ServiceException( "Unsupported filter [" + filterName + "]", 501 );
}
// ToDo we could add checks to make sure the type of a filters value is correct (e.g., an integer)
filterSet.add( param );
}
@ -161,6 +164,42 @@ public class EC2InstanceFilterSet {
for (String group : groupSet)
if (containsString(group, valueSet)) return true;
return false;
}
else if (filterName.equalsIgnoreCase("tag-key"))
{
EC2TagKeyValue[] tagSet = vm.getResourceTags();
for (EC2TagKeyValue tag : tagSet)
if (containsString(tag.getKey(), valueSet)) return true;
return false;
}
else if (filterName.equalsIgnoreCase("tag-value"))
{
EC2TagKeyValue[] tagSet = vm.getResourceTags();
for (EC2TagKeyValue tag : tagSet) {
if (tag.getValue() == null) {
if (containsEmptyValue(valueSet)) return true;
}
else {
if (containsString(tag.getValue(), valueSet)) return true;
}
}
return false;
}
else if (filterName.startsWith("tag:"))
{
String key = filterName.split(":")[1];
EC2TagKeyValue[] tagSet = vm.getResourceTags();
for (EC2TagKeyValue tag : tagSet){
if (tag.getKey().equalsIgnoreCase(key)) {
if (tag.getValue() == null) {
if (containsEmptyValue(valueSet)) return true;
}
else {
if (containsString(tag.getValue(), valueSet)) return true;
}
}
}
return false;
}
else return false;
}
@ -177,8 +216,14 @@ public class EC2InstanceFilterSet {
}
return false;
}
private boolean containsEmptyValue( String[] set )
{
for( int i=0; i < set.length; i++ )
if (set[i].isEmpty()) return true;
return false;
}
private boolean containsInteger( int lookingFor, String[] set )
{
for( int i=0; i < set.length; i++ )

View File

@ -45,20 +45,23 @@ public class EC2SnapshotFilterSet {
filterTypes.put( "status", "string" );
filterTypes.put( "volume-id", "string" );
filterTypes.put( "volume-size", "string" );
filterTypes.put( "tag-key", "string" );
filterTypes.put( "tag-value", "string" );
}
public void addFilter( EC2Filter param )
{
String filterName = param.getName();
String value = (String) filterTypes.get( filterName );
if (null == value)
throw new EC2ServiceException( "Unsupported filter [" + filterName + "] - 1", 501 );
if (null != value && value.equalsIgnoreCase( "null" ))
throw new EC2ServiceException( "Unsupported filter [" + filterName + "] - 2", 501 );
if (!filterName.startsWith("tag:")) {
String value = (String) filterTypes.get( filterName );
if (null == value)
throw new EC2ServiceException( "Unsupported filter [" + filterName + "] - 1", 501 );
if (null != value && value.equalsIgnoreCase( "null" ))
throw new EC2ServiceException( "Unsupported filter [" + filterName + "] - 2", 501 );
}
// ToDo we could add checks to make sure the type of a filters value is correct (e.g., an integer)
filterSet.add( param );
}
@ -139,6 +142,42 @@ public class EC2SnapshotFilterSet {
{
return containsLong( snap.getVolumeSize(), valueSet );
}
else if (filterName.equalsIgnoreCase("tag-key"))
{
EC2TagKeyValue[] tagSet = snap.getResourceTags();
for (EC2TagKeyValue tag : tagSet)
if (containsString(tag.getKey(), valueSet)) return true;
return false;
}
else if (filterName.equalsIgnoreCase("tag-value"))
{
EC2TagKeyValue[] tagSet = snap.getResourceTags();
for (EC2TagKeyValue tag : tagSet){
if (tag.getValue() == null) {
if (containsEmptyValue(valueSet)) return true;
}
else {
if (containsString(tag.getValue(), valueSet)) return true;
}
}
return false;
}
else if (filterName.startsWith("tag:"))
{
String key = filterName.split(":")[1];
EC2TagKeyValue[] tagSet = snap.getResourceTags();
for (EC2TagKeyValue tag : tagSet){
if (tag.getKey().equalsIgnoreCase(key)){
if (tag.getValue() == null) {
if (containsEmptyValue(valueSet)) return true;
}
else {
if (containsString(tag.getValue(), valueSet)) return true;
}
}
}
return false;
}
else return false;
}
@ -154,7 +193,13 @@ public class EC2SnapshotFilterSet {
return false;
}
private boolean containsEmptyValue( String[] set )
{
for( int i=0; i < set.length; i++ )
if (set[i].isEmpty()) return true;
return false;
}
private boolean containsLong( long lookingFor, String[] set )
{
for (String s : set) {

View File

@ -48,8 +48,8 @@ public class EC2VolumeFilterSet {
filterTypes.put( "size", "integer" );
filterTypes.put( "snapshot-id", "string" );
filterTypes.put( "status", "set:creating|available|in-use|deleting|deleted|error" );
filterTypes.put( "tag-key", "null" );
filterTypes.put( "tag-value", "null" );
filterTypes.put( "tag-key", "string" );
filterTypes.put( "tag-value", "string" );
filterTypes.put( "volume-id", "string" );
// filterTypes.put( "tag:*", "null" );
}
@ -58,14 +58,15 @@ public class EC2VolumeFilterSet {
public void addFilter( EC2Filter param )
{
String filterName = param.getName();
String value = (String) filterTypes.get( filterName );
if (!filterName.startsWith("tag:")) {
String value = (String) filterTypes.get( filterName );
if (null == value)
throw new EC2ServiceException( "Unsupported filter [" + filterName + "] - 1", 501 );
if (null != value && value.equalsIgnoreCase( "null" ))
throw new EC2ServiceException( "Unsupported filter [" + filterName + "] - 2", 501 );
if (null == value)
throw new EC2ServiceException( "Unsupported filter [" + filterName + "] - 1", 501 );
if (null != value && value.equalsIgnoreCase( "null" ))
throw new EC2ServiceException( "Unsupported filter [" + filterName + "] - 2", 501 );
}
// ToDo we could add checks to make sure the type of a filters value is correct (e.g., an integer)
filterSet.add( param );
}
@ -135,7 +136,43 @@ public class EC2VolumeFilterSet {
else if (filterName.equalsIgnoreCase( "attachment.device" ))
return containsDevice(vol.getDeviceId(), valueSet );
else if (filterName.equalsIgnoreCase( "attachment.instance-id" ))
return containsString(String.valueOf(vol.getInstanceId()), valueSet );
return containsString(String.valueOf(vol.getInstanceId()), valueSet );
else if (filterName.equalsIgnoreCase("tag-key"))
{
EC2TagKeyValue[] tagSet = vol.getResourceTags();
for (EC2TagKeyValue tag : tagSet)
if (containsString(tag.getKey(), valueSet)) return true;
return false;
}
else if (filterName.equalsIgnoreCase("tag-value"))
{
EC2TagKeyValue[] tagSet = vol.getResourceTags();
for (EC2TagKeyValue tag : tagSet){
if (tag.getValue() == null) {
if (containsEmptyValue(valueSet)) return true;
}
else {
if (containsString(tag.getValue(), valueSet)) return true;
}
}
return false;
}
else if (filterName.startsWith("tag:"))
{
String key = filterName.split(":")[1];
EC2TagKeyValue[] tagSet = vol.getResourceTags();
for (EC2TagKeyValue tag : tagSet){
if (tag.getKey().equalsIgnoreCase(key)) {
if (tag.getValue() == null) {
if (containsEmptyValue(valueSet)) return true;
}
else {
if (containsString(tag.getValue(), valueSet)) return true;
}
}
}
return false;
}
else return false;
}
@ -149,6 +186,12 @@ public class EC2VolumeFilterSet {
return false;
}
private boolean containsEmptyValue( String[] set )
{
for( int i=0; i < set.length; i++ )
if (set[i].isEmpty()) return true;
return false;
}
private boolean containsLong( long lookingFor, String[] set )
{