Skip to content

Commit a7c2105

Browse files
committed
Add methods to extract connectiondetails both from extra resources and observable resources
Signed-off-by: Knut-Erik Johnsen <[email protected]>
1 parent 272e596 commit a7c2105

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

crossplane-function-springboot-starter/src/main/java/io/crossplane/compositefunctions/starter/conversion/CrossplaneExtraResourcesService.java

+35
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
import org.slf4j.LoggerFactory;
1212

1313
import java.util.ArrayList;
14+
import java.util.HashMap;
1415
import java.util.List;
1516
import java.util.Map;
1617
import java.util.Optional;
1718

19+
1820
/**
1921
* Class that helps with the extra resources map and also to create ResourceSelector in order to get extra resources
2022
* to the function
@@ -56,6 +58,39 @@ public <T> List<Optional<T>> getExtraResources(Map<String, Resources> extraResou
5658
return result;
5759
}
5860

61+
public Map<String, String> getConnectionDetails(Map<String, Resources> extraResources, String resourceName) {
62+
List<Map<String, String>> resources = getConnectionDetails(extraResources, resourceName, 1);
63+
64+
if (resources.isEmpty()) {
65+
return new HashMap<>();
66+
}
67+
return resources.get(0);
68+
}
69+
70+
public List<Map<String, String>> getConnectionDetails(Map<String, Resources> extraResources, String resourceName, int expectedResources) {
71+
List<Map<String, String>> result = new ArrayList<>();
72+
Resources resources = extraResources.get(resourceName);
73+
74+
if (resources != null && resources.getItemsCount() == expectedResources) {
75+
for (int i = 0; i < expectedResources; i++) {
76+
try {
77+
logger.debug("We have connectiondetails " + resourceName);
78+
Map<String, String> currentDetails = new HashMap<>();
79+
resources.getItems(i).getConnectionDetailsMap().forEach((key, value) ->
80+
currentDetails.put(key, value.toStringUtf8())
81+
);
82+
result.add(currentDetails);
83+
} catch (Exception e) {
84+
throw new CrossplaneUnmarshallException("Error when unmarshalling the connectionDetails", e);
85+
}
86+
}
87+
88+
}
89+
return result;
90+
}
91+
92+
93+
5994
public Map<String, ResourceSelector> createExtraResourcesSelector(String resourceName, HasMetadata type) {
6095
ResourceSelector resourceSelector = ResourceSelector.newBuilder()
6196
.setApiVersion(type.getApiVersion())

crossplane-function-springboot-starter/src/main/java/io/crossplane/compositefunctions/starter/conversion/CrossplaneObservableService.java

+20
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import org.slf4j.Logger;
1010
import org.slf4j.LoggerFactory;
1111

12+
import java.util.HashMap;
13+
import java.util.Map;
1214
import java.util.Optional;
1315

1416
/**
@@ -42,4 +44,22 @@ public <T> Optional<T> getObservableResource(String resourceName, State observed
4244
return result;
4345
}
4446

47+
public Map<String, String> getObservableConnectionDetails(String resourceName, State observedState) {
48+
Resource observedResource = observedState.getResourcesOrDefault(resourceName, null);
49+
Map<String, String> result = new HashMap<>();
50+
if (observedResource != null) {
51+
try {
52+
logger.debug("We have an observed connectionDetails for " + resourceName);
53+
observedResource.getConnectionDetailsMap().forEach((key, value) ->
54+
result.put(key, value.toStringUtf8())
55+
);
56+
} catch (Exception e) {
57+
58+
throw new CrossplaneUnmarshallException("Error when unmarshalling the connectionDetails for " + resourceName, e);
59+
}
60+
}
61+
return result;
62+
}
63+
64+
4565
}

0 commit comments

Comments
 (0)