InstancesRepositoryImpl implements InstancesRepository { @Override public Map<String, Map<String, String>> getInstancesMetadata(FindInstancesRequest findInstancesRequest) { QueryResult instancesResult = queryRepository.query(findInstancesRequest.getPayload()); Data instancesData = instancesResult.getData(); Map<String, String> instanceIdsNames = instancesData.getRows().stream() .collect(Collectors.toMap( r -> valueOf(get(instancesData, r, "instances.id")), r -> valueOf(get(instancesData, r, "instances.name")) )); Map<String, Map<String, String>> instancesMetadata = new HashMap<>(); Set<String> instanceIds = instanceIdsNames.keySet(); if (!instanceIds.isEmpty()) { Query query = new SelectQuery() .all() .from() .table("instance_metadata") .where() .in("instance_id", instanceIds) .getQuery(); QueryResult metadataResult = queryRepository.query(query); Data metadataData = metadataResult.getData(); metadataData.getRows().forEach(r -> { ValueFormatter vf = new ValueFormatter(metadataData, r); String instanceId = vf.getString("instance_id"); String instanceName = instanceIdsNames.get(instanceId); String key = vf.getString("key"); String value = vf.getString("value"); instancesMetadata.compute(instanceName, (k, ov) -> new HashMap<String, String>(){ { if (ov != null) { putAll(ov); } put(key, value); } }); }); } return instancesMetadata; }  @Override List<FindInstancesResult> findInstances(FindInstancesRequest findInstancesRequest); @Override Map<String, Map<String, String>> getInstancesMetadata(FindInstancesRequest findInstancesRequest); @Override Set<String> addInstances(List<Instance> instances); @Override Set<String> startInstances(Collection<String> instanceIds); @Override Set<String> shutdownInstances(Collection<String> instanceIds); @Override Set<String> deleteInstances(Collection<String> instanceIds); @Override Set<String> renameInstances(Map<String, String> newNames); @Override Set<String> rebootInstances(Collection<String> instanceIds); @Override Set<String> rebuildInstances(String imageId, Collection<String> instanceIds); @Override Set<String> resizeInstances(String flavorId, Collection<String> instanceIds); @Override Set<String> pauseInstances(Collection<String> instanceIds); @Override Set<String> resumeInstances(Collection<String> instanceIds); @Override Set<String> suspendInstances(Collection<String> instanceIds); @Override Set<String> hardRebootInstances(Collection<String> instanceIds);  }
@Test public void testGetInstanceMetadata() { Project project = EntityGenerator.getProject(); Instance instance = EntityGenerator.getInstance(); Image image = EntityGenerator.getImage(); Flavor flavor = EntityGenerator.getFlavor(); mockQueryRepository.addQueryResult(createMockInstancesResult(project, instance, image, flavor)); mockQueryRepository.addQueryResult(createMockInstanceMetadataResult(instance)); FindInstancesRequest findInstancesRequest = requestProvider.get(FindInstancesRequest.class); Map<String, Map<String, String>> instancesMetadata = instancesRepository.getInstancesMetadata(findInstancesRequest); assertThat(instancesMetadata.keySet(), contains(instance.getId())); Map<String, String> metadata = instancesMetadata.get(instance.getId()); assertThat(metadata, equalTo(Collections.singletonMap( REGION.value(), instance.getMetadata().get(REGION) ))); }