I developed a microservice which calls the smartrule microservice. I found that: http://www.cumulocity.com/guides/reference/microservice-runtime#Access-to-the-platform-and-other-microservices
However, this doesn’t address if the microservices are subscript to another tenant. This means the base URL can’t be used for that.
I come up with following solution, it is working but I would ask if there is a better approach? I also see the risk of building up URLs which are different to the actual tenant. I can remember at a Cumulocity training that the URL pattern can be differ and the tenant must not be exact in the URL reflected! Maybe the Microservice SDK has some better support to call another microserver in the context?
private boolean callSmartRuleService(SmartRuleRepresentation requestBody, String groupId) {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.AUTHORIZATION, "Basic " + getBase64Credentials(contextService.getContext().getUsername(), contextService.getContext().getPassword()));
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<SmartRuleRepresentation> requestEntity = new HttpEntity<>(requestBody, headers);
StringBuilder urlBuilder = new StringBuilder();
urlBuilder.append("https://")
.append(contextService.getContext().getTenant())
.append(c8yBaseURL.substring(c8yBaseURL.indexOf(".", 8)))
.append("/service/smartrule/managedObjects/")
.append(groupId).append("/smartrules");
ResponseEntity<String> response = restTemplate.exchange(urlBuilder.toString(), HttpMethod.POST, requestEntity, String.class);
if (response.getStatusCode().is2xxSuccessful()) {
return true;
} else
return false;
}