Search code examples
javaopenstackopenstack-novaopenstack-api

How to get right OSClient from token in Openstack4j java api for v2 and v3?


I am using openstack4j java api. I am able to access 2 openstack projects (image for projects that I can access), given instance id I want to shut down the instance. While getting OSClient, I loop over clients for both projects and check in which project that instance belongs.

Then with OSClient handle I just call Shut down.os.compute().servers().action(instanceId, Action.STOP); Now one thread keeps running to track current instance status and if current instance status reaches desired state then I perform some operation.

My Problem : I am creating (ModelEntity) token from OSClient handle.

        ModelEntity token = null;
        if(apiVersion.equals(OpenstackCloudCredential.API_VERSION.V2)) {
            token = ((OSClient.OSClientV2) os).getAccess();
        }else if(apiVersion.equals(OpenstackCloudCredential.API_VERSION.V3)){
            token = ((OSClient.OSClientV3) os).getToken();
        }
        return token;
and then I am trying to get same OSClient handle in my other class in which thread is to be executed. If I just pass same OSClient handle then I get some invalid session error message. So, from this token I m trying to get OSClient handle in other class. In other class I do the following to get OSClient :
    OSClient os = null;
    if(apiVersion.equals(OpenstackCloudCredential.API_VERSION.V2)){
        os = OSFactory.clientFromAccess((Access) token);
    }else if(apiVersion.equals(OpenstackCloudCredential.API_VERSION.V3)){
        os = OSFactory.clientFromToken((Token) token);
    }
    _logger.info(" ================= rishi token : " + token.hashCode());

    return os;

I am getting OSClient handle but it points to different project(first project in the list). I am generating token from right OSClient handle but why I am not getting right OSClient from the same token. Any suggestions on how to get the right OSClient handle ?


Solution

  • Got the answer !!

    My intention was to get OSClient handle for all the tenants or projects. I made a list of OSClient handle. I authenticated first tenant to get the client and then I add it to the clients list then I authenticated 2nd tenant and then again I added it to the list. Now, since I authenticated 2nd tenant, session for 1st tenant is inactive and first client in list also points to instances of second client.

    I am soo dumb ;)