2

I'm trying to call an API for password authentication in OpenStack. This is API that I curl and get the access token: Password authentication with unscoped authorization

I want to do the same thing in NodeJS and I'm a little confused. Can I do it cause I'm thinking maybe the problem is that it is not possible to do it like this; however this is the code that I've been trying:

var pkgcloud = require('pkgcloud');

var client = pkgcloud.compute.createClient({
    provider: 'openstack',
    username: <username>,
    password: <password>,
    authUrl: 'http://<ip>:<port>/',
    basePath: 'v3'
});
  client.getFlavors(function (err, flavors) {
    console.log("Error", err)
    console.log("Flavors", flavors)
})

client.getServers(function (err, servers) {
    console.log("Error", err)
    console.log("Servers", servers)
})

The getFlavors and getServers functions return 405 Method Not Allowed - The method is not allowed for the requested URL.

what is wrong with my code? and if I have to use another URL for getFlavors or any other function, where should I put it?

2 Answers2

2

I did what Iarsks has said and also added a tenant id and it worked fine.

This is the config that I used:

var config = {
    provider: 'openstack',
    keystoneAuthVersion: 'v3',
    authUrl: '...',
    domainId: 'default',
    username: <username>,
    password: <password>,
    region: <region name which is provided in the cloud.yaml file in OpenStack>,
    tenantId: <the same as the Project ID>
}
1

First, rather than specifying basePath: 'v3', you should explicitly be setting the Keystone version via keystoneAUthVersion. Simply setting basePath like that will ultimately result in bad requests.

When using v3 authentication, you need to provide a domainId (or domainName) as part of your request.

Lastly, you may need to ensure you are providing a valid region.

The following works correctly against my OpenStack environment:

var pkgcloud = require('pkgcloud');

var client = pkgcloud.compute.createClient({
    provider: 'openstack',
    keystoneAuthVersion: 'v3',
    authUrl: '...',
    domainId: 'default',
    username: 'lars',
    password: '...',
    region: 'my-region'
})

client.getFlavors(function (err, flavors) {
    console.log("Error", err)
    console.log("Flavors", flavors)
})

larsks
  • 277,717
  • 41
  • 399
  • 399
  • thanks! but I keep getting an error: "Cannot read property of 'id' of undefined" and I searched but nothing related came up. Do you know what it is for? – Shaghayegh Tavakoli Jan 26 '20 at 08:50
  • I don't. It sounds like something is unexpectedly returning an undefined value, which can sometimes suggest a bug or unexpected response from the server, but I don't know enough javascript to suggest where to start debugging. The above works for me with `pkgcloud` version `2.2.0`. – larsks Jan 26 '20 at 13:17