【问题标题】:CF Connect to the cloud controllerCF 连接云控制器
【发布时间】:2017-05-18 17:28:33
【问题描述】:

我使用下面的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);
});
  1. 我尝试针对我们的 API 运行它,但它不起作用,我在控制台中没有收到任何错误消息,它可能是什么?

  2. 这里的空间/组织在哪里处理?因为当我从 cli 连接时,它会询问我要连接到哪个空间/组织...

我能够通过 CLI 登录,仅通过 我无法访问的代码,知道这里缺少什么吗?

我运行它时的问题我没有收到任何有助于了解根本原因的错误

【问题讨论】:

  • 您使用的端点是什么?我刚刚使用api.ng.bluemix.net(Bluemix 端点)尝试了您的代码,它运行良好。
  • @AlexdaSilva - API 是我的公司 API,但在我能够在 CLI 中连接之前,我使用代理 ...比如 https_proxy=mycompany.corp:8080 ,我认为这个库不支持这个...
  • API 不支持代理。我查看了代码,它使用请求模块,但没有设置代理的选项。您必须添加代理支持或在 github 中打开问题请求它。

标签: javascript node.js ibm-cloud cloud-foundry


【解决方案1】:

在图书馆的 github 网站上让我印象深刻的第一件事是警告:

注意:此包尚未准备好用于生产应用程序。

该项目似乎也没有得到维护,因为有很多票已经几个月前没有得到回应。

无论如何,要弄清楚为什么库不工作并且不产生错误消息,我会检查库源代码并添加一些控制台日志记录语句,可能以HttpUtils 开头。例如:

requestWithDefaults(options, function (error, response, body) {
    console.log("requestWithDefaults error: ", error)
    console.log("requestWithDefaults response: ", response)
    console.log("requestWithDefaults body: ", body)
    ...
}

或者,您可以尝试使用 nodejs debugger 在库中的 requestWithDefaults 和其他关键位置添加断点来调试代码。

你也可以尝试调试类似于how to monitor the network on node.js similar to chrome/firefox developer tools?的网络调用

要了解如何使用该库,我将查看测试文件夹并查找与您的用例相似的测试。如果测试在test/lib/model/cloudcontroller 文件夹中看起来有用,则有一个合理的数量。

关于空间的问题,我找到了example,您可以在其中传入空间 guid 以返回该空间 guid 的应用程序。

CloudFoundrySpaces.getSpaceApps(space_guid, filter).then( ... )

我假设您使用的呼叫 App.getApps() 将返回所有空间/组织的应用程序。

【讨论】:

  • 嗨,克里斯,谢谢!,我也用 github.com/IBM-Bluemix/cf-nodejs-client 对其进行了测试,但它不起作用......,我放了一个 BP &,控制台登录 requestWithDefaults 如你所述,它不会停止或打印日志,知道为什么吗?这对你有用吗?
  • 过了一会儿我收到了这个错误 requestWithDefaults 错误:{ 错误:在 Export._exceptionWithHostPort (util.exports._errnoException (util.js:1022:11) 处连接 ETIMEDOUT 35.156.96.41:443) js:1045:20) 在 TCPConnectWrap.afterConnect [as oncomplete] (net.js:1087:14)
  • 我在登录时在命令行中做的一件事我输入了我公司的代理,比如 export https_proxy=mycompany.corp:8080 ,我应该把它也放在代码中吗?如果是的话怎么办?谢谢!
  • 看起来 restler 和 request 库用于 http 调用。请参阅此处了解restler github.com/danwrong/restler/issues/9 的可能解决方法,并查看请求github.com/request/request#requestoptions-callback 的代理选项。您可能必须更改 cf-nodejs-client 源以实现代理支持。
【解决方案2】:

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

基本上你要做的是在调用request方法之前添加一个proxy参数(这是在整个包中完成的,因此需要进行一些修改),例如这是针对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);
    }

您可以在下面的 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);
});

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

【讨论】:

  • 嗨亚历克斯谢谢我提供了赏金:) 我的问题是是否有更清洁的方法来做到这一点?而是使用代理覆盖所有需要的文件(组织应用程序空间等)构造函数?
  • 不幸的是,我不知道另一种方法可以做到这一点。理想情况下,请求应该包含在 CloudControllerBase 类中,但是由于每个类的每个方法都使用自己的 options 变量显式调用请求,我不知道另一种方法可以做到这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-30
  • 1970-01-01
  • 2011-11-18
  • 1970-01-01
相关资源
最近更新 更多