use of com.amazonaws.services.ec2.AmazonEC2AsyncClient in project photon-model by vmware.
the class AWSComputeDiskDay2Service method startInstance.
/**
* start the instance and on success updates the disk and compute state to reflect the detach information.
*/
private void startInstance(AmazonEC2AsyncClient client, DiskContext c, DeferredResult<DiskContext> dr, OperationContext opCtx) {
StartInstancesRequest startRequest = new StartInstancesRequest();
startRequest.withInstanceIds(c.baseAdapterContext.child.id);
client.startInstancesAsync(startRequest, new AWSAsyncHandler<StartInstancesRequest, StartInstancesResult>() {
@Override
protected void handleError(Exception e) {
service.logSevere(() -> String.format("[AWSComputeDiskDay2Service] Failed to start the instance %s. %s", c.baseAdapterContext.child.id, Utils.toString(e)));
OperationContext.restoreOperationContext(opCtx);
c.error = e;
dr.complete(c);
}
@Override
protected void handleSuccess(StartInstancesRequest request, StartInstancesResult result) {
AWSUtils.waitForTransitionCompletion(getHost(), result.getStartingInstances(), "running", client, (is, e) -> {
if (e != null) {
service.logSevere(() -> String.format("[AWSComputeDiskDay2Service] Instance %s failed to reach " + "running state. %s", c.baseAdapterContext.child.id, Utils.toString(e)));
OperationContext.restoreOperationContext(opCtx);
c.error = e;
dr.complete(c);
return;
}
logInfo(() -> String.format("[AWSComputeDiskDay2Service] Successfully started the " + "instance %s", result.getStartingInstances().get(0).getInstanceId()));
updateComputeAndDiskState(dr, c, opCtx);
});
}
});
}
use of com.amazonaws.services.ec2.AmazonEC2AsyncClient in project photon-model by vmware.
the class TestAWSSetupUtils method setUpTestVpc.
public static void setUpTestVpc(AmazonEC2AsyncClient client, Map<String, Object> awsTestContext, boolean isMock, String zoneId) {
// If the pre-set VPC does not exist, get the test VPC for the given account and use it in the tests.
if (!isMock && !vpcIdExists(client, AWS_DEFAULT_VPC_ID)) {
String vpcId = createorGetVPCForAccount(client);
awsTestContext.put(VPC_KEY, vpcId);
Subnet subnet = createOrGetSubnet(client, AWS_DEFAULT_SUBNET_CIDR, vpcId, zoneId);
awsTestContext.put(SUBNET_KEY, subnet.getSubnetId());
String internetGatewayId = createOrGetInternetGatewayForGivenVPC(client, vpcId);
awsTestContext.put(INTERNET_GATEWAY_KEY, internetGatewayId);
SecurityGroup sg = createOrGetDefaultSecurityGroupForGivenVPC(client, vpcId);
awsTestContext.put(SECURITY_GROUP_KEY, sg.getGroupId());
awsTestContext.put(SECURITY_GROUP_NAME_KEY, sg.getGroupName());
NetSpec network = new NetSpec(vpcId, vpcId, AWS_DEFAULT_VPC_CIDR);
List<NetSpec> subnets = new ArrayList<>();
subnets.add(new NetSpec(subnet.getSubnetId(), AWS_DEFAULT_SUBNET_NAME, subnet.getCidrBlock(), zoneId == null ? TestAWSSetupUtils.zoneId + avalabilityZoneIdentifier : zoneId));
NicSpec nicSpec = NicSpec.create().withSubnetSpec(subnets.get(0)).withDynamicIpAssignment();
awsTestContext.put(NIC_SPECS_KEY, new AwsNicSpecs(network, Collections.singletonList(nicSpec)));
return;
}
awsTestContext.put(VPC_KEY, AWS_DEFAULT_VPC_ID);
awsTestContext.put(NIC_SPECS_KEY, SINGLE_NIC_SPEC);
awsTestContext.put(SUBNET_KEY, AWS_DEFAULT_SUBNET_ID);
awsTestContext.put(SECURITY_GROUP_KEY, AWS_DEFAULT_GROUP_ID);
awsTestContext.put(SECURITY_GROUP_NAME_KEY, AWS_DEFAULT_GROUP_NAME);
}
use of com.amazonaws.services.ec2.AmazonEC2AsyncClient in project photon-model by vmware.
the class TestAWSSetupUtils method createOrGetSubnet.
/**
* Creates a Subnet if not exist and return the Subnet id.
*/
public static Subnet createOrGetSubnet(AmazonEC2AsyncClient client, String subnetCidr, String vpcId, String zoneId) {
List<Filter> filters = new ArrayList<>();
Filter vpcFilter = new Filter();
vpcFilter.withName(VPC_KEY);
vpcFilter.withValues(vpcId);
filters.add(vpcFilter);
if (zoneId != null) {
Filter azFilter = new Filter();
azFilter.withName(AVAILABILITY_ZONE_FILTER);
azFilter.withValues(zoneId);
filters.add(azFilter);
}
DescribeSubnetsResult result = client.describeSubnets(new DescribeSubnetsRequest().withFilters(filters));
List<Subnet> defaultSubnets = new ArrayList<>();
result.getSubnets().stream().forEach(subnet -> {
subnet.getTags().stream().filter(tag -> tag.getKey().equalsIgnoreCase(TAG_KEY_FOR_TEST_RESOURCES) && tag.getValue().equalsIgnoreCase(AWS_DEFAULT_SUBNET_NAME)).forEach(tag -> defaultSubnets.add(subnet));
});
if (defaultSubnets != null && !defaultSubnets.isEmpty()) {
return defaultSubnets.get(0);
} else {
CreateSubnetRequest req = new CreateSubnetRequest().withCidrBlock(subnetCidr).withVpcId(vpcId);
if (zoneId != null) {
req.withAvailabilityZone(zoneId);
}
CreateSubnetResult res = client.createSubnet(req);
String subnetId = res.getSubnet().getSubnetId();
tagResources(client, Arrays.asList(subnetId), TAG_KEY_FOR_TEST_RESOURCES, AWS_DEFAULT_SUBNET_NAME);
return res.getSubnet();
}
}
use of com.amazonaws.services.ec2.AmazonEC2AsyncClient in project photon-model by vmware.
the class TestAWSSetupUtils method getAwsInstancesByIds.
/**
* Method to get Instance details directly from Amazon
*
* @throws Throwable
*/
public static List<Instance> getAwsInstancesByIds(AmazonEC2AsyncClient client, VerificationHost host, List<String> instanceIds) throws Throwable {
host.log("Getting instances with ids " + instanceIds + " from the AWS endpoint using the EC2 client.");
DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest().withInstanceIds(instanceIds);
DescribeInstancesResult describeInstancesResult = client.describeInstances(describeInstancesRequest);
return describeInstancesResult.getReservations().stream().flatMap(r -> r.getInstances().stream()).collect(Collectors.toList());
}
use of com.amazonaws.services.ec2.AmazonEC2AsyncClient in project photon-model by vmware.
the class TestAWSImageEnumerationTask method beforeTest.
@Before
public final void beforeTest() throws Throwable {
CommandLineArgumentParser.parseFromProperties(this);
if (this.isMock) {
return;
}
if (AMAZON_PARAVIRTUAL_IMAGE_NAME == null || AMAZON_HVM_IMAGE_NAME == null) {
// create credentials
AuthCredentialsServiceState creds = new AuthCredentialsServiceState();
creds.privateKey = this.secretKey;
creds.privateKeyId = this.accessKey;
AmazonEC2AsyncClient client = AWSUtils.getAsyncClient(creds, this.regionId, getExecutor());
// Get arbitrary PARAVIRTUAL image name
AMAZON_PARAVIRTUAL_IMAGE_NAME = lookupAwsImage(client, AWSConstants.AWS_IMAGE_VIRTUALIZATION_TYPE_PARAVIRTUAL);
// Get arbitrary HVM image name
AMAZON_HVM_IMAGE_NAME = lookupAwsImage(client, AWSConstants.AWS_IMAGE_VIRTUALIZATION_TYPE_HVM);
{
Filter nameFilter = new Filter("name").withValues(AMAZON_PARAVIRTUAL_IMAGE_NAME);
// Serialize the list of filters to JSON string
AMAZON_PUBLIC_IMAGE_FILTER_SINGLE = Utils.toJson(Arrays.asList(nameFilter));
}
{
Filter nameFilter = new Filter("name").withValues(AMAZON_PARAVIRTUAL_IMAGE_NAME, AMAZON_HVM_IMAGE_NAME);
// Serialize the list of filters to JSON string
AMAZON_PUBLIC_IMAGE_FILTER_PARTITIONING = Utils.toJson(Arrays.asList(nameFilter));
}
}
}
Aggregations