use of com.amazonaws.services.ec2.AmazonEC2AsyncClient in project photon-model by vmware.
the class TestAWSUtils method testResourceNaming.
@Test
public void testResourceNaming() throws Throwable {
boolean tagFound = false;
AmazonEC2AsyncClient client = TestUtils.getClient(this.privateKeyId, this.privateKey, this.region, false);
// create something to name
AWSNetworkClient svc = new AWSNetworkClient(client);
String vpcID = svc.createVPC("10.20.0.0/16");
AWSUtils.tagResourcesWithName(client, TEST_NAME, vpcID);
List<TagDescription> tags = AWSUtils.getResourceTags(vpcID, client);
for (TagDescription tagDesc : tags) {
if (tagDesc.getKey().equalsIgnoreCase(AWS_TAG_NAME)) {
assertTrue(tagDesc.getValue().equalsIgnoreCase(TEST_NAME));
tagFound = true;
break;
}
}
// ensure we found the tag
assertTrue(tagFound);
svc.deleteVPC(vpcID);
}
use of com.amazonaws.services.ec2.AmazonEC2AsyncClient in project photon-model by vmware.
the class TestAWSSetupUtils method checkInstancesStopped.
/**
* Checks if instances have their status set to stopped.
*
* @param client
* @param host
* @param instanceIdsToStop
* @param stopFlags
* @throws Throwable
*/
public static void checkInstancesStopped(AmazonEC2AsyncClient client, VerificationHost host, List<String> instanceIdsToStop, ArrayList<Boolean> stopFlags) throws Throwable {
AWSEnumerationAsyncHandler enumerationHandler = new AWSEnumerationAsyncHandler(host, AWSEnumerationAsyncHandler.MODE.CHECK_STOP, null, null, stopFlags, null, null);
DescribeInstancesRequest request = new DescribeInstancesRequest().withInstanceIds(instanceIdsToStop);
client.describeInstancesAsync(request, enumerationHandler);
// Waiting to get a response from AWS before the state computation is done for the list of
// VMs.
host.waitFor("Waiting to get response from AWS ", () -> {
return enumerationHandler.responseReceived;
});
}
use of com.amazonaws.services.ec2.AmazonEC2AsyncClient in project photon-model by vmware.
the class TestAWSSetupUtils method createOrGetDefaultSecurityGroupForGivenVPC.
/**
* Returns an existing security group for a VPC if it exists otherwise creates a new security group.
*/
public static SecurityGroup createOrGetDefaultSecurityGroupForGivenVPC(AmazonEC2AsyncClient client, String vpcID) {
List<SecurityGroup> securityGroupsInVPC = client.describeSecurityGroups().getSecurityGroups().stream().filter(sg -> sg.getVpcId().equals(vpcID)).collect(Collectors.toList());
if (securityGroupsInVPC != null && !securityGroupsInVPC.isEmpty()) {
for (SecurityGroup sg : securityGroupsInVPC) {
// Do not use newly provisioned security groups as this could interfere with the cleanup logic of other tests.
if (!sg.getGroupName().startsWith(AWS_NEW_GROUP_PREFIX)) {
return sg;
}
}
}
String securityGroupId = new AWSSecurityGroupClient(client).createDefaultSecurityGroup(vpcID);
tagResources(client, Arrays.asList(securityGroupId), TAG_KEY_FOR_TEST_RESOURCES, TAG_VALUE_FOR_TEST_RESOURCES + TAG_SG);
DescribeSecurityGroupsResult result = client.describeSecurityGroups(new DescribeSecurityGroupsRequest().withGroupIds(Arrays.asList(securityGroupId)));
return result.getSecurityGroups().get(0);
}
use of com.amazonaws.services.ec2.AmazonEC2AsyncClient in project photon-model by vmware.
the class TestAWSSetupUtils method tearDownTestVpc.
public static void tearDownTestVpc(AmazonEC2AsyncClient client, VerificationHost host, Map<String, Object> awsTestContext, boolean isMock) {
// if we feel the need to delete resources on every test run.
if (!isMock && awsTestContext.containsKey(DELETE_RESOURCES_KEY)) {
final String vpcId = (String) awsTestContext.get(VPC_KEY);
final String subnetId = (String) awsTestContext.get(SUBNET_KEY);
final String internetGatewayId = (String) awsTestContext.get(INTERNET_GATEWAY_KEY);
final String securityGroupId = (String) awsTestContext.get(SECURITY_GROUP_KEY);
// clean up VPC and all its dependencies if creating one at setUp
deleteSecurityGroupUsingEC2Client(client, host, securityGroupId);
SecurityGroup securityGroup = new AWSSecurityGroupClient(client).getSecurityGroup(AWS_DEFAULT_GROUP_NAME, vpcId);
if (securityGroup != null) {
deleteSecurityGroupUsingEC2Client(client, host, securityGroup.getGroupId());
}
deleteSubnet(client, subnetId);
detachInternetGateway(client, vpcId, internetGatewayId);
deleteInternetGateway(client, internetGatewayId);
deleteVPC(client, vpcId);
}
}
use of com.amazonaws.services.ec2.AmazonEC2AsyncClient in project photon-model by vmware.
the class AWSUtils method getOrCreateDefaultSecurityGroup.
public static List<String> getOrCreateDefaultSecurityGroup(AmazonEC2AsyncClient amazonEC2Client, AWSNicContext nicCtx) {
AWSSecurityGroupClient client = new AWSSecurityGroupClient(amazonEC2Client);
// in case no group is configured in the properties, attempt to discover the default one
if (nicCtx != null && nicCtx.vpc != null) {
try {
SecurityGroup group = client.getSecurityGroup(DEFAULT_SECURITY_GROUP_NAME, nicCtx.vpc.getVpcId());
if (group != null) {
return Arrays.asList(group.getGroupId());
}
} catch (AmazonServiceException t) {
if (!t.getMessage().contains(DEFAULT_SECURITY_GROUP_NAME)) {
throw t;
}
}
}
// if the group doesn't exist an exception is thrown. We won't throw a
// missing group exception
// we will continue and create the group
String groupId = client.createDefaultSecurityGroupWithDefaultRules(nicCtx.vpc);
return Collections.singletonList(groupId);
}
Aggregations