cloudfoundry-community / node-cfenv

easy access to your Cloud Foundry application environment for node
Apache License 2.0
73 stars 20 forks source link

Cannot extract credentials from a "user-provided" definition in a local vcap file #29

Closed mkaste closed 6 years ago

mkaste commented 6 years ago

Hi!

I am having issues with cfenv and a local vcap file. I created a user defined service with credentials in bluemix. I then exported that vcap definition from BM to use in my development environment.

{
"user-provided": [
        {
            "credentials": {
                "secret": "xxxxxxx",
                "user": "dummyuser"
            },
            "syslog_drain_url": "",
            "volume_mounts": [],
            "label": "user-provided",
            "name": "myservice",
            "tags": []
        }
    ]
}

I followed the instructions to configure cfenv with the local vcap:

...
var appEnv = cfenv.getAppEnv({
  vcap: vcapLocal
});
...

But when I try to fetch the credentials of the user-provided service, I always get an empty object.

var myserviceCreds = appEnv.getServiceCreds(/myservice/) || {};

Now I am not sure if cfenv considers user-provided sections or only "services"? Is my vcap file wrong?

Thanks in advance for any feedback.

pmuellr commented 6 years ago

The code is expecting the vcap option to have a services property:

vcap - provide values for the VCAP_APPLICATION and VCAP_SERVICES environment variable, when running locally. The object can have properties application and/or services, whose values are the same as the values serialized in the respective environment variables.

In your example, change

var appEnv = cfenv.getAppEnv({
  vcap: vcapLocal
})

to

var appEnv = cfenv.getAppEnv({
  vcap: { services: vcapLocal }
})

and I think it will work.

Here's a complete example: https://runkit.com/pmuellr/cfenv-issue-29

mkaste commented 6 years ago

Yes, that solved it. I missed the "object can have properties .. and/or services". Thanks a lot for a quick reply.