How to get c8y Hostname

What product/components do you use and which version/fix level are you on?

Cumulocity IoT 10.10

Is your question related to the free trail, or to a production (customer) instance?

Customer License

What are you trying to achieve? Please describe in detail.

We want to call one of the endpoints of microservice hosted on c8y from another microservice which is hosted on the same c8y instance. We are trying to get the platform hostname using the class com.cumulocity.sdk.client.PlatformParameters
String hostname = platformParameters.getHost();
But its returning the hostname as http://cumulocity:8111/ in our test environments and that is not working while calling the microservice endpoint as we are getting HTTP code 422.
We wanted to know if there are any other option available?

Hi Robin,
I am not sure if the connection to http://cumulocity:8111/ should actually work and something else is missing there.
Can you share how you build the URL to the microservice API endpoint in detail?
Did you elaborate the usage of RestConnector, which helps in performing custom API calls to Cumulocity - like microservice API calls?

Alternatively below is a other solution, which I would only consider as an (ugly) work around to get the base URL.
You can read it from the system or spring environment:

	private static final String SPRING_ENV_c8y_baseUrl = "C8Y.baseURL";
	private static final String C8Y_BASEURL = "C8Y_BASEURL";

	private final Environment environment;
	private final MicroserviceSubscriptionsService subscriptions;

	public String getC8yBaseUrl() {
		String baseUrl = null;
		if (System.getenv().containsKey(C8Y_BASEURL)) {
			baseUrl = System.getenv(C8Y_BASEURL);
			log.debug("Using sysenv C8Y_BASEURL:{}", baseUrl);
		} else {
			baseUrl = environment.getProperty(SPRING_ENV_c8y_baseUrl);
			log.debug("Using spring environemnt C8y.baseUrl:{}", baseUrl);
		}
		if (!baseUrl.endsWith("/")) {
			baseUrl = baseUrl + "/";
		}
		return baseUrl;
	}

Regards
Kai

Hi Robin,

Accessing other microservices requires to go through the external loadbalancer.

you could try using the currentTenant endpoint and to use the domainName attribute of the tenant.
By using the external domain specified with domainName you should be able to access the other microservice from within your microservice.

Iā€™m sadly not aware which classes you can use within java microservice SDK to do this.

Regards,
Tristan

1 Like

Hi Kai,

Thank you very much for the reply.
So I tried below options to get c8y base url

  1. PlatformParameters platformParameters;
    String hostname = platformParameters.getHost();
    This gave me URL as http://cumulocity:8111/ which did not work as I was getting 422 error code

  2. I also tried getting the hostname from managedObject API using getSelf() method.
    That gave me URL as https://t554656253.eu-latest.cumulocity.com which again did not work as it was timing out.

  3. Finally, I created a tenant option hostname(https://bsci-dev.eu-latest.cumulocity.com/) and used that to build url

        String softwarePackageUrl = "get Hostname from tenant option" + "/service/sw-package-service/device/" + deviceId + "/software";
             HttpClient httpClient = HttpClient.newHttpClient();
             HttpRequest getRequest = HttpRequest.newBuilder()
                     .uri(URI.create(softwarePackageUrl))
                     .headers("Authorization", subscriptionsService.getCredentials(subscriptionsService.getTenant()).get().toCumulocityCredentials().getAuthenticationString(),
                             "Accept", "application/json")
                     .build();
           HttpResponse<String> response = httpClient.send(getRequest, HttpResponse.BodyHandlers.ofString());
    

Hi Robin, Hi Tristan,

As @Tristan_Bastian mentioned you can use the currentTenant endpoint as well.
Which reminded me of the following:
Sadly the Microservice SDK does not support this, but it can be easily implemented, see below.
Be aware that there is already a TenantRepresentation.class existing in the SDK (com.cumulocity.rest.representation.tenant.TenantRepresentation), unfortunately this does not have the required domainName attribute.

package mypackage;
public class TenantRepresentation {
	private String domainName;

	public String getDomainName() {
		return domainName;
	}
}
import mypackage.TenantRepresentation;

@Autowired	
private RestConnector restConnector;

public Optional<TenantRepresentation> getCurrentTenant() {
	try {
		return Optional.ofNullable(restConnector.get("/tenant/currentTenant", CumulocityMediaType.APPLICATION_JSON_TYPE, TenantRepresentation.class));
	} catch (final SDKException e) {
		log.error("Tenant#getCurrentTenant operation resulted in " + e.getMessage(), e);
	}
	return Optional.empty();
}

Thanks Kai and Tristan. Your suggestion really helped.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.