incubator-cloudstack-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Likitha Shetty <likitha.she...@citrix.com>
Subject [PATCH] CS-15122. Provide filter support for ec2-describe-availability-zones. Component: awsapi.
Date Mon, 04 Jun 2012 11:10:50 GMT

Signed-off-by: Likitha Shetty <likitha.shetty@citrix.com>
---
 .../cloud/bridge/service/EC2SoapServiceImpl.java   |   30 ++++++
 .../core/ec2/EC2AvailabilityZonesFilterSet.java    |   99 ++++++++++++++++++++
 .../core/ec2/EC2DescribeAvailabilityZones.java     |   10 ++
 .../cloud/bridge/service/core/ec2/EC2Engine.java   |   14 ++-
 4 files changed, 149 insertions(+), 4 deletions(-)
 create mode 100644 awsapi/src/com/cloud/bridge/service/core/ec2/EC2AvailabilityZonesFilterSet.java

diff --git a/awsapi/src/com/cloud/bridge/service/EC2SoapServiceImpl.java b/awsapi/src/com/cloud/bridge/service/EC2SoapServiceImpl.java
index a70d930..de3dcd4 100644
--- a/awsapi/src/com/cloud/bridge/service/EC2SoapServiceImpl.java
+++ b/awsapi/src/com/cloud/bridge/service/EC2SoapServiceImpl.java
@@ -27,6 +27,7 @@ import com.cloud.bridge.service.core.ec2.EC2Address;
 import com.cloud.bridge.service.core.ec2.EC2AddressFilterSet;
 import com.cloud.bridge.service.core.ec2.EC2AssociateAddress;
 import com.cloud.bridge.service.core.ec2.EC2AuthorizeRevokeSecurityGroup;
+import com.cloud.bridge.service.core.ec2.EC2AvailabilityZonesFilterSet;
 import com.cloud.bridge.service.core.ec2.EC2CreateImage;
 import com.cloud.bridge.service.core.ec2.EC2CreateImageResponse;
 import com.cloud.bridge.service.core.ec2.EC2CreateKeyPair;
@@ -232,6 +233,12 @@ public class EC2SoapServiceImpl implements AmazonEC2SkeletonInterface
 {
 		if (null != items) {  // -> can be empty
 			for( int i=0; i < items.length; i++ ) request.addZone( items[i].getZoneName());
 		}
+
+        FilterSetType fst = dazt.getFilterSet();
+        if (fst != null) {
+            request.setFilterSet( toAvailabiltyZonesFilterSet(fst));
+        }
+
 		return toDescribeAvailabilityZonesResponse( engine.handleRequest( request ));
 	}
 
@@ -1065,6 +1072,29 @@ public class EC2SoapServiceImpl implements AmazonEC2SkeletonInterface
 {
 		return ifs;
 	}
 
