Bootstrap FreeKB - OpenShift - List projects using NodeJS
OpenShift - List projects using NodeJS

Updated:   |  OpenShift articles

Let's create a temporary directory (on a Linux system).

mkdir /tmp/foo

 

And move into the temporary directory.

cd /tmp/foo

 

And let's use the npm install command to install the NodeJS openshift-rest-client package. 

npm install openshift-rest-client

 

Assuming you have already logged into OpenShift for the first time, you should have a file named "config" in your /home/username/.kube directory. If not, check out my article FreeKB - OpenShift - Log into OpenShift using the oc login command.

And let's create list_projects.js with the following markup. Notice in this example that /home/john.doe/.kube/config is used to authenticate with OpenShift using john.doe .kube config file.

In this example, there is a for loop that loops through the response.body.items list. For more details on for loops, check out my article FreeKB - NodeJS - Getting Started with for loop.

const openshiftRestClient = require('openshift-rest-client').OpenshiftClient;

const config = "/home/john.doe/.kube/config";

openshiftRestClient({config}).then((client) => {
  client.apis['project.openshift.io'].v1.projects.get().then((response) => {
    for ( let i = 0; i < response.body.items.length; i++ ) {
      console.log(response.body.items[i].metadata.name);
    }
  });
})

 

Something like this should be returned, the name of your OpenShift projects.

bar-project
foo-project
my-project

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 3f3428 in the box below so that we can be sure you are a human.