mirror of
https://github.com/apache/cloudstack.git
synced 2025-10-26 08:42:29 +01:00
CS-15122. Provide filter support for ec2-describe-availability-zones. Component: awsapi. Updated the header in the new file to conform with ASF guidelines as per review comments.
Signed-off-by: Likitha Shetty <likitha.shetty@citrix.com>
This commit is contained in:
parent
650386c994
commit
e135ee3eb1
@ -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 )
|
||||
{
|
||||
|
||||
@ -0,0 +1,102 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you 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;
|
||||
}
|
||||
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -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;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user