+
+    private EC2AvailabilityZonesFilterSet toAvailabiltyZonesFilterSet( FilterSetType fst
)	{
+        EC2AvailabilityZonesFilterSet azfs = new EC2AvailabilityZonesFilterSet();
+
+        FilterType[] items = fst.getItem();
+        if (items != null) {
+            for (FilterType item : items) {
+                EC2Filter oneFilter = new EC2Filter();
+                String filterName = item.getName();
+                oneFilter.setName( filterName );
+
+                ValueSetType vft = item.getValueSet();
+                ValueType[] valueItems = vft.getItem();
+                for (ValueType valueItem : valueItems) {
+                    oneFilter.addValueEncoded( valueItem.getValue());
+                }
+                azfs.addFilter( oneFilter );
+            }
+        }
+        return azfs;
+    }
+	
+	
 	// toMethods
 	public static DescribeVolumesResponse toDescribeVolumesResponse( EC2DescribeVolumesResponse
engineResponse ) 
 	{
diff --git a/awsapi/src/com/cloud/bridge/service/core/ec2/EC2AvailabilityZonesFilterSet.java
b/awsapi/src/com/cloud/bridge/service/core/ec2/EC2AvailabilityZonesFilterSet.java
new file mode 100644
index 0000000..c8d9c7d
--- /dev/null
+++ b/awsapi/src/com/cloud/bridge/service/core/ec2/EC2AvailabilityZonesFilterSet.java
@@ -0,0 +1,99 @@
+/*
+ * Copyright (C) 2011 Citrix Systems, Inc.  All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.cloud.bridge.service.core.ec2;
+
+
+import java.text.ParseException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+
+import com.cloud.bridge.service.exception.EC2ServiceException;
+
+
+public class EC2AvailabilityZonesFilterSet {
+    protected List<EC2Filter> filterSet = new ArrayList<EC2Filter>();    
+
+    private Map<String,String> filterTypes = new HashMap<String,String>();
+
+    public EC2AvailabilityZonesFilterSet() {
+        // -> use these values to check that the proper filter is passed to this type
of filter set
+        filterTypes.put( "zone-name", "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
);
+
+        filterSet.add( param );
+    }
+
+    public EC2Filter[] getFilterSet() {
+        return filterSet.toArray(new EC2Filter[0]);
+    }
+
+    public List<String> evaluate( EC2DescribeAvailabilityZonesResponse availabilityZones)
throws ParseException	{
+        List<String> resultList = new ArrayList<String>();
+
+        boolean matched;
+
+        EC2Filter[] filterSet = getFilterSet();
+        for ( String availableZone : availabilityZones.getZoneSet() ) {
+            matched = true;
+            if (filterSet != null) {
+                for (EC2Filter filter : filterSet) {
+                    if (!filterMatched(availableZone, filter)) {
+                        matched = false;
+                        break;
+                    }
+                }
+            }
+            if (matched == true)
+                resultList.add(availableZone);
+        }
+        return resultList;
+    }
+
+    private boolean filterMatched( String availableZone, EC2Filter filter ) throws ParseException
{
+        String filterName = filter.getName();
+        String[] valueSet = filter.getValueSet();
+
+        if ( filterName.equalsIgnoreCase("zone-name")) {
+            return containsString(availableZone, valueSet);
+        } 
+        return false;
+    }
+
+    private boolean containsString( String lookingFor, String[] set ){
+        if (lookingFor == null) 
+            return false;
+
+        for (String filter: set) {
+            if (lookingFor.matches( filter )) return true;
+        }
+        return false;
+    }
+
+}
diff --git a/awsapi/src/com/cloud/bridge/service/core/ec2/EC2DescribeAvailabilityZones.java
b/awsapi/src/com/cloud/bridge/service/core/ec2/EC2DescribeAvailabilityZones.java
index ba44567..9da2fc4 100644
--- a/awsapi/src/com/cloud/bridge/service/core/ec2/EC2DescribeAvailabilityZones.java
+++ b/awsapi/src/com/cloud/bridge/service/core/ec2/EC2DescribeAvailabilityZones.java
@@ -21,6 +21,7 @@ import java.util.List;
 public class EC2DescribeAvailabilityZones {
 
 	private List<String> zoneSet = new ArrayList<String>();    // a list of strings
identifying zones
+    private EC2AvailabilityZonesFilterSet azfs = null;
 
 	public EC2DescribeAvailabilityZones() {
 	}
@@ -32,4 +33,13 @@ public class EC2DescribeAvailabilityZones {
 	public String[] getZoneSet() {
 		return zoneSet.toArray(new String[0]);
 	}
+	
+    public EC2AvailabilityZonesFilterSet getFilterSet() {
+        return azfs;
+    }
+
+    public void setFilterSet( EC2AvailabilityZonesFilterSet param ) {
+        azfs = param;
+    }
+
 }
diff --git a/awsapi/src/com/cloud/bridge/service/core/ec2/EC2Engine.java b/awsapi/src/com/cloud/bridge/service/core/ec2/EC2Engine.java
index b21940d..3c14b89 100644
--- a/awsapi/src/com/cloud/bridge/service/core/ec2/EC2Engine.java
+++ b/awsapi/src/com/cloud/bridge/service/core/ec2/EC2Engine.java
@@ -1120,10 +1120,16 @@ public class EC2Engine {
 	 */
 	public EC2DescribeAvailabilityZonesResponse handleRequest(EC2DescribeAvailabilityZones request)
{	
 		try {
-		    CloudStackAccount caller = getCurrentAccount();
-		    
-			return listZones(request.getZoneSet(), null);
-
+		    EC2DescribeAvailabilityZonesResponse availableZones = listZones(request.getZoneSet(),
null);
+            EC2AvailabilityZonesFilterSet azfs = request.getFilterSet();
+            if ( null == azfs )
+                return availableZones;
+            else {
+                List<String> matchedAvailableZones = azfs.evaluate(availableZones);
+                if (matchedAvailableZones.isEmpty())
+                    return new EC2DescribeAvailabilityZonesResponse();
+                return listZones(matchedAvailableZones.toArray(new String[0]), null);
+            }
 		} catch( EC2ServiceException error ) {
 			logger.error( "EC2 DescribeAvailabilityZones - ", error);
 			throw error;
-- 
1.7.5.4


Mime
View raw message