use of com.sequenceiq.cloudbreak.cloud.model.CloudInstance 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;
}
use of com.sequenceiq.cloudbreak.cloud.model.CloudInstance in project cloudbreak by hortonworks.
the class AwsMetadataCollector method groupByInstanceGroup.
private ListMultimap<String, CloudInstance> groupByInstanceGroup(Iterable<CloudInstance> vms) {
ListMultimap<String, CloudInstance> groupByInstanceGroup = ArrayListMultimap.create();
for (CloudInstance vm : vms) {
String groupName = vm.getTemplate().getGroupName();
groupByInstanceGroup.put(groupName, vm);
}
return groupByInstanceGroup;
}
use of com.sequenceiq.cloudbreak.cloud.model.CloudInstance in project cloudbreak by hortonworks.
the class AwsMetadataCollector method collectGroupMetaData.
private Collection<CloudVmMetaDataStatus> collectGroupMetaData(AuthenticatedContext ac, AmazonAutoScaling amazonASClient, AmazonEC2 amazonEC2Client, AmazonCloudFormation amazonCFClient, String groupName, Iterable<CloudInstance> cloudInstances) {
Collection<CloudVmMetaDataStatus> cloudVmMetaDataStatuses = new ArrayList<>();
String asGroupName = cloudFormationStackUtil.getAutoscalingGroupName(ac, amazonCFClient, groupName);
List<String> instanceIds = cloudFormationStackUtil.getInstanceIds(amazonASClient, asGroupName);
DescribeInstancesRequest instancesRequest = cloudFormationStackUtil.createDescribeInstancesRequest(instanceIds);
DescribeInstancesResult instancesResult = amazonEC2Client.describeInstances(instancesRequest);
// contains instances with instanceId
Map<String, CloudInstance> mapByInstanceId = mapByInstanceId(cloudInstances);
// contains instances with privateId (without instanceId)
Queue<CloudInstance> untrackedInstances = untrackedInstances(cloudInstances);
for (Reservation reservation : instancesResult.getReservations()) {
LOGGER.info("Number of instances found in reservation: {}", reservation.getInstances().size());
for (Instance instance : reservation.getInstances()) {
String instanceId = instance.getInstanceId();
CloudInstance cloudInstance = ensureInstanceTag(mapByInstanceId, instance, instanceId, untrackedInstances, amazonEC2Client);
if (cloudInstance != null) {
CloudInstanceMetaData md = new CloudInstanceMetaData(instance.getPrivateIpAddress(), instance.getPublicIpAddress());
CloudVmInstanceStatus cloudVmInstanceStatus = new CloudVmInstanceStatus(cloudInstance, InstanceStatus.CREATED);
CloudVmMetaDataStatus cloudVmMetaDataStatus = new CloudVmMetaDataStatus(cloudVmInstanceStatus, md);
cloudVmMetaDataStatuses.add(cloudVmMetaDataStatus);
}
}
}
return cloudVmMetaDataStatuses;
}
use of com.sequenceiq.cloudbreak.cloud.model.CloudInstance in project cloudbreak by hortonworks.
the class AwsMetaDataCollectorTest method collectNewOneGroup.
@Test
public void collectNewOneGroup() {
List<CloudInstance> vms = new ArrayList<>();
List<Volume> volumes = new ArrayList<>();
InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");
vms.add(new CloudInstance(null, new InstanceTemplate("fla", "cbgateway", 5L, volumes, InstanceStatus.CREATED, null, 0L), instanceAuthentication));
when(awsClient.createCloudFormationClient(any(AwsCredentialView.class), eq("region"))).thenReturn(amazonCFClient);
when(awsClient.createAutoScalingClient(any(AwsCredentialView.class), eq("region"))).thenReturn(amazonASClient);
when(awsClient.createAccess(any(AwsCredentialView.class), eq("region"))).thenReturn(amazonEC2Client);
when(cloudFormationStackUtil.getAutoscalingGroupName(any(AuthenticatedContext.class), any(AmazonCloudFormationClient.class), eq("cbgateway"))).thenReturn("cbgateway-AAA");
List<String> gatewayIds = Collections.singletonList("i-1");
when(cloudFormationStackUtil.getInstanceIds(any(AmazonAutoScalingClient.class), eq("cbgateway-AAA"))).thenReturn(gatewayIds);
when(cloudFormationStackUtil.createDescribeInstancesRequest(eq(gatewayIds))).thenReturn(describeInstancesRequestGw);
when(amazonEC2Client.describeInstances(describeInstancesRequestGw)).thenReturn(describeInstancesResultGw);
Instance instance = Mockito.mock(Instance.class);
when(instance.getInstanceId()).thenReturn("i-1");
when(instance.getPrivateIpAddress()).thenReturn("privateIp");
when(instance.getPublicIpAddress()).thenReturn("publicIp");
List<Reservation> gatewayReservations = Collections.singletonList(getReservation(instance));
when(describeInstancesResultGw.getReservations()).thenReturn(gatewayReservations);
AuthenticatedContext ac = authenticatedContext();
List<CloudVmMetaDataStatus> statuses = awsMetadataCollector.collect(ac, null, vms);
verify(amazonEC2Client).createTags(any(CreateTagsRequest.class));
Assert.assertEquals(1, statuses.size());
Assert.assertEquals("i-1", statuses.get(0).getCloudVmInstanceStatus().getCloudInstance().getInstanceId());
Assert.assertEquals("privateIp", statuses.get(0).getMetaData().getPrivateIp());
Assert.assertEquals("publicIp", statuses.get(0).getMetaData().getPublicIp());
}
use of com.sequenceiq.cloudbreak.cloud.model.CloudInstance in project cloudbreak by hortonworks.
the class AwsMetaDataCollectorTest method collectAlreadyTaggedOneGroup.
@Test
public void collectAlreadyTaggedOneGroup() {
List<CloudInstance> vms = new ArrayList<>();
List<Volume> volumes = new ArrayList<>();
InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");
vms.add(new CloudInstance("i-1", new InstanceTemplate("fla", "cbgateway", 5L, volumes, InstanceStatus.CREATED, null, 0L), instanceAuthentication));
when(awsClient.createCloudFormationClient(any(AwsCredentialView.class), eq("region"))).thenReturn(amazonCFClient);
when(awsClient.createAutoScalingClient(any(AwsCredentialView.class), eq("region"))).thenReturn(amazonASClient);
when(awsClient.createAccess(any(AwsCredentialView.class), eq("region"))).thenReturn(amazonEC2Client);
when(cloudFormationStackUtil.getAutoscalingGroupName(any(AuthenticatedContext.class), any(AmazonCloudFormationClient.class), eq("cbgateway"))).thenReturn("cbgateway-AAA");
List<String> gatewayIds = Collections.singletonList("i-1");
when(cloudFormationStackUtil.getInstanceIds(any(AmazonAutoScalingClient.class), eq("cbgateway-AAA"))).thenReturn(gatewayIds);
when(cloudFormationStackUtil.createDescribeInstancesRequest(eq(gatewayIds))).thenReturn(describeInstancesRequestGw);
when(amazonEC2Client.describeInstances(describeInstancesRequestGw)).thenReturn(describeInstancesResultGw);
Instance instance = Mockito.mock(Instance.class);
when(instance.getInstanceId()).thenReturn("i-1");
when(instance.getPrivateIpAddress()).thenReturn("privateIp");
when(instance.getPublicIpAddress()).thenReturn("publicIp");
Tag tag = new Tag();
tag.setKey("cbname");
tag.setValue("somevalue");
when(instance.getTags()).thenReturn(Collections.singletonList(tag));
List<Reservation> gatewayReservations = Collections.singletonList(getReservation(instance));
when(describeInstancesResultGw.getReservations()).thenReturn(gatewayReservations);
AuthenticatedContext ac = authenticatedContext();
List<CloudVmMetaDataStatus> statuses = awsMetadataCollector.collect(ac, null, vms);
verify(amazonEC2Client, never()).createTags(any(CreateTagsRequest.class));
Assert.assertEquals(1, statuses.size());
Assert.assertEquals("i-1", statuses.get(0).getCloudVmInstanceStatus().getCloudInstance().getInstanceId());
Assert.assertEquals("privateIp", statuses.get(0).getMetaData().getPrivateIp());
Assert.assertEquals("publicIp", statuses.get(0).getMetaData().getPublicIp());
}
Aggregations