CLOUDSTACK-1128: [EC2 Query API] DescribeAvailabilityZones, support for message filter

Add message in the response element of EC2DesrcibeAvailibilityZones. Add support for filter 'message'.
The value of 'message' should be set to the allocation_state of the zone
Code cleanup
This commit is contained in:
Prachi Damle 2013-02-12 13:34:49 -08:00
parent 5828e526b3
commit cd039206d3
5 changed files with 113 additions and 57 deletions

View File

@ -41,6 +41,7 @@ import com.cloud.bridge.service.core.ec2.EC2DescribeAvailabilityZones;
import com.cloud.bridge.service.core.ec2.EC2DescribeAvailabilityZonesResponse;
import com.cloud.bridge.service.core.ec2.EC2DescribeImageAttribute;
import com.cloud.bridge.service.core.ec2.EC2AvailabilityZone;
import com.cloud.bridge.service.core.ec2.EC2DescribeImages;
import com.cloud.bridge.service.core.ec2.EC2DescribeImagesResponse;
import com.cloud.bridge.service.core.ec2.EC2DescribeInstances;
@ -1775,14 +1776,18 @@ public class EC2SoapServiceImpl implements AmazonEC2SkeletonInterface {
DescribeAvailabilityZonesResponse response = new DescribeAvailabilityZonesResponse();
DescribeAvailabilityZonesResponseType param1 = new DescribeAvailabilityZonesResponseType();
AvailabilityZoneSetType param2 = new AvailabilityZoneSetType();
String[] zones = engineResponse.getZoneSet();
for (String zone : zones) {
EC2AvailabilityZone[] zones = engineResponse.getAvailabilityZoneSet();
for (EC2AvailabilityZone zone : zones) {
AvailabilityZoneItemType param3 = new AvailabilityZoneItemType();
AvailabilityZoneMessageSetType param4 = new AvailabilityZoneMessageSetType();
param3.setZoneName( zone );
param3.setZoneName( zone.getName() );
param3.setZoneState( "available" );
param3.setRegionName( "" );
AvailabilityZoneMessageSetType param4 = new AvailabilityZoneMessageSetType();
AvailabilityZoneMessageType param5 = new AvailabilityZoneMessageType();
param5.setMessage(zone.getMessage());
param4.addItem(param5);
param3.setMessageSet( param4 );
param2.addItem( param3 );
}

View File

@ -0,0 +1,55 @@
// 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;
public class EC2AvailabilityZone {
private String id;
private String name;
private String message;
public EC2AvailabilityZone() {
id = null;
name = null;
message = null;
}
public void setId( String id ) {
this.id = id;
}
public String getId() {
return this.id;
}
public void setName( String name ) {
this.name = name;
}
public String getName() {
return this.name;
}
public void setMessage( String message ) {
this.message = message;
}
public String getMessage() {
return this.message;
}
}

View File

@ -36,6 +36,7 @@ public class EC2AvailabilityZonesFilterSet {
public EC2AvailabilityZonesFilterSet() {
// -> use these values to check that the proper filter is passed to this type of filter set
filterTypes.put( "zone-name", "String" );
filterTypes.put( "message", "String");
}
public void addFilter( EC2Filter param ) {
@ -55,13 +56,14 @@ public class EC2AvailabilityZonesFilterSet {
return filterSet.toArray(new EC2Filter[0]);
}
public List<String> evaluate( EC2DescribeAvailabilityZonesResponse availabilityZones) throws ParseException {
List<String> resultList = new ArrayList<String>();
public EC2DescribeAvailabilityZonesResponse evaluate( EC2DescribeAvailabilityZonesResponse availabilityZones)
throws ParseException {
EC2DescribeAvailabilityZonesResponse resultList = new EC2DescribeAvailabilityZonesResponse();
boolean matched;
EC2Filter[] filterSet = getFilterSet();
for ( String availableZone : availabilityZones.getZoneSet() ) {
for ( EC2AvailabilityZone availableZone : availabilityZones.getAvailabilityZoneSet() ) {
matched = true;
if (filterSet != null) {
for (EC2Filter filter : filterSet) {
@ -71,19 +73,22 @@ public class EC2AvailabilityZonesFilterSet {
}
}
}
if (matched == true)
resultList.add(availableZone);
if (matched)
resultList.addAvailabilityZone(availableZone);
}
return resultList;
}
private boolean filterMatched( String availableZone, EC2Filter filter ) throws ParseException {
private boolean filterMatched( EC2AvailabilityZone availableZone, EC2Filter filter ) throws ParseException {
String filterName = filter.getName();
String[] valueSet = filter.getValueSet();
if ( filterName.equalsIgnoreCase("zone-name")) {
return containsString(availableZone, valueSet);
}
return containsString(availableZone.getName(), valueSet);
}
else if (filterName.equalsIgnoreCase("message")) {
return containsString(availableZone.getMessage(), valueSet);
}
return false;
}

View File

@ -20,31 +20,17 @@ import java.util.ArrayList;
import java.util.List;
public class EC2DescribeAvailabilityZonesResponse {
private List<EC2AvailabilityZone> availabilityZoneSet = new ArrayList<EC2AvailabilityZone>();
private List<String> zoneIds = new ArrayList<String>();
private List<String> zoneNames = new ArrayList<String>();
public EC2DescribeAvailabilityZonesResponse() {
}
public void addAvailabilityZone( EC2AvailabilityZone param ) {
availabilityZoneSet.add( param );
}
public EC2AvailabilityZone[] getAvailabilityZoneSet() {
return availabilityZoneSet.toArray(new EC2AvailabilityZone[0]);
}
public EC2DescribeAvailabilityZonesResponse() {
}
public void addZone(String id, String name) {
zoneIds.add(id);
zoneNames.add(name);
}
/**
* The Amazon API only cares about the names of zones not their ID value.
*
* @return an array containing a set of zone names
*/
public String[] getZoneSet() {
return zoneNames.toArray(new String[0]);
}
public String getZoneIdAt(int index) {
if (zoneIds.isEmpty() || index >= zoneIds.size()) {
return null;
}
return zoneIds.get(index);
}
}

View File

@ -1060,12 +1060,8 @@ public class EC2Engine extends ManagerBase {
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);
}
else
return azfs.evaluate(availableZones);
} catch( EC2ServiceException error ) {
logger.error( "EC2 DescribeAvailabilityZones - ", error);
throw error;
@ -1691,9 +1687,11 @@ public class EC2Engine extends ManagerBase {
zones = listZones(interestedZones, domainId);
if (zones == null || zones.getZoneIdAt( 0 ) == null)
if (zones == null || zones.getAvailabilityZoneSet().length == 0)
throw new EC2ServiceException(ClientError.InvalidParameterValue, "Unknown zoneName value - " + zoneName);
return zones.getZoneIdAt(0);
EC2AvailabilityZone[] zoneSet = zones.getAvailabilityZoneSet();
return zoneSet[0].getId();
}
@ -1768,24 +1766,31 @@ public class EC2Engine extends ManagerBase {
*
* @return EC2DescribeAvailabilityZonesResponse
*/
private EC2DescribeAvailabilityZonesResponse listZones(String[] interestedZones, String domainId) throws Exception
{
private EC2DescribeAvailabilityZonesResponse listZones(String[] interestedZones, String domainId)
throws Exception {
EC2DescribeAvailabilityZonesResponse zones = new EC2DescribeAvailabilityZonesResponse();
List<CloudStackZone> cloudZones = getApi().listZones(true, domainId, null, null);
if(cloudZones != null) {
if(cloudZones != null && cloudZones.size() > 0) {
for(CloudStackZone cloudZone : cloudZones) {
if ( null != interestedZones && 0 < interestedZones.length ) {
for( int j=0; j < interestedZones.length; j++ ) {
if (interestedZones[j].equalsIgnoreCase( cloudZone.getName())) {
zones.addZone(cloudZone.getId().toString(), cloudZone.getName());
boolean matched = false;
if (interestedZones.length > 0) {
for (String zoneName : interestedZones){
if (zoneName.equalsIgnoreCase( cloudZone.getName())) {
matched = true;
break;
}
}
} else {
zones.addZone(cloudZone.getId().toString(), cloudZone.getName());
} else {
matched = true;
}
if (!matched) continue;
EC2AvailabilityZone ec2Zone = new EC2AvailabilityZone();
ec2Zone.setId(cloudZone.getId().toString());
ec2Zone.setMessage(cloudZone.getAllocationState());
ec2Zone.setName(cloudZone.getName());
zones.addAvailabilityZone(ec2Zone);
}
}
return zones;
@ -1952,7 +1957,7 @@ public class EC2Engine extends ManagerBase {
* @throws ParserConfigurationException
* @throws ParseException
*/
public EC2DescribeSecurityGroupsResponse listSecurityGroups( String[] interestedGroups ) throws Exception {
private EC2DescribeSecurityGroupsResponse listSecurityGroups( String[] interestedGroups ) throws Exception {
try {
EC2DescribeSecurityGroupsResponse groupSet = new EC2DescribeSecurityGroupsResponse();