use of software.amazon.awssdk.services.ec2.model.Instance in project photon-model by vmware.
the class TestAWSProvisionTask method testProvision.
// Creates a AWS instance via a provision task.
@Test
public void testProvision() throws Throwable {
initResourcePoolAndComputeHost();
// create a AWS VM compute resoruce
boolean addNonExistingSecurityGroup = true;
this.vmState = createAWSVMResource(this.host, this.computeHost, this.endpointState, this.getClass(), this.currentTestName.getMethodName() + "_vm1", zoneId, regionId, null, /* tagLinks */
this.singleNicSpec, addNonExistingSecurityGroup, this.awsTestContext);
// set placement link
String zoneId = TestAWSSetupUtils.zoneId + avalabilityZoneIdentifier;
ComputeState zoneComputeState = createAWSComputeHost(this.host, this.endpointState, zoneId, regionId, this.isAwsClientMock, this.awsMockEndpointReference, null);
zoneComputeState.id = zoneId;
zoneComputeState = TestUtils.doPatch(this.host, zoneComputeState, ComputeState.class, UriUtils.buildUri(this.host, zoneComputeState.documentSelfLink));
if (this.vmState.customProperties == null) {
this.vmState.customProperties = new HashMap<>();
}
this.vmState.customProperties.put(PLACEMENT_LINK, zoneComputeState.documentSelfLink);
TestUtils.doPatch(this.host, this.vmState, ComputeState.class, UriUtils.buildUri(this.host, this.vmState.documentSelfLink));
// kick off a provision task to do the actual VM creation
ProvisionComputeTaskState provisionTask = new ProvisionComputeTaskService.ProvisionComputeTaskState();
provisionTask.computeLink = this.vmState.documentSelfLink;
provisionTask.isMockRequest = this.isMock;
provisionTask.taskSubStage = ProvisionComputeTaskState.SubStage.CREATING_HOST;
// Wait for default request timeout in minutes for the machine to be powered ON before
// reporting failure to the parent task.
provisionTask.documentExpirationTimeMicros = Utils.getNowMicrosUtc() + TimeUnit.MINUTES.toMicros(AWS_VM_REQUEST_TIMEOUT_MINUTES);
provisionTask.tenantLinks = this.endpointState.tenantLinks;
provisionTask = TestUtils.doPost(this.host, provisionTask, ProvisionComputeTaskState.class, UriUtils.buildUri(this.host, ProvisionComputeTaskService.FACTORY_LINK));
this.host.waitForFinishedTask(ProvisionComputeTaskState.class, provisionTask.documentSelfLink);
// check that the VM has been created
ProvisioningUtils.queryComputeInstances(this.host, 3);
if (!this.isMock) {
ComputeState compute = getCompute(this.host, this.vmState.documentSelfLink);
List<Instance> instances = getAwsInstancesByIds(this.client, this.host, Collections.singletonList(compute.id));
Instance instance = instances.get(0);
assertTags(Collections.emptySet(), instance, this.vmState.name);
assertVmNetworksConfiguration(instance);
assertStorageConfiguration(this.client, instance, compute);
assertEquals(zoneId, instance.getPlacement().getAvailabilityZone());
}
this.host.setTimeoutSeconds(600);
this.host.waitFor("Error waiting for stats with default collection windows", () -> {
try {
this.host.log(Level.INFO, "Issuing stats request for VM with default collection window.");
issueStatsRequest(this.vmState, null);
} catch (Throwable t) {
return false;
}
return true;
});
// store the network links and disk links for removal check later
List<String> resourcesToDelete = new ArrayList<>();
if (this.vmState.diskLinks != null) {
resourcesToDelete.addAll(this.vmState.diskLinks);
}
if (this.vmState.networkInterfaceLinks != null) {
resourcesToDelete.addAll(this.vmState.networkInterfaceLinks);
}
// delete vm
TestAWSSetupUtils.deleteVMs(this.vmState.documentSelfLink, this.isMock, this.host);
// validates the local documents of network links and disk links have been removed
verifyRemovalOfResourceState(this.host, resourcesToDelete);
// create another AWS VM
List<String> instanceIdList = new ArrayList<>();
Set<TagState> tags = createTags(null, "testProvisionKey1", "testProvisionValue1", "testProvisionKey2", "testProvisionValue2");
Set<String> tagLinks = tags.stream().map(t -> t.documentSelfLink).collect(Collectors.toSet());
addNonExistingSecurityGroup = false;
this.vmState = createAWSVMResource(this.host, this.computeHost, this.endpointState, this.getClass(), this.currentTestName.getMethodName() + "_vm2", TestAWSSetupUtils.zoneId, regionId, tagLinks, this.singleNicSpec, addNonExistingSecurityGroup, this.awsTestContext);
TestAWSSetupUtils.provisionMachine(this.host, this.vmState, this.isMock, instanceIdList);
if (!this.isMock) {
ComputeState compute = getCompute(this.host, this.vmState.documentSelfLink);
List<Instance> instances = getAwsInstancesByIds(this.client, this.host, Collections.singletonList(compute.id));
assertTags(tags, instances.get(0), this.vmState.name);
assertVmNetworksConfiguration(instances.get(0));
assertStorageConfiguration(this.client, instances.get(0), compute);
// reach out to AWS and get the current state
TestAWSSetupUtils.getBaseLineInstanceCount(this.host, this.client, null);
}
// delete just the local representation of the resource
TestAWSSetupUtils.deleteVMs(this.vmState.documentSelfLink, this.isMock, this.host, true);
if (!this.isMock) {
try {
TestAWSSetupUtils.getBaseLineInstanceCount(this.host, this.client, null);
} finally {
TestAWSSetupUtils.deleteVMsUsingEC2Client(this.client, this.host, instanceIdList);
deleteSecurityGroupUsingEC2Client(this.client, this.host, this.sgToCleanUp);
}
}
this.vmState = null;
this.sgToCleanUp = null;
}
use of software.amazon.awssdk.services.ec2.model.Instance in project photon-model by vmware.
the class TestAWSProvisionTask method assertTags.
private void assertTags(Set<TagState> expectedTagStates, Instance instance, String instanceName) {
Set<Tag> expectedTags = expectedTagStates.stream().map(ts -> new Tag(ts.key, ts.value)).collect(Collectors.toSet());
Set<Tag> actualTags = new HashSet<>(instance.getTags());
// account for the name tag
assertEquals(expectedTags.size() + 1, actualTags.size());
assertTrue(actualTags.containsAll(expectedTags));
Tag nameTag = new Tag(AWSConstants.AWS_TAG_NAME, instanceName);
assertTrue(actualTags.contains(nameTag));
}
use of software.amazon.awssdk.services.ec2.model.Instance in project photon-model by vmware.
the class AWSUtils method getOrCreateSecurityGroups.
/*
* method will create new or validate existing security group has the necessary settings for CM
* to function. It will return the security group id that is required during instance
* provisioning. for each nicContext element provided, for each of its securityGroupStates,
* security group is discovered from AWS in case that there are no securityGroupStates, security
* group ID is obtained from the custom properties in case that none of the above methods
* discover a security group, the default one is discovered from AWS in case that none of the
* above method discover a security group, a new security group is created
*/
public static List<String> getOrCreateSecurityGroups(AWSInstanceContext aws, AWSNicContext nicCtx) {
String groupId;
SecurityGroup group;
List<String> groupIds = new ArrayList<>();
AWSSecurityGroupClient client = new AWSSecurityGroupClient(aws.amazonEC2Client);
if (nicCtx != null) {
if (nicCtx.securityGroupStates != null && !nicCtx.securityGroupStates.isEmpty()) {
List<String> securityGroupNames = nicCtx.securityGroupStates.stream().map(securityGroupState -> securityGroupState.name).collect(Collectors.toList());
List<SecurityGroup> securityGroups = client.getSecurityGroups(new ArrayList<>(securityGroupNames), nicCtx.vpc.getVpcId());
for (SecurityGroup securityGroup : securityGroups) {
groupIds.add(securityGroup.getGroupId());
}
return groupIds;
}
}
// use the security group provided in the description properties
String sgId = getFromCustomProperties(aws.child.description, AWSConstants.AWS_SECURITY_GROUP_ID);
if (sgId != null) {
return Arrays.asList(sgId);
}
// in case no group is configured in the properties, attempt to discover the default one
if (nicCtx != null && nicCtx.vpc != null) {
try {
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
groupId = createSecurityGroupOnDefaultVPC(aws);
return Collections.singletonList(groupId);
}
use of software.amazon.awssdk.services.ec2.model.Instance in project photon-model by vmware.
the class AWSComputeDiskDay2Service method getAvailableDeviceName.
private String getAvailableDeviceName(DiskContext context, String instanceId) {
DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest().withInstanceIds(instanceId);
DescribeInstancesResult instancesResult = context.amazonEC2Client.describeInstances(describeInstancesRequest);
List<InstanceBlockDeviceMapping> blockDeviceMappings = null;
AWSSupportedOS platform = null;
AWSSupportedVirtualizationTypes virtualizationTypes = null;
String instanceType = null;
for (Reservation reservation : instancesResult.getReservations()) {
for (Instance instance : reservation.getInstances()) {
if (instance.getInstanceId().equals(instanceId)) {
blockDeviceMappings = instance.getBlockDeviceMappings();
platform = AWSSupportedOS.get(instance.getPlatform());
virtualizationTypes = AWSSupportedVirtualizationTypes.get(instance.getVirtualizationType());
instanceType = instance.getInstanceType();
break;
}
}
}
String deviceName = null;
if (blockDeviceMappings != null) {
List<String> usedDeviceNames = getUsedDeviceNames(blockDeviceMappings);
List<String> availableDiskNames = AWSBlockDeviceNameMapper.getAvailableNames(platform, virtualizationTypes, AWSStorageType.EBS, instanceType, usedDeviceNames);
deviceName = availableDiskNames.get(0);
}
return deviceName;
}
use of software.amazon.awssdk.services.ec2.model.Instance 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);
});
}
});
}
Aggregations