Search code examples
javascriptnode.jsibm-cloudcloud-foundry

CF Connect to the cloud controller


I use the following lib to connect to the cloud controller

https://github.com/prosociallearnEU/cf-nodejs-client

const endpoint = "https://api.mycompany.com/";
const username = "myuser";
const password = "mypass";

const CloudController = new (require("cf-client")).CloudController(endpoint);
const UsersUAA = new (require("cf-client")).UsersUAA;
const Apps = new (require("cf-client")).Apps(endpoint);

CloudController.getInfo().then((result) => {
    UsersUAA.setEndPoint(result.authorization_endpoint);
    return UsersUAA.login(username, password);
}).then((result) => {
    Apps.setToken(result);
    return Apps.getApps();
}).then((result) => {
    console.log(result);
}).catch((reason) => {
    console.error("Error: " + reason);
});
  1. I try to run it against our API and its not working and Im not getting no error message in the console, what it can be ?

  2. where does the space/org is handled here ? since when I connect from the cli it ask me to which space/org I want to connect...

Im able to login via the CLI, just from the code I cant, any idea what is missing here?

The issue it when I run it I dont get any error that can help to understand what is the root cause


Solution

  • I cloned the original git repository and modified some methods to support proxy. Please note that I modified just some methods to get the sample code working, but a complete refactor of the package is needed.

    Basically what you have to do is to add a proxy parameter before calling the request method (this is done throughout the package, so several modifications are needed), for example this is for one of the methods in the Organization.js file:

    getSummary (guid) {
    
            const url = `${this.API_URL}/v2/organizations/${guid}/summary`;
            const proxy = `${this.API_PROXY}`;
            const options = {
                method: "GET",
                url: url,
                proxy: proxy,
                headers: {
                    Authorization: `${this.UAA_TOKEN.token_type} ${this.UAA_TOKEN.access_token}`
                }
            };
    
            return this.REST.request(options, this.HttpStatus.OK, true);
        }
    

    You can find my changes in the git repository below:

    https://github.com/adasilva70/cf-nodejs-client.git

    I have also created a new sample below. This sample lists all organizations for a user, gets the first organization returned and lists its spaces. You can modify the code to provide a similar functionality that cf login provides (allow you to select an organization then a space).

    const endpoint = "https://api.mycompany.com/";
    const username = "youruser";
    const password = "yourpassword";
    const proxy = "http://proxy.mycompany.com:8080";
    
    const CloudController = new (require("cf-nodejs-client")).CloudController(endpoint, proxy);
    const UsersUAA = new (require("cf-nodejs-client")).UsersUAA;
    const Apps = new (require("cf-nodejs-client")).Apps(endpoint, proxy);
    const Orgs = new (require("cf-nodejs-client")).Organizations(endpoint, proxy);
    
    CloudController.getInfo().then((result) => {
        console.log(result);
        UsersUAA.setEndPoint(result.authorization_endpoint, proxy);
        return UsersUAA.login(username, password);
    }).then((result) => {
        //Apps.setToken(result);
        //return Apps.getApps();
        Orgs.setToken(result);
        return Orgs.getOrganizations();
    }).then((result) => {
        console.log(result);
        org_guid = result.resources[1].metadata.guid;
        return Orgs.getSummary(org_guid);
    }).then((result) => {
        console.log(result);
    }).catch((reason) => {
        console.error("Error: " + reason);
    });
    

    I have done just minor tests to make sure the sample works, so use carefully. Also, the changes will only work for a case where proxy is needed now.