@Test public void testOneNewInstance() throws CloudbreakImageNotFoundException { Stack stack = new Stack(); stack.setId(STACK_ID); Image image = getEmptyImage(); when(imageService.getImage(STACK_ID)).thenReturn(image); InstanceGroup instanceGroup = new InstanceGroup(); instanceGroup.setId(INSTANCE_GROUP_ID); instanceGroup.setGroupName(GROUP_NAME); Set<InstanceGroup> instanceGroupSet = new TreeSet<>(); instanceGroupSet.add(instanceGroup); when(instanceGroupService.findByStackId(anyLong())).thenReturn(instanceGroupSet); when(clock.getCurrentTimeMillis()).thenReturn(CURRENT_TIME); Iterable<CloudVmMetaDataStatus> cloudVmMetaDataStatuses = getCloudVmMetaDataStatuses(InstanceStatus.CREATED); int newInstances = underTest.saveInstanceMetaData(stack, cloudVmMetaDataStatuses, CREATED); assertEquals(1, newInstances); verify(imageService).getImage(STACK_ID); verify(instanceGroupService).findByStackId(STACK_ID); verify(instanceMetaDataService).save(captor.capture()); InstanceMetaData instanceMetaData = captor.getValue(); assertCommonProperties(instanceMetaData); assertEquals(CREATED, instanceMetaData.getInstanceStatus()); assertNotNull(instanceMetaData.getImage()); }
public int saveInstanceMetaData(Stack stack, Iterable<CloudVmMetaDataStatus> cloudVmMetaDataStatusList, InstanceStatus status) { try { LOGGER.info("Save instance metadata for stack: {}", stack.getName()); int newInstances = 0; Set<InstanceMetaData> allInstanceMetadata = instanceMetaDataService.findNotTerminatedForStack(stack.getId()); boolean primaryIgSelected = allInstanceMetadata.stream().anyMatch(imd -> imd.getInstanceMetadataType() == InstanceMetadataType.GATEWAY_PRIMARY); Json imageJson = new Json(imageService.getImage(stack.getId())); Map<String, InstanceGroup> instanceGroups = instanceGroupService.findByStackId(stack.getId()) .stream() .collect(Collectors.toMap(InstanceGroup::getGroupName, instanceGroup -> instanceGroup)); for (CloudVmMetaDataStatus cloudVmMetaDataStatus : cloudVmMetaDataStatusList) { CloudInstance cloudInstance = cloudVmMetaDataStatus.getCloudVmInstanceStatus().getCloudInstance(); CloudInstanceMetaData md = cloudVmMetaDataStatus.getMetaData(); Long privateId = cloudInstance.getTemplate().getPrivateId(); String instanceId = cloudInstance.getInstanceId(); InstanceMetaData instanceMetaDataEntry = createInstanceMetadataIfAbsent(allInstanceMetadata, privateId, instanceId); if (instanceMetaDataEntry.getInstanceId() == null && cloudVmMetaDataStatus.getCloudVmInstanceStatus().getStatus() == CREATED) { newInstances++; } InstanceGroup ig = instanceMetaDataEntry.getInstanceGroup(); String group = ig == null ? cloudInstance.getTemplate().getGroupName() : ig.getGroupName(); InstanceGroup instanceGroup = instanceGroups.get(group); instanceMetaDataEntry.setPrivateIp(md.getPrivateIp()); instanceMetaDataEntry.setPublicIp(md.getPublicIp()); instanceMetaDataEntry.setSshPort(md.getSshPort()); instanceMetaDataEntry.setLocalityIndicator(md.getLocalityIndicator()); instanceMetaDataEntry.setInstanceGroup(instanceGroup); instanceMetaDataEntry.setInstanceId(instanceId); instanceMetaDataEntry.setPrivateId(privateId); instanceMetaDataEntry.setStartDate(clock.getCurrentTimeMillis()); instanceMetaDataEntry.setSubnetId(cloudInstance.getStringParameter(CloudInstance.SUBNET_ID)); instanceMetaDataEntry.setInstanceName(cloudInstance.getStringParameter(CloudInstance.INSTANCE_NAME)); instanceMetaDataEntry.setServer(Boolean.FALSE); instanceMetaDataEntry.setLifeCycle(InstanceLifeCycle.fromCloudInstanceLifeCycle(md.getLifeCycle())); if (instanceMetaDataEntry.getInstanceMetadataType() == null) { if (ig != null) { if (InstanceGroupType.GATEWAY.equals(ig.getInstanceGroupType())) { if (!primaryIgSelected) { primaryIgSelected = true; instanceMetaDataEntry.setInstanceMetadataType(InstanceMetadataType.GATEWAY_PRIMARY); instanceMetaDataEntry.setServer(Boolean.TRUE); LOGGER.info("Primary gateway is not selected, let's select this instance: {}", instanceMetaDataEntry.getInstanceId()); } else { LOGGER.info("Primary gateway was selected"); instanceMetaDataEntry.setInstanceMetadataType(InstanceMetadataType.GATEWAY); } } else { LOGGER.info("Instance is a core instance: {}", instanceMetaDataEntry.getInstanceId()); instanceMetaDataEntry.setInstanceMetadataType(InstanceMetadataType.CORE); } } else { LOGGER.info("Instance group is null, instance will be a core instance: {}", instanceMetaDataEntry.getInstanceId()); instanceMetaDataEntry.setInstanceMetadataType(InstanceMetadataType.CORE); } } if (status != null) { if (cloudVmMetaDataStatus.getCloudVmInstanceStatus().getStatus() == TERMINATED) { instanceMetaDataEntry.setInstanceStatus(InstanceStatus.TERMINATED); } else { instanceMetaDataEntry.setInstanceStatus(status); instanceMetaDataEntry.setImage(imageJson); } } instanceMetaDataService.save(instanceMetaDataEntry); } return newInstances; } catch (CloudbreakImageNotFoundException | IllegalArgumentException ex) { throw new CloudbreakServiceException("Instance metadata collection failed", ex); } }
MetadataSetupService { public int saveInstanceMetaData(Stack stack, Iterable<CloudVmMetaDataStatus> cloudVmMetaDataStatusList, InstanceStatus status) { try { LOGGER.info("Save instance metadata for stack: {}", stack.getName()); int newInstances = 0; Set<InstanceMetaData> allInstanceMetadata = instanceMetaDataService.findNotTerminatedForStack(stack.getId()); boolean primaryIgSelected = allInstanceMetadata.stream().anyMatch(imd -> imd.getInstanceMetadataType() == InstanceMetadataType.GATEWAY_PRIMARY); Json imageJson = new Json(imageService.getImage(stack.getId())); Map<String, InstanceGroup> instanceGroups = instanceGroupService.findByStackId(stack.getId()) .stream() .collect(Collectors.toMap(InstanceGroup::getGroupName, instanceGroup -> instanceGroup)); for (CloudVmMetaDataStatus cloudVmMetaDataStatus : cloudVmMetaDataStatusList) { CloudInstance cloudInstance = cloudVmMetaDataStatus.getCloudVmInstanceStatus().getCloudInstance(); CloudInstanceMetaData md = cloudVmMetaDataStatus.getMetaData(); Long privateId = cloudInstance.getTemplate().getPrivateId(); String instanceId = cloudInstance.getInstanceId(); InstanceMetaData instanceMetaDataEntry = createInstanceMetadataIfAbsent(allInstanceMetadata, privateId, instanceId); if (instanceMetaDataEntry.getInstanceId() == null && cloudVmMetaDataStatus.getCloudVmInstanceStatus().getStatus() == CREATED) { newInstances++; } InstanceGroup ig = instanceMetaDataEntry.getInstanceGroup(); String group = ig == null ? cloudInstance.getTemplate().getGroupName() : ig.getGroupName(); InstanceGroup instanceGroup = instanceGroups.get(group); instanceMetaDataEntry.setPrivateIp(md.getPrivateIp()); instanceMetaDataEntry.setPublicIp(md.getPublicIp()); instanceMetaDataEntry.setSshPort(md.getSshPort()); instanceMetaDataEntry.setLocalityIndicator(md.getLocalityIndicator()); instanceMetaDataEntry.setInstanceGroup(instanceGroup); instanceMetaDataEntry.setInstanceId(instanceId); instanceMetaDataEntry.setPrivateId(privateId); instanceMetaDataEntry.setStartDate(clock.getCurrentTimeMillis()); instanceMetaDataEntry.setSubnetId(cloudInstance.getStringParameter(CloudInstance.SUBNET_ID)); instanceMetaDataEntry.setInstanceName(cloudInstance.getStringParameter(CloudInstance.INSTANCE_NAME)); instanceMetaDataEntry.setServer(Boolean.FALSE); instanceMetaDataEntry.setLifeCycle(InstanceLifeCycle.fromCloudInstanceLifeCycle(md.getLifeCycle())); if (instanceMetaDataEntry.getInstanceMetadataType() == null) { if (ig != null) { if (InstanceGroupType.GATEWAY.equals(ig.getInstanceGroupType())) { if (!primaryIgSelected) { primaryIgSelected = true; instanceMetaDataEntry.setInstanceMetadataType(InstanceMetadataType.GATEWAY_PRIMARY); instanceMetaDataEntry.setServer(Boolean.TRUE); LOGGER.info("Primary gateway is not selected, let's select this instance: {}", instanceMetaDataEntry.getInstanceId()); } else { LOGGER.info("Primary gateway was selected"); instanceMetaDataEntry.setInstanceMetadataType(InstanceMetadataType.GATEWAY); } } else { LOGGER.info("Instance is a core instance: {}", instanceMetaDataEntry.getInstanceId()); instanceMetaDataEntry.setInstanceMetadataType(InstanceMetadataType.CORE); } } else { LOGGER.info("Instance group is null, instance will be a core instance: {}", instanceMetaDataEntry.getInstanceId()); instanceMetaDataEntry.setInstanceMetadataType(InstanceMetadataType.CORE); } } if (status != null) { if (cloudVmMetaDataStatus.getCloudVmInstanceStatus().getStatus() == TERMINATED) { instanceMetaDataEntry.setInstanceStatus(InstanceStatus.TERMINATED); } else { instanceMetaDataEntry.setInstanceStatus(status); instanceMetaDataEntry.setImage(imageJson); } } instanceMetaDataService.save(instanceMetaDataEntry); } return newInstances; } catch (CloudbreakImageNotFoundException | IllegalArgumentException ex) { throw new CloudbreakServiceException("Instance metadata collection failed", ex); } } }
MetadataSetupService { public int saveInstanceMetaData(Stack stack, Iterable<CloudVmMetaDataStatus> cloudVmMetaDataStatusList, InstanceStatus status) { try { LOGGER.info("Save instance metadata for stack: {}", stack.getName()); int newInstances = 0; Set<InstanceMetaData> allInstanceMetadata = instanceMetaDataService.findNotTerminatedForStack(stack.getId()); boolean primaryIgSelected = allInstanceMetadata.stream().anyMatch(imd -> imd.getInstanceMetadataType() == InstanceMetadataType.GATEWAY_PRIMARY); Json imageJson = new Json(imageService.getImage(stack.getId())); Map<String, InstanceGroup> instanceGroups = instanceGroupService.findByStackId(stack.getId()) .stream() .collect(Collectors.toMap(InstanceGroup::getGroupName, instanceGroup -> instanceGroup)); for (CloudVmMetaDataStatus cloudVmMetaDataStatus : cloudVmMetaDataStatusList) { CloudInstance cloudInstance = cloudVmMetaDataStatus.getCloudVmInstanceStatus().getCloudInstance(); CloudInstanceMetaData md = cloudVmMetaDataStatus.getMetaData(); Long privateId = cloudInstance.getTemplate().getPrivateId(); String instanceId = cloudInstance.getInstanceId(); InstanceMetaData instanceMetaDataEntry = createInstanceMetadataIfAbsent(allInstanceMetadata, privateId, instanceId); if (instanceMetaDataEntry.getInstanceId() == null && cloudVmMetaDataStatus.getCloudVmInstanceStatus().getStatus() == CREATED) { newInstances++; } InstanceGroup ig = instanceMetaDataEntry.getInstanceGroup(); String group = ig == null ? cloudInstance.getTemplate().getGroupName() : ig.getGroupName(); InstanceGroup instanceGroup = instanceGroups.get(group); instanceMetaDataEntry.setPrivateIp(md.getPrivateIp()); instanceMetaDataEntry.setPublicIp(md.getPublicIp()); instanceMetaDataEntry.setSshPort(md.getSshPort()); instanceMetaDataEntry.setLocalityIndicator(md.getLocalityIndicator()); instanceMetaDataEntry.setInstanceGroup(instanceGroup); instanceMetaDataEntry.setInstanceId(instanceId); instanceMetaDataEntry.setPrivateId(privateId); instanceMetaDataEntry.setStartDate(clock.getCurrentTimeMillis()); instanceMetaDataEntry.setSubnetId(cloudInstance.getStringParameter(CloudInstance.SUBNET_ID)); instanceMetaDataEntry.setInstanceName(cloudInstance.getStringParameter(CloudInstance.INSTANCE_NAME)); instanceMetaDataEntry.setServer(Boolean.FALSE); instanceMetaDataEntry.setLifeCycle(InstanceLifeCycle.fromCloudInstanceLifeCycle(md.getLifeCycle())); if (instanceMetaDataEntry.getInstanceMetadataType() == null) { if (ig != null) { if (InstanceGroupType.GATEWAY.equals(ig.getInstanceGroupType())) { if (!primaryIgSelected) { primaryIgSelected = true; instanceMetaDataEntry.setInstanceMetadataType(InstanceMetadataType.GATEWAY_PRIMARY); instanceMetaDataEntry.setServer(Boolean.TRUE); LOGGER.info("Primary gateway is not selected, let's select this instance: {}", instanceMetaDataEntry.getInstanceId()); } else { LOGGER.info("Primary gateway was selected"); instanceMetaDataEntry.setInstanceMetadataType(InstanceMetadataType.GATEWAY); } } else { LOGGER.info("Instance is a core instance: {}", instanceMetaDataEntry.getInstanceId()); instanceMetaDataEntry.setInstanceMetadataType(InstanceMetadataType.CORE); } } else { LOGGER.info("Instance group is null, instance will be a core instance: {}", instanceMetaDataEntry.getInstanceId()); instanceMetaDataEntry.setInstanceMetadataType(InstanceMetadataType.CORE); } } if (status != null) { if (cloudVmMetaDataStatus.getCloudVmInstanceStatus().getStatus() == TERMINATED) { instanceMetaDataEntry.setInstanceStatus(InstanceStatus.TERMINATED); } else { instanceMetaDataEntry.setInstanceStatus(status); instanceMetaDataEntry.setImage(imageJson); } } instanceMetaDataService.save(instanceMetaDataEntry); } return newInstances; } catch (CloudbreakImageNotFoundException | IllegalArgumentException ex) { throw new CloudbreakServiceException("Instance metadata collection failed", ex); } }  }
MetadataSetupService { public int saveInstanceMetaData(Stack stack, Iterable<CloudVmMetaDataStatus> cloudVmMetaDataStatusList, InstanceStatus status) { try { LOGGER.info("Save instance metadata for stack: {}", stack.getName()); int newInstances = 0; Set<InstanceMetaData> allInstanceMetadata = instanceMetaDataService.findNotTerminatedForStack(stack.getId()); boolean primaryIgSelected = allInstanceMetadata.stream().anyMatch(imd -> imd.getInstanceMetadataType() == InstanceMetadataType.GATEWAY_PRIMARY); Json imageJson = new Json(imageService.getImage(stack.getId())); Map<String, InstanceGroup> instanceGroups = instanceGroupService.findByStackId(stack.getId()) .stream() .collect(Collectors.toMap(InstanceGroup::getGroupName, instanceGroup -> instanceGroup)); for (CloudVmMetaDataStatus cloudVmMetaDataStatus : cloudVmMetaDataStatusList) { CloudInstance cloudInstance = cloudVmMetaDataStatus.getCloudVmInstanceStatus().getCloudInstance(); CloudInstanceMetaData md = cloudVmMetaDataStatus.getMetaData(); Long privateId = cloudInstance.getTemplate().getPrivateId(); String instanceId = cloudInstance.getInstanceId(); InstanceMetaData instanceMetaDataEntry = createInstanceMetadataIfAbsent(allInstanceMetadata, privateId, instanceId); if (instanceMetaDataEntry.getInstanceId() == null && cloudVmMetaDataStatus.getCloudVmInstanceStatus().getStatus() == CREATED) { newInstances++; } InstanceGroup ig = instanceMetaDataEntry.getInstanceGroup(); String group = ig == null ? cloudInstance.getTemplate().getGroupName() : ig.getGroupName(); InstanceGroup instanceGroup = instanceGroups.get(group); instanceMetaDataEntry.setPrivateIp(md.getPrivateIp()); instanceMetaDataEntry.setPublicIp(md.getPublicIp()); instanceMetaDataEntry.setSshPort(md.getSshPort()); instanceMetaDataEntry.setLocalityIndicator(md.getLocalityIndicator()); instanceMetaDataEntry.setInstanceGroup(instanceGroup); instanceMetaDataEntry.setInstanceId(instanceId); instanceMetaDataEntry.setPrivateId(privateId); instanceMetaDataEntry.setStartDate(clock.getCurrentTimeMillis()); instanceMetaDataEntry.setSubnetId(cloudInstance.getStringParameter(CloudInstance.SUBNET_ID)); instanceMetaDataEntry.setInstanceName(cloudInstance.getStringParameter(CloudInstance.INSTANCE_NAME)); instanceMetaDataEntry.setServer(Boolean.FALSE); instanceMetaDataEntry.setLifeCycle(InstanceLifeCycle.fromCloudInstanceLifeCycle(md.getLifeCycle())); if (instanceMetaDataEntry.getInstanceMetadataType() == null) { if (ig != null) { if (InstanceGroupType.GATEWAY.equals(ig.getInstanceGroupType())) { if (!primaryIgSelected) { primaryIgSelected = true; instanceMetaDataEntry.setInstanceMetadataType(InstanceMetadataType.GATEWAY_PRIMARY); instanceMetaDataEntry.setServer(Boolean.TRUE); LOGGER.info("Primary gateway is not selected, let's select this instance: {}", instanceMetaDataEntry.getInstanceId()); } else { LOGGER.info("Primary gateway was selected"); instanceMetaDataEntry.setInstanceMetadataType(InstanceMetadataType.GATEWAY); } } else { LOGGER.info("Instance is a core instance: {}", instanceMetaDataEntry.getInstanceId()); instanceMetaDataEntry.setInstanceMetadataType(InstanceMetadataType.CORE); } } else { LOGGER.info("Instance group is null, instance will be a core instance: {}", instanceMetaDataEntry.getInstanceId()); instanceMetaDataEntry.setInstanceMetadataType(InstanceMetadataType.CORE); } } if (status != null) { if (cloudVmMetaDataStatus.getCloudVmInstanceStatus().getStatus() == TERMINATED) { instanceMetaDataEntry.setInstanceStatus(InstanceStatus.TERMINATED); } else { instanceMetaDataEntry.setInstanceStatus(status); instanceMetaDataEntry.setImage(imageJson); } } instanceMetaDataService.save(instanceMetaDataEntry); } return newInstances; } catch (CloudbreakImageNotFoundException | IllegalArgumentException ex) { throw new CloudbreakServiceException("Instance metadata collection failed", ex); } }  void cleanupRequestedInstances(Long stackId); void cleanupRequestedInstances(Stack stack, String instanceGroupName); int saveInstanceMetaData(Stack stack, Iterable<CloudVmMetaDataStatus> cloudVmMetaDataStatusList, InstanceStatus status); }
MetadataSetupService { public int saveInstanceMetaData(Stack stack, Iterable<CloudVmMetaDataStatus> cloudVmMetaDataStatusList, InstanceStatus status) { try { LOGGER.info("Save instance metadata for stack: {}", stack.getName()); int newInstances = 0; Set<InstanceMetaData> allInstanceMetadata = instanceMetaDataService.findNotTerminatedForStack(stack.getId()); boolean primaryIgSelected = allInstanceMetadata.stream().anyMatch(imd -> imd.getInstanceMetadataType() == InstanceMetadataType.GATEWAY_PRIMARY); Json imageJson = new Json(imageService.getImage(stack.getId())); Map<String, InstanceGroup> instanceGroups = instanceGroupService.findByStackId(stack.getId()) .stream() .collect(Collectors.toMap(InstanceGroup::getGroupName, instanceGroup -> instanceGroup)); for (CloudVmMetaDataStatus cloudVmMetaDataStatus : cloudVmMetaDataStatusList) { CloudInstance cloudInstance = cloudVmMetaDataStatus.getCloudVmInstanceStatus().getCloudInstance(); CloudInstanceMetaData md = cloudVmMetaDataStatus.getMetaData(); Long privateId = cloudInstance.getTemplate().getPrivateId(); String instanceId = cloudInstance.getInstanceId(); InstanceMetaData instanceMetaDataEntry = createInstanceMetadataIfAbsent(allInstanceMetadata, privateId, instanceId); if (instanceMetaDataEntry.getInstanceId() == null && cloudVmMetaDataStatus.getCloudVmInstanceStatus().getStatus() == CREATED) { newInstances++; } InstanceGroup ig = instanceMetaDataEntry.getInstanceGroup(); String group = ig == null ? cloudInstance.getTemplate().getGroupName() : ig.getGroupName(); InstanceGroup instanceGroup = instanceGroups.get(group); instanceMetaDataEntry.setPrivateIp(md.getPrivateIp()); instanceMetaDataEntry.setPublicIp(md.getPublicIp()); instanceMetaDataEntry.setSshPort(md.getSshPort()); instanceMetaDataEntry.setLocalityIndicator(md.getLocalityIndicator()); instanceMetaDataEntry.setInstanceGroup(instanceGroup); instanceMetaDataEntry.setInstanceId(instanceId); instanceMetaDataEntry.setPrivateId(privateId); instanceMetaDataEntry.setStartDate(clock.getCurrentTimeMillis()); instanceMetaDataEntry.setSubnetId(cloudInstance.getStringParameter(CloudInstance.SUBNET_ID)); instanceMetaDataEntry.setInstanceName(cloudInstance.getStringParameter(CloudInstance.INSTANCE_NAME)); instanceMetaDataEntry.setServer(Boolean.FALSE); instanceMetaDataEntry.setLifeCycle(InstanceLifeCycle.fromCloudInstanceLifeCycle(md.getLifeCycle())); if (instanceMetaDataEntry.getInstanceMetadataType() == null) { if (ig != null) { if (InstanceGroupType.GATEWAY.equals(ig.getInstanceGroupType())) { if (!primaryIgSelected) { primaryIgSelected = true; instanceMetaDataEntry.setInstanceMetadataType(InstanceMetadataType.GATEWAY_PRIMARY); instanceMetaDataEntry.setServer(Boolean.TRUE); LOGGER.info("Primary gateway is not selected, let's select this instance: {}", instanceMetaDataEntry.getInstanceId()); } else { LOGGER.info("Primary gateway was selected"); instanceMetaDataEntry.setInstanceMetadataType(InstanceMetadataType.GATEWAY); } } else { LOGGER.info("Instance is a core instance: {}", instanceMetaDataEntry.getInstanceId()); instanceMetaDataEntry.setInstanceMetadataType(InstanceMetadataType.CORE); } } else { LOGGER.info("Instance group is null, instance will be a core instance: {}", instanceMetaDataEntry.getInstanceId()); instanceMetaDataEntry.setInstanceMetadataType(InstanceMetadataType.CORE); } } if (status != null) { if (cloudVmMetaDataStatus.getCloudVmInstanceStatus().getStatus() == TERMINATED) { instanceMetaDataEntry.setInstanceStatus(InstanceStatus.TERMINATED); } else { instanceMetaDataEntry.setInstanceStatus(status); instanceMetaDataEntry.setImage(imageJson); } } instanceMetaDataService.save(instanceMetaDataEntry); } return newInstances; } catch (CloudbreakImageNotFoundException | IllegalArgumentException ex) { throw new CloudbreakServiceException("Instance metadata collection failed", ex); } }  void cleanupRequestedInstances(Long stackId); void cleanupRequestedInstances(Stack stack, String instanceGroupName); int saveInstanceMetaData(Stack stack, Iterable<CloudVmMetaDataStatus> cloudVmMetaDataStatusList, InstanceStatus status);  }