PaaS: Managing Your Cloud Infrastructure via RESTful API in Node.js

Once upon a time when someone needs to publish their own website on the internet, they had to go buy expensive server hoping that performance will be enough for growing user base or buy even more expensive server that they were not going to use at its full capacity. But this is only a 20% of the work, then they had to contact an ISP and buy a public IP for the server and pay the monthly internet bill and power bill and then make sure the server is running 24×7.

But today we have a hosting service to make sure every single thing is handled for us for a reasonable price. Whether you are a blogger, entrepreneur, reseller or a developer you just have to sit back and relax. Whether you are growing 1 million users per day or having a rough patch, you only need to pay for what you have used.

For example, imagine that you have built a rocket to go to mars and you are building a mobile app to sell tickets. You published your app today and when you wake up tomorrow there are millions of people who want to buy a ticket, but your app is broken since your VPS cannot handle the load or you are a VPS reseller who has a website and manually ordering servers from other platforms.

But what if, you can automate this without ever being wanted to log in to your dashboard again. In this article, we are going to see how to develop it using NodeJS. For this article, I will use the Veesp’s API to do that.

Further details about this API can be found in the official documentation at following link https://secure.veesp.com/userapi

Veesp

So let’s get started!

Install the npm Request Module

In order to perform any kind of HTTP requests, you need an HTTP client library. You are free to use your own NodeJS HTTP client. For this article, we are going to use npm request module.

So first let’s get started by installing the request module using npm,

Then create a file called app.js and import the request module.

Ordering a VPS

Veesp provide a variety of service categories including VPS hosting, domain names, DNS hosting and few other services. Also, VPS hosting is divided into a few types, Linux SSD VPS, Linux HDD VPS, Windows VPS and dedicated servers.Veesp.com

Veesp API uses HTTP basic user authentication for authenticating users. You should send your username and password with the request headers in order to use endpoints.

We will leave out this module import, and username password authentication after this example. But you should send these authentication details in each request as we mentioned before,

So here is the code to get a list of service categories from the API.

When you want to execute the code you can open your terminal and run node app.js. Your console output should look something like this,

So we have a list of categories and relevant ID for each category. For this example, we are going to order a new Linux SSD VPS where the ID is 18 as we can see in the response.

Then we can use this category ID to get a list of products under this category as below.

After executing this code, your console output should look something like this,

Now we can order the product. Pass your hostname as a parameter ‘domain’ in the request body. You can select the ID of the product you want to order, for this example we are going to order 512 Mb Ram, 10 Gb SSD with 100 Gb traffic and 200 Mbps network interface. ID relevant to that product is 212 as you can see in the response.

If you do not get any error from the server, you VPS should now be up and running.

Listing the Current Services

You may want to get a list of current VPS to analyze your current usage, and also to control stop or start a VPS when needed.

Let’s take a list of services by calling the /service API as below

Execute this code and you should get a response like this,

Because we have created a Linux SSD VPS above we will get that and a list of all other services we are using. If you only need VPS servers or a certain category only, you can filter by the category_url or category as you wish.

For example, if you only want to select only Linux SSD VPS category

Stop and Start a VPS

Now we have a list of services and their IDs. Finally, now we can get a list of VMs related to the service as below. You should pass your service ID with the following URL. Please, note that the service ID of the above request 29618 is used to get the VM list.

You should get a response like this from the server with VM IDs and info including the password,

As you can see you have the assigned public IP and the password to authenticate the VPS in the response. Now you can automate deploying your application and scaling logic at this stage or give back these credentials to your customers if you are a reseller.

Now you can perform stop and start operation on your VPS as below,

To stop the server,

To start the server,

Other than that you can rebuild your VPS, restart your VPS, change the boot order of your VPS using this API.

So this is the basics of how you can use automation and application scaling using HTTP API. Hope you will find this useful to automate and scale up your business as your customer base grows, without even needing to login to the dashboard ever again.

Happy scaling!

Thanks for spending a few minutes to read this article!

Leave a Comment

Your email address will not be published. Required fields are marked *