@Test public void collectNewAndExistingOne() { 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, "imageId"), instanceAuthentication)); vms.add(new CloudInstance("i-1", new InstanceTemplate("fla", "cbgateway", 5L, volumes, InstanceStatus.CREATED, null, 0L, "imageId"), instanceAuthentication)); when(awsClient.createCloudFormationRetryClient(any(AwsCredentialView.class), eq("region"))).thenReturn(amazonCFClient); when(awsClient.createAutoScalingRetryClient(any(AwsCredentialView.class), eq("region"))).thenReturn(amazonASClient); when(cloudFormationStackUtil.getAutoscalingGroupName(any(AuthenticatedContext.class), any(AmazonCloudFormationRetryClient.class), eq("cbgateway"))) .thenReturn("cbgateway-AAA"); List<String> gatewayIds = Arrays.asList("i-1", "i-2"); when(cloudFormationStackUtil.getInstanceIds(any(AmazonAutoScalingRetryClient.class), eq("cbgateway-AAA"))) .thenReturn(gatewayIds); when(cloudFormationStackUtil.createDescribeInstancesRequest(eq(gatewayIds))).thenReturn(describeInstancesRequestGw); when(amazonEC2Client.describeInstances(describeInstancesRequestGw)).thenReturn(describeInstancesResultGw); Mockito.when(awsLifeCycleMapper.getLifeCycle(any())).thenReturn(CLOUD_INSTANCE_LIFE_CYCLE); Instance instance1 = Mockito.mock(Instance.class); when(instance1.getInstanceId()).thenReturn("i-1"); when(instance1.getPrivateIpAddress()).thenReturn("privateIp1"); when(instance1.getPublicIpAddress()).thenReturn("publicIp1"); Instance instance2 = Mockito.mock(Instance.class); when(instance2.getInstanceId()).thenReturn("i-2"); when(instance2.getPrivateIpAddress()).thenReturn("privateIp2"); when(instance2.getPublicIpAddress()).thenReturn("publicIp2"); List<Reservation> gatewayReservations = Collections.singletonList(getReservation(instance1, instance2)); when(describeInstancesResultGw.getReservations()).thenReturn(gatewayReservations); AuthenticatedContext ac = authenticatedContext(); List<CloudVmMetaDataStatus> statuses = awsMetadataCollector.collect(ac, Collections.emptyList(), vms, vms); Assert.assertEquals(2L, statuses.size()); Assert.assertTrue(statuses.stream().anyMatch(predicate -> "i-1".equals(predicate.getCloudVmInstanceStatus().getCloudInstance().getInstanceId()))); Assert.assertTrue(statuses.stream().anyMatch(predicate -> "privateIp1".equals(predicate.getMetaData().getPrivateIp()))); Assert.assertTrue(statuses.stream().anyMatch(predicate -> "publicIp1".equals(predicate.getMetaData().getPublicIp()))); Assert.assertTrue(statuses.stream().anyMatch(predicate -> "i-2".equals(predicate.getCloudVmInstanceStatus().getCloudInstance().getInstanceId()))); Assert.assertTrue(statuses.stream().anyMatch(predicate -> "privateIp2".equals(predicate.getMetaData().getPrivateIp()))); Assert.assertTrue(statuses.stream().anyMatch(predicate -> "publicIp2".equals(predicate.getMetaData().getPublicIp()))); Assert.assertTrue(statuses.stream().allMatch(predicate -> CLOUD_INSTANCE_LIFE_CYCLE.equals(predicate.getMetaData().getLifeCycle()))); }
@Override public List<CloudVmMetaDataStatus> collect(AuthenticatedContext ac, List<CloudResource> resources, List<CloudInstance> vms, List<CloudInstance> allInstances) { LOGGER.debug("Collect AWS instance metadata, for cluster {} resources: {}, vms: {}, allInstances: {}", ac.getCloudContext().getName(), resources.size(), vms.size(), allInstances.size()); try { List<String> knownInstanceIdList = allInstances.stream() .filter(cloudInstance -> cloudInstance.getInstanceId() != null) .map(CloudInstance::getInstanceId) .collect(Collectors.toList()); return collectCloudVmMetaDataStatuses(ac, vms, knownInstanceIdList); } catch (RuntimeException e) { throw new CloudConnectorException(e.getMessage(), e); } }
AwsMetadataCollector implements MetadataCollector { @Override public List<CloudVmMetaDataStatus> collect(AuthenticatedContext ac, List<CloudResource> resources, List<CloudInstance> vms, List<CloudInstance> allInstances) { LOGGER.debug("Collect AWS instance metadata, for cluster {} resources: {}, vms: {}, allInstances: {}", ac.getCloudContext().getName(), resources.size(), vms.size(), allInstances.size()); try { List<String> knownInstanceIdList = allInstances.stream() .filter(cloudInstance -> cloudInstance.getInstanceId() != null) .map(CloudInstance::getInstanceId) .collect(Collectors.toList()); return collectCloudVmMetaDataStatuses(ac, vms, knownInstanceIdList); } catch (RuntimeException e) { throw new CloudConnectorException(e.getMessage(), e); } } }
AwsMetadataCollector implements MetadataCollector { @Override public List<CloudVmMetaDataStatus> collect(AuthenticatedContext ac, List<CloudResource> resources, List<CloudInstance> vms, List<CloudInstance> allInstances) { LOGGER.debug("Collect AWS instance metadata, for cluster {} resources: {}, vms: {}, allInstances: {}", ac.getCloudContext().getName(), resources.size(), vms.size(), allInstances.size()); try { List<String> knownInstanceIdList = allInstances.stream() .filter(cloudInstance -> cloudInstance.getInstanceId() != null) .map(CloudInstance::getInstanceId) .collect(Collectors.toList()); return collectCloudVmMetaDataStatuses(ac, vms, knownInstanceIdList); } catch (RuntimeException e) { throw new CloudConnectorException(e.getMessage(), e); } }  }
AwsMetadataCollector implements MetadataCollector { @Override public List<CloudVmMetaDataStatus> collect(AuthenticatedContext ac, List<CloudResource> resources, List<CloudInstance> vms, List<CloudInstance> allInstances) { LOGGER.debug("Collect AWS instance metadata, for cluster {} resources: {}, vms: {}, allInstances: {}", ac.getCloudContext().getName(), resources.size(), vms.size(), allInstances.size()); try { List<String> knownInstanceIdList = allInstances.stream() .filter(cloudInstance -> cloudInstance.getInstanceId() != null) .map(CloudInstance::getInstanceId) .collect(Collectors.toList()); return collectCloudVmMetaDataStatuses(ac, vms, knownInstanceIdList); } catch (RuntimeException e) { throw new CloudConnectorException(e.getMessage(), e); } }  @Override List<CloudVmMetaDataStatus> collect(AuthenticatedContext ac, List<CloudResource> resources, List<CloudInstance> vms,
            List<CloudInstance> allInstances); }
AwsMetadataCollector implements MetadataCollector { @Override public List<CloudVmMetaDataStatus> collect(AuthenticatedContext ac, List<CloudResource> resources, List<CloudInstance> vms, List<CloudInstance> allInstances) { LOGGER.debug("Collect AWS instance metadata, for cluster {} resources: {}, vms: {}, allInstances: {}", ac.getCloudContext().getName(), resources.size(), vms.size(), allInstances.size()); try { List<String> knownInstanceIdList = allInstances.stream() .filter(cloudInstance -> cloudInstance.getInstanceId() != null) .map(CloudInstance::getInstanceId) .collect(Collectors.toList()); return collectCloudVmMetaDataStatuses(ac, vms, knownInstanceIdList); } catch (RuntimeException e) { throw new CloudConnectorException(e.getMessage(), e); } }  @Override List<CloudVmMetaDataStatus> collect(AuthenticatedContext ac, List<CloudResource> resources, List<CloudInstance> vms,
            List<CloudInstance> allInstances);  }