DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Practical Reactor Operations: Retrieve Cloud Foundry App Details

Practical Reactor Operations: Retrieve Cloud Foundry App Details

You can use Reactor, or rather the CF-Java-Client library built on it, get access to Cloud Foundry's Cloud Controller API to obtain, transform, and combine data.

Biju Kunjummen user avatar by
Biju Kunjummen
·
Jan. 12, 17 · Tutorial
Like (1)
Save
Tweet
Share
8.62K Views

Join the DZone community and get the full member experience.

Join For Free

CF-Java-Client is a library that enables programmatic access to a Cloud Foundry Cloud Controller API. It is built on top of Project Reactor, an implementation of the Reactive Streams specification, and it is a fun exercise to use this library to do something practical in a Cloud Foundry environment.

Consider a sample use case: Given an application ID, I need to find a little more detail on this application — more details of the application along with the details of the organization and the space that it belongs to.

To start with, the basis of all API operations with CF-Java-Client is a type unsurprisingly called the CloudFoundryClient(org.cloudfoundry.client.CloudFoundryClient). CF-Java-Client's GitHub page 

has details on how to get ahold of an instance of this type.

Given a CloudFoundryClient instance, the details of an application given its id can be obtained as follows:

Mono<GetApplicationResponse> applicationResponseMono = this.cloudFoundryClient
  .applicationsV2().get(GetApplicationRequest.builder().applicationId(applicationId).build());


Note that the API returns a reactor "Mono" 

type, this is in general the behavior of all the API calls of cf-java-client.

  • If an API returns one item then typically a Mono type is returned
  • If the API is expected to return more than one item then a Flux type is returned, and
  • If the API is called purely for side effects — say printing some information then it returns a Mono<Void> type

The next step is to retrieve the space identifier from the response and make an API call to retrieve the details of the space. That looks like this:

Mono<Tuple2<GetApplicationResponse, GetSpaceResponse>> appAndSpaceMono = applicationResponseMono
  .and(appResponse -> this.cloudFoundryClient.spaces()
    .get(GetSpaceRequest.builder()
      .spaceId(appResponse.getEntity().getSpaceId()).build()));



Here, I am using an "and" 

operator to combine the application response with another Mono that returns the space information. The result is a "Tuple2" 

type holding both pieces of information — the application detail and the detail of the space that it is in.

Finally, to retrieve the organization that the app is deployed in:

Mono<Tuple3<GetApplicationResponse, GetSpaceResponse, GetOrganizationResponse>> t3 =
  appAndSpaceMono.then(tup2 -> this.cloudFoundryClient.organizations()
      .get(GetOrganizationRequest.builder()
        .organizationId(tup2.getT2().getEntity()
          .getOrganizationId())
        .build())
      .map(orgResp -> Tuples.of(tup2.getT1(), tup2.getT2(),
        orgResp)));


Here, a "then" 

operation is being used to retrieve the organization detail. Using the ID from the previous step and the result added onto the previous tuple, we create a Tuple3 

type holding the "Application Detail," "Space Detail," and the "Organization Detail". "Then" is the equivalent of a

flatMap operator familiar in the Scala and ReactiveX world.

This essentially covers the way you would typically deal with the CF-Java-Client library and use the fact that it is built on the excellent Reactor library, and its collection of very useful operators, to get results together.

Now, we move onto the final step of transforming the result to a type that may be more relevant to your domain (handling any errors along the way):

Mono<AppDetail> appDetail =  
   t3.map(tup3 -> {
       String appName = tup3.getT1().getEntity().getName();
       String spaceName = tup3.getT2().getEntity().getName();
       String orgName = tup3.getT3().getEntity().getName();
       return new AppDetail(appName, orgName, spaceName);
}).otherwiseReturn(new AppDetail("", "", ""));


If you are interested in trying out a working sample, I have an example available in my GitHub repo here.

And the code shown in the article is available here.

Cloud Foundry Cloud app application API

Published at DZone with permission of Biju Kunjummen, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How To Handle Secrets in Docker
  • How We Solved an OOM Issue in TiDB with GOMEMLIMIT
  • Top 5 Data Streaming Trends for 2023
  • Introduction To OpenSSH

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: