Skip to content

Commit

Permalink
Add methods to extract connectiondetails both from extra resources an…
Browse files Browse the repository at this point in the history
…d observable resources

Signed-off-by: Knut-Erik Johnsen <[email protected]>
  • Loading branch information
knutejoh committed Nov 13, 2024
1 parent 272e596 commit a7c2105
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
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
Expand Down Expand Up @@ -56,6 +58,39 @@ public <T> List<Optional<T>> getExtraResources(Map<String, Resources> extraResou
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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
Expand Down Expand Up @@ -42,4 +44,22 @@ public <T> Optional<T> getObservableResource(String resourceName, State observed
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;
}


}

0 comments on commit a7c2105

Please sign in to comment.