-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for storing subnet id in ec2instance and report vpc for stored subnet id in describeInstances #58
base: master
Are you sure you want to change the base?
Changes from all commits
651a140
6512cee
a25ae2a
f805ad4
030d256
f1daaca
87bcbd0
22725b0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
package com.tlswe.awsmock.ec2.control; | ||
|
||
import java.io.IOException; | ||
import java.sql.Date; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
|
@@ -20,7 +19,6 @@ | |
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.commons.lang3.math.NumberUtils; | ||
import org.joda.time.DateTime; | ||
import org.omg.CORBA.SystemException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
|
@@ -90,7 +88,6 @@ | |
import com.tlswe.awsmock.ec2.cxf_generated.SecurityGroupItemType; | ||
import com.tlswe.awsmock.ec2.cxf_generated.SecurityGroupSetType; | ||
import com.tlswe.awsmock.ec2.cxf_generated.StartInstancesResponseType; | ||
import com.tlswe.awsmock.ec2.cxf_generated.StateReasonType; | ||
import com.tlswe.awsmock.ec2.cxf_generated.StopInstancesResponseType; | ||
import com.tlswe.awsmock.ec2.cxf_generated.SubnetSetType; | ||
import com.tlswe.awsmock.ec2.cxf_generated.SubnetType; | ||
|
@@ -501,8 +498,14 @@ public void handle(final Map<String, String[]> queryParams, | |
int minCount = Integer.parseInt(queryParams.get("MinCount")[0]); | ||
int maxCount = Integer.parseInt(queryParams.get("MaxCount")[0]); | ||
|
||
final String[] subnetIdArray = queryParams.get("SubnetId"); | ||
String subnetId = null; | ||
if (subnetIdArray != null) { | ||
subnetId = subnetIdArray[0]; | ||
} | ||
|
||
responseXml = JAXBUtil.marshall( | ||
runInstances(imageID, instanceType, minCount, maxCount), | ||
runInstances(imageID, instanceType, minCount, maxCount, subnetId), | ||
"RunInstancesResponse", version); | ||
|
||
} else if ("DescribeImages".equals(action)) { | ||
|
@@ -1139,9 +1142,9 @@ private DescribeInstancesResponseType describeInstances(final Set<String> instan | |
instItem.setDnsName(instance.getPubDns()); | ||
|
||
// set network information | ||
instItem.setVpcId(MOCK_VPC_ID); | ||
instItem.setVpcId(getVpcForSubnetId(instance.getSubnetId())); | ||
instItem.setPrivateIpAddress(MOCK_PRIVATE_IP_ADDRESS); | ||
instItem.setSubnetId(MOCK_SUBNET_ID); | ||
instItem.setSubnetId(instance.getSubnetId()); | ||
|
||
instsSet.getItem().add(instItem); | ||
|
||
|
@@ -1180,7 +1183,8 @@ protected String generateToken() { | |
} | ||
|
||
/** | ||
* Handles "runInstances" request, with only simplified filters of imageId, instanceType, minCount and maxCount. | ||
* Handles "runInstances" request, with only simplified filters of imageId, instanceType, minCount, maxCount | ||
* and subnetId. | ||
* | ||
* @param imageId | ||
* AMI of new mock ec2 instance(s) | ||
|
@@ -1190,10 +1194,12 @@ protected String generateToken() { | |
* max count of instances to run | ||
* @param maxCount | ||
* min count of instances to run | ||
* @param subnetId | ||
* subnet ID of new mock ec2 instance(s). | ||
* @return a RunInstancesResponse that includes all information for the started new mock ec2 instances | ||
*/ | ||
private RunInstancesResponseType runInstances(final String imageId, final String instanceType, | ||
final int minCount, final int maxCount) { | ||
final int minCount, final int maxCount, final String subnetId) { | ||
|
||
RunInstancesResponseType ret = new RunInstancesResponseType(); | ||
|
||
|
@@ -1215,7 +1221,7 @@ private RunInstancesResponseType runInstances(final String imageId, final String | |
List<AbstractMockEc2Instance> newInstances = null; | ||
|
||
newInstances = mockEc2Controller | ||
.runInstances(clazzOfMockEc2Instance, imageId, instanceType, minCount, maxCount); | ||
.runInstances(clazzOfMockEc2Instance, imageId, instanceType, minCount, maxCount, subnetId); | ||
|
||
for (AbstractMockEc2Instance i : newInstances) { | ||
RunningInstancesItemType instItem = new RunningInstancesItemType(); | ||
|
@@ -1230,9 +1236,9 @@ private RunInstancesResponseType runInstances(final String imageId, final String | |
instItem.setPlacement(DEFAULT_MOCK_PLACEMENT); | ||
|
||
// set network information | ||
instItem.setVpcId(MOCK_VPC_ID); | ||
instItem.setSubnetId(subnetId); | ||
instItem.setVpcId(getVpcForSubnetId(subnetId)); | ||
instItem.setPrivateIpAddress(MOCK_PRIVATE_IP_ADDRESS); | ||
instItem.setSubnetId(MOCK_SUBNET_ID); | ||
|
||
instSet.getItem().add(instItem); | ||
|
||
|
@@ -2048,4 +2054,19 @@ private String getBlankResponseXml() { | |
} | ||
return ret; | ||
} | ||
|
||
/** | ||
* Gets the VPC id for a given subnetId. | ||
* | ||
* @param subnetId The subnet id. | ||
* @return The VPC id. Returns null, if no matching subnet is found. | ||
*/ | ||
private String getVpcForSubnetId(final String subnetId) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be more meaningful if we change the name to getVpcIdForSubnet There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed |
||
for (MockSubnet subnet : mockSubnetController.describeSubnets()) { | ||
if (subnet.getSubnetId().equals(subnetId)) { | ||
return subnet.getVpcId(); | ||
} | ||
} | ||
return null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since it's always expected that a VPC is tied to a subnet - it's better if we throw an Exception than returning null. It will also be difficult to catch if for some reason we do return null. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we use a default value for not provided subnetIds, then we need here either a check for MOCK_SUBNET_ID where we return MOCK_VPC_ID. Or we just return MOCK_VPC_ID by default.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel we can return |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ public class DefaultMockEc2Instance extends AbstractMockEc2Instance { | |
* | ||
* @see Serializable | ||
*/ | ||
private static final long serialVersionUID = 1L; | ||
private static final long serialVersionUID = 2L; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you explain why the version is incremented for this class? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are right, the base model would be a better place for the increment. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah not necessary a change here. |
||
|
||
@Override | ||
public void onStarted() { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if
SubnetId
is not provided? Can we allocate a random uuid as the subnetId for an instance?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another possibilty would be, that we use the MOCK_SUBNET_ID as default.
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that we use MOCK_SUBNET_ID as default.