Add methods to extract connectiondetails both from extra resources and observable resources

Signed-off-by: Knut-Erik Johnsen <abstract@knut-erik.org>
This commit is contained in:
Knut-Erik Johnsen 2024-11-13 16:26:15 +01:00
parent 272e596c69
commit a7c2105af2
2 changed files with 55 additions and 0 deletions

View File

@ -11,10 +11,12 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
/**
* Class that helps with the extra resources map and also to create ResourceSelector in order to get extra resources
* to the function
@ -56,6 +58,39 @@ public class CrossplaneExtraResourcesService {
return result;
}
public Map<String, String> getConnectionDetails(Map<String, Resources> extraResources, String resourceName) {
List<Map<String, String>> resources = getConnectionDetails(extraResources, resourceName, 1);
if (resources.isEmpty()) {
return new HashMap<>();
}
return resources.get(0);
}
public List<Map<String, String>> getConnectionDetails(Map<String, Resources> extraResources, String resourceName, int expectedResources) {
List<Map<String, String>> result = new ArrayList<>();
Resources resources = extraResources.get(resourceName);
if (resources != null && resources.getItemsCount() == expectedResources) {
for (int i = 0; i < expectedResources; i++) {
try {
logger.debug("We have connectiondetails " + resourceName);
Map<String, String> currentDetails = new HashMap<>();
resources.getItems(i).getConnectionDetailsMap().forEach((key, value) ->
currentDetails.put(key, value.toStringUtf8())
);
result.add(currentDetails);
} catch (Exception e) {
throw new CrossplaneUnmarshallException("Error when unmarshalling the connectionDetails", e);
}
}
}
return result;
}
public Map<String, ResourceSelector> createExtraResourcesSelector(String resourceName, HasMetadata type) {
ResourceSelector resourceSelector = ResourceSelector.newBuilder()
.setApiVersion(type.getApiVersion())

View File

@ -9,6 +9,8 @@ import io.fabric8.kubernetes.client.utils.Serialization;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
/**
@ -42,4 +44,22 @@ public class CrossplaneObservableService {
return result;
}
public Map<String, String> getObservableConnectionDetails(String resourceName, State observedState) {
Resource observedResource = observedState.getResourcesOrDefault(resourceName, null);
Map<String, String> result = new HashMap<>();
if (observedResource != null) {
try {
logger.debug("We have an observed connectionDetails for " + resourceName);
observedResource.getConnectionDetailsMap().forEach((key, value) ->
result.put(key, value.toStringUtf8())
);
} catch (Exception e) {
throw new CrossplaneUnmarshallException("Error when unmarshalling the connectionDetails for " + resourceName, e);
}
}
return result;
}
}