Pushkar
Pushkar

Reputation: 23

fetch the microservice subscribed tenant status value using cumulocity java sdk client

I want to get the microservice subscribed tenant status value using cumulocity java sdk client. i am able to fetch the tenant status value("ACTIVE"/"SUSPENDED"/"DELETED") using the Rest api request GET url: {{baseurl}}/tenant/tenants/{tenantId}.

I didn't find any direct way to get the subscribed tenancy status using cumulocity java sdk or microservice sdk.

http://resources.cumulocity.com/documentation/javasdk/current/index.html http://resources.cumulocity.com/documentation/microservicesdk/current/

So currently i am trying to get the tenant status value using "com.cumulocity.sdk.client.RestConnector" to make the above http GET url call but i am getting the HTTP Error: 403 Forbidden and "Access is denied".

This is credentials/permission issue at microservice level..

    import com.cumulocity.microservice.context.credentials.MicroserviceCredentials;
    import com.cumulocity.microservice.subscription.service.MicroserviceSubscriptionsService;
    import com.cumulocity.rest.representation.tenant.TenantRepresentation;
    import com.cumulocity.model.authentication.CumulocityBasicCredentials;
    ....
    @Value("#{systemEnvironment['C8Y_BASEURL']}")
    private String baseUrl;
    
    @Autowired
    private MicroserviceSubscriptionsService subscriptionsService;
    
    ....
    
    Iterator<MicroserviceCredentials> tenantCredentials = subscriptionsService.getAll().iterator();
                while (tenantCredentials.hasNext()) {
                    MicroserviceCredentials tenantCredential = tenantCredentials.next();
                    subscriptionsService.runForTenant
    (tenantCredential.getTenant(), ()-> {
                        String path = baseUrl+"/tenant/tenants/"+tenantCredential.getTenant();
                        Platform platform = new PlatformImpl(baseUrl, CumulocityBasicCredentials.builder()
                                    .tenantId(tenantCredential.getTenant())
                                    .username(tenantCredential.getUsername())
                                    .password(tenantCredential.getPassword())
                                    .build());
                        TenantRepresentation tenantReps = platform.rest().get(path, CumulocityMediaType.APPLICATION_JSON_TYPE, TenantRepresentation.class);
                            if (!isNullOrEmpty(tenantReps)) {
                                String tenantStatus = tenantReps.getStatus();
                                log.info("tenant status value : " + tenantStatus);
                            }
                        }
                    });
                }

My java microserive has "MULTI_TENANT" isolation. cumulocity.json File:

....

"isolation": "MULTI_TENANT",
  "requiredRoles": [
    "ROLE_TENANT_MANAGEMENT_ADMIN",
    "ROLE_TENANT_MANAGEMENT_READ",
    "ROLE_TENANT_ADMIN"
    ..
  ],

....

How we can fetch the microservice subscribe tenant status value("ACTIVE"/"SUSPENDED"/"DELETED") using java sdk. Here, i am trying to fetch the TenantRepresentation.getStatus() value. Appreciate your help here.

Upvotes: 0

Views: 102

Answers (2)

TyrManuZ
TyrManuZ

Reputation: 2049

What you are looking for is the following API

GET /application/currentApplication/subscription

This will return you a list with all tenants that are currently subscribed to this microservice and the service credential users.

In order to access this API you need to use the microservice bootstrap credentials.

Documentation can be found here:

https://cumulocity.com/guides/microservice-sdk/rest/#acquiring-microservice-credentials

Upvotes: 1

Switschel
Switschel

Reputation: 42

I think the issue is, that you are not using the parent tenant credentials but the credentials of the tenant. The documentation states:

ROLE_TENANT_MANAGEMENT_READ AND the current tenant is its parent OR is the management tenant

So either you use /tenant/currentTenant with a tenant-user or you use /tenant/tenants/{tenantId} only with a parent-tenant user.

Upvotes: 1

Related Questions