Search code examples
cloud-foundrycloudfoundry-java-client

get serviceInstanceName and serviceKeyName for cloud foundry user provider serves by using cloudFoundryOperations


im trying to get the Credentials of UPS in cloud foundry: using:

Mono<ServiceKey> serviceKey = (Mono<ServiceKey>) cloudFoundryOperations
    .services()
    .getServiceKey(
        GetServiceKeyRequest.builder()
            .serviceKeyName("digital_cassandra")
            .serviceInstanceName("2a5aa377-e992-4f88-9f85-d9cec5c3bea9")
            .build())
    .subscribe();

serviceKey.map(serviceKey1 -> {
    System.out.println(serviceKey1.getCredentials().toString());
    return serviceKey1.getCredentials().get(0);
}); 

but nothing printed. how to fet the serviceKeyName and serviceInstanceName by cloudFoundryOperations? i need to print all the serviceKeyName and serviceInstanceName in my space.


Solution

  • .serviceInstanceName("2a5aa377-e992-4f88-9f85-d9cec5c3bea9")

    It should be the actual name, not the guid. Like "my-key" or whatever you called your key.

    but nothing printed. how to fet the serviceKeyName and serviceInstanceName by cloudFoundryOperations?

    If you just want to print to the console, try something like this:

            cloudFoundryOperations
                .services()
                .getServiceKey(GetServiceKeyRequest.builder()
                        .serviceInstanceName("reservation-db")
                        .serviceKeyName("cf-mysql")
                        .build())
                .doOnNext(key -> {
                    System.out.println("Key:");
                    System.out.println("  " + key.getName() + " (" + key.getId() + ")");
                    key.getCredentials().forEach((k, v) -> {
                        System.out.println("    " + k + " => " + v);
                    });
                })
                .block();
    

    The GetServiceKeyRequest determines which service key is looked up. The doOnNext call allows you to inspect but not consume the key, which works fine to print it out. Then the example calls .block() to wait for the results, which is fine cause this is just an example. You wouldn't want to do that in your actual code though. You'd probably want one of the subscribe() variants (you could swap subscribe() for doOnNext() too, just depends on what you're code is doing).

    i need to print all the serviceKeyName and serviceInstanceName in my space.

    To get all the keys for all the service instances:

           cloudFoundryOperations
               .services()
               .listInstances()
               .doOnNext(si -> {
                   System.out.println("  " + si.getName() + "  (" + si.getId() + ")");
               })
               .flatMap((ServiceInstanceSummary si) -> {
                    return ops
                          .services()
                          .listServiceKeys(ListServiceKeysRequest.builder()
                                  .serviceInstanceName(si.getName())
                                  .build())
                          .doOnNext(key -> {
                                System.out.println("Key:");
                                System.out.println("  " + key.getName() + " (" + key.getId() + ")");
                                key.getCredentials().forEach((k, v) -> {
                                    System.out.println("    " + k + " => " + v);
                                });
                          });
               })
               .blockLast();
    

    This one is enumerating all the service instances, printing the name/id, then using flatMap to go out and get the service keys for each service instance. It then merges them all into one Flux<ServiceKey>. The doOnNext() is just for printing. You don't necessarily have to do that. You could consume the result in a number of ways, like collect it into a list or subscribe to it, this just works nicely for an example. Use what works best for your code.