CF连接到云控制器

Joh*_*rby 7 javascript node.js cloud-foundry pivotal-cloud-foundry ibm-cloud

我使用以下lib连接到云控制器

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);
});
Run Code Online (Sandbox Code Playgroud)
  1. 我尝试针对我们的API运行它并且它无法工作,我在控制台中没有收到任何错误消息,它可以是什么?

  2. 这里处理空间/组织的位置是什么?因为当我从cli连接时它问我要连接哪个空间/组织...

我能够通过CLI登录,只是从代码我不能,任何想法在这里缺少什么?

我运行它时的问题我没有得到任何错误,可以帮助理解什么是根本原因

Ale*_*lva 4

我克隆了原始的git存储库并修改了一些方法来支持代理。请注意,我只修改了一些方法来使示例代码正常工作,但需要对包进行完整的重构。

基本上,您要做的就是proxy在调用request方法之前添加一个参数(这是在整个包中完成的,因此需要进行多次修改),例如这是文件中的方法之一Organization.js

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);
    }
Run Code Online (Sandbox Code Playgroud)

您可以在下面的 git 存储库中找到我的更改:

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

我还在下面创建了一个新示例。此示例列出用户的所有组织,获取返回的第一个组织并列出其空间。您可以修改代码以提供与 cf login 提供的类似功能(允许您选择组织,然后选择空间)。

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);
});
Run Code Online (Sandbox Code Playgroud)

我只做了一些小测试来确保示例有效,所以请小心使用。此外,这些更改仅适用于现在需要代理的情况。