use of software.amazon.awssdk.services.ec2.model.Reservation in project photon-model by vmware.
the class AWSRemoteCleanup method deleteAwsEc2instances.
private void deleteAwsEc2instances(List<String> vpcIdsToBeDeleted, DescribeInstancesResult describeInstancesResult, AmazonEC2 ec2Client) {
List<String> instanceIdsToBeDeleted = new ArrayList<>();
List<Reservation> reservations = describeInstancesResult.getReservations();
for (Reservation reservation : reservations) {
List<Instance> instances = reservation.getInstances();
for (Instance instance : instances) {
long instanceLaunchTimeMicros = TimeUnit.MILLISECONDS.toMicros(instance.getLaunchTime().getTime());
long timeDifference = Utils.getNowMicrosUtc() - instanceLaunchTimeMicros;
if (timeDifference > TimeUnit.HOURS.toMicros(1) && vpcIdsToBeDeleted.contains(instance.getVpcId()) && shouldDelete(instance)) {
this.host.log(Level.INFO, "Marking %s instance for deletion", instance.getInstanceId());
instanceIdsToBeDeleted.add(instance.getInstanceId());
}
}
}
triggerEC2Deletion(instanceIdsToBeDeleted, ec2Client);
}
use of software.amazon.awssdk.services.ec2.model.Reservation 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.Reservation in project Gatekeeper by FINRAOS.
the class Ec2LookupServiceTests method fakeInstance.
private Reservation fakeInstance(String instanceID, String instanceIP, String instanceName, String instanceApplication, String platform) {
Reservation container = new Reservation();
List<Instance> instances = new ArrayList<>();
Instance i = new Instance();
List<Tag> tags = new ArrayList<>();
i.setInstanceId(instanceID);
i.setPrivateIpAddress(instanceIP);
Tag nameTag = new Tag();
nameTag.setKey("Name");
nameTag.setValue(instanceName);
Tag applicationTag = new Tag();
applicationTag.setKey("Application");
applicationTag.setValue(instanceApplication);
tags.add(applicationTag);
tags.add(nameTag);
i.setTags(tags);
i.setPlatform(platform);
instances.add(i);
container.setInstances(instances);
return container;
}
use of software.amazon.awssdk.services.ec2.model.Reservation in project Gatekeeper by FINRAOS.
the class Ec2LookupServiceTests method before.
@Before
public void before() {
awsEnvironment = new AWSEnvironment("Dev", "us-west-2");
mockedResult = new DescribeInstancesResult();
mockedResult.setReservations(Arrays.asList(new Reservation[] { fakeInstance("i-12345", "1.2.3.4", "TestOne", "TEP", "Linux"), fakeInstance("i-abcde", "123.2.3.4", "TestOneTwo", "TST", "Linux"), fakeInstance("i-123ab", "456.2.3.4", "HelloOne", "TEP", "Linux"), fakeInstance("i-456cd", "123.22.3.4", "HelloTwo", "TEP", "Linux"), fakeInstance("i-12347", "132.23.43.4", "TestThree", "TEP", "Linux") }));
Mockito.when(gatekeeperEC2Properties.getAppIdentityTag()).thenReturn("Application");
Mockito.when(awsSessionService.getEC2Session(any())).thenReturn(amazonEC2Client);
Mockito.when(amazonEC2Client.describeInstances(any())).thenReturn(mockedResult);
}
use of software.amazon.awssdk.services.ec2.model.Reservation in project cloudbreak by hortonworks.
the class AwsInstanceConnector method check.
@Override
public List<CloudVmInstanceStatus> check(AuthenticatedContext ac, List<CloudInstance> vms) {
List<CloudVmInstanceStatus> cloudVmInstanceStatuses = new ArrayList<>();
for (CloudInstance vm : vms) {
try {
String region = ac.getCloudContext().getLocation().getRegion().value();
DescribeInstancesResult result = awsClient.createAccess(new AwsCredentialView(ac.getCloudCredential()), ac.getCloudContext().getLocation().getRegion().value()).describeInstances(new DescribeInstancesRequest().withInstanceIds(vm.getInstanceId()));
for (Reservation reservation : result.getReservations()) {
for (Instance instance : reservation.getInstances()) {
if ("Stopped".equalsIgnoreCase(instance.getState().getName())) {
LOGGER.info("AWS instance [{}] is in {} state, region: {}, stack: {}", instance.getInstanceId(), instance.getState().getName(), region, ac.getCloudContext().getId());
cloudVmInstanceStatuses.add(new CloudVmInstanceStatus(vm, InstanceStatus.STOPPED));
} else if ("Running".equalsIgnoreCase(instance.getState().getName())) {
LOGGER.info("AWS instance [{}] is in {} state, region: {}, stack: {}", instance.getInstanceId(), instance.getState().getName(), region, ac.getCloudContext().getId());
cloudVmInstanceStatuses.add(new CloudVmInstanceStatus(vm, InstanceStatus.STARTED));
} else if ("Terminated".equalsIgnoreCase(instance.getState().getName())) {
LOGGER.info("AWS instance [{}] is in {} state, region: {}, stack: {}", instance.getInstanceId(), instance.getState().getName(), region, ac.getCloudContext().getId());
cloudVmInstanceStatuses.add(new CloudVmInstanceStatus(vm, InstanceStatus.TERMINATED));
} else {
LOGGER.info("AWS instance [{}] is in {} state, region: {}, stack: {}", instance.getInstanceId(), instance.getState().getName(), region, ac.getCloudContext().getId());
cloudVmInstanceStatuses.add(new CloudVmInstanceStatus(vm, InstanceStatus.IN_PROGRESS));
}
}
}
} catch (AmazonEC2Exception e) {
LOGGER.warn("Instance does not exist with this id: {}, original message: {}", vm.getInstanceId(), e.getMessage());
}
}
return cloudVmInstanceStatuses;
}
Aggregations