CS-14632 Not able to deploy VM in security groups using -g option for ec2-run-instances

Resolved. Updated the request and response for ec2-run-instances to
deploy VM in multiple security groups and output the corresponding security groups.
reviewed by: Prachi
This commit is contained in:
Likitha Shetty 2012-05-10 11:41:54 +05:30
parent 96dffa2155
commit 26f9cc6475
4 changed files with 70 additions and 36 deletions

View File

@ -618,7 +618,9 @@ public class EC2SoapServiceImpl implements AmazonEC2SkeletonInterface {
// -> we can only support one group per instance
if (null != gst) {
GroupItemType[] items = gst.getItem();
if (null != items && 0 < items.length) request.setGroupId( items[0].getGroupId());
if (null != items) {
for( int i=0; i < items.length; i++ ) request.addGroupName(items[i].getGroupId());
}
}
return toRunInstancesResponse( engine.runInstances( request ), engine);
}
@ -1468,12 +1470,6 @@ public class EC2SoapServiceImpl implements AmazonEC2SkeletonInterface {
param1.setReservationId( "" );
GroupSetType param2 = new GroupSetType();
GroupItemType param3 = new GroupItemType();
param3.setGroupId( "" );
param2.addItem( param3 );
param1.setGroupSet( param2 );
RunningInstancesSetType param6 = new RunningInstancesSetType();
EC2Instance[] instances = engineResponse.getInstanceSet();
for (EC2Instance inst : instances) {
@ -1487,6 +1483,21 @@ public class EC2SoapServiceImpl implements AmazonEC2SkeletonInterface {
param1.setOwnerId(ownerId);
String[] groups = inst.getGroupSet();
GroupSetType param2 = new GroupSetType();
if (null == groups || 0 == groups.length) {
GroupItemType param3 = new GroupItemType();
param3.setGroupId("");
param2.addItem( param3 );
} else {
for (String group : groups) {
GroupItemType param3 = new GroupItemType();
param3.setGroupId(group);
param2.addItem( param3 );
}
}
param1.setGroupSet(param2);
InstanceStateType param8 = new InstanceStateType();
param8.setCode( toAmazonCode( inst.getState()));
param8.setName( toAmazonStateName( inst.getState()));
@ -1570,7 +1581,7 @@ public class EC2SoapServiceImpl implements AmazonEC2SkeletonInterface {
}
param1.setInstancesSet( param6 );
param1.setRequesterId( "" );
param1.setRequestId( UUID.randomUUID().toString());
response.setRunInstancesResponse( param1 );
return response;

View File

@ -330,11 +330,11 @@ public class EC2Engine {
}
CloudStackSecurityGroupIngress resp = null;
if (ipPerm.getProtocol().equalsIgnoreCase("icmp")) {
resp = getApi().authorizeSecurityGroupIngress(null, constructCIDRList(ipPerm.getIpRangeSet()), null, null,
resp = getApi().authorizeSecurityGroupIngress(null, constructList(ipPerm.getIpRangeSet()), null, null,
ipPerm.getToPort().toString(), ipPerm.getFromPort().toString(), ipPerm.getProtocol(), null,
request.getName(), null, secGroupList);
} else {
resp = getApi().authorizeSecurityGroupIngress(null, constructCIDRList(ipPerm.getIpRangeSet()), null,
resp = getApi().authorizeSecurityGroupIngress(null, constructList(ipPerm.getIpRangeSet()), null,
ipPerm.getToPort().longValue(), null, null, ipPerm.getProtocol(), null, request.getName(),
ipPerm.getFromPort().longValue(), secGroupList);
}
@ -408,25 +408,7 @@ public class EC2Engine {
return null;
else return permRight.getRuleId();
}
/**
* Cloud Stack API takes a comma separated list of IP ranges as one parameter.
*
* @throws UnsupportedEncodingException
*/
private String constructCIDRList( String[] ipRanges ) throws UnsupportedEncodingException
{
if (null == ipRanges || 0 == ipRanges.length) return null;
StringBuffer cidrList = new StringBuffer();
for( int i=0; i < ipRanges.length; i++ ) {
if (0 < i) cidrList.append( "," );
cidrList.append( ipRanges[i] );
}
return cidrList.toString();
}
/**
* Returns a list of all snapshots
*
@ -1415,7 +1397,7 @@ public class EC2Engine {
CloudStackUserVm resp = getApi().deployVirtualMachine(svcOffering.getId(),
request.getTemplateId(), zoneId, null, null, null, null,
null, null, null, request.getKeyName(), null, (network != null ? network.getId() : null),
null, null, request.getSize().longValue(), request.getUserData());
null, constructList(request.getGroupSet()), request.getSize().longValue(), request.getUserData());
EC2Instance vm = new EC2Instance();
vm.setId(resp.getId().toString());
vm.setName(resp.getName());
@ -1423,9 +1405,11 @@ public class EC2Engine {
vm.setTemplateId(resp.getTemplateId().toString());
if (resp.getSecurityGroupList() != null && resp.getSecurityGroupList().size() > 0) {
// TODO, we have a list of security groups, just return the first one?
CloudStackSecurityGroup securityGroup = resp.getSecurityGroupList().get(0);
vm.setGroup(securityGroup.getName());
}
List<CloudStackSecurityGroup> securityGroupList = resp.getSecurityGroupList();
for (CloudStackSecurityGroup securityGroup : securityGroupList) {
vm.addGroupName(securityGroup.getName());
}
}
vm.setState(resp.getState());
vm.setCreated(resp.getCreated());
vm.setIpAddress(resp.getIpAddress());
@ -2265,4 +2249,20 @@ public class EC2Engine {
throw new EC2ServiceException(ServerError.InternalError, e.getMessage() != null ? e.getMessage() : "An unexpected error occurred.");
}
}
/**
* Cloud Stack API takes a comma separated list as a parameter.
*
* @throws UnsupportedEncodingException
*/
private String constructList( String[] elements ) throws UnsupportedEncodingException {
if (null == elements || 0 == elements.length) return null;
StringBuffer elementList = new StringBuffer();
for( int i=0; i < elements.length; i++ ) {
if (0 < i) elementList.append( "," );
elementList.append( elements[i] );
}
return elementList.toString();
}
}

View File

@ -15,7 +15,9 @@
*/
package com.cloud.bridge.service.core.ec2;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import com.cloud.bridge.util.EC2RestAuth;
@ -37,6 +39,7 @@ public class EC2Instance {
private String hypervisor;
private String rootDeviceType;
private String rootDeviceId;
private List<String> groupSet;
public EC2Instance() {
id = null;
@ -55,6 +58,7 @@ public class EC2Instance {
hypervisor = null;
rootDeviceType = null;
rootDeviceId = null;
groupSet = new ArrayList<String>();
}
public void setId( String id ) {
@ -182,6 +186,15 @@ public class EC2Instance {
}
public void setRootDeviceId(String param) {
rootDeviceId = param;
rootDeviceId = param;
}
public void addGroupName( String param ) {
groupSet.add( param );
}
public String[] getGroupSet() {
return groupSet.toArray(new String[0]);
}
}

View File

@ -15,6 +15,9 @@
*/
package com.cloud.bridge.service.core.ec2;
import java.util.ArrayList;
import java.util.List;
public class EC2RunInstances {
private String instanceType;
@ -26,7 +29,7 @@ public class EC2RunInstances {
private int maxCount;
private int minCount;
private Integer size; // <- in gigs
private List<String> groupSet = new ArrayList<String>();
public EC2RunInstances() {
instanceType = null;
@ -34,7 +37,7 @@ public class EC2RunInstances {
templateId = null;
groupId = null;
userData = null;
keyName = null;
keyName = null;
maxCount = 0;
minCount = 0;
size = 0;
@ -112,4 +115,11 @@ public class EC2RunInstances {
this.size = size;
}
public void addGroupName( String param ) {
groupSet.add( param );
}
public String[] getGroupSet() {
return groupSet.toArray(new String[0]);
}
